File.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/DeprecatedString.h>
  8. #include <AK/Error.h>
  9. #include <LibCore/IODevice.h>
  10. #include <sys/stat.h>
  11. // FIXME: Make this a bit prettier.
  12. #define DEFAULT_PATH "/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"
  13. #define DEFAULT_PATH_SV "/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"sv
  14. namespace Core {
  15. ///
  16. /// Use of Core::File for reading/writing data is deprecated.
  17. /// Please use Core::Stream::File and Core::Stream::BufferedFile instead.
  18. ///
  19. class File final : public IODevice {
  20. C_OBJECT(File)
  21. public:
  22. virtual ~File() override;
  23. static ErrorOr<NonnullRefPtr<File>> open(DeprecatedString filename, OpenMode, mode_t = 0644);
  24. DeprecatedString filename() const { return m_filename; }
  25. void set_filename(const DeprecatedString filename) { m_filename = move(filename); }
  26. bool is_directory() const;
  27. static bool is_directory(DeprecatedString const& filename);
  28. static bool is_directory(int fd);
  29. bool is_device() const;
  30. static bool is_device(DeprecatedString const& filename);
  31. static bool is_device(int fd);
  32. bool is_block_device() const;
  33. static bool is_block_device(DeprecatedString const& filename);
  34. bool is_char_device() const;
  35. static bool is_char_device(DeprecatedString const& filename);
  36. bool is_link() const;
  37. static bool is_link(DeprecatedString const& filename);
  38. bool looks_like_shared_library() const;
  39. static bool looks_like_shared_library(DeprecatedString const& filename);
  40. static bool exists(StringView filename);
  41. static ErrorOr<size_t> size(DeprecatedString const& filename);
  42. static DeprecatedString current_working_directory();
  43. static DeprecatedString absolute_path(DeprecatedString const& path);
  44. static bool can_delete_or_move(StringView path);
  45. enum class RecursionMode {
  46. Allowed,
  47. Disallowed
  48. };
  49. enum class LinkMode {
  50. Allowed,
  51. Disallowed
  52. };
  53. enum class AddDuplicateFileMarker {
  54. Yes,
  55. No,
  56. };
  57. enum class PreserveMode {
  58. Nothing = 0,
  59. Permissions = (1 << 0),
  60. Ownership = (1 << 1),
  61. Timestamps = (1 << 2),
  62. };
  63. struct CopyError : public Error {
  64. CopyError(int error_code, bool t)
  65. : Error(error_code)
  66. , tried_recursing(t)
  67. {
  68. }
  69. bool tried_recursing;
  70. };
  71. static ErrorOr<void, CopyError> copy_file(DeprecatedString const& dst_path, struct stat const& src_stat, File& source, PreserveMode = PreserveMode::Nothing);
  72. static ErrorOr<void, CopyError> copy_directory(DeprecatedString const& dst_path, DeprecatedString const& src_path, struct stat const& src_stat, LinkMode = LinkMode::Disallowed, PreserveMode = PreserveMode::Nothing);
  73. static ErrorOr<void, CopyError> copy_file_or_directory(DeprecatedString const& dst_path, DeprecatedString const& src_path, RecursionMode = RecursionMode::Allowed, LinkMode = LinkMode::Disallowed, AddDuplicateFileMarker = AddDuplicateFileMarker::Yes, PreserveMode = PreserveMode::Nothing);
  74. static DeprecatedString real_path_for(DeprecatedString const& filename);
  75. static ErrorOr<DeprecatedString> read_link(DeprecatedString const& link_path);
  76. static ErrorOr<void> link_file(DeprecatedString const& dst_path, DeprecatedString const& src_path);
  77. static ErrorOr<void> remove(StringView path, RecursionMode);
  78. virtual bool open(OpenMode) override;
  79. enum class ShouldCloseFileDescriptor {
  80. No = 0,
  81. Yes
  82. };
  83. bool open(int fd, OpenMode, ShouldCloseFileDescriptor);
  84. [[nodiscard]] int leak_fd();
  85. static NonnullRefPtr<File> standard_input();
  86. static NonnullRefPtr<File> standard_output();
  87. static NonnullRefPtr<File> standard_error();
  88. static Optional<DeprecatedString> resolve_executable_from_environment(StringView filename);
  89. private:
  90. File(Object* parent = nullptr)
  91. : IODevice(parent)
  92. {
  93. }
  94. explicit File(DeprecatedString filename, Object* parent = nullptr);
  95. bool open_impl(OpenMode, mode_t);
  96. DeprecatedString m_filename;
  97. ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
  98. };
  99. AK_ENUM_BITWISE_OPERATORS(File::PreserveMode);
  100. }