fs_remap.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. #include "env/fs_remap.h"
  6. namespace ROCKSDB_NAMESPACE {
  7. RemapFileSystem::RemapFileSystem(const std::shared_ptr<FileSystem>& base)
  8. : FileSystemWrapper(base) {}
  9. std::pair<IOStatus, std::string> RemapFileSystem::EncodePathWithNewBasename(
  10. const std::string& path) {
  11. // No difference by default
  12. return EncodePath(path);
  13. }
  14. Status RemapFileSystem::RegisterDbPaths(const std::vector<std::string>& paths) {
  15. std::vector<std::string> encoded_paths;
  16. encoded_paths.reserve(paths.size());
  17. for (auto& path : paths) {
  18. auto status_and_enc_path = EncodePathWithNewBasename(path);
  19. if (!status_and_enc_path.first.ok()) {
  20. return status_and_enc_path.first;
  21. }
  22. encoded_paths.emplace_back(status_and_enc_path.second);
  23. }
  24. return FileSystemWrapper::RegisterDbPaths(encoded_paths);
  25. }
  26. Status RemapFileSystem::UnregisterDbPaths(
  27. const std::vector<std::string>& paths) {
  28. std::vector<std::string> encoded_paths;
  29. encoded_paths.reserve(paths.size());
  30. for (auto& path : paths) {
  31. auto status_and_enc_path = EncodePathWithNewBasename(path);
  32. if (!status_and_enc_path.first.ok()) {
  33. return status_and_enc_path.first;
  34. }
  35. encoded_paths.emplace_back(status_and_enc_path.second);
  36. }
  37. return FileSystemWrapper::UnregisterDbPaths(encoded_paths);
  38. }
  39. IOStatus RemapFileSystem::NewSequentialFile(
  40. const std::string& fname, const FileOptions& options,
  41. std::unique_ptr<FSSequentialFile>* result, IODebugContext* dbg) {
  42. auto status_and_enc_path = EncodePathWithNewBasename(fname);
  43. if (!status_and_enc_path.first.ok()) {
  44. return status_and_enc_path.first;
  45. }
  46. return FileSystemWrapper::NewSequentialFile(status_and_enc_path.second,
  47. options, result, dbg);
  48. }
  49. IOStatus RemapFileSystem::NewRandomAccessFile(
  50. const std::string& fname, const FileOptions& options,
  51. std::unique_ptr<FSRandomAccessFile>* result, IODebugContext* dbg) {
  52. auto status_and_enc_path = EncodePathWithNewBasename(fname);
  53. if (!status_and_enc_path.first.ok()) {
  54. return status_and_enc_path.first;
  55. }
  56. return FileSystemWrapper::NewRandomAccessFile(status_and_enc_path.second,
  57. options, result, dbg);
  58. }
  59. IOStatus RemapFileSystem::NewWritableFile(
  60. const std::string& fname, const FileOptions& options,
  61. std::unique_ptr<FSWritableFile>* result, IODebugContext* dbg) {
  62. auto status_and_enc_path = EncodePathWithNewBasename(fname);
  63. if (!status_and_enc_path.first.ok()) {
  64. return status_and_enc_path.first;
  65. }
  66. return FileSystemWrapper::NewWritableFile(status_and_enc_path.second, options,
  67. result, dbg);
  68. }
  69. IOStatus RemapFileSystem::ReopenWritableFile(
  70. const std::string& fname, const FileOptions& options,
  71. std::unique_ptr<FSWritableFile>* result, IODebugContext* dbg) {
  72. auto status_and_enc_path = EncodePathWithNewBasename(fname);
  73. if (!status_and_enc_path.first.ok()) {
  74. return status_and_enc_path.first;
  75. }
  76. return FileSystemWrapper::ReopenWritableFile(status_and_enc_path.second,
  77. options, result, dbg);
  78. }
  79. IOStatus RemapFileSystem::ReuseWritableFile(
  80. const std::string& fname, const std::string& old_fname,
  81. const FileOptions& options, std::unique_ptr<FSWritableFile>* result,
  82. IODebugContext* dbg) {
  83. auto status_and_enc_path = EncodePathWithNewBasename(fname);
  84. if (!status_and_enc_path.first.ok()) {
  85. return status_and_enc_path.first;
  86. }
  87. auto status_and_old_enc_path = EncodePath(old_fname);
  88. if (!status_and_old_enc_path.first.ok()) {
  89. return status_and_old_enc_path.first;
  90. }
  91. return FileSystemWrapper::ReuseWritableFile(status_and_old_enc_path.second,
  92. status_and_old_enc_path.second,
  93. options, result, dbg);
  94. }
  95. IOStatus RemapFileSystem::NewRandomRWFile(
  96. const std::string& fname, const FileOptions& options,
  97. std::unique_ptr<FSRandomRWFile>* result, IODebugContext* dbg) {
  98. auto status_and_enc_path = EncodePathWithNewBasename(fname);
  99. if (!status_and_enc_path.first.ok()) {
  100. return status_and_enc_path.first;
  101. }
  102. return FileSystemWrapper::NewRandomRWFile(status_and_enc_path.second, options,
  103. result, dbg);
  104. }
  105. IOStatus RemapFileSystem::NewDirectory(const std::string& dir,
  106. const IOOptions& options,
  107. std::unique_ptr<FSDirectory>* result,
  108. IODebugContext* dbg) {
  109. // A hassle to remap DirFsyncOptions::renamed_new_name
  110. class RemapFSDirectory : public FSDirectoryWrapper {
  111. public:
  112. RemapFSDirectory(RemapFileSystem* fs, std::unique_ptr<FSDirectory>&& t)
  113. : FSDirectoryWrapper(std::move(t)), fs_(fs) {}
  114. IOStatus FsyncWithDirOptions(
  115. const IOOptions& options, IODebugContext* dbg,
  116. const DirFsyncOptions& dir_fsync_options) override {
  117. if (dir_fsync_options.renamed_new_name.empty()) {
  118. return FSDirectoryWrapper::FsyncWithDirOptions(options, dbg,
  119. dir_fsync_options);
  120. } else {
  121. auto status_and_enc_path =
  122. fs_->EncodePath(dir_fsync_options.renamed_new_name);
  123. if (status_and_enc_path.first.ok()) {
  124. DirFsyncOptions mapped_options = dir_fsync_options;
  125. mapped_options.renamed_new_name = status_and_enc_path.second;
  126. return FSDirectoryWrapper::FsyncWithDirOptions(options, dbg,
  127. mapped_options);
  128. } else {
  129. return status_and_enc_path.first;
  130. }
  131. }
  132. }
  133. private:
  134. RemapFileSystem* const fs_;
  135. };
  136. auto status_and_enc_path = EncodePathWithNewBasename(dir);
  137. if (!status_and_enc_path.first.ok()) {
  138. return status_and_enc_path.first;
  139. }
  140. IOStatus ios = FileSystemWrapper::NewDirectory(status_and_enc_path.second,
  141. options, result, dbg);
  142. if (ios.ok()) {
  143. *result = std::make_unique<RemapFSDirectory>(this, std::move(*result));
  144. }
  145. return ios;
  146. }
  147. IOStatus RemapFileSystem::FileExists(const std::string& fname,
  148. const IOOptions& options,
  149. IODebugContext* dbg) {
  150. auto status_and_enc_path = EncodePathWithNewBasename(fname);
  151. if (!status_and_enc_path.first.ok()) {
  152. return status_and_enc_path.first;
  153. }
  154. return FileSystemWrapper::FileExists(status_and_enc_path.second, options,
  155. dbg);
  156. }
  157. IOStatus RemapFileSystem::GetChildren(const std::string& dir,
  158. const IOOptions& options,
  159. std::vector<std::string>* result,
  160. IODebugContext* dbg) {
  161. auto status_and_enc_path = EncodePath(dir);
  162. if (!status_and_enc_path.first.ok()) {
  163. return status_and_enc_path.first;
  164. }
  165. return FileSystemWrapper::GetChildren(status_and_enc_path.second, options,
  166. result, dbg);
  167. }
  168. IOStatus RemapFileSystem::GetChildrenFileAttributes(
  169. const std::string& dir, const IOOptions& options,
  170. std::vector<FileAttributes>* result, IODebugContext* dbg) {
  171. auto status_and_enc_path = EncodePath(dir);
  172. if (!status_and_enc_path.first.ok()) {
  173. return status_and_enc_path.first;
  174. }
  175. return FileSystemWrapper::GetChildrenFileAttributes(
  176. status_and_enc_path.second, options, result, dbg);
  177. }
  178. IOStatus RemapFileSystem::DeleteFile(const std::string& fname,
  179. const IOOptions& options,
  180. IODebugContext* dbg) {
  181. auto status_and_enc_path = EncodePath(fname);
  182. if (!status_and_enc_path.first.ok()) {
  183. return status_and_enc_path.first;
  184. }
  185. return FileSystemWrapper::DeleteFile(status_and_enc_path.second, options,
  186. dbg);
  187. }
  188. IOStatus RemapFileSystem::CreateDir(const std::string& dirname,
  189. const IOOptions& options,
  190. IODebugContext* dbg) {
  191. auto status_and_enc_path = EncodePathWithNewBasename(dirname);
  192. if (!status_and_enc_path.first.ok()) {
  193. return status_and_enc_path.first;
  194. }
  195. return FileSystemWrapper::CreateDir(status_and_enc_path.second, options, dbg);
  196. }
  197. IOStatus RemapFileSystem::CreateDirIfMissing(const std::string& dirname,
  198. const IOOptions& options,
  199. IODebugContext* dbg) {
  200. auto status_and_enc_path = EncodePathWithNewBasename(dirname);
  201. if (!status_and_enc_path.first.ok()) {
  202. return status_and_enc_path.first;
  203. }
  204. return FileSystemWrapper::CreateDirIfMissing(status_and_enc_path.second,
  205. options, dbg);
  206. }
  207. IOStatus RemapFileSystem::DeleteDir(const std::string& dirname,
  208. const IOOptions& options,
  209. IODebugContext* dbg) {
  210. auto status_and_enc_path = EncodePath(dirname);
  211. if (!status_and_enc_path.first.ok()) {
  212. return status_and_enc_path.first;
  213. }
  214. return FileSystemWrapper::DeleteDir(status_and_enc_path.second, options, dbg);
  215. }
  216. IOStatus RemapFileSystem::GetFileSize(const std::string& fname,
  217. const IOOptions& options,
  218. uint64_t* file_size,
  219. IODebugContext* dbg) {
  220. auto status_and_enc_path = EncodePath(fname);
  221. if (!status_and_enc_path.first.ok()) {
  222. return status_and_enc_path.first;
  223. }
  224. return FileSystemWrapper::GetFileSize(status_and_enc_path.second, options,
  225. file_size, dbg);
  226. }
  227. IOStatus RemapFileSystem::GetFileModificationTime(const std::string& fname,
  228. const IOOptions& options,
  229. uint64_t* file_mtime,
  230. IODebugContext* dbg) {
  231. auto status_and_enc_path = EncodePath(fname);
  232. if (!status_and_enc_path.first.ok()) {
  233. return status_and_enc_path.first;
  234. }
  235. return FileSystemWrapper::GetFileModificationTime(status_and_enc_path.second,
  236. options, file_mtime, dbg);
  237. }
  238. IOStatus RemapFileSystem::IsDirectory(const std::string& path,
  239. const IOOptions& options, bool* is_dir,
  240. IODebugContext* dbg) {
  241. auto status_and_enc_path = EncodePath(path);
  242. if (!status_and_enc_path.first.ok()) {
  243. return status_and_enc_path.first;
  244. }
  245. return FileSystemWrapper::IsDirectory(status_and_enc_path.second, options,
  246. is_dir, dbg);
  247. }
  248. IOStatus RemapFileSystem::RenameFile(const std::string& src,
  249. const std::string& dest,
  250. const IOOptions& options,
  251. IODebugContext* dbg) {
  252. auto status_and_src_enc_path = EncodePath(src);
  253. if (!status_and_src_enc_path.first.ok()) {
  254. if (status_and_src_enc_path.first.IsNotFound()) {
  255. const IOStatus& s = status_and_src_enc_path.first;
  256. status_and_src_enc_path.first = IOStatus::PathNotFound(s.ToString());
  257. }
  258. return status_and_src_enc_path.first;
  259. }
  260. auto status_and_dest_enc_path = EncodePathWithNewBasename(dest);
  261. if (!status_and_dest_enc_path.first.ok()) {
  262. return status_and_dest_enc_path.first;
  263. }
  264. return FileSystemWrapper::RenameFile(status_and_src_enc_path.second,
  265. status_and_dest_enc_path.second, options,
  266. dbg);
  267. }
  268. IOStatus RemapFileSystem::LinkFile(const std::string& src,
  269. const std::string& dest,
  270. const IOOptions& options,
  271. IODebugContext* dbg) {
  272. auto status_and_src_enc_path = EncodePath(src);
  273. if (!status_and_src_enc_path.first.ok()) {
  274. return status_and_src_enc_path.first;
  275. }
  276. auto status_and_dest_enc_path = EncodePathWithNewBasename(dest);
  277. if (!status_and_dest_enc_path.first.ok()) {
  278. return status_and_dest_enc_path.first;
  279. }
  280. return FileSystemWrapper::LinkFile(status_and_src_enc_path.second,
  281. status_and_dest_enc_path.second, options,
  282. dbg);
  283. }
  284. IOStatus RemapFileSystem::LockFile(const std::string& fname,
  285. const IOOptions& options, FileLock** lock,
  286. IODebugContext* dbg) {
  287. auto status_and_enc_path = EncodePathWithNewBasename(fname);
  288. if (!status_and_enc_path.first.ok()) {
  289. return status_and_enc_path.first;
  290. }
  291. // FileLock subclasses may store path (e.g., PosixFileLock stores it). We
  292. // can skip stripping the chroot directory from this path because callers
  293. // shouldn't use it.
  294. return FileSystemWrapper::LockFile(status_and_enc_path.second, options, lock,
  295. dbg);
  296. }
  297. IOStatus RemapFileSystem::NewLogger(const std::string& fname,
  298. const IOOptions& options,
  299. std::shared_ptr<Logger>* result,
  300. IODebugContext* dbg) {
  301. auto status_and_enc_path = EncodePathWithNewBasename(fname);
  302. if (!status_and_enc_path.first.ok()) {
  303. return status_and_enc_path.first;
  304. }
  305. return FileSystemWrapper::NewLogger(status_and_enc_path.second, options,
  306. result, dbg);
  307. }
  308. IOStatus RemapFileSystem::GetAbsolutePath(const std::string& db_path,
  309. const IOOptions& options,
  310. std::string* output_path,
  311. IODebugContext* dbg) {
  312. auto status_and_enc_path = EncodePathWithNewBasename(db_path);
  313. if (!status_and_enc_path.first.ok()) {
  314. return status_and_enc_path.first;
  315. }
  316. return FileSystemWrapper::GetAbsolutePath(status_and_enc_path.second, options,
  317. output_path, dbg);
  318. }
  319. } // namespace ROCKSDB_NAMESPACE