diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/system/fs.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/system/fs.h')
-rw-r--r-- | util/system/fs.h | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/util/system/fs.h b/util/system/fs.h new file mode 100644 index 0000000000..237daf2d2d --- /dev/null +++ b/util/system/fs.h @@ -0,0 +1,156 @@ +#pragma once + +#include <util/generic/flags.h> +#include <util/generic/string.h> +#include <util/generic/yexception.h> + +namespace NFs { + enum EFilePermission { + FP_ALL_EXEC = 01, + FP_ALL_WRITE = 02, + FP_ALL_READ = 04, + FP_GROUP_READ = 040, + FP_GROUP_WRITE = 020, + FP_GROUP_EXEC = 010, + FP_OWNER_READ = 0400, + FP_OWNER_WRITE = 0200, + FP_OWNER_EXEC = 0100, + + FP_COMMON_FILE = 0777, + FP_SECRET_FILE = 0700, + FP_NONSECRET_FILE = 0744, + }; + + Y_DECLARE_FLAGS(EFilePermissions, EFilePermission); + + /// Remove a file or empty directory + /// + /// @param[in] path Path to file or directory + /// @returns true on success or false otherwise + /// LastSystemError() is set in case of failure + bool Remove(const TString& path); + + /// Remove a file or directory with contents + /// + /// @param[in] path Path to file or directory + /// @throws + void RemoveRecursive(const TString& path); + + /// Creates directory + /// + /// @param[in] path Path to the directory + /// @param[in] mode Access permissions field; NOTE: ignored on win + /// @returns true on success or false otherwise + bool MakeDirectory(const TString& path, EFilePermissions mode); + + /// Creates directory + /// + /// @param[in] path Path to the directory + /// @returns true on success or false otherwise + /// NOTE: access permissions is set to 0777 + inline bool MakeDirectory(const TString& path) { + return MakeDirectory(path, FP_COMMON_FILE); + } + + /// Creates directory and all non-existings parents + /// + /// @param[in] path Path to be created + /// @param[in] mode Access permissions field; NOTE: ignored on win + /// @param[in] alwaysCreate Throw if path already exists or failed to create + /// @returns true if target path created or exists (and directory) + bool MakeDirectoryRecursive(const TString& path, EFilePermissions mode, bool alwaysCreate); + + /// Creates directory and all non-existings parents + /// + /// @param[in] path Path to be created + /// @param[in] mode Access permissions field; NOTE: ignored on win + /// @returns true if target path created or exists (and directory) + inline bool MakeDirectoryRecursive(const TString& path, EFilePermissions mode) { + return MakeDirectoryRecursive(path, mode, false); + } + + /// Creates directory and all non-existings parents + /// + /// @param[in] path Path to be created + /// @returns true if target path created or exists (and directory) + inline bool MakeDirectoryRecursive(const TString& path) { + return MakeDirectoryRecursive(path, FP_COMMON_FILE, false); + } + + /// Rename a file or directory. + /// Removes newPath if it exists + /// + /// @param[in] oldPath Path to file or directory to rename + /// @param[in] newPath New path of file or directory + /// @returns true on success or false otherwise + /// LastSystemError() is set in case of failure + bool Rename(const TString& oldPath, const TString& newPath); + + /// Creates a new directory entry for a file + /// or creates a new one with the same content + /// + /// @param[in] existingPath Path to an existing file + /// @param[in] newPath New path of file + void HardLinkOrCopy(const TString& existingPath, const TString& newPath); + + /// Creates a new directory entry for a file + /// + /// @param[in] existingPath Path to an existing file + /// @param[in] newPath New path of file + /// @returns true if new link was created or false otherwise + /// LastSystemError() is set in case of failure + bool HardLink(const TString& existingPath, const TString& newPath); + + /// Creates a symlink to a file + /// + /// @param[in] targetPath Path to a target file + /// @param[in] linkPath Path of symlink + /// @returns true if new link was created or false otherwise + /// LastSystemError() is set in case of failure + bool SymLink(const TString& targetPath, const TString& linkPath); + + /// Reads value of a symbolic link + /// + /// @param[in] path Path to a symlink + /// @returns File path that a symlink points to + TString ReadLink(const TString& path); + + /// Append contents of a file to a new file + /// + /// @param[in] dstPath Path to a destination file + /// @param[in] srcPath Path to a source file + void Cat(const TString& dstPath, const TString& srcPath); + + /// Copy contents of a file to a new file + /// + /// @param[in] existingPath Path to an existing file + /// @param[in] newPath New path of file + void Copy(const TString& existingPath, const TString& newPath); + + /// Returns path to the current working directory + /// + /// Note: is not threadsafe + TString CurrentWorkingDirectory(); + + /// Changes current working directory + /// + /// @param[in] path Path for new cwd + /// Note: is not threadsafe + void SetCurrentWorkingDirectory(TString path); + + /// Checks if file exists + /// + /// @param[in] path Path to check + bool Exists(const TString& path); + + /// Ensures that file exists + /// + /// @param[in] path Path to check + /// @returns input argument + inline const TString& EnsureExists(const TString& path) { + Y_ENSURE_EX(Exists(path), TFileError{} << "Path " << path << " does not exists (checked from cwd:" << NFs::CurrentWorkingDirectory() << ")"); + return path; + } +} + +Y_DECLARE_OPERATORS_FOR_FLAGS(NFs::EFilePermissions) |