fs_remap.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) Facebook, Inc. and its affiliates. 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. #pragma once
  6. #include <utility>
  7. #include "rocksdb/file_system.h"
  8. namespace ROCKSDB_NAMESPACE {
  9. // An abstract FileSystem wrapper that creates a view of an existing
  10. // FileSystem by remapping names in some way.
  11. //
  12. // This class has not been fully analyzed for providing strong security
  13. // guarantees.
  14. class RemapFileSystem : public FileSystemWrapper {
  15. public:
  16. explicit RemapFileSystem(const std::shared_ptr<FileSystem>& base);
  17. protected:
  18. // Returns status and mapped-to path in the wrapped filesystem.
  19. // If it returns non-OK status, the returned path should not be used.
  20. virtual std::pair<IOStatus, std::string> EncodePath(
  21. const std::string& path) = 0;
  22. // Similar to EncodePath() except used in cases in which it is OK for
  23. // no file or directory on 'path' to already exist, such as if the
  24. // operation would create one. However, the parent of 'path' is expected
  25. // to exist for the operation to succeed.
  26. // Default implementation: call EncodePath
  27. virtual std::pair<IOStatus, std::string> EncodePathWithNewBasename(
  28. const std::string& path);
  29. public:
  30. // Left abstract:
  31. // const char* Name() const override { ... }
  32. static const char* kClassName() { return "RemapFileSystem"; }
  33. bool IsInstanceOf(const std::string& id) const override {
  34. if (id == kClassName()) {
  35. return true;
  36. } else {
  37. return FileSystemWrapper::IsInstanceOf(id);
  38. }
  39. }
  40. Status RegisterDbPaths(const std::vector<std::string>& paths) override;
  41. Status UnregisterDbPaths(const std::vector<std::string>& paths) override;
  42. IOStatus NewSequentialFile(const std::string& fname,
  43. const FileOptions& options,
  44. std::unique_ptr<FSSequentialFile>* result,
  45. IODebugContext* dbg) override;
  46. IOStatus NewRandomAccessFile(const std::string& fname,
  47. const FileOptions& options,
  48. std::unique_ptr<FSRandomAccessFile>* result,
  49. IODebugContext* dbg) override;
  50. IOStatus NewWritableFile(const std::string& fname, const FileOptions& options,
  51. std::unique_ptr<FSWritableFile>* result,
  52. IODebugContext* dbg) override;
  53. IOStatus ReopenWritableFile(const std::string& fname,
  54. const FileOptions& options,
  55. std::unique_ptr<FSWritableFile>* result,
  56. IODebugContext* dbg) override;
  57. IOStatus ReuseWritableFile(const std::string& fname,
  58. const std::string& old_fname,
  59. const FileOptions& options,
  60. std::unique_ptr<FSWritableFile>* result,
  61. IODebugContext* dbg) override;
  62. IOStatus NewRandomRWFile(const std::string& fname, const FileOptions& options,
  63. std::unique_ptr<FSRandomRWFile>* result,
  64. IODebugContext* dbg) override;
  65. IOStatus NewDirectory(const std::string& dir, const IOOptions& options,
  66. std::unique_ptr<FSDirectory>* result,
  67. IODebugContext* dbg) override;
  68. IOStatus FileExists(const std::string& fname, const IOOptions& options,
  69. IODebugContext* dbg) override;
  70. IOStatus GetChildren(const std::string& dir, const IOOptions& options,
  71. std::vector<std::string>* result,
  72. IODebugContext* dbg) override;
  73. IOStatus GetChildrenFileAttributes(const std::string& dir,
  74. const IOOptions& options,
  75. std::vector<FileAttributes>* result,
  76. IODebugContext* dbg) override;
  77. IOStatus DeleteFile(const std::string& fname, const IOOptions& options,
  78. IODebugContext* dbg) override;
  79. IOStatus CreateDir(const std::string& dirname, const IOOptions& options,
  80. IODebugContext* dbg) override;
  81. IOStatus CreateDirIfMissing(const std::string& dirname,
  82. const IOOptions& options,
  83. IODebugContext* dbg) override;
  84. IOStatus DeleteDir(const std::string& dirname, const IOOptions& options,
  85. IODebugContext* dbg) override;
  86. IOStatus GetFileSize(const std::string& fname, const IOOptions& options,
  87. uint64_t* file_size, IODebugContext* dbg) override;
  88. IOStatus GetFileModificationTime(const std::string& fname,
  89. const IOOptions& options,
  90. uint64_t* file_mtime,
  91. IODebugContext* dbg) override;
  92. IOStatus IsDirectory(const std::string& path, const IOOptions& options,
  93. bool* is_dir, IODebugContext* dbg) override;
  94. IOStatus RenameFile(const std::string& src, const std::string& dest,
  95. const IOOptions& options, IODebugContext* dbg) override;
  96. IOStatus LinkFile(const std::string& src, const std::string& dest,
  97. const IOOptions& options, IODebugContext* dbg) override;
  98. IOStatus LockFile(const std::string& fname, const IOOptions& options,
  99. FileLock** lock, IODebugContext* dbg) override;
  100. IOStatus NewLogger(const std::string& fname, const IOOptions& options,
  101. std::shared_ptr<Logger>* result,
  102. IODebugContext* dbg) override;
  103. IOStatus GetAbsolutePath(const std::string& db_path, const IOOptions& options,
  104. std::string* output_path,
  105. IODebugContext* dbg) override;
  106. };
  107. } // namespace ROCKSDB_NAMESPACE