• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
25 #include "android-base/off64_t.h"
26 
27 #if !defined(_WIN32) && !defined(O_BINARY)
28 /** Windows needs O_BINARY, but Unix never mangles line endings. */
29 #define O_BINARY 0
30 #endif
31 
32 #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 
37 class 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.
DoNotRemove()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 
60 class TemporaryDir {
61  public:
62   TemporaryDir();
63   ~TemporaryDir();
64   // Don't remove the temporary dir in the destructor.
DoNotRemove()65   void DoNotRemove() { remove_dir_and_contents_ = false; }
66 
67   char path[1024];
68 
69  private:
70   bool init(const std::string& tmp_dir);
71 
72   bool remove_dir_and_contents_ = true;
73 
74   DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
75 };
76 
77 namespace android {
78 namespace base {
79 
80 bool ReadFdToString(int fd, std::string* content);
81 bool ReadFileToString(const std::string& path, std::string* content,
82                       bool follow_symlinks = false);
83 
84 bool WriteStringToFile(const std::string& content, const std::string& path,
85                        bool follow_symlinks = false);
86 bool WriteStringToFd(const std::string& content, int fd);
87 
88 #if !defined(_WIN32)
89 bool WriteStringToFile(const std::string& content, const std::string& path,
90                        mode_t mode, uid_t owner, gid_t group,
91                        bool follow_symlinks = false);
92 #endif
93 
94 bool ReadFully(int fd, void* data, size_t byte_count);
95 
96 // Reads `byte_count` bytes from the file descriptor at the specified offset.
97 // Returns false if there was an IO error or EOF was reached before reading `byte_count` bytes.
98 //
99 // NOTE: On Linux/Mac, this function wraps pread, which provides atomic read support without
100 // modifying the read pointer of the file descriptor. On Windows, however, the read pointer does
101 // get modified. This means that ReadFullyAtOffset can be used concurrently with other calls to the
102 // same function, but concurrently seeking or reading incrementally can lead to unexpected
103 // behavior.
104 bool ReadFullyAtOffset(int fd, void* data, size_t byte_count, off64_t offset);
105 
106 bool WriteFully(int fd, const void* data, size_t byte_count);
107 
108 bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr);
109 
110 #if !defined(_WIN32)
111 bool Realpath(const std::string& path, std::string* result);
112 bool Readlink(const std::string& path, std::string* result);
113 #endif
114 
115 std::string GetExecutablePath();
116 std::string GetExecutableDirectory();
117 
118 // Like the regular basename and dirname, but thread-safe on all
119 // platforms and capable of correctly handling exotic Windows paths.
120 std::string Basename(const std::string& path);
121 std::string Dirname(const std::string& path);
122 
123 }  // namespace base
124 }  // namespace android
125