DeprecatedFile.h 3.2 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/DeprecatedString.h>
  8. #include <AK/Error.h>
  9. #include <LibCore/IODevice.h>
  10. #include <sys/stat.h>
  11. namespace Core {
  12. ///
  13. /// Use of Core::File for reading/writing data is deprecated.
  14. /// Please use Core::File and Core::InputBufferedFile instead.
  15. ///
  16. class DeprecatedFile final : public IODevice {
  17. C_OBJECT(DeprecatedFile)
  18. public:
  19. virtual ~DeprecatedFile() override;
  20. static ErrorOr<NonnullRefPtr<DeprecatedFile>> open(DeprecatedString filename, OpenMode, mode_t = 0644);
  21. DeprecatedString filename() const { return m_filename; }
  22. bool is_directory() const;
  23. bool is_device() const;
  24. bool is_block_device() const;
  25. bool is_char_device() const;
  26. bool is_link() const;
  27. static DeprecatedString current_working_directory();
  28. static DeprecatedString absolute_path(DeprecatedString const& path);
  29. enum class RecursionMode {
  30. Allowed,
  31. Disallowed
  32. };
  33. enum class LinkMode {
  34. Allowed,
  35. Disallowed
  36. };
  37. enum class AddDuplicateFileMarker {
  38. Yes,
  39. No,
  40. };
  41. enum class PreserveMode {
  42. Nothing = 0,
  43. Permissions = (1 << 0),
  44. Ownership = (1 << 1),
  45. Timestamps = (1 << 2),
  46. };
  47. struct CopyError : public Error {
  48. CopyError(int error_code, bool t)
  49. : Error(error_code)
  50. , tried_recursing(t)
  51. {
  52. }
  53. bool tried_recursing;
  54. };
  55. static ErrorOr<void, CopyError> copy_file(DeprecatedString const& dst_path, struct stat const& src_stat, DeprecatedFile& source, PreserveMode = PreserveMode::Nothing);
  56. 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);
  57. 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);
  58. static DeprecatedString real_path_for(DeprecatedString const& filename);
  59. static ErrorOr<DeprecatedString> read_link(DeprecatedString const& link_path);
  60. virtual bool open(OpenMode) override;
  61. enum class ShouldCloseFileDescriptor {
  62. No = 0,
  63. Yes
  64. };
  65. bool open(int fd, OpenMode, ShouldCloseFileDescriptor);
  66. [[nodiscard]] int leak_fd();
  67. static NonnullRefPtr<DeprecatedFile> standard_input();
  68. static NonnullRefPtr<DeprecatedFile> standard_output();
  69. static NonnullRefPtr<DeprecatedFile> standard_error();
  70. static Optional<DeprecatedString> resolve_executable_from_environment(StringView filename);
  71. private:
  72. DeprecatedFile(Object* parent = nullptr)
  73. : IODevice(parent)
  74. {
  75. }
  76. explicit DeprecatedFile(DeprecatedString filename, Object* parent = nullptr);
  77. bool open_impl(OpenMode, mode_t);
  78. DeprecatedString m_filename;
  79. ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
  80. };
  81. AK_ENUM_BITWISE_OPERATORS(DeprecatedFile::PreserveMode);
  82. }