sandboxed-api/sandboxed_api/sandbox2/util/fileops.h
Christian Blichmann 441201884a Update license header with recommended best practices
PiperOrigin-RevId: 290250533
Change-Id: Ic34b253446463cf971a055b70a242df93a598ee3
2020-01-17 05:05:29 -08:00

104 lines
3.8 KiB
C++

// Copyright 2019 Google LLC
//
// 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 {
namespace fileops {
// RAII helper class to automatically close file descriptors.
class FDCloser {
public:
explicit FDCloser(int fd) : fd_{fd} {}
FDCloser(const FDCloser&) = delete;
FDCloser& operator=(const FDCloser&) = delete;
FDCloser(FDCloser&& other) : fd_(other.Release()) {}
~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.
bool ListDirectoryEntries(const std::string& directory,
std::vector<std::string>* entries,
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.
bool CopyFile(const std::string& old_path, const std::string& new_path,
int new_mode);
// 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_