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 
8 #include <string>
9 #include <algorithm>
10 
11 #include "file/random_access_file_reader.h"
12 #include "file/sequence_file_reader.h"
13 #include "file/sst_file_manager_impl.h"
14 #include "file/writable_file_writer.h"
15 #include "rocksdb/env.h"
16 
17 namespace ROCKSDB_NAMESPACE {
18 
19 // Utility function to copy a file up to a specified length
CopyFile(FileSystem * fs,const std::string & source,const std::string & destination,uint64_t size,bool use_fsync,const std::shared_ptr<IOTracer> & io_tracer)20 IOStatus CopyFile(FileSystem* fs, const std::string& source,
21                   const std::string& destination, uint64_t size, bool use_fsync,
22                   const std::shared_ptr<IOTracer>& io_tracer) {
23   const FileOptions soptions;
24   IOStatus io_s;
25   std::unique_ptr<SequentialFileReader> src_reader;
26   std::unique_ptr<WritableFileWriter> dest_writer;
27 
28   {
29     std::unique_ptr<FSSequentialFile> srcfile;
30     io_s = fs->NewSequentialFile(source, soptions, &srcfile, nullptr);
31     if (!io_s.ok()) {
32       return io_s;
33     }
34     std::unique_ptr<FSWritableFile> destfile;
35     io_s = fs->NewWritableFile(destination, soptions, &destfile, nullptr);
36     if (!io_s.ok()) {
37       return io_s;
38     }
39 
40     if (size == 0) {
41       // default argument means copy everything
42       io_s = fs->GetFileSize(source, IOOptions(), &size, nullptr);
43       if (!io_s.ok()) {
44         return io_s;
45       }
46     }
47     src_reader.reset(
48         new SequentialFileReader(std::move(srcfile), source, io_tracer));
49     dest_writer.reset(
50         new WritableFileWriter(std::move(destfile), destination, soptions));
51   }
52 
53   char buffer[4096];
54   Slice slice;
55   while (size > 0) {
56     size_t bytes_to_read = std::min(sizeof(buffer), static_cast<size_t>(size));
57     io_s = status_to_io_status(src_reader->Read(bytes_to_read, &slice, buffer));
58     if (!io_s.ok()) {
59       return io_s;
60     }
61     if (slice.size() == 0) {
62       return IOStatus::Corruption("file too small");
63     }
64     io_s = dest_writer->Append(slice);
65     if (!io_s.ok()) {
66       return io_s;
67     }
68     size -= slice.size();
69   }
70   return dest_writer->Sync(use_fsync);
71 }
72 
73 // Utility function to create a file with the provided contents
CreateFile(FileSystem * fs,const std::string & destination,const std::string & contents,bool use_fsync)74 IOStatus CreateFile(FileSystem* fs, const std::string& destination,
75                     const std::string& contents, bool use_fsync) {
76   const EnvOptions soptions;
77   IOStatus io_s;
78   std::unique_ptr<WritableFileWriter> dest_writer;
79 
80   std::unique_ptr<FSWritableFile> destfile;
81   io_s = fs->NewWritableFile(destination, soptions, &destfile, nullptr);
82   if (!io_s.ok()) {
83     return io_s;
84   }
85   dest_writer.reset(
86       new WritableFileWriter(std::move(destfile), destination, soptions));
87   io_s = dest_writer->Append(Slice(contents));
88   if (!io_s.ok()) {
89     return io_s;
90   }
91   return dest_writer->Sync(use_fsync);
92 }
93 
DeleteDBFile(const ImmutableDBOptions * db_options,const std::string & fname,const std::string & dir_to_sync,const bool force_bg,const bool force_fg)94 Status DeleteDBFile(const ImmutableDBOptions* db_options,
95                     const std::string& fname, const std::string& dir_to_sync,
96                     const bool force_bg, const bool force_fg) {
97 #ifndef ROCKSDB_LITE
98   SstFileManagerImpl* sfm =
99       static_cast<SstFileManagerImpl*>(db_options->sst_file_manager.get());
100   if (sfm && !force_fg) {
101     return sfm->ScheduleFileDeletion(fname, dir_to_sync, force_bg);
102   } else {
103     return db_options->env->DeleteFile(fname);
104   }
105 #else
106   (void)dir_to_sync;
107   (void)force_bg;
108   (void)force_fg;
109   // SstFileManager is not supported in ROCKSDB_LITE
110   // Delete file immediately
111   return db_options->env->DeleteFile(fname);
112 #endif
113 }
114 
115 // requested_checksum_func_name brings the function name of the checksum
116 // generator in checksum_factory. Empty string is permitted, in which case the
117 // name of the generator created by the factory is unchecked. When
118 // `requested_checksum_func_name` is non-empty, however, the created generator's
119 // name must match it, otherwise an `InvalidArgument` error is returned.
GenerateOneFileChecksum(FileSystem * fs,const std::string & file_path,FileChecksumGenFactory * checksum_factory,const std::string & requested_checksum_func_name,std::string * file_checksum,std::string * file_checksum_func_name,size_t verify_checksums_readahead_size,bool allow_mmap_reads,std::shared_ptr<IOTracer> & io_tracer,RateLimiter * rate_limiter)120 IOStatus GenerateOneFileChecksum(
121     FileSystem* fs, const std::string& file_path,
122     FileChecksumGenFactory* checksum_factory,
123     const std::string& requested_checksum_func_name, std::string* file_checksum,
124     std::string* file_checksum_func_name,
125     size_t verify_checksums_readahead_size, bool allow_mmap_reads,
126     std::shared_ptr<IOTracer>& io_tracer, RateLimiter* rate_limiter) {
127   if (checksum_factory == nullptr) {
128     return IOStatus::InvalidArgument("Checksum factory is invalid");
129   }
130   assert(file_checksum != nullptr);
131   assert(file_checksum_func_name != nullptr);
132 
133   FileChecksumGenContext gen_context;
134   gen_context.requested_checksum_func_name = requested_checksum_func_name;
135   gen_context.file_name = file_path;
136   std::unique_ptr<FileChecksumGenerator> checksum_generator =
137       checksum_factory->CreateFileChecksumGenerator(gen_context);
138   if (checksum_generator == nullptr) {
139     std::string msg =
140         "Cannot get the file checksum generator based on the requested "
141         "checksum function name: " +
142         requested_checksum_func_name +
143         " from checksum factory: " + checksum_factory->Name();
144     return IOStatus::InvalidArgument(msg);
145   } else {
146     // For backward compatibility and use in file ingestion clients where there
147     // is no stored checksum function name, `requested_checksum_func_name` can
148     // be empty. If we give the requested checksum function name, we expect it
149     // is the same name of the checksum generator.
150     if (!requested_checksum_func_name.empty() &&
151         checksum_generator->Name() != requested_checksum_func_name) {
152       std::string msg = "Expected file checksum generator named '" +
153                         requested_checksum_func_name +
154                         "', while the factory created one "
155                         "named '" +
156                         checksum_generator->Name() + "'";
157       return IOStatus::InvalidArgument(msg);
158     }
159   }
160 
161   uint64_t size;
162   IOStatus io_s;
163   std::unique_ptr<RandomAccessFileReader> reader;
164   {
165     std::unique_ptr<FSRandomAccessFile> r_file;
166     io_s = fs->NewRandomAccessFile(file_path, FileOptions(), &r_file, nullptr);
167     if (!io_s.ok()) {
168       return io_s;
169     }
170     io_s = fs->GetFileSize(file_path, IOOptions(), &size, nullptr);
171     if (!io_s.ok()) {
172       return io_s;
173     }
174     reader.reset(new RandomAccessFileReader(std::move(r_file), file_path,
175                                             nullptr /*Env*/, io_tracer, nullptr,
176                                             0, nullptr, rate_limiter));
177   }
178 
179   // Found that 256 KB readahead size provides the best performance, based on
180   // experiments, for auto readahead. Experiment data is in PR #3282.
181   size_t default_max_read_ahead_size = 256 * 1024;
182   size_t readahead_size = (verify_checksums_readahead_size != 0)
183                               ? verify_checksums_readahead_size
184                               : default_max_read_ahead_size;
185 
186   FilePrefetchBuffer prefetch_buffer(
187       reader.get(), readahead_size /* readahead_size */,
188       readahead_size /* max_readahead_size */, !allow_mmap_reads /* enable */);
189 
190   Slice slice;
191   uint64_t offset = 0;
192   IOOptions opts;
193   while (size > 0) {
194     size_t bytes_to_read =
195         static_cast<size_t>(std::min(uint64_t{readahead_size}, size));
196     if (!prefetch_buffer.TryReadFromCache(opts, offset, bytes_to_read, &slice,
197                                           nullptr, false)) {
198       return IOStatus::Corruption("file read failed");
199     }
200     if (slice.size() == 0) {
201       return IOStatus::Corruption("file too small");
202     }
203     checksum_generator->Update(slice.data(), slice.size());
204     size -= slice.size();
205     offset += slice.size();
206   }
207   checksum_generator->Finalize();
208   *file_checksum = checksum_generator->GetChecksum();
209   *file_checksum_func_name = checksum_generator->Name();
210   return IOStatus::OK();
211 }
212 
DestroyDir(Env * env,const std::string & dir)213 Status DestroyDir(Env* env, const std::string& dir) {
214   Status s;
215   if (env->FileExists(dir).IsNotFound()) {
216     return s;
217   }
218   std::vector<std::string> files_in_dir;
219   s = env->GetChildren(dir, &files_in_dir);
220   if (s.ok()) {
221     for (auto& file_in_dir : files_in_dir) {
222       std::string path = dir + "/" + file_in_dir;
223       bool is_dir = false;
224       s = env->IsDirectory(path, &is_dir);
225       if (s.ok()) {
226         if (is_dir) {
227           s = DestroyDir(env, path);
228         } else {
229           s = env->DeleteFile(path);
230         }
231       } else if (s.IsNotSupported()) {
232         s = Status::OK();
233       }
234       if (!s.ok()) {
235         // IsDirectory, etc. might not report NotFound
236         if (s.IsNotFound() || env->FileExists(path).IsNotFound()) {
237           // Allow files to be deleted externally
238           s = Status::OK();
239         } else {
240           break;
241         }
242       }
243     }
244   }
245 
246   if (s.ok()) {
247     s = env->DeleteDir(dir);
248     // DeleteDir might or might not report NotFound
249     if (!s.ok() && (s.IsNotFound() || env->FileExists(dir).IsNotFound())) {
250       // Allow to be deleted externally
251       s = Status::OK();
252     }
253   }
254   return s;
255 }
256 
257 }  // namespace ROCKSDB_NAMESPACE
258