2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
#ifndef SANDBOXED_API_UTIL_FILEOPS_H_
|
|
|
|
#define SANDBOXED_API_UTIL_FILEOPS_H_
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <cstddef>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <string>
|
2021-01-11 19:33:03 +08:00
|
|
|
#include <utility>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "absl/strings/string_view.h"
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace sapi::file_util::fileops {
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// RAII helper class to automatically close file descriptors.
|
|
|
|
class FDCloser {
|
|
|
|
public:
|
2021-01-11 19:33:03 +08:00
|
|
|
explicit FDCloser(int fd = kCanonicalInvalidFd) : fd_{fd} {}
|
2019-04-12 22:54:10 +08:00
|
|
|
FDCloser(const FDCloser&) = delete;
|
|
|
|
FDCloser& operator=(const FDCloser&) = delete;
|
|
|
|
FDCloser(FDCloser&& other) : fd_(other.Release()) {}
|
2021-01-11 19:33:03 +08:00
|
|
|
FDCloser& operator=(FDCloser&& other) {
|
|
|
|
Swap(other);
|
|
|
|
other.Close();
|
|
|
|
return *this;
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
~FDCloser();
|
|
|
|
|
|
|
|
int get() const { return fd_; }
|
|
|
|
bool Close();
|
2021-01-11 19:33:03 +08:00
|
|
|
void Swap(FDCloser& other) { std::swap(fd_, other.fd_); }
|
2019-03-19 00:21:48 +08:00
|
|
|
int Release();
|
|
|
|
|
|
|
|
private:
|
2021-01-11 19:33:03 +08:00
|
|
|
static constexpr int kCanonicalInvalidFd = -1;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
int fd_;
|
|
|
|
};
|
|
|
|
|
2019-06-25 20:48:56 +08:00
|
|
|
// Returns the current working directory. On error, returns an empty string. Use
|
|
|
|
// errno/GetLastError() to check the root cause in that case.
|
|
|
|
std::string GetCWD();
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Returns the target of a symlink. Returns an empty string on failure.
|
|
|
|
std::string ReadLink(const std::string& filename);
|
|
|
|
|
|
|
|
// Reads the absolute path to the symlink target into result. Returns true on
|
|
|
|
// success. result and filename may be aliased.
|
|
|
|
bool ReadLinkAbsolute(const std::string& filename, std::string* result);
|
|
|
|
|
|
|
|
// Removes the last path component. Returns false if there was no path
|
|
|
|
// component (path is / or ""). If this function returns false, *output will be
|
|
|
|
// equal to "/" or "" if the file is absolute or relative, respectively. output
|
|
|
|
// and file may refer to the same string.
|
|
|
|
bool RemoveLastPathComponent(const std::string& file, std::string* output);
|
|
|
|
|
|
|
|
// Returns a file's basename, i.e.
|
|
|
|
// If the input path has a trailing slash, the basename is assumed to be
|
|
|
|
// empty, e.g. StripBasename("/hello/") == "/hello".
|
|
|
|
// Does no path cleanups; the result is always a prefix/ suffix of the
|
|
|
|
// passed string.
|
|
|
|
std::string Basename(absl::string_view path);
|
|
|
|
|
|
|
|
// Like above, but returns a file's directory name.
|
|
|
|
std::string StripBasename(absl::string_view path);
|
|
|
|
|
|
|
|
// Tests whether filename exists. If fully_resolve is true, then all symlinks
|
|
|
|
// are resolved to verify the target exists. Otherwise, this function
|
|
|
|
// verifies only that the file exists. It may still be a symlink with a
|
|
|
|
// missing target.
|
|
|
|
bool Exists(const std::string& filename, bool fully_resolve);
|
|
|
|
|
|
|
|
// Reads a directory and fills entries with all the files in that directory.
|
|
|
|
// On error, false is returned and error is set to a description of the
|
|
|
|
// error. The filenames in entries are just the basenames of the
|
|
|
|
// files found.
|
2019-03-26 22:54:02 +08:00
|
|
|
bool ListDirectoryEntries(const std::string& directory,
|
|
|
|
std::vector<std::string>* entries,
|
2019-03-19 00:21:48 +08:00
|
|
|
std::string* error);
|
|
|
|
|
2023-02-17 02:43:25 +08:00
|
|
|
// Recursively creates a directory, skipping segments that already exist.
|
|
|
|
bool CreateDirectoryRecursively(const std::string& path, int mode);
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Deletes the specified file or directory, including any sub-directories.
|
|
|
|
bool DeleteRecursively(const std::string& filename);
|
|
|
|
|
|
|
|
// Copies a file from one location to another. The file will be overwritten if
|
|
|
|
// it already exists. If it does not exist, its mode will be new_mode. Returns
|
|
|
|
// true on success. On failure, a partial copy of the file may remain.
|
2019-03-26 22:54:02 +08:00
|
|
|
bool CopyFile(const std::string& old_path, const std::string& new_path,
|
|
|
|
int new_mode);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Makes filename absolute with respect to base. Returns an empty string on
|
|
|
|
// failure.
|
|
|
|
std::string MakeAbsolute(const std::string& filename, const std::string& base);
|
|
|
|
|
|
|
|
// Writes data to a file descriptor. The file descriptor should be blocking.
|
|
|
|
// Returns true on success.
|
|
|
|
bool WriteToFD(int fd, const char* data, size_t size);
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
} // namespace sapi::file_util::fileops
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
#endif // SANDBOXED_API_UTIL_FILEOPS_H_
|