File.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/Error.h>
  8. #include <AK/OSError.h>
  9. #include <AK/Result.h>
  10. #include <AK/String.h>
  11. #include <LibCore/IODevice.h>
  12. #include <sys/stat.h>
  13. namespace Core {
  14. class File final : public IODevice {
  15. C_OBJECT(File)
  16. public:
  17. virtual ~File() override;
  18. static Result<NonnullRefPtr<File>, OSError> open(String filename, OpenMode, mode_t = 0644);
  19. String filename() const { return m_filename; }
  20. void set_filename(const String filename) { m_filename = move(filename); }
  21. bool is_directory() const;
  22. static bool is_directory(String const& filename);
  23. bool is_device() const;
  24. static bool is_device(String const& filename);
  25. bool is_link() const;
  26. static bool is_link(String const& filename);
  27. static bool exists(String const& filename);
  28. static Result<size_t, OSError> size(String const& filename);
  29. static bool ensure_parent_directories(String const& path);
  30. static String current_working_directory();
  31. static String absolute_path(String const& path);
  32. enum class RecursionMode {
  33. Allowed,
  34. Disallowed
  35. };
  36. enum class LinkMode {
  37. Allowed,
  38. Disallowed
  39. };
  40. enum class AddDuplicateFileMarker {
  41. Yes,
  42. No,
  43. };
  44. enum class PreserveMode {
  45. Nothing,
  46. PermissionsOwnershipTimestamps,
  47. };
  48. struct CopyError : public Error {
  49. CopyError(int error_code, bool t)
  50. : Error(error_code)
  51. , tried_recursing(t)
  52. {
  53. }
  54. bool tried_recursing;
  55. };
  56. static ErrorOr<void, CopyError> copy_file(String const& dst_path, struct stat const& src_stat, File& source, PreserveMode = PreserveMode::Nothing);
  57. static ErrorOr<void, CopyError> copy_directory(String const& dst_path, String const& src_path, struct stat const& src_stat, LinkMode = LinkMode::Disallowed, PreserveMode = PreserveMode::Nothing);
  58. static ErrorOr<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);
  59. static String real_path_for(String const& filename);
  60. static String read_link(String const& link_path);
  61. static ErrorOr<void> link_file(String const& dst_path, String const& src_path);
  62. struct RemoveError : public Error {
  63. RemoveError(String f, int error_code)
  64. : Error(error_code)
  65. , file(move(f))
  66. {
  67. }
  68. String file;
  69. };
  70. static ErrorOr<void, RemoveError> remove(String const& path, RecursionMode, bool force);
  71. virtual bool open(OpenMode) override;
  72. enum class ShouldCloseFileDescriptor {
  73. No = 0,
  74. Yes
  75. };
  76. bool open(int fd, OpenMode, ShouldCloseFileDescriptor);
  77. [[nodiscard]] int leak_fd();
  78. static NonnullRefPtr<File> standard_input();
  79. static NonnullRefPtr<File> standard_output();
  80. static NonnullRefPtr<File> standard_error();
  81. private:
  82. File(Object* parent = nullptr)
  83. : IODevice(parent)
  84. {
  85. }
  86. explicit File(String filename, Object* parent = nullptr);
  87. bool open_impl(OpenMode, mode_t);
  88. String m_filename;
  89. ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
  90. };
  91. }