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/String.h>
  9. #include <LibCore/IODevice.h>
  10. #include <sys/stat.h>
  11. namespace Core {
  12. class File final : public IODevice {
  13. C_OBJECT(File)
  14. public:
  15. virtual ~File() override;
  16. static ErrorOr<NonnullRefPtr<File>> open(String filename, OpenMode, mode_t = 0644);
  17. String filename() const { return m_filename; }
  18. void set_filename(const String filename) { m_filename = move(filename); }
  19. bool is_directory() const;
  20. static bool is_directory(String const& filename);
  21. bool is_device() const;
  22. static bool is_device(String const& filename);
  23. bool is_link() const;
  24. static bool is_link(String const& filename);
  25. bool looks_like_shared_library() const;
  26. static bool looks_like_shared_library(String const& filename);
  27. static bool exists(String const& filename);
  28. static ErrorOr<size_t> size(String const& filename);
  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 : 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(String const& dst_path, struct stat const& src_stat, File& source, PreserveMode = PreserveMode::Nothing);
  56. 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);
  57. 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);
  58. static String real_path_for(String const& filename);
  59. static ErrorOr<String> read_link(String const& link_path);
  60. static ErrorOr<void> link_file(String const& dst_path, String const& src_path);
  61. struct RemoveError : public Error {
  62. RemoveError(String f, int error_code)
  63. : Error(error_code)
  64. , file(move(f))
  65. {
  66. }
  67. String file;
  68. };
  69. static ErrorOr<void, RemoveError> remove(String const& path, RecursionMode, bool force);
  70. virtual bool open(OpenMode) override;
  71. enum class ShouldCloseFileDescriptor {
  72. No = 0,
  73. Yes
  74. };
  75. bool open(int fd, OpenMode, ShouldCloseFileDescriptor);
  76. [[nodiscard]] int leak_fd();
  77. static NonnullRefPtr<File> standard_input();
  78. static NonnullRefPtr<File> standard_output();
  79. static NonnullRefPtr<File> standard_error();
  80. private:
  81. File(Object* parent = nullptr)
  82. : IODevice(parent)
  83. {
  84. }
  85. explicit File(String filename, Object* parent = nullptr);
  86. bool open_impl(OpenMode, mode_t);
  87. String m_filename;
  88. ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
  89. };
  90. }