file_util.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (c) 2011-present, Facebook, Inc. 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. //
  6. #include "file/file_util.h"
  7. #include <string>
  8. #include <algorithm>
  9. #include "file/random_access_file_reader.h"
  10. #include "file/sequence_file_reader.h"
  11. #include "file/sst_file_manager_impl.h"
  12. #include "file/writable_file_writer.h"
  13. #include "rocksdb/env.h"
  14. namespace ROCKSDB_NAMESPACE {
  15. // Utility function to copy a file up to a specified length
  16. Status CopyFile(FileSystem* fs, const std::string& source,
  17. const std::string& destination, uint64_t size, bool use_fsync) {
  18. const FileOptions soptions;
  19. Status s;
  20. std::unique_ptr<SequentialFileReader> src_reader;
  21. std::unique_ptr<WritableFileWriter> dest_writer;
  22. {
  23. std::unique_ptr<FSSequentialFile> srcfile;
  24. s = fs->NewSequentialFile(source, soptions, &srcfile, nullptr);
  25. if (!s.ok()) {
  26. return s;
  27. }
  28. std::unique_ptr<FSWritableFile> destfile;
  29. s = fs->NewWritableFile(destination, soptions, &destfile, nullptr);
  30. if (!s.ok()) {
  31. return s;
  32. }
  33. if (size == 0) {
  34. // default argument means copy everything
  35. s = fs->GetFileSize(source, IOOptions(), &size, nullptr);
  36. if (!s.ok()) {
  37. return s;
  38. }
  39. }
  40. src_reader.reset(new SequentialFileReader(std::move(srcfile), source));
  41. dest_writer.reset(
  42. new WritableFileWriter(std::move(destfile), destination, soptions));
  43. }
  44. char buffer[4096];
  45. Slice slice;
  46. while (size > 0) {
  47. size_t bytes_to_read = std::min(sizeof(buffer), static_cast<size_t>(size));
  48. s = src_reader->Read(bytes_to_read, &slice, buffer);
  49. if (!s.ok()) {
  50. return s;
  51. }
  52. if (slice.size() == 0) {
  53. return Status::Corruption("file too small");
  54. }
  55. s = dest_writer->Append(slice);
  56. if (!s.ok()) {
  57. return s;
  58. }
  59. size -= slice.size();
  60. }
  61. return dest_writer->Sync(use_fsync);
  62. }
  63. // Utility function to create a file with the provided contents
  64. Status CreateFile(FileSystem* fs, const std::string& destination,
  65. const std::string& contents, bool use_fsync) {
  66. const EnvOptions soptions;
  67. Status s;
  68. std::unique_ptr<WritableFileWriter> dest_writer;
  69. std::unique_ptr<FSWritableFile> destfile;
  70. s = fs->NewWritableFile(destination, soptions, &destfile, nullptr);
  71. if (!s.ok()) {
  72. return s;
  73. }
  74. dest_writer.reset(
  75. new WritableFileWriter(std::move(destfile), destination, soptions));
  76. s = dest_writer->Append(Slice(contents));
  77. if (!s.ok()) {
  78. return s;
  79. }
  80. return dest_writer->Sync(use_fsync);
  81. }
  82. Status DeleteDBFile(const ImmutableDBOptions* db_options,
  83. const std::string& fname, const std::string& dir_to_sync,
  84. const bool force_bg, const bool force_fg) {
  85. #ifndef ROCKSDB_LITE
  86. SstFileManagerImpl* sfm =
  87. static_cast<SstFileManagerImpl*>(db_options->sst_file_manager.get());
  88. if (sfm && !force_fg) {
  89. return sfm->ScheduleFileDeletion(fname, dir_to_sync, force_bg);
  90. } else {
  91. return db_options->env->DeleteFile(fname);
  92. }
  93. #else
  94. (void)dir_to_sync;
  95. (void)force_bg;
  96. (void)force_fg;
  97. // SstFileManager is not supported in ROCKSDB_LITE
  98. // Delete file immediately
  99. return db_options->env->DeleteFile(fname);
  100. #endif
  101. }
  102. bool IsWalDirSameAsDBPath(const ImmutableDBOptions* db_options) {
  103. bool same = false;
  104. assert(!db_options->db_paths.empty());
  105. Status s = db_options->env->AreFilesSame(db_options->wal_dir,
  106. db_options->db_paths[0].path, &same);
  107. if (s.IsNotSupported()) {
  108. same = db_options->wal_dir == db_options->db_paths[0].path;
  109. }
  110. return same;
  111. }
  112. } // namespace ROCKSDB_NAMESPACE