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 #pragma once
7 #include <string>
8 
9 #include "file/filename.h"
10 #include "options/db_options.h"
11 #include "rocksdb/env.h"
12 #include "rocksdb/file_system.h"
13 #include "rocksdb/sst_file_writer.h"
14 #include "rocksdb/status.h"
15 #include "rocksdb/system_clock.h"
16 #include "rocksdb/types.h"
17 #include "trace_replay/io_tracer.h"
18 
19 namespace ROCKSDB_NAMESPACE {
20 // use_fsync maps to options.use_fsync, which determines the way that
21 // the file is synced after copying.
22 extern IOStatus CopyFile(FileSystem* fs, const std::string& source,
23                          const std::string& destination, uint64_t size,
24                          bool use_fsync,
25                          const std::shared_ptr<IOTracer>& io_tracer = nullptr);
26 inline IOStatus CopyFile(const std::shared_ptr<FileSystem>& fs,
27                          const std::string& source,
28                          const std::string& destination, uint64_t size,
29                          bool use_fsync,
30                          const std::shared_ptr<IOTracer>& io_tracer = nullptr) {
31   return CopyFile(fs.get(), source, destination, size, use_fsync, io_tracer);
32 }
33 
34 extern IOStatus CreateFile(FileSystem* fs, const std::string& destination,
35                            const std::string& contents, bool use_fsync);
36 
CreateFile(const std::shared_ptr<FileSystem> & fs,const std::string & destination,const std::string & contents,bool use_fsync)37 inline IOStatus CreateFile(const std::shared_ptr<FileSystem>& fs,
38                            const std::string& destination,
39                            const std::string& contents, bool use_fsync) {
40   return CreateFile(fs.get(), destination, contents, use_fsync);
41 }
42 
43 extern Status DeleteDBFile(const ImmutableDBOptions* db_options,
44                            const std::string& fname,
45                            const std::string& path_to_sync, const bool force_bg,
46                            const bool force_fg);
47 
48 extern bool IsWalDirSameAsDBPath(const ImmutableDBOptions* db_options);
49 
50 extern IOStatus GenerateOneFileChecksum(
51     FileSystem* fs, const std::string& file_path,
52     FileChecksumGenFactory* checksum_factory,
53     const std::string& requested_checksum_func_name, std::string* file_checksum,
54     std::string* file_checksum_func_name,
55     size_t verify_checksums_readahead_size, bool allow_mmap_reads,
56     std::shared_ptr<IOTracer>& io_tracer, RateLimiter* rate_limiter = nullptr);
57 
GenerateOneFileChecksum(const std::shared_ptr<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)58 inline IOStatus GenerateOneFileChecksum(
59     const std::shared_ptr<FileSystem>& fs, const std::string& file_path,
60     FileChecksumGenFactory* checksum_factory,
61     const std::string& requested_checksum_func_name, std::string* file_checksum,
62     std::string* file_checksum_func_name,
63     size_t verify_checksums_readahead_size, bool allow_mmap_reads,
64     std::shared_ptr<IOTracer>& io_tracer) {
65   return GenerateOneFileChecksum(
66       fs.get(), file_path, checksum_factory, requested_checksum_func_name,
67       file_checksum, file_checksum_func_name, verify_checksums_readahead_size,
68       allow_mmap_reads, io_tracer);
69 }
70 
PrepareIOFromReadOptions(const ReadOptions & ro,SystemClock * clock,IOOptions & opts)71 inline IOStatus PrepareIOFromReadOptions(const ReadOptions& ro,
72                                          SystemClock* clock, IOOptions& opts) {
73   if (ro.deadline.count()) {
74     std::chrono::microseconds now =
75         std::chrono::microseconds(clock->NowMicros());
76     // Ensure there is atleast 1us available. We don't want to pass a value of
77     // 0 as that means no timeout
78     if (now >= ro.deadline) {
79       return IOStatus::TimedOut("Deadline exceeded");
80     }
81     opts.timeout = ro.deadline - now;
82   }
83 
84   if (ro.io_timeout.count() &&
85       (!opts.timeout.count() || ro.io_timeout < opts.timeout)) {
86     opts.timeout = ro.io_timeout;
87   }
88   return IOStatus::OK();
89 }
90 
91 // Test method to delete the input directory and all of its contents.
92 // This method is destructive and is meant for use only in tests!!!
93 Status DestroyDir(Env* env, const std::string& dir);
94 }  // namespace ROCKSDB_NAMESPACE
95