1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2015 Couchbase, Inc.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 #include "buckets.h"
18 #include "mc_time.h"
19 
Bucket(const Bucket& other)20 Bucket::Bucket(const Bucket& other)
21 {
22     std::lock_guard<std::mutex> guard(other.mutex);
23     clients = other.clients;
24     state = other.state.load();
25     type = other.type;
26     std::copy(std::begin(other.name), std::end(other.name),
27               std::begin(name));
28     engine_event_handlers = other.engine_event_handlers;
29     engine = other.engine;
30     stats = other.stats;
31     timings = other.timings;
32     subjson_operation_times = other.subjson_operation_times;
33     topkeys = other.topkeys;
34     responseCounters = other.responseCounters;
35 }
36 
37 namespace BucketValidator {
validateBucketName(const std::string& name, std::string& errors)38     bool validateBucketName(const std::string& name, std::string& errors) {
39         if (name.empty()) {
40             errors.assign("BucketValidator::validateBucketName: "
41                               "Name can't be empty");
42             return false;
43         }
44 
45         if (name.length() > MAX_BUCKET_NAME_LENGTH) {
46             errors.assign("BucketValidator::validateBucketName: Name"
47                               " too long (exceeds " +
48                           std::to_string(MAX_BUCKET_NAME_LENGTH) +
49                           ")");
50             return false;
51         }
52 
53         // Verify that the bucket name only consists of legal characters
54         for (const uint8_t ii : name) {
55             if (!(isupper(ii) || islower(ii) || isdigit(ii))) {
56                 switch (ii) {
57                 case '_':
58                 case '-':
59                 case '.':
60                 case '%':
61                     break;
62                 default:
63                     errors.assign("BucketValidator::validateBucketName: "
64                                       "name contains invalid characters");
65                     return false;
66                 }
67             }
68         }
69 
70         return true;
71     }
72 
validateBucketType(const BucketType& type, std::string& errors)73     bool validateBucketType(const BucketType& type, std::string& errors) {
74         if (type == BucketType::Unknown) {
75             errors.assign("BucketValidator::validateBucketType: "
76                               "Unsupported bucket type");
77             return false;
78         }
79 
80         return true;
81     }
82 }
83