1 /*
2  * Copyright (C) 2015 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 #ifndef ANDROID_BASE_FILE_H
18 #define ANDROID_BASE_FILE_H
19 
20 #include <sys/stat.h>
21 #include <string>
22 
23 #if !defined(_WIN32) && !defined(O_BINARY)
24 #define O_BINARY 0
25 #endif
26 
27 namespace android {
28 namespace base {
29 
30 bool ReadFdToString(int fd, std::string* content);
31 bool ReadFileToString(const std::string& path, std::string* content,
32                       bool follow_symlinks = false);
33 
34 bool WriteStringToFile(const std::string& content, const std::string& path,
35                        bool follow_symlinks = false);
36 bool WriteStringToFd(const std::string& content, int fd);
37 
38 #if !defined(_WIN32)
39 bool WriteStringToFile(const std::string& content, const std::string& path,
40                        mode_t mode, uid_t owner, gid_t group,
41                        bool follow_symlinks = false);
42 #endif
43 
44 bool ReadFully(int fd, void* data, size_t byte_count);
45 bool WriteFully(int fd, const void* data, size_t byte_count);
46 
47 bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr);
48 
49 #if !defined(_WIN32)
50 bool Realpath(const std::string& path, std::string* result);
51 bool Readlink(const std::string& path, std::string* result);
52 #endif
53 
54 std::string GetExecutablePath();
55 std::string GetExecutableDirectory();
56 
57 // Like the regular basename and dirname, but thread-safe on all
58 // platforms and capable of correctly handling exotic Windows paths.
59 std::string Basename(const std::string& path);
60 std::string Dirname(const std::string& path);
61 
62 }  // namespace base
63 }  // namespace android
64 
65 #endif // ANDROID_BASE_FILE_H
66