Serenity Operating System
1/*
2 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/DeprecatedString.h>
10#include <AK/Error.h>
11#include <LibCore/IODevice.h>
12#include <sys/stat.h>
13
14// FIXME: Make this a bit prettier.
15#define DEFAULT_PATH "/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"
16#define DEFAULT_PATH_SV "/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"sv
17
18namespace Core {
19
20///
21/// Use of Core::File for reading/writing data is deprecated.
22/// Please use Core::File and Core::BufferedFile instead.
23///
24class DeprecatedFile final : public IODevice {
25 C_OBJECT(DeprecatedFile)
26public:
27 virtual ~DeprecatedFile() override;
28
29 static ErrorOr<NonnullRefPtr<DeprecatedFile>> open(DeprecatedString filename, OpenMode, mode_t = 0644);
30
31 DeprecatedString filename() const { return m_filename; }
32 void set_filename(const DeprecatedString filename) { m_filename = move(filename); }
33
34 bool is_directory() const;
35 static bool is_directory(DeprecatedString const& filename);
36 static bool is_directory(int fd);
37
38 bool is_device() const;
39 static bool is_device(DeprecatedString const& filename);
40 static bool is_device(int fd);
41 bool is_block_device() const;
42 static bool is_block_device(DeprecatedString const& filename);
43 bool is_char_device() const;
44 static bool is_char_device(DeprecatedString const& filename);
45
46 bool is_link() const;
47 static bool is_link(DeprecatedString const& filename);
48
49 bool looks_like_shared_library() const;
50 static bool looks_like_shared_library(DeprecatedString const& filename);
51
52 static bool exists(StringView filename);
53 static ErrorOr<size_t> size(DeprecatedString const& filename);
54 static DeprecatedString current_working_directory();
55 static DeprecatedString absolute_path(DeprecatedString const& path);
56 static bool can_delete_or_move(StringView path);
57
58 enum class RecursionMode {
59 Allowed,
60 Disallowed
61 };
62
63 enum class LinkMode {
64 Allowed,
65 Disallowed
66 };
67
68 enum class AddDuplicateFileMarker {
69 Yes,
70 No,
71 };
72
73 enum class PreserveMode {
74 Nothing = 0,
75 Permissions = (1 << 0),
76 Ownership = (1 << 1),
77 Timestamps = (1 << 2),
78 };
79
80 struct CopyError : public Error {
81 CopyError(int error_code, bool t)
82 : Error(error_code)
83 , tried_recursing(t)
84 {
85 }
86 bool tried_recursing;
87 };
88
89 static ErrorOr<void, CopyError> copy_file(DeprecatedString const& dst_path, struct stat const& src_stat, DeprecatedFile& source, PreserveMode = PreserveMode::Nothing);
90 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);
91 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);
92
93 static DeprecatedString real_path_for(DeprecatedString const& filename);
94 static ErrorOr<DeprecatedString> read_link(DeprecatedString const& link_path);
95 static ErrorOr<void> link_file(DeprecatedString const& dst_path, DeprecatedString const& src_path);
96
97 static ErrorOr<void> remove(StringView path, RecursionMode);
98
99 virtual bool open(OpenMode) override;
100
101 enum class ShouldCloseFileDescriptor {
102 No = 0,
103 Yes
104 };
105 bool open(int fd, OpenMode, ShouldCloseFileDescriptor);
106 [[nodiscard]] int leak_fd();
107
108 static NonnullRefPtr<DeprecatedFile> standard_input();
109 static NonnullRefPtr<DeprecatedFile> standard_output();
110 static NonnullRefPtr<DeprecatedFile> standard_error();
111
112 static Optional<DeprecatedString> resolve_executable_from_environment(StringView filename);
113
114private:
115 DeprecatedFile(Object* parent = nullptr)
116 : IODevice(parent)
117 {
118 }
119 explicit DeprecatedFile(DeprecatedString filename, Object* parent = nullptr);
120
121 bool open_impl(OpenMode, mode_t);
122
123 DeprecatedString m_filename;
124 ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
125};
126
127AK_ENUM_BITWISE_OPERATORS(DeprecatedFile::PreserveMode);
128
129}