File.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 Result<size_t, OSError> size(String const& filename);
  28. static bool ensure_parent_directories(String const& path);
  29. static String current_working_directory();
  30. static String absolute_path(String const& path);
  31. enum class RecursionMode {
  32. Allowed,
  33. Disallowed
  34. };
  35. enum class LinkMode {
  36. Allowed,
  37. Disallowed
  38. };
  39. enum class AddDuplicateFileMarker {
  40. Yes,
  41. No,
  42. };
  43. enum class PreserveMode {
  44. Nothing,
  45. PermissionsOwnershipTimestamps,
  46. };
  47. struct CopyError {
  48. OSError error_code;
  49. bool tried_recursing;
  50. };
  51. static Result<void, CopyError> copy_file(String const& dst_path, struct stat const& src_stat, File& source, PreserveMode = PreserveMode::Nothing);
  52. 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);
  53. 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);
  54. static String real_path_for(String const& filename);
  55. static String read_link(String const& link_path);
  56. static ErrorOr<void> link_file(String const& dst_path, String const& src_path);
  57. struct RemoveError : public Error {
  58. RemoveError(String f, int error_code)
  59. : Error(error_code)
  60. , file(move(f))
  61. {
  62. }
  63. String file;
  64. };
  65. static ErrorOr<void, RemoveError> remove(String const& path, RecursionMode, bool force);
  66. virtual bool open(OpenMode) override;
  67. enum class ShouldCloseFileDescriptor {
  68. No = 0,
  69. Yes
  70. };
  71. bool open(int fd, OpenMode, ShouldCloseFileDescriptor);
  72. [[nodiscard]] int leak_fd();
  73. static NonnullRefPtr<File> standard_input();
  74. static NonnullRefPtr<File> standard_output();
  75. static NonnullRefPtr<File> standard_error();
  76. private:
  77. File(Object* parent = nullptr)
  78. : IODevice(parent)
  79. {
  80. }
  81. explicit File(String filename, Object* parent = nullptr);
  82. bool open_impl(OpenMode, mode_t);
  83. String m_filename;
  84. ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
  85. };
  86. }