FileSystem.cpp 12 KB

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