xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/util/fileops.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2019 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li #ifndef SANDBOXED_API_UTIL_FILEOPS_H_
16*ec63e07aSXin Li #define SANDBOXED_API_UTIL_FILEOPS_H_
17*ec63e07aSXin Li 
18*ec63e07aSXin Li #include <cstddef>
19*ec63e07aSXin Li #include <string>
20*ec63e07aSXin Li #include <utility>
21*ec63e07aSXin Li #include <vector>
22*ec63e07aSXin Li 
23*ec63e07aSXin Li #include "absl/strings/string_view.h"
24*ec63e07aSXin Li 
25*ec63e07aSXin Li namespace sapi::file_util::fileops {
26*ec63e07aSXin Li 
27*ec63e07aSXin Li // RAII helper class to automatically close file descriptors.
28*ec63e07aSXin Li class FDCloser {
29*ec63e07aSXin Li  public:
30*ec63e07aSXin Li   explicit FDCloser(int fd = kCanonicalInvalidFd) : fd_{fd} {}
31*ec63e07aSXin Li   FDCloser(const FDCloser&) = delete;
32*ec63e07aSXin Li   FDCloser& operator=(const FDCloser&) = delete;
FDCloser(FDCloser && other)33*ec63e07aSXin Li   FDCloser(FDCloser&& other) : fd_(other.Release()) {}
34*ec63e07aSXin Li   FDCloser& operator=(FDCloser&& other) {
35*ec63e07aSXin Li     Swap(other);
36*ec63e07aSXin Li     other.Close();
37*ec63e07aSXin Li     return *this;
38*ec63e07aSXin Li   }
39*ec63e07aSXin Li   ~FDCloser();
40*ec63e07aSXin Li 
get()41*ec63e07aSXin Li   int get() const { return fd_; }
42*ec63e07aSXin Li   bool Close();
Swap(FDCloser & other)43*ec63e07aSXin Li   void Swap(FDCloser& other) { std::swap(fd_, other.fd_); }
44*ec63e07aSXin Li   int Release();
45*ec63e07aSXin Li 
46*ec63e07aSXin Li  private:
47*ec63e07aSXin Li   static constexpr int kCanonicalInvalidFd = -1;
48*ec63e07aSXin Li 
49*ec63e07aSXin Li   int fd_;
50*ec63e07aSXin Li };
51*ec63e07aSXin Li 
52*ec63e07aSXin Li // Returns the current working directory. On error, returns an empty string. Use
53*ec63e07aSXin Li // errno/GetLastError() to check the root cause in that case.
54*ec63e07aSXin Li std::string GetCWD();
55*ec63e07aSXin Li 
56*ec63e07aSXin Li // Returns the target of a symlink. Returns an empty string on failure.
57*ec63e07aSXin Li std::string ReadLink(const std::string& filename);
58*ec63e07aSXin Li 
59*ec63e07aSXin Li // Reads the absolute path to the symlink target into result. Returns true on
60*ec63e07aSXin Li // success. result and filename may be aliased.
61*ec63e07aSXin Li bool ReadLinkAbsolute(const std::string& filename, std::string* result);
62*ec63e07aSXin Li 
63*ec63e07aSXin Li // Removes the last path component. Returns false if there was no path
64*ec63e07aSXin Li // component (path is / or ""). If this function returns false, *output will be
65*ec63e07aSXin Li // equal to "/" or "" if the file is absolute or relative, respectively. output
66*ec63e07aSXin Li // and file may refer to the same string.
67*ec63e07aSXin Li bool RemoveLastPathComponent(const std::string& file, std::string* output);
68*ec63e07aSXin Li 
69*ec63e07aSXin Li // Returns a file's basename, i.e.
70*ec63e07aSXin Li // If the input path has a trailing slash, the basename is assumed to be
71*ec63e07aSXin Li // empty, e.g. StripBasename("/hello/") == "/hello".
72*ec63e07aSXin Li // Does no path cleanups; the result is always a prefix/ suffix of the
73*ec63e07aSXin Li // passed string.
74*ec63e07aSXin Li std::string Basename(absl::string_view path);
75*ec63e07aSXin Li 
76*ec63e07aSXin Li // Like above, but returns a file's directory name.
77*ec63e07aSXin Li std::string StripBasename(absl::string_view path);
78*ec63e07aSXin Li 
79*ec63e07aSXin Li // Tests whether filename exists. If fully_resolve is true, then all symlinks
80*ec63e07aSXin Li // are resolved to verify the target exists. Otherwise, this function
81*ec63e07aSXin Li // verifies only that the file exists. It may still be a symlink with a
82*ec63e07aSXin Li // missing target.
83*ec63e07aSXin Li bool Exists(const std::string& filename, bool fully_resolve);
84*ec63e07aSXin Li 
85*ec63e07aSXin Li // Reads a directory and fills entries with all the files in that directory.
86*ec63e07aSXin Li // On error, false is returned and error is set to a description of the
87*ec63e07aSXin Li // error. The filenames in entries are just the basenames of the
88*ec63e07aSXin Li // files found.
89*ec63e07aSXin Li bool ListDirectoryEntries(const std::string& directory,
90*ec63e07aSXin Li                           std::vector<std::string>* entries,
91*ec63e07aSXin Li                           std::string* error);
92*ec63e07aSXin Li 
93*ec63e07aSXin Li // Recursively creates a directory, skipping segments that already exist.
94*ec63e07aSXin Li bool CreateDirectoryRecursively(const std::string& path, int mode);
95*ec63e07aSXin Li 
96*ec63e07aSXin Li // Deletes the specified file or directory, including any sub-directories.
97*ec63e07aSXin Li bool DeleteRecursively(const std::string& filename);
98*ec63e07aSXin Li 
99*ec63e07aSXin Li // Copies a file from one location to another. The file will be overwritten  if
100*ec63e07aSXin Li // it already exists. If it does not exist, its mode will be new_mode. Returns
101*ec63e07aSXin Li // true on success. On failure, a partial copy of the file may remain.
102*ec63e07aSXin Li bool CopyFile(const std::string& old_path, const std::string& new_path,
103*ec63e07aSXin Li               int new_mode);
104*ec63e07aSXin Li 
105*ec63e07aSXin Li // Makes filename absolute with respect to base. Returns an empty string on
106*ec63e07aSXin Li // failure.
107*ec63e07aSXin Li std::string MakeAbsolute(const std::string& filename, const std::string& base);
108*ec63e07aSXin Li 
109*ec63e07aSXin Li // Writes data to a file descriptor. The file descriptor should be blocking.
110*ec63e07aSXin Li // Returns true on success.
111*ec63e07aSXin Li bool WriteToFD(int fd, const char* data, size_t size);
112*ec63e07aSXin Li 
113*ec63e07aSXin Li }  // namespace sapi::file_util::fileops
114*ec63e07aSXin Li 
115*ec63e07aSXin Li #endif  // SANDBOXED_API_UTIL_FILEOPS_H_
116