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