Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

106 linhas
2.7 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
há 4 anos
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
há 4 anos
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
há 4 anos
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
há 4 anos
  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 "leveldb/env.h"
  5. // This workaround can be removed when leveldb::Env::DeleteFile is removed.
  6. // See env.h for justification.
  7. #if defined(_WIN32) && defined(LEVELDB_DELETEFILE_UNDEFINED)
  8. #undef DeleteFile
  9. #endif
  10. namespace leveldb {
  11. Env::Env() = default;
  12. Env::~Env() = default;
  13. Status Env::NewAppendableFile(const std::string& fname, WritableFile** result) {
  14. return Status::NotSupported("NewAppendableFile", fname);
  15. }
  16. Status Env::RemoveDir(const std::string& dirname) { return DeleteDir(dirname); }
  17. Status Env::DeleteDir(const std::string& dirname) { return RemoveDir(dirname); }
  18. Status Env::RemoveFile(const std::string& fname) { return DeleteFile(fname); }
  19. Status Env::DeleteFile(const std::string& fname) { return RemoveFile(fname); }
  20. SequentialFile::~SequentialFile() = default;
  21. RandomAccessFile::~RandomAccessFile() = default;
  22. WritableFile::~WritableFile() = default;
  23. Logger::~Logger() = default;
  24. FileLock::~FileLock() = default;
  25. void Log(Logger* info_log, const char* format, ...) {
  26. if (info_log != nullptr) {
  27. va_list ap;
  28. va_start(ap, format);
  29. info_log->Logv(format, ap);
  30. va_end(ap);
  31. }
  32. }
  33. static Status DoWriteStringToFile(Env* env, const Slice& data,
  34. const std::string& fname, bool should_sync) {
  35. WritableFile* file;
  36. Status s = env->NewWritableFile(fname, &file);
  37. if (!s.ok()) {
  38. return s;
  39. }
  40. s = file->Append(data);
  41. if (s.ok() && should_sync) {
  42. s = file->Sync();
  43. }
  44. if (s.ok()) {
  45. s = file->Close();
  46. }
  47. delete file; // Will auto-close if we did not close above
  48. if (!s.ok()) {
  49. env->RemoveFile(fname);
  50. }
  51. return s;
  52. }
  53. Status WriteStringToFile(Env* env, const Slice& data,
  54. const std::string& fname) {
  55. return DoWriteStringToFile(env, data, fname, false);
  56. }
  57. Status WriteStringToFileSync(Env* env, const Slice& data,
  58. const std::string& fname) {
  59. return DoWriteStringToFile(env, data, fname, true);
  60. }
  61. Status ReadFileToString(Env* env, const std::string& fname, std::string* data) {
  62. data->clear();
  63. SequentialFile* file;
  64. Status s = env->NewSequentialFile(fname, &file);
  65. if (!s.ok()) {
  66. return s;
  67. }
  68. static const int kBufferSize = 8192;
  69. char* space = new char[kBufferSize];
  70. while (true) {
  71. Slice fragment;
  72. s = file->Read(kBufferSize, &fragment, space);
  73. if (!s.ok()) {
  74. break;
  75. }
  76. data->append(fragment.data(), fragment.size());
  77. if (fragment.empty()) {
  78. break;
  79. }
  80. }
  81. delete[] space;
  82. delete file;
  83. return s;
  84. }
  85. EnvWrapper::~EnvWrapper() {}
  86. } // namespace leveldb