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
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_SANDBOX2_UTIL_FILEOPS_H_
|
|
|
|
#define SANDBOXED_API_SANDBOX2_UTIL_FILEOPS_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "absl/strings/string_view.h"
|
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
namespace file_util {
|
2019-03-19 18:40:51 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
namespace fileops {
|
|
|
|
|
|
|
|
// RAII helper class to automatically close file descriptors.
|
|
|
|
class FDCloser {
|
|
|
|
public:
|
|
|
|
explicit FDCloser(int fd) : 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()) {}
|
2019-03-19 00:21:48 +08:00
|
|
|
~FDCloser();
|
|
|
|
|
|
|
|
int get() const { return fd_; }
|
|
|
|
bool Close();
|
|
|
|
int Release();
|
|
|
|
|
|
|
|
private:
|
|
|
|
int fd_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
} // namespace fileops
|
|
|
|
} // namespace file_util
|
|
|
|
} // namespace sandbox2
|
|
|
|
|
|
|
|
#endif // SANDBOXED_API_SANDBOX2_UTIL_FILEOPS_H_
|