File.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/OSError.h>
  8. #include <AK/Result.h>
  9. #include <AK/String.h>
  10. #include <LibCore/IODevice.h>
  11. #include <sys/stat.h>
  12. namespace Core {
  13. class File final : public IODevice {
  14. C_OBJECT(File)
  15. public:
  16. virtual ~File() override;
  17. static Result<NonnullRefPtr<File>, OSError> open(String filename, OpenMode, mode_t = 0644);
  18. String filename() const { return m_filename; }
  19. void set_filename(const String filename) { m_filename = move(filename); }
  20. bool is_directory() const;
  21. static bool is_directory(String const& filename);
  22. bool is_device() const;
  23. static bool is_device(String const& filename);
  24. bool is_link() const;
  25. static bool is_link(String const& filename);
  26. static bool exists(String const& filename);
  27. static bool ensure_parent_directories(String const& path);
  28. static String current_working_directory();
  29. static String absolute_path(String const& path);
  30. enum class RecursionMode {
  31. Allowed,
  32. Disallowed
  33. };
  34. enum class LinkMode {
  35. Allowed,
  36. Disallowed
  37. };
  38. enum class AddDuplicateFileMarker {
  39. Yes,
  40. No,
  41. };
  42. enum class PreserveMode {
  43. Nothing,
  44. PermissionsOwnershipTimestamps,
  45. };
  46. struct CopyError {
  47. OSError error_code;
  48. bool tried_recursing;
  49. };
  50. static Result<void, CopyError> copy_file(String const& dst_path, struct stat const& src_stat, File& source, PreserveMode = PreserveMode::Nothing);
  51. static Result<void, CopyError> copy_directory(String const& dst_path, String const& src_path, struct stat const& src_stat, LinkMode = LinkMode::Disallowed, PreserveMode = PreserveMode::Nothing);
  52. static Result<void, CopyError> copy_file_or_directory(String const& dst_path, String const& src_path, RecursionMode = RecursionMode::Allowed, LinkMode = LinkMode::Disallowed, AddDuplicateFileMarker = AddDuplicateFileMarker::Yes, PreserveMode = PreserveMode::Nothing);
  53. static String real_path_for(String const& filename);
  54. static String read_link(String const& link_path);
  55. static Result<void, OSError> link_file(String const& dst_path, String const& src_path);
  56. struct RemoveError {
  57. String file;
  58. OSError error_code;
  59. };
  60. static Result<void, RemoveError> remove(String const& path, RecursionMode, bool force);
  61. virtual bool open(OpenMode) override;
  62. enum class ShouldCloseFileDescriptor {
  63. No = 0,
  64. Yes
  65. };
  66. bool open(int fd, OpenMode, ShouldCloseFileDescriptor);
  67. [[nodiscard]] int leak_fd();
  68. static NonnullRefPtr<File> standard_input();
  69. static NonnullRefPtr<File> standard_output();
  70. static NonnullRefPtr<File> standard_error();
  71. private:
  72. File(Object* parent = nullptr)
  73. : IODevice(parent)
  74. {
  75. }
  76. explicit File(String filename, Object* parent = nullptr);
  77. bool open_impl(OpenMode, mode_t);
  78. String m_filename;
  79. ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
  80. };
  81. }