blob: aa18ea7967ba75db88cdfb4735e70e4fba34a194 [file] [log] [blame]
Elliott Hughesdec12b22015-02-02 17:31:27 -08001/*
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
Elliott Hughes54c72aa2016-03-23 15:04:52 -070017#ifndef ANDROID_BASE_FILE_H
18#define ANDROID_BASE_FILE_H
Elliott Hughesdec12b22015-02-02 17:31:27 -080019
Nicolas Geoffraya7870d82015-03-16 10:29:36 +000020#include <sys/stat.h>
Dan Albertc007bc32015-03-16 10:08:46 -070021#include <string>
Elliott Hughesdec12b22015-02-02 17:31:27 -080022
Yabin Cui8e6f7222016-02-08 16:26:33 -080023#if !defined(_WIN32) && !defined(O_BINARY)
24#define O_BINARY 0
25#endif
26
Elliott Hughesdec12b22015-02-02 17:31:27 -080027namespace android {
Dan Albertc007bc32015-03-16 10:08:46 -070028namespace base {
Elliott Hughesdec12b22015-02-02 17:31:27 -080029
Elliott Hughesf682b472015-02-06 12:19:48 -080030bool ReadFdToString(int fd, std::string* content);
Elliott Hughesdec12b22015-02-02 17:31:27 -080031bool ReadFileToString(const std::string& path, std::string* content);
Elliott Hughesf682b472015-02-06 12:19:48 -080032
Elliott Hughesdec12b22015-02-02 17:31:27 -080033bool WriteStringToFile(const std::string& content, const std::string& path);
Elliott Hughesf682b472015-02-06 12:19:48 -080034bool WriteStringToFd(const std::string& content, int fd);
Elliott Hughesd79801d2015-02-05 08:21:37 -080035
36#if !defined(_WIN32)
Elliott Hughes202f0242015-02-04 13:19:13 -080037bool WriteStringToFile(const std::string& content, const std::string& path,
38 mode_t mode, uid_t owner, gid_t group);
Elliott Hughesd79801d2015-02-05 08:21:37 -080039#endif
Elliott Hughesdec12b22015-02-02 17:31:27 -080040
Elliott Hughes56085ed2015-04-24 21:57:16 -070041bool ReadFully(int fd, void* data, size_t byte_count);
42bool WriteFully(int fd, const void* data, size_t byte_count);
43
Yabin Cuib6e314a2016-01-29 17:25:54 -080044bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr);
45
Dan Albertc007bc32015-03-16 10:08:46 -070046} // namespace base
47} // namespace android
Elliott Hughesdec12b22015-02-02 17:31:27 -080048
Elliott Hughes54c72aa2016-03-23 15:04:52 -070049#endif // ANDROID_BASE_FILE_H