File.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2018-2020, 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>, String> 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(const String& filename);
  22. bool is_device() const;
  23. static bool is_device(const String& filename);
  24. static bool exists(const String& filename);
  25. static bool ensure_parent_directories(const String& path);
  26. enum class RecursionMode {
  27. Allowed,
  28. Disallowed
  29. };
  30. enum class LinkMode {
  31. Allowed,
  32. Disallowed
  33. };
  34. enum class AddDuplicateFileMarker {
  35. Yes,
  36. No,
  37. };
  38. struct CopyError {
  39. OSError error_code;
  40. bool tried_recursing;
  41. };
  42. static Result<void, CopyError> copy_file(const String& dst_path, const struct stat& src_stat, File& source);
  43. static Result<void, CopyError> copy_directory(const String& dst_path, const String& src_path, const struct stat& src_stat, LinkMode = LinkMode::Disallowed);
  44. static Result<void, CopyError> copy_file_or_directory(const String& dst_path, const String& src_path, RecursionMode = RecursionMode::Allowed, LinkMode = LinkMode::Disallowed, AddDuplicateFileMarker = AddDuplicateFileMarker::Yes);
  45. static String real_path_for(const String& filename);
  46. static String read_link(String const& link_path);
  47. static Result<void, OSError> link_file(const String& dst_path, const String& src_path);
  48. struct RemoveError {
  49. String file;
  50. OSError error_code;
  51. };
  52. static Result<void, RemoveError> remove(const String& path, RecursionMode, bool force);
  53. virtual bool open(OpenMode) override;
  54. enum class ShouldCloseFileDescriptor {
  55. No = 0,
  56. Yes
  57. };
  58. bool open(int fd, OpenMode, ShouldCloseFileDescriptor);
  59. [[nodiscard]] int leak_fd();
  60. static NonnullRefPtr<File> standard_input();
  61. static NonnullRefPtr<File> standard_output();
  62. static NonnullRefPtr<File> standard_error();
  63. private:
  64. File(Object* parent = nullptr)
  65. : IODevice(parent)
  66. {
  67. }
  68. explicit File(String filename, Object* parent = nullptr);
  69. bool open_impl(OpenMode, mode_t);
  70. String m_filename;
  71. ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
  72. };
  73. }