File.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. static NonnullRefPtr<File> standard_input();
  60. static NonnullRefPtr<File> standard_output();
  61. static NonnullRefPtr<File> standard_error();
  62. private:
  63. File(Object* parent = nullptr)
  64. : IODevice(parent)
  65. {
  66. }
  67. explicit File(String filename, Object* parent = nullptr);
  68. bool open_impl(OpenMode, mode_t);
  69. String m_filename;
  70. ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
  71. };
  72. }