Android-cuttlefish cvd tool
file.h
Go to the documentation of this file.
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#pragma once
18
19#include <sys/stat.h>
20#include <sys/types.h>
21
22#include <string>
23
24#include "android-base/macros.h"
27
28#if !defined(_WIN32) && !defined(O_BINARY)
30#define O_BINARY 0
31#endif
32
33#if defined(_WIN32) && !defined(O_CLOEXEC)
35#define O_CLOEXEC O_NOINHERIT
36#endif
37
39 public:
41 explicit TemporaryFile(const std::string& tmp_dir);
43
44 // Release the ownership of fd, caller is responsible for closing the
45 // fd or stream properly.
46 int release();
47 // Don't remove the temporary file in the destructor.
48 void DoNotRemove() { remove_file_ = false; }
49
50 int fd;
51 char path[1024];
52
53 private:
54 void init(const std::string& tmp_dir);
55
56 bool remove_file_ = true;
57
59};
60
62 public:
65 // Don't remove the temporary dir in the destructor.
67
68 char path[1024];
69
70 private:
71 bool init(const std::string& tmp_dir);
72
74
76};
77
78namespace android {
79namespace base {
80
81bool ReadFdToString(borrowed_fd fd, std::string* content);
82bool ReadFileToString(const std::string& path, std::string* content,
83 bool follow_symlinks = false);
84
85bool WriteStringToFile(const std::string& content, const std::string& path,
86 bool follow_symlinks = false);
87bool WriteStringToFd(std::string_view content, borrowed_fd fd);
88
89#if !defined(_WIN32)
90bool WriteStringToFile(const std::string& content, const std::string& path,
91 mode_t mode, uid_t owner, gid_t group,
92 bool follow_symlinks = false);
93#endif
94
95bool ReadFully(borrowed_fd fd, void* data, size_t byte_count);
96
97// Reads `byte_count` bytes from the file descriptor at the specified offset.
98// Returns false if there was an IO error or EOF was reached before reading `byte_count` bytes.
99//
100// NOTE: On Linux/Mac, this function wraps pread, which provides atomic read support without
101// modifying the read pointer of the file descriptor. On Windows, however, the read pointer does
102// get modified. This means that ReadFullyAtOffset can be used concurrently with other calls to the
103// same function, but concurrently seeking or reading incrementally can lead to unexpected
104// behavior.
105bool ReadFullyAtOffset(borrowed_fd fd, void* data, size_t byte_count, off64_t offset);
106
107bool WriteFully(borrowed_fd fd, const void* data, size_t byte_count);
108bool WriteFullyAtOffset(borrowed_fd fd, const void* data, size_t byte_count, off64_t offset);
109
110bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr);
111
112#if !defined(_WIN32)
113bool Realpath(const std::string& path, std::string* result);
114bool Readlink(const std::string& path, std::string* result);
115#endif
116
117std::string GetExecutablePath();
118std::string GetExecutableDirectory();
119
120// Like the regular basename and dirname, but thread-safe on all
121// platforms and capable of correctly handling exotic Windows paths.
122std::string Basename(std::string_view path);
123std::string Dirname(std::string_view path);
124
125} // namespace base
126} // namespace android
Definition: file.h:61
DISALLOW_COPY_AND_ASSIGN(TemporaryDir)
TemporaryDir()
Definition: file.cpp:168
char path[1024]
Definition: file.h:68
bool remove_dir_and_contents_
Definition: file.h:73
void DoNotRemove()
Definition: file.h:66
~TemporaryDir()
Definition: file.cpp:172
bool init(const std::string &tmp_dir)
Definition: file.cpp:203
Definition: file.h:38
bool remove_file_
Definition: file.h:56
void init(const std::string &tmp_dir)
Definition: file.cpp:159
int fd
Definition: file.h:50
int release()
Definition: file.cpp:153
~TemporaryFile()
Definition: file.cpp:144
DISALLOW_COPY_AND_ASSIGN(TemporaryFile)
char path[1024]
Definition: file.h:51
TemporaryFile()
Definition: file.cpp:136
void DoNotRemove()
Definition: file.h:48
char data[Size]
Definition: incremental_server.cpp:1
bool ReadFdToString(borrowed_fd fd, std::string *content)
Definition: file.cpp:218
bool ReadFully(borrowed_fd fd, void *data, size_t byte_count)
Definition: file.cpp:311
bool WriteFullyAtOffset(borrowed_fd fd, const void *data, size_t byte_count, off64_t offset)
Definition: file.cpp:369
bool WriteStringToFile(const std::string &content, const std::string &path, mode_t mode, uid_t owner, gid_t group, bool follow_symlinks)
Definition: file.cpp:271
std::string GetExecutableDirectory()
Definition: file.cpp:498
std::string GetExecutablePath()
Definition: file.cpp:470
bool ReadFileToString(const std::string &path, std::string *content, bool follow_symlinks)
Definition: file.cpp:237
std::string Basename(std::string_view path)
Definition: file.cpp:580
bool Realpath(const std::string &path, std::string *result)
Definition: file.cpp:452
std::string Dirname(std::string_view path)
Definition: file.cpp:672
bool ReadFullyAtOffset(borrowed_fd fd, void *data, size_t byte_count, off64_t offset)
Definition: file.cpp:357
bool WriteFully(borrowed_fd fd, const void *data, size_t byte_count)
Definition: file.cpp:382
bool Readlink(const std::string &path, std::string *result)
Definition: file.cpp:428
bool WriteStringToFd(std::string_view content, borrowed_fd fd)
Definition: file.cpp:248
bool RemoveFileIfExists(const std::string &path, std::string *err)
Definition: file.cpp:394
Definition: map_ptr.h:34