fs.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /// Remove a file or empty directory
  22. ///
  23. /// @param[in] path Path to file or directory
  24. /// @returns true on success or false otherwise
  25. /// LastSystemError() is set in case of failure
  26. bool Remove(const TString& path);
  27. /// Remove a file or directory with contents
  28. /// Does nothing if path does not exist
  29. ///
  30. /// @param[in] path Path to file or directory
  31. /// @throws
  32. void RemoveRecursive(const TString& path);
  33. /// Creates directory
  34. ///
  35. /// @param[in] path Path to the directory
  36. /// @param[in] mode Access permissions field; NOTE: ignored on win
  37. /// @returns true on success or false otherwise
  38. bool MakeDirectory(const TString& path, EFilePermissions mode);
  39. /// Creates directory
  40. ///
  41. /// @param[in] path Path to the directory
  42. /// @returns true on success or false otherwise
  43. /// NOTE: access permissions is set to 0777
  44. inline bool MakeDirectory(const TString& path) {
  45. return MakeDirectory(path, FP_COMMON_FILE);
  46. }
  47. /// Creates directory and all non-existings parents
  48. ///
  49. /// @param[in] path Path to be created
  50. /// @param[in] mode Access permissions field; NOTE: ignored on win
  51. /// @param[in] alwaysCreate Throw if path already exists or failed to create
  52. /// @returns true if target path created or exists (and directory)
  53. bool MakeDirectoryRecursive(const TString& path, EFilePermissions mode, bool alwaysCreate);
  54. /// Creates directory and all non-existings parents
  55. ///
  56. /// @param[in] path Path to be created
  57. /// @param[in] mode Access permissions field; NOTE: ignored on win
  58. /// @returns true if target path created or exists (and directory)
  59. inline bool MakeDirectoryRecursive(const TString& path, EFilePermissions mode) {
  60. return MakeDirectoryRecursive(path, mode, false);
  61. }
  62. /// Creates directory and all non-existings parents
  63. ///
  64. /// @param[in] path Path to be created
  65. /// @returns true if target path created or exists (and directory)
  66. inline bool MakeDirectoryRecursive(const TString& path) {
  67. return MakeDirectoryRecursive(path, FP_COMMON_FILE, false);
  68. }
  69. /// Rename a file or directory.
  70. /// Removes newPath if it exists
  71. ///
  72. /// @param[in] oldPath Path to file or directory to rename
  73. /// @param[in] newPath New path of file or directory
  74. /// @returns true on success or false otherwise
  75. /// LastSystemError() is set in case of failure
  76. bool Rename(const TString& oldPath, const TString& newPath);
  77. /// Creates a new directory entry for a file
  78. /// or creates a new one with the same content
  79. ///
  80. /// @param[in] existingPath Path to an existing file
  81. /// @param[in] newPath New path of file
  82. void HardLinkOrCopy(const TString& existingPath, const TString& newPath);
  83. /// Creates a new directory entry for a file
  84. ///
  85. /// @param[in] existingPath Path to an existing file
  86. /// @param[in] newPath New path of file
  87. /// @returns true if new link was created or false otherwise
  88. /// LastSystemError() is set in case of failure
  89. bool HardLink(const TString& existingPath, const TString& newPath);
  90. /// Creates a symlink to a file
  91. ///
  92. /// @param[in] targetPath Path to a target file
  93. /// @param[in] linkPath Path of symlink
  94. /// @returns true if new link was created or false otherwise
  95. /// LastSystemError() is set in case of failure
  96. bool SymLink(const TString& targetPath, const TString& linkPath);
  97. /// Reads value of a symbolic link
  98. ///
  99. /// @param[in] path Path to a symlink
  100. /// @returns File path that a symlink points to
  101. TString ReadLink(const TString& path);
  102. /// Append contents of a file to a new file
  103. ///
  104. /// @param[in] dstPath Path to a destination file
  105. /// @param[in] srcPath Path to a source file
  106. void Cat(const TString& dstPath, const TString& srcPath);
  107. /// Copy contents of a file to a new file
  108. ///
  109. /// @param[in] existingPath Path to an existing file
  110. /// @param[in] newPath New path of file
  111. void Copy(const TString& existingPath, const TString& newPath);
  112. /// Returns path to the current working directory
  113. ///
  114. /// Note: is not threadsafe
  115. TString CurrentWorkingDirectory();
  116. /// Changes current working directory
  117. ///
  118. /// @param[in] path Path for new cwd
  119. /// Note: is not threadsafe
  120. void SetCurrentWorkingDirectory(const TString& path);
  121. /// Checks if file exists
  122. ///
  123. /// @param[in] path Path to check
  124. bool Exists(const TString& path);
  125. /// Ensures that file exists
  126. ///
  127. /// @param[in] path Path to check
  128. /// @returns input argument
  129. inline const TString& EnsureExists(const TString& path) {
  130. Y_ENSURE_EX(Exists(path), TFileError{} << "Path " << path << " does not exists (checked from cwd:" << NFs::CurrentWorkingDirectory() << ")");
  131. return path;
  132. }
  133. }
  134. Y_DECLARE_OPERATORS_FOR_FLAGS(NFs::EFilePermissions);