]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
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> | |
11fdf7f2 TL |
12 | #ifdef OS_WIN |
13 | #include <string.h> | |
14 | #endif | |
7c673cae FG |
15 | #include <cstring> |
16 | #include "port/port.h" | |
17 | ||
18 | namespace rocksdb { | |
19 | ||
20 | const char* Status::CopyState(const char* state) { | |
11fdf7f2 TL |
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); | |
7c673cae | 28 | return result; |
11fdf7f2 TL |
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 | |
7c673cae FG |
33 | } |
34 | ||
11fdf7f2 TL |
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 | }; | |
46 | ||
47 | Status::Status(Code _code, SubCode _subcode, const Slice& msg, | |
48 | const Slice& msg2) | |
49 | : code_(_code), subcode_(_subcode), sev_(kNoError) { | |
7c673cae FG |
50 | assert(code_ != kOk); |
51 | assert(subcode_ != kMaxSubCode); | |
52 | const size_t len1 = msg.size(); | |
53 | const size_t len2 = msg2.size(); | |
54 | const size_t size = len1 + (len2 ? (2 + len2) : 0); | |
55 | char* const result = new char[size + 1]; // +1 for null terminator | |
56 | memcpy(result, msg.data(), len1); | |
57 | if (len2) { | |
58 | result[len1] = ':'; | |
59 | result[len1 + 1] = ' '; | |
60 | memcpy(result + len1 + 2, msg2.data(), len2); | |
61 | } | |
62 | result[size] = '\0'; // null terminator for C style string | |
63 | state_ = result; | |
64 | } | |
65 | ||
66 | std::string Status::ToString() const { | |
67 | char tmp[30]; | |
68 | const char* type; | |
69 | switch (code_) { | |
70 | case kOk: | |
71 | return "OK"; | |
72 | case kNotFound: | |
73 | type = "NotFound: "; | |
74 | break; | |
75 | case kCorruption: | |
76 | type = "Corruption: "; | |
77 | break; | |
78 | case kNotSupported: | |
79 | type = "Not implemented: "; | |
80 | break; | |
81 | case kInvalidArgument: | |
82 | type = "Invalid argument: "; | |
83 | break; | |
84 | case kIOError: | |
85 | type = "IO error: "; | |
86 | break; | |
87 | case kMergeInProgress: | |
88 | type = "Merge in progress: "; | |
89 | break; | |
90 | case kIncomplete: | |
91 | type = "Result incomplete: "; | |
92 | break; | |
93 | case kShutdownInProgress: | |
94 | type = "Shutdown in progress: "; | |
95 | break; | |
96 | case kTimedOut: | |
97 | type = "Operation timed out: "; | |
98 | break; | |
99 | case kAborted: | |
100 | type = "Operation aborted: "; | |
101 | break; | |
102 | case kBusy: | |
103 | type = "Resource busy: "; | |
104 | break; | |
105 | case kExpired: | |
106 | type = "Operation expired: "; | |
107 | break; | |
108 | case kTryAgain: | |
109 | type = "Operation failed. Try again.: "; | |
110 | break; | |
111 | default: | |
112 | snprintf(tmp, sizeof(tmp), "Unknown code(%d): ", | |
113 | static_cast<int>(code())); | |
114 | type = tmp; | |
115 | break; | |
116 | } | |
117 | std::string result(type); | |
118 | if (subcode_ != kNone) { | |
119 | uint32_t index = static_cast<int32_t>(subcode_); | |
120 | assert(sizeof(msgs) > index); | |
121 | result.append(msgs[index]); | |
122 | } | |
123 | ||
124 | if (state_ != nullptr) { | |
125 | result.append(state_); | |
126 | } | |
127 | return result; | |
128 | } | |
129 | ||
130 | } // namespace rocksdb |