FileSystem.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/LexicalPath.h>
  8. #include <LibCore/DirIterator.h>
  9. #include <LibCore/System.h>
  10. #include <LibFileSystem/FileSystem.h>
  11. #include <limits.h>
  12. #ifdef AK_OS_SERENITY
  13. # include <serenity.h>
  14. #endif
  15. // On Linux distros that use glibc `basename` is defined as a macro that expands to `__xpg_basename`, so we undefine it
  16. #if defined(AK_OS_LINUX) && defined(basename)
  17. # undef basename
  18. #endif
  19. namespace FileSystem {
  20. ErrorOr<String> current_working_directory()
  21. {
  22. auto cwd = TRY(Core::System::getcwd());
  23. return TRY(String::from_deprecated_string({ cwd }));
  24. }
  25. ErrorOr<String> absolute_path(StringView path)
  26. {
  27. if (exists(path))
  28. return TRY(real_path(path));
  29. if (path.starts_with("/"sv))
  30. return TRY(String::from_deprecated_string(LexicalPath::canonicalized_path(path)));
  31. auto working_directory = TRY(current_working_directory());
  32. auto full_path = LexicalPath::join(working_directory, path).string();
  33. return TRY(String::from_deprecated_string(LexicalPath::canonicalized_path(full_path)));
  34. }
  35. ErrorOr<String> real_path(StringView path)
  36. {
  37. if (path.is_null())
  38. return Error::from_errno(ENOENT);
  39. char buffer[PATH_MAX];
  40. DeprecatedString dep_path = path;
  41. auto* real_path = realpath(dep_path.characters(), buffer);
  42. if (!real_path)
  43. return Error::from_syscall("realpath"sv, -errno);
  44. return TRY(String::from_utf8({ real_path, strlen(real_path) }));
  45. }
  46. bool exists(StringView path)
  47. {
  48. return !Core::System::stat(path).is_error();
  49. }
  50. bool exists(int fd)
  51. {
  52. return !Core::System::fstat(fd).is_error();
  53. }
  54. bool is_device(StringView path)
  55. {
  56. auto st_or_error = Core::System::stat(path);
  57. if (st_or_error.is_error())
  58. return false;
  59. auto st = st_or_error.release_value();
  60. return S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode);
  61. }
  62. bool is_device(int fd)
  63. {
  64. auto st_or_error = Core::System::fstat(fd);
  65. if (st_or_error.is_error())
  66. return false;
  67. auto st = st_or_error.release_value();
  68. return S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode);
  69. }
  70. bool is_block_device(StringView path)
  71. {
  72. auto st_or_error = Core::System::stat(path);
  73. if (st_or_error.is_error())
  74. return false;
  75. auto st = st_or_error.release_value();
  76. return S_ISBLK(st.st_mode);
  77. }
  78. bool is_block_device(int fd)
  79. {
  80. auto st_or_error = Core::System::fstat(fd);
  81. if (st_or_error.is_error())
  82. return false;
  83. auto st = st_or_error.release_value();
  84. return S_ISBLK(st.st_mode);
  85. }
  86. bool is_char_device(StringView path)
  87. {
  88. auto st_or_error = Core::System::stat(path);
  89. if (st_or_error.is_error())
  90. return false;
  91. auto st = st_or_error.release_value();
  92. return S_ISCHR(st.st_mode);
  93. }
  94. bool is_char_device(int fd)
  95. {
  96. auto st_or_error = Core::System::fstat(fd);
  97. if (st_or_error.is_error())
  98. return false;
  99. auto st = st_or_error.release_value();
  100. return S_ISCHR(st.st_mode);
  101. }
  102. bool is_directory(StringView path)
  103. {
  104. auto st_or_error = Core::System::stat(path);
  105. if (st_or_error.is_error())
  106. return false;
  107. auto st = st_or_error.release_value();
  108. return S_ISDIR(st.st_mode);
  109. }
  110. bool is_directory(int fd)
  111. {
  112. auto st_or_error = Core::System::fstat(fd);
  113. if (st_or_error.is_error())
  114. return false;
  115. auto st = st_or_error.release_value();
  116. return S_ISDIR(st.st_mode);
  117. }
  118. bool is_link(StringView path)
  119. {
  120. auto st_or_error = Core::System::lstat(path);
  121. if (st_or_error.is_error())
  122. return false;
  123. auto st = st_or_error.release_value();
  124. return S_ISLNK(st.st_mode);
  125. }
  126. bool is_link(int fd)
  127. {
  128. auto st_or_error = Core::System::fstat(fd);
  129. if (st_or_error.is_error())
  130. return false;
  131. auto st = st_or_error.release_value();
  132. return S_ISLNK(st.st_mode);
  133. }
  134. static ErrorOr<String> get_duplicate_file_name(StringView path)
  135. {
  136. int duplicate_count = 0;
  137. LexicalPath lexical_path(path);
  138. auto parent_path = LexicalPath::canonicalized_path(lexical_path.dirname());
  139. auto basename = lexical_path.basename();
  140. auto current_name = TRY(String::from_deprecated_string(LexicalPath::join(parent_path, basename).string()));
  141. while (exists(current_name)) {
  142. ++duplicate_count;
  143. current_name = TRY(String::from_deprecated_string(LexicalPath::join(parent_path, TRY(String::formatted("{} ({})", basename, duplicate_count))).string()));
  144. }
  145. return current_name;
  146. }
  147. ErrorOr<void> copy_file(StringView destination_path, StringView source_path, struct stat const& source_stat, Core::File& source, PreserveMode preserve_mode)
  148. {
  149. auto destination_or_error = Core::File::open(destination_path, Core::File::OpenMode::Write, 0666);
  150. if (destination_or_error.is_error()) {
  151. if (destination_or_error.error().code() != EISDIR)
  152. return destination_or_error.release_error();
  153. auto destination_dir_path = TRY(String::formatted("{}/{}", destination_path, LexicalPath::basename(source_path)));
  154. destination_or_error = TRY(Core::File::open(destination_dir_path, Core::File::OpenMode::Write, 0666));
  155. }
  156. auto destination = destination_or_error.release_value();
  157. if (source_stat.st_size > 0)
  158. TRY(destination->truncate(source_stat.st_size));
  159. while (true) {
  160. auto bytes_read = TRY(source.read_until_eof());
  161. if (bytes_read.is_empty())
  162. break;
  163. TRY(destination->write_until_depleted(bytes_read));
  164. }
  165. auto my_umask = umask(0);
  166. umask(my_umask);
  167. // NOTE: We don't copy the set-uid and set-gid bits unless requested.
  168. if (!has_flag(preserve_mode, PreserveMode::Permissions))
  169. my_umask |= 06000;
  170. TRY(Core::System::fchmod(destination->fd(), source_stat.st_mode & ~my_umask));
  171. if (has_flag(preserve_mode, PreserveMode::Ownership))
  172. TRY(Core::System::fchown(destination->fd(), source_stat.st_uid, source_stat.st_gid));
  173. if (has_flag(preserve_mode, PreserveMode::Timestamps)) {
  174. struct timespec times[2] = {
  175. #ifdef AK_OS_MACOS
  176. source_stat.st_atimespec,
  177. source_stat.st_mtimespec,
  178. #else
  179. source_stat.st_atim,
  180. source_stat.st_mtim,
  181. #endif
  182. };
  183. TRY(Core::System::utimensat(AT_FDCWD, destination_path, times, 0));
  184. }
  185. return {};
  186. }
  187. ErrorOr<void> copy_directory(StringView destination_path, StringView source_path, struct stat const& source_stat, LinkMode link, PreserveMode preserve_mode)
  188. {
  189. TRY(Core::System::mkdir(destination_path, 0755));
  190. auto source_rp = TRY(real_path(source_path));
  191. source_rp = TRY(String::formatted("{}/", source_rp));
  192. auto destination_rp = TRY(real_path(destination_path));
  193. destination_rp = TRY(String::formatted("{}/", destination_rp));
  194. if (!destination_rp.is_empty() && destination_rp.starts_with_bytes(source_rp))
  195. return Error::from_errno(EINVAL);
  196. Core::DirIterator di(source_path, Core::DirIterator::SkipParentAndBaseDir);
  197. if (di.has_error())
  198. return di.error();
  199. while (di.has_next()) {
  200. auto filename = TRY(String::from_deprecated_string(di.next_path()));
  201. TRY(copy_file_or_directory(
  202. TRY(String::formatted("{}/{}", destination_path, filename)),
  203. TRY(String::formatted("{}/{}", source_path, filename)),
  204. RecursionMode::Allowed, link, AddDuplicateFileMarker::Yes, preserve_mode));
  205. }
  206. auto my_umask = umask(0);
  207. umask(my_umask);
  208. TRY(Core::System::chmod(destination_path, source_stat.st_mode & ~my_umask));
  209. if (has_flag(preserve_mode, PreserveMode::Ownership))
  210. TRY(Core::System::chown(destination_path, source_stat.st_uid, source_stat.st_gid));
  211. if (has_flag(preserve_mode, PreserveMode::Timestamps)) {
  212. struct timespec times[2] = {
  213. #ifdef AK_OS_MACOS
  214. source_stat.st_atimespec,
  215. source_stat.st_mtimespec,
  216. #else
  217. source_stat.st_atim,
  218. source_stat.st_mtim,
  219. #endif
  220. };
  221. TRY(Core::System::utimensat(AT_FDCWD, destination_path, times, 0));
  222. }
  223. return {};
  224. }
  225. ErrorOr<void> copy_file_or_directory(StringView destination_path, StringView source_path, RecursionMode recursion_mode, LinkMode link_mode, AddDuplicateFileMarker add_duplicate_file_marker, PreserveMode preserve_mode)
  226. {
  227. String final_destination_path;
  228. if (add_duplicate_file_marker == AddDuplicateFileMarker::Yes)
  229. final_destination_path = TRY(get_duplicate_file_name(destination_path));
  230. else
  231. final_destination_path = TRY(String::from_utf8(destination_path));
  232. auto source = TRY(Core::File::open(source_path, Core::File::OpenMode::Read));
  233. auto source_stat = TRY(Core::System::fstat(source->fd()));
  234. if (is_directory(source_path)) {
  235. if (recursion_mode == RecursionMode::Disallowed) {
  236. return Error::from_errno(EISDIR);
  237. }
  238. return copy_directory(final_destination_path, source_path, source_stat);
  239. }
  240. if (link_mode == LinkMode::Allowed)
  241. return TRY(Core::System::link(source_path, final_destination_path));
  242. return copy_file(final_destination_path, source_path, source_stat, *source, preserve_mode);
  243. }
  244. ErrorOr<void> remove(StringView path, RecursionMode mode)
  245. {
  246. if (is_directory(path) && mode == RecursionMode::Allowed) {
  247. auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir);
  248. if (di.has_error())
  249. return di.error();
  250. while (di.has_next())
  251. TRY(remove(di.next_full_path(), RecursionMode::Allowed));
  252. TRY(Core::System::rmdir(path));
  253. } else {
  254. TRY(Core::System::unlink(path));
  255. }
  256. return {};
  257. }
  258. ErrorOr<size_t> size(StringView path)
  259. {
  260. auto st = TRY(Core::System::stat(path));
  261. return st.st_size;
  262. }
  263. bool can_delete_or_move(StringView path)
  264. {
  265. VERIFY(!path.is_empty());
  266. auto directory = LexicalPath::dirname(path);
  267. auto directory_has_write_access = !Core::System::access(directory, W_OK).is_error();
  268. if (!directory_has_write_access)
  269. return false;
  270. auto stat_or_empty = [](StringView path) {
  271. auto stat_or_error = Core::System::stat(path);
  272. if (stat_or_error.is_error()) {
  273. struct stat stat { };
  274. return stat;
  275. }
  276. return stat_or_error.release_value();
  277. };
  278. auto directory_stat = stat_or_empty(directory);
  279. bool is_directory_sticky = (directory_stat.st_mode & S_ISVTX) != 0;
  280. if (!is_directory_sticky)
  281. return true;
  282. // Directory is sticky, only the file owner, directory owner, and root can modify (rename, remove) it.
  283. auto user_id = geteuid();
  284. return user_id == 0 || directory_stat.st_uid == user_id || stat_or_empty(path).st_uid == user_id;
  285. }
  286. ErrorOr<String> read_link(StringView link_path)
  287. {
  288. return TRY(String::from_deprecated_string(TRY(Core::System::readlink(link_path))));
  289. }
  290. ErrorOr<void> link_file(StringView destination_path, StringView source_path)
  291. {
  292. return TRY(Core::System::symlink(source_path, TRY(get_duplicate_file_name(destination_path))));
  293. }
  294. bool looks_like_shared_library(StringView path)
  295. {
  296. return path.ends_with(".so"sv) || path.contains(".so."sv);
  297. }
  298. }