blob: 80d71346d9975f9a9da5867732999795750c0215 [file] [log] [blame]
Dan Albertaac6b7c2015-03-16 10:08:46 -07001/*
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 Hughese3c5a2a2018-06-26 17:17:41 -070017#pragma once
Dan Albertaac6b7c2015-03-16 10:08:46 -070018
19#include <sys/stat.h>
Adam Lesinski617977a2017-07-25 16:26:23 -070020#include <sys/types.h>
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080021
Dan Albertaac6b7c2015-03-16 10:08:46 -070022#include <string>
23
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080024#include <android-base/macros.h>
Elliott Hughesc0f3e4a2018-10-25 16:29:02 -070025#include "android-base/off64_t.h"
26
Yabin Cui5b3be172016-02-08 16:26:33 -080027#if !defined(_WIN32) && !defined(O_BINARY)
Elliott Hughes2f5c3ff2018-10-24 14:06:45 -070028/** Windows needs O_BINARY, but Unix never mangles line endings. */
Yabin Cui5b3be172016-02-08 16:26:33 -080029#define O_BINARY 0
30#endif
31
Elliott Hughes2f5c3ff2018-10-24 14:06:45 -070032#if defined(_WIN32) && !defined(O_CLOEXEC)
33/** Windows has O_CLOEXEC but calls it O_NOINHERIT for some reason. */
34#define O_CLOEXEC O_NOINHERIT
35#endif
36
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080037class TemporaryFile {
38 public:
39 TemporaryFile();
40 explicit TemporaryFile(const std::string& tmp_dir);
41 ~TemporaryFile();
42
43 // Release the ownership of fd, caller is reponsible for closing the
44 // fd or stream properly.
45 int release();
46 // Don't remove the temporary file in the destructor.
47 void DoNotRemove() { remove_file_ = false; }
48
49 int fd;
50 char path[1024];
51
52 private:
53 void init(const std::string& tmp_dir);
54
55 bool remove_file_ = true;
56
57 DISALLOW_COPY_AND_ASSIGN(TemporaryFile);
58};
59
60class TemporaryDir {
61 public:
62 TemporaryDir();
63 ~TemporaryDir();
64
65 char path[1024];
66
67 private:
68 bool init(const std::string& tmp_dir);
69
70 DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
71};
72
Dan Albertaac6b7c2015-03-16 10:08:46 -070073namespace android {
74namespace base {
75
76bool ReadFdToString(int fd, std::string* content);
Josh Gao8e1f0d82016-09-14 16:11:45 -070077bool ReadFileToString(const std::string& path, std::string* content,
78 bool follow_symlinks = false);
Dan Albertaac6b7c2015-03-16 10:08:46 -070079
Josh Gao8e1f0d82016-09-14 16:11:45 -070080bool WriteStringToFile(const std::string& content, const std::string& path,
81 bool follow_symlinks = false);
Dan Albertaac6b7c2015-03-16 10:08:46 -070082bool WriteStringToFd(const std::string& content, int fd);
83
84#if !defined(_WIN32)
85bool WriteStringToFile(const std::string& content, const std::string& path,
Josh Gao8e1f0d82016-09-14 16:11:45 -070086 mode_t mode, uid_t owner, gid_t group,
87 bool follow_symlinks = false);
Dan Albertaac6b7c2015-03-16 10:08:46 -070088#endif
89
Elliott Hughes20abc872015-04-24 21:57:16 -070090bool ReadFully(int fd, void* data, size_t byte_count);
Adam Lesinski6d6f9b32017-06-19 10:27:38 -070091
92// Reads `byte_count` bytes from the file descriptor at the specified offset.
93// Returns false if there was an IO error or EOF was reached before reading `byte_count` bytes.
94//
95// NOTE: On Linux/Mac, this function wraps pread, which provides atomic read support without
96// modifying the read pointer of the file descriptor. On Windows, however, the read pointer does
97// get modified. This means that ReadFullyAtOffset can be used concurrently with other calls to the
98// same function, but concurrently seeking or reading incrementally can lead to unexpected
99// behavior.
100bool ReadFullyAtOffset(int fd, void* data, size_t byte_count, off64_t offset);
101
Elliott Hughes20abc872015-04-24 21:57:16 -0700102bool WriteFully(int fd, const void* data, size_t byte_count);
103
Yabin Cui8f6a5a02016-01-29 17:25:54 -0800104bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr);
105
Elliott Hughesa634a9a2016-08-23 15:53:45 -0700106#if !defined(_WIN32)
Dimitry Ivanov4edc04f2016-09-09 10:49:21 -0700107bool Realpath(const std::string& path, std::string* result);
Elliott Hughesa634a9a2016-08-23 15:53:45 -0700108bool Readlink(const std::string& path, std::string* result);
109#endif
110
Elliott Hughes48f0eb52016-08-31 15:07:18 -0700111std::string GetExecutablePath();
Colin Cross2909be02017-02-23 17:41:56 -0800112std::string GetExecutableDirectory();
Elliott Hughes48f0eb52016-08-31 15:07:18 -0700113
Colin Cross2e732e22017-02-23 21:23:05 -0800114// Like the regular basename and dirname, but thread-safe on all
115// platforms and capable of correctly handling exotic Windows paths.
116std::string Basename(const std::string& path);
117std::string Dirname(const std::string& path);
118
Dan Albertaac6b7c2015-03-16 10:08:46 -0700119} // namespace base
120} // namespace android