summaryrefslogtreecommitdiff
path: root/system/gd/os/files.h
blob: 5802aa1e674c027cb574cbc8b25731e80741c92e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <chrono>
#include <iterator>
#include <optional>

namespace bluetooth {
namespace os {

// Return true if |path| exists on disk
bool FileExists(const std::string& path);

// Rename file from |from| to |to|
bool RenameFile(const std::string& from, const std::string& to);

// Implement ability to read a whole file from |path| into a C++ string, return std::nullopt on failure
//
// Do not use this with large files
std::optional<std::string> ReadSmallFile(const std::string& path);

// Implement ability to safely write to a file. This function is needed because of deficiencies in existing C++ file
// libraries, namely:
// - The ability to open and sync directories with storage media
// - The ability to block and sync file to storage media
// Return true on success, false on failure
bool WriteToFile(const std::string& path, const std::string& data);

// Remove file and print error message if failed
// Print error log when file is failed to be removed, hence user should make sure file exists before calling this
// Return true on success, false on failure (e.g. file not exist, failed to remove, etc)
bool RemoveFile(const std::string& path);

// Returns created time_point of given file, return std::nullopt on failure
std::optional<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> FileCreatedTime(
    const std::string& path);

}  // namespace os
}  // namespace bluetooth