10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.0 KiB

Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}. The "DeleteFile" method name causes pain for Windows developers, because <windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA. Current code uses workarounds, like #undefining DeleteFile everywhere an Env is declared, implemented, or used. This CL removes the need for workarounds by renaming Env::DeleteFile to Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to Env::RemoveDir. A few internal methods are also renamed for consistency. Software that supports Windows is expected to migrate any Env implementations and usage to Remove{File,Dir}, and never use the name Env::Delete{File,Dir} in its code. The renaming is done in a backwards-compatible way, at the risk of making it slightly more difficult to build a new correct Env implementation. The backwards compatibility is achieved using the following hacks: 1) Env::Remove{File,Dir} methods are added, with a default implementation that calls into Env::Delete{File,Dir}. This makes old Env implementations compatible with code that calls into the updated API. 2) The Env::Delete{File,Dir} methods are no longer pure virtuals. Instead, they gain a default implementation that calls into Env::Remove{File,Dir}. This makes updated Env implementations compatible with code that calls into the old API. The cost of this approach is that it's possible to write an Env without overriding either Rename{File,Dir} or Delete{File,Dir}, without getting a compiler warning. However, attempting to run the test suite will immediately fail with an infinite call stack ending in {Remove,Delete}{File,Dir}, making developers aware of the problem. PiperOrigin-RevId: 288710907
4 years ago
  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include "db/filename.h"
  5. #include <cassert>
  6. #include <cstdio>
  7. #include "db/dbformat.h"
  8. #include "leveldb/env.h"
  9. #include "util/logging.h"
  10. namespace leveldb {
  11. // A utility routine: write "data" to the named file and Sync() it.
  12. Status WriteStringToFileSync(Env* env, const Slice& data,
  13. const std::string& fname);
  14. static std::string MakeFileName(const std::string& dbname, uint64_t number,
  15. const char* suffix) {
  16. char buf[100];
  17. std::snprintf(buf, sizeof(buf), "/%06llu.%s",
  18. static_cast<unsigned long long>(number), suffix);
  19. return dbname + buf;
  20. }
  21. std::string LogFileName(const std::string& dbname, uint64_t number) {
  22. assert(number > 0);
  23. return MakeFileName(dbname, number, "log");
  24. }
  25. std::string TableFileName(const std::string& dbname, uint64_t number) {
  26. assert(number > 0);
  27. return MakeFileName(dbname, number, "ldb");
  28. }
  29. std::string SSTTableFileName(const std::string& dbname, uint64_t number) {
  30. assert(number > 0);
  31. return MakeFileName(dbname, number, "sst");
  32. }
  33. std::string DescriptorFileName(const std::string& dbname, uint64_t number) {
  34. assert(number > 0);
  35. char buf[100];
  36. std::snprintf(buf, sizeof(buf), "/MANIFEST-%06llu",
  37. static_cast<unsigned long long>(number));
  38. return dbname + buf;
  39. }
  40. std::string CurrentFileName(const std::string& dbname) {
  41. return dbname + "/CURRENT";
  42. }
  43. std::string LockFileName(const std::string& dbname) { return dbname + "/LOCK"; }
  44. std::string TempFileName(const std::string& dbname, uint64_t number) {
  45. assert(number > 0);
  46. return MakeFileName(dbname, number, "dbtmp");
  47. }
  48. std::string InfoLogFileName(const std::string& dbname) {
  49. return dbname + "/LOG";
  50. }
  51. // Return the name of the old info log file for "dbname".
  52. std::string OldInfoLogFileName(const std::string& dbname) {
  53. return dbname + "/LOG.old";
  54. }
  55. // Owned filenames have the form:
  56. // dbname/CURRENT
  57. // dbname/LOCK
  58. // dbname/LOG
  59. // dbname/LOG.old
  60. // dbname/MANIFEST-[0-9]+
  61. // dbname/[0-9]+.(log|sst|ldb)
  62. bool ParseFileName(const std::string& filename, uint64_t* number,
  63. FileType* type) {
  64. Slice rest(filename);
  65. if (rest == "CURRENT") {
  66. *number = 0;
  67. *type = kCurrentFile;
  68. } else if (rest == "LOCK") {
  69. *number = 0;
  70. *type = kDBLockFile;
  71. } else if (rest == "LOG" || rest == "LOG.old") {
  72. *number = 0;
  73. *type = kInfoLogFile;
  74. } else if (rest.starts_with("MANIFEST-")) {
  75. rest.remove_prefix(strlen("MANIFEST-"));
  76. uint64_t num;
  77. if (!ConsumeDecimalNumber(&rest, &num)) {
  78. return false;
  79. }
  80. if (!rest.empty()) {
  81. return false;
  82. }
  83. *type = kDescriptorFile;
  84. *number = num;
  85. } else {
  86. // Avoid strtoull() to keep filename format independent of the
  87. // current locale
  88. uint64_t num;
  89. if (!ConsumeDecimalNumber(&rest, &num)) {
  90. return false;
  91. }
  92. Slice suffix = rest;
  93. if (suffix == Slice(".log")) {
  94. *type = kLogFile;
  95. } else if (suffix == Slice(".sst") || suffix == Slice(".ldb")) {
  96. *type = kTableFile;
  97. } else if (suffix == Slice(".dbtmp")) {
  98. *type = kTempFile;
  99. } else {
  100. return false;
  101. }
  102. *number = num;
  103. }
  104. return true;
  105. }
  106. Status SetCurrentFile(Env* env, const std::string& dbname,
  107. uint64_t descriptor_number) {
  108. // Remove leading "dbname/" and add newline to manifest file name
  109. std::string manifest = DescriptorFileName(dbname, descriptor_number);
  110. Slice contents = manifest;
  111. assert(contents.starts_with(dbname + "/"));
  112. contents.remove_prefix(dbname.size() + 1);
  113. std::string tmp = TempFileName(dbname, descriptor_number);
  114. Status s = WriteStringToFileSync(env, contents.ToString() + "\n", tmp);
  115. if (s.ok()) {
  116. s = env->RenameFile(tmp, CurrentFileName(dbname));
  117. }
  118. if (!s.ok()) {
  119. env->RemoveFile(tmp);
  120. }
  121. return s;
  122. }
  123. } // namespace leveldb