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 failure
33 //
34 // Do not use this with large files
35 std::optional<std::string> ReadSmallFile(const std::string& path);
36 
37 // Implement ability to safely write to a file. This function is needed because of deficiencies in existing C++ file
38 // libraries, namely:
39 // - The ability to open and sync directories with storage media
40 // - The ability to block and sync file to storage media
41 // Return true on success, false on failure
42 bool WriteToFile(const std::string& path, const std::string& data);
43 
44 // Remove file and print error message if failed
45 // Print error log when file is failed to be removed, hence user should make sure file exists before calling this
46 // Return true on success, false on failure (e.g. file not exist, failed to remove, etc)
47 bool RemoveFile(const std::string& path);
48 
49 // Returns created time_point of given file, return std::nullopt on failure
50 std::optional<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> FileCreatedTime(
51     const std::string& path);
52 
53 }  // namespace os
54 }  // namespace bluetooth