1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <chrono>
20 #include <iterator>
21 #include <optional>
22 
23 namespace bluetooth {
24 namespace os {
25 
26 // Return true if |path| exists on disk
27 bool FileExists(const std::string& path);
28 
29 // Rename file from |from| to |to|
30 bool RenameFile(const std::string& from, const std::string& to);
31 
32 // Implement ability to read a whole file from |path| into a C++ string, return std::nullopt on
33 // failure
34 //
35 // Do not use this with large files
36 std::optional<std::string> ReadSmallFile(const std::string& path);
37 
38 // Implement ability to safely write to a file. This function is needed because of deficiencies in
39 // existing C++ file libraries, namely:
40 // - The ability to open and sync directories with storage media
41 // - The ability to block and sync file to storage media
42 // Return true on success, false on failure
43 bool WriteToFile(const std::string& path, const std::string& data);
44 
45 // Remove file and print error message if failed
46 // Print error log when file is failed to be removed, hence user should make sure file exists before
47 // calling this Return true on success, false on failure (e.g. file not exist, failed to remove,
48 // etc)
49 bool RemoveFile(const std::string& path);
50 
51 // Returns created time_point of given file, return std::nullopt on failure
52 std::optional<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>>
53 FileCreatedTime(const std::string& path);
54 
55 }  // namespace os
56 }  // namespace bluetooth
57