]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/status.cc
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / rocksdb / util / status.cc
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5 //
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
9
10 #include "rocksdb/status.h"
11 #include <stdio.h>
12 #ifdef OS_WIN
13 #include <string.h>
14 #endif
15 #include <cstring>
16 #include "port/port.h"
17
18 namespace rocksdb {
19
20 const char* Status::CopyState(const char* state) {
21 #ifdef OS_WIN
22 const size_t cch = std::strlen(state) + 1; // +1 for the null terminator
23 char* result = new char[cch];
24 errno_t ret;
25 ret = strncpy_s(result, cch, state, cch - 1);
26 result[cch - 1] = '\0';
27 assert(ret == 0);
28 return result;
29 #else
30 const size_t cch = std::strlen(state) + 1; // +1 for the null terminator
31 return std::strncpy(new char[cch], state, cch);
32 #endif
33 }
34
35 static const char* msgs[static_cast<int>(Status::kMaxSubCode)] = {
36 "", // kNone
37 "Timeout Acquiring Mutex", // kMutexTimeout
38 "Timeout waiting to lock key", // kLockTimeout
39 "Failed to acquire lock due to max_num_locks limit", // kLockLimit
40 "No space left on device", // kNoSpace
41 "Deadlock", // kDeadlock
42 "Stale file handle", // kStaleFile
43 "Memory limit reached", // kMemoryLimit
44 "Space limit reached", // kSpaceLimit
45 "No such file or directory", // kPathNotFound
46 };
47
48 Status::Status(Code _code, SubCode _subcode, const Slice& msg,
49 const Slice& msg2)
50 : code_(_code), subcode_(_subcode), sev_(kNoError) {
51 assert(code_ != kOk);
52 assert(subcode_ != kMaxSubCode);
53 const size_t len1 = msg.size();
54 const size_t len2 = msg2.size();
55 const size_t size = len1 + (len2 ? (2 + len2) : 0);
56 char* const result = new char[size + 1]; // +1 for null terminator
57 memcpy(result, msg.data(), len1);
58 if (len2) {
59 result[len1] = ':';
60 result[len1 + 1] = ' ';
61 memcpy(result + len1 + 2, msg2.data(), len2);
62 }
63 result[size] = '\0'; // null terminator for C style string
64 state_ = result;
65 }
66
67 std::string Status::ToString() const {
68 char tmp[30];
69 const char* type;
70 switch (code_) {
71 case kOk:
72 return "OK";
73 case kNotFound:
74 type = "NotFound: ";
75 break;
76 case kCorruption:
77 type = "Corruption: ";
78 break;
79 case kNotSupported:
80 type = "Not implemented: ";
81 break;
82 case kInvalidArgument:
83 type = "Invalid argument: ";
84 break;
85 case kIOError:
86 type = "IO error: ";
87 break;
88 case kMergeInProgress:
89 type = "Merge in progress: ";
90 break;
91 case kIncomplete:
92 type = "Result incomplete: ";
93 break;
94 case kShutdownInProgress:
95 type = "Shutdown in progress: ";
96 break;
97 case kTimedOut:
98 type = "Operation timed out: ";
99 break;
100 case kAborted:
101 type = "Operation aborted: ";
102 break;
103 case kBusy:
104 type = "Resource busy: ";
105 break;
106 case kExpired:
107 type = "Operation expired: ";
108 break;
109 case kTryAgain:
110 type = "Operation failed. Try again.: ";
111 break;
112 default:
113 snprintf(tmp, sizeof(tmp), "Unknown code(%d): ",
114 static_cast<int>(code()));
115 type = tmp;
116 break;
117 }
118 std::string result(type);
119 if (subcode_ != kNone) {
120 uint32_t index = static_cast<int32_t>(subcode_);
121 assert(sizeof(msgs) > index);
122 result.append(msgs[index]);
123 }
124
125 if (state_ != nullptr) {
126 result.append(state_);
127 }
128 return result;
129 }
130
131 } // namespace rocksdb