fs.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #pragma once
  2. #include <util/generic/flags.h>
  3. #include <util/generic/string.h>
  4. #include <util/generic/yexception.h>
  5. namespace NFs {
  6. enum EFilePermission {
  7. FP_ALL_EXEC = 01,
  8. FP_ALL_WRITE = 02,
  9. FP_ALL_READ = 04,
  10. FP_GROUP_READ = 040,
  11. FP_GROUP_WRITE = 020,
  12. FP_GROUP_EXEC = 010,
  13. FP_OWNER_READ = 0400,
  14. FP_OWNER_WRITE = 0200,
  15. FP_OWNER_EXEC = 0100,
  16. FP_COMMON_FILE = 0777,
  17. FP_SECRET_FILE = 0700,
  18. FP_NONSECRET_FILE = 0744,
  19. };
  20. Y_DECLARE_FLAGS(EFilePermissions, EFilePermission);
  21. /// Add executable bit
  22. ///
  23. /// @param[in] path Path to mark as executable
  24. /// @param[in] exec New value of execution bit
  25. /// @returns true if bit has changed or false otherwise
  26. bool SetExecutable(const TString& path, bool exec);
  27. /// Remove a file or empty directory
  28. ///
  29. /// @param[in] path Path to file or directory
  30. /// @returns true on success or false otherwise
  31. /// LastSystemError() is set in case of failure
  32. bool Remove(const TString& path);
  33. /// Remove a file or directory with contents
  34. /// Does nothing if path does not exist
  35. ///
  36. /// @param[in] path Path to file or directory
  37. /// @throws
  38. void RemoveRecursive(const TString& path);
  39. /// Creates directory
  40. ///
  41. /// @param[in] path Path to the directory
  42. /// @param[in] mode Access permissions field; NOTE: ignored on win
  43. /// @returns true on success or false otherwise
  44. bool MakeDirectory(const TString& path, EFilePermissions mode);
  45. /// Creates directory
  46. ///
  47. /// @param[in] path Path to the directory
  48. /// @returns true on success or false otherwise
  49. /// NOTE: access permissions is set to 0777
  50. inline bool MakeDirectory(const TString& path) {
  51. return MakeDirectory(path, FP_COMMON_FILE);
  52. }
  53. /// Creates directory and all non-existings parents
  54. ///
  55. /// @param[in] path Path to be created
  56. /// @param[in] mode Access permissions field; NOTE: ignored on win
  57. /// @param[in] alwaysCreate Throw if path already exists or failed to create
  58. /// @returns true if target path created or exists (and directory)
  59. bool MakeDirectoryRecursive(const TString& path, EFilePermissions mode, bool alwaysCreate);
  60. /// Creates directory and all non-existings parents
  61. ///
  62. /// @param[in] path Path to be created
  63. /// @param[in] mode Access permissions field; NOTE: ignored on win
  64. /// @returns true if target path created or exists (and directory)
  65. inline bool MakeDirectoryRecursive(const TString& path, EFilePermissions mode) {
  66. return MakeDirectoryRecursive(path, mode, false);
  67. }
  68. /// Creates directory and all non-existings parents
  69. ///
  70. /// @param[in] path Path to be created
  71. /// @returns true if target path created or exists (and directory)
  72. inline bool MakeDirectoryRecursive(const TString& path) {
  73. return MakeDirectoryRecursive(path, FP_COMMON_FILE, false);
  74. }
  75. /// Rename a file or directory.
  76. /// Removes newPath if it exists
  77. ///
  78. /// @param[in] oldPath Path to file or directory to rename
  79. /// @param[in] newPath New path of file or directory
  80. /// @returns true on success or false otherwise
  81. /// LastSystemError() is set in case of failure
  82. bool Rename(const TString& oldPath, const TString& newPath);
  83. /// Creates a new directory entry for a file
  84. /// or creates a new one with the same content
  85. ///
  86. /// @param[in] existingPath Path to an existing file
  87. /// @param[in] newPath New path of file
  88. void HardLinkOrCopy(const TString& existingPath, const TString& newPath);
  89. /// Creates a new directory entry for a file
  90. ///
  91. /// @param[in] existingPath Path to an existing file
  92. /// @param[in] newPath New path of file
  93. /// @returns true if new link was created or false otherwise
  94. /// LastSystemError() is set in case of failure
  95. bool HardLink(const TString& existingPath, const TString& newPath);
  96. /// Creates a symlink to a file
  97. ///
  98. /// @param[in] targetPath Path to a target file
  99. /// @param[in] linkPath Path of symlink
  100. /// @returns true if new link was created or false otherwise
  101. /// LastSystemError() is set in case of failure
  102. bool SymLink(const TString& targetPath, const TString& linkPath);
  103. /// Reads value of a symbolic link
  104. ///
  105. /// @param[in] path Path to a symlink
  106. /// @returns File path that a symlink points to
  107. TString ReadLink(const TString& path);
  108. /// Append contents of a file to a new file
  109. ///
  110. /// @param[in] dstPath Path to a destination file
  111. /// @param[in] srcPath Path to a source file
  112. void Cat(const TString& dstPath, const TString& srcPath);
  113. /// Copy contents of a file to a new file
  114. ///
  115. /// @param[in] existingPath Path to an existing file
  116. /// @param[in] newPath New path of file
  117. void Copy(const TString& existingPath, const TString& newPath);
  118. /// Returns path to the current working directory
  119. ///
  120. /// Note: is not threadsafe
  121. TString CurrentWorkingDirectory();
  122. /// Changes current working directory
  123. ///
  124. /// @param[in] path Path for new cwd
  125. /// Note: is not threadsafe
  126. void SetCurrentWorkingDirectory(const TString& path);
  127. /// Checks if file exists
  128. ///
  129. /// @param[in] path Path to check
  130. bool Exists(const TString& path);
  131. /// Ensures that file exists
  132. ///
  133. /// @param[in] path Path to check
  134. /// @returns input argument
  135. inline const TString& EnsureExists(const TString& path) {
  136. Y_ENSURE_EX(Exists(path), TFileError{} << "Path " << path << " does not exists (checked from cwd:" << NFs::CurrentWorkingDirectory() << ")");
  137. return path;
  138. }
  139. } // namespace NFs
  140. Y_DECLARE_OPERATORS_FOR_FLAGS(NFs::EFilePermissions);