FileSystem.cpp 12 KB

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