fs_readonly.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "rocksdb/file_system.h"
  7. namespace ROCKSDB_NAMESPACE {
  8. // A FileSystem wrapper that only allows read-only operation.
  9. //
  10. // This class has not been fully analyzed for providing strong security
  11. // guarantees.
  12. class ReadOnlyFileSystem : public FileSystemWrapper {
  13. static inline IOStatus FailReadOnly() {
  14. IOStatus s = IOStatus::IOError("Attempted write to ReadOnlyFileSystem");
  15. assert(s.GetRetryable() == false);
  16. return s;
  17. }
  18. public:
  19. explicit ReadOnlyFileSystem(const std::shared_ptr<FileSystem>& base)
  20. : FileSystemWrapper(base) {}
  21. static const char* kClassName() { return "ReadOnlyFileSystem"; }
  22. const char* Name() const override { return kClassName(); }
  23. IOStatus NewWritableFile(const std::string& /*fname*/,
  24. const FileOptions& /*options*/,
  25. std::unique_ptr<FSWritableFile>* /*result*/,
  26. IODebugContext* /*dbg*/) override {
  27. return FailReadOnly();
  28. }
  29. IOStatus ReuseWritableFile(const std::string& /*fname*/,
  30. const std::string& /*old_fname*/,
  31. const FileOptions& /*options*/,
  32. std::unique_ptr<FSWritableFile>* /*result*/,
  33. IODebugContext* /*dbg*/) override {
  34. return FailReadOnly();
  35. }
  36. IOStatus NewRandomRWFile(const std::string& /*fname*/,
  37. const FileOptions& /*options*/,
  38. std::unique_ptr<FSRandomRWFile>* /*result*/,
  39. IODebugContext* /*dbg*/) override {
  40. return FailReadOnly();
  41. }
  42. IOStatus NewDirectory(const std::string& /*dir*/,
  43. const IOOptions& /*options*/,
  44. std::unique_ptr<FSDirectory>* /*result*/,
  45. IODebugContext* /*dbg*/) override {
  46. return FailReadOnly();
  47. }
  48. IOStatus DeleteFile(const std::string& /*fname*/,
  49. const IOOptions& /*options*/,
  50. IODebugContext* /*dbg*/) override {
  51. return FailReadOnly();
  52. }
  53. IOStatus CreateDir(const std::string& /*dirname*/,
  54. const IOOptions& /*options*/,
  55. IODebugContext* /*dbg*/) override {
  56. return FailReadOnly();
  57. }
  58. IOStatus CreateDirIfMissing(const std::string& dirname,
  59. const IOOptions& options,
  60. IODebugContext* dbg) override {
  61. // Allow if dir already exists
  62. bool is_dir = false;
  63. IOStatus s = IsDirectory(dirname, options, &is_dir, dbg);
  64. if (s.ok() && is_dir) {
  65. return s;
  66. } else {
  67. return FailReadOnly();
  68. }
  69. }
  70. IOStatus DeleteDir(const std::string& /*dirname*/,
  71. const IOOptions& /*options*/,
  72. IODebugContext* /*dbg*/) override {
  73. return FailReadOnly();
  74. }
  75. IOStatus RenameFile(const std::string& /*src*/, const std::string& /*dest*/,
  76. const IOOptions& /*options*/,
  77. IODebugContext* /*dbg*/) override {
  78. return FailReadOnly();
  79. }
  80. IOStatus LinkFile(const std::string& /*src*/, const std::string& /*dest*/,
  81. const IOOptions& /*options*/,
  82. IODebugContext* /*dbg*/) override {
  83. return FailReadOnly();
  84. }
  85. IOStatus LockFile(const std::string& /*fname*/, const IOOptions& /*options*/,
  86. FileLock** /*lock*/, IODebugContext* /*dbg*/) override {
  87. return FailReadOnly();
  88. }
  89. IOStatus NewLogger(const std::string& /*fname*/, const IOOptions& /*options*/,
  90. std::shared_ptr<Logger>* /*result*/,
  91. IODebugContext* /*dbg*/) override {
  92. return FailReadOnly();
  93. }
  94. };
  95. } // namespace ROCKSDB_NAMESPACE