DeprecatedFile.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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::File and Core::BufferedFile instead.
  18. ///
  19. class DeprecatedFile final : public IODevice {
  20. C_OBJECT(DeprecatedFile)
  21. public:
  22. virtual ~DeprecatedFile() override;
  23. static ErrorOr<NonnullRefPtr<DeprecatedFile>> 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. bool is_device() const;
  28. bool is_block_device() const;
  29. bool is_char_device() const;
  30. bool is_link() const;
  31. bool looks_like_shared_library() const;
  32. static DeprecatedString current_working_directory();
  33. static DeprecatedString absolute_path(DeprecatedString const& path);
  34. enum class RecursionMode {
  35. Allowed,
  36. Disallowed
  37. };
  38. enum class LinkMode {
  39. Allowed,
  40. Disallowed
  41. };
  42. enum class AddDuplicateFileMarker {
  43. Yes,
  44. No,
  45. };
  46. enum class PreserveMode {
  47. Nothing = 0,
  48. Permissions = (1 << 0),
  49. Ownership = (1 << 1),
  50. Timestamps = (1 << 2),
  51. };
  52. struct CopyError : public Error {
  53. CopyError(int error_code, bool t)
  54. : Error(error_code)
  55. , tried_recursing(t)
  56. {
  57. }
  58. bool tried_recursing;
  59. };
  60. static ErrorOr<void, CopyError> copy_file(DeprecatedString const& dst_path, struct stat const& src_stat, DeprecatedFile& source, PreserveMode = PreserveMode::Nothing);
  61. 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);
  62. 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);
  63. static DeprecatedString real_path_for(DeprecatedString const& filename);
  64. static ErrorOr<DeprecatedString> read_link(DeprecatedString const& link_path);
  65. virtual bool open(OpenMode) override;
  66. enum class ShouldCloseFileDescriptor {
  67. No = 0,
  68. Yes
  69. };
  70. bool open(int fd, OpenMode, ShouldCloseFileDescriptor);
  71. [[nodiscard]] int leak_fd();
  72. static NonnullRefPtr<DeprecatedFile> standard_input();
  73. static NonnullRefPtr<DeprecatedFile> standard_output();
  74. static NonnullRefPtr<DeprecatedFile> standard_error();
  75. static Optional<DeprecatedString> resolve_executable_from_environment(StringView filename);
  76. private:
  77. DeprecatedFile(Object* parent = nullptr)
  78. : IODevice(parent)
  79. {
  80. }
  81. explicit DeprecatedFile(DeprecatedString filename, Object* parent = nullptr);
  82. bool open_impl(OpenMode, mode_t);
  83. DeprecatedString m_filename;
  84. ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
  85. };
  86. AK_ENUM_BITWISE_OPERATORS(DeprecatedFile::PreserveMode);
  87. }