2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-08-20 11:16:42 +02:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-04-10 20:22:23 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2021-11-07 01:42:54 +01:00
|
|
|
#include <AK/Error.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/IODevice.h>
|
2021-02-21 02:51:55 +02:00
|
|
|
#include <sys/stat.h>
|
2019-04-10 20:22:23 +02:00
|
|
|
|
2022-08-20 17:01:53 +02:00
|
|
|
// FIXME: Make this a bit prettier.
|
|
|
|
|
#define DEFAULT_PATH "/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"
|
|
|
|
|
#define DEFAULT_PATH_SV "/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"sv
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
namespace Core {
|
|
|
|
|
|
2022-09-12 21:38:38 +01:00
|
|
|
///
|
|
|
|
|
/// Use of Core::File for reading/writing data is deprecated.
|
2023-02-09 03:02:46 +01:00
|
|
|
/// Please use Core::File and Core::BufferedFile instead.
|
2022-09-12 21:38:38 +01:00
|
|
|
///
|
2023-02-08 21:08:01 +01:00
|
|
|
class DeprecatedFile final : public IODevice {
|
|
|
|
|
C_OBJECT(DeprecatedFile)
|
2019-04-10 20:22:23 +02:00
|
|
|
public:
|
2023-02-08 21:08:01 +01:00
|
|
|
virtual ~DeprecatedFile() override;
|
2019-04-10 20:22:23 +02:00
|
|
|
|
2023-02-08 21:08:01 +01:00
|
|
|
static ErrorOr<NonnullRefPtr<DeprecatedFile>> open(DeprecatedString filename, OpenMode, mode_t = 0644);
|
2020-03-30 12:08:25 +02:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString filename() const { return m_filename; }
|
|
|
|
|
void set_filename(const DeprecatedString filename) { m_filename = move(filename); }
|
2019-04-10 20:22:23 +02:00
|
|
|
|
2020-02-09 11:50:18 +01:00
|
|
|
bool is_directory() const;
|
2021-03-30 00:36:19 +03:00
|
|
|
bool is_device() const;
|
2022-06-26 19:04:06 -04:00
|
|
|
bool is_block_device() const;
|
|
|
|
|
bool is_char_device() const;
|
2021-07-11 15:38:55 -05:00
|
|
|
bool is_link() const;
|
2021-11-03 12:05:23 +01:00
|
|
|
bool looks_like_shared_library() const;
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
static DeprecatedString current_working_directory();
|
|
|
|
|
static DeprecatedString absolute_path(DeprecatedString const& path);
|
2021-02-21 02:51:55 +02:00
|
|
|
|
|
|
|
|
enum class RecursionMode {
|
|
|
|
|
Allowed,
|
|
|
|
|
Disallowed
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class LinkMode {
|
|
|
|
|
Allowed,
|
|
|
|
|
Disallowed
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class AddDuplicateFileMarker {
|
|
|
|
|
Yes,
|
|
|
|
|
No,
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-15 12:24:00 +02:00
|
|
|
enum class PreserveMode {
|
2022-07-12 23:28:10 +02:00
|
|
|
Nothing = 0,
|
|
|
|
|
Permissions = (1 << 0),
|
|
|
|
|
Ownership = (1 << 1),
|
|
|
|
|
Timestamps = (1 << 2),
|
2021-08-15 12:24:00 +02:00
|
|
|
};
|
|
|
|
|
|
2021-11-07 01:42:54 +01:00
|
|
|
struct CopyError : public Error {
|
|
|
|
|
CopyError(int error_code, bool t)
|
|
|
|
|
: Error(error_code)
|
|
|
|
|
, tried_recursing(t)
|
|
|
|
|
{
|
|
|
|
|
}
|
2021-02-21 02:51:55 +02:00
|
|
|
bool tried_recursing;
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-08 21:08:01 +01:00
|
|
|
static ErrorOr<void, CopyError> copy_file(DeprecatedString const& dst_path, struct stat const& src_stat, DeprecatedFile& source, PreserveMode = PreserveMode::Nothing);
|
2022-12-04 18:02:33 +00:00
|
|
|
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);
|
|
|
|
|
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);
|
2021-02-21 02:51:55 +02:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
static DeprecatedString real_path_for(DeprecatedString const& filename);
|
|
|
|
|
static ErrorOr<DeprecatedString> read_link(DeprecatedString const& link_path);
|
2020-02-12 21:17:00 +01:00
|
|
|
|
2021-05-12 13:56:43 +04:30
|
|
|
virtual bool open(OpenMode) override;
|
2019-04-10 20:22:23 +02:00
|
|
|
|
2020-10-25 11:31:27 +00:00
|
|
|
enum class ShouldCloseFileDescriptor {
|
2019-05-28 11:53:16 +02:00
|
|
|
No = 0,
|
|
|
|
|
Yes
|
|
|
|
|
};
|
2021-05-12 13:56:43 +04:30
|
|
|
bool open(int fd, OpenMode, ShouldCloseFileDescriptor);
|
2021-07-04 14:42:51 +10:00
|
|
|
[[nodiscard]] int leak_fd();
|
2019-04-26 02:22:21 +02:00
|
|
|
|
2023-02-08 21:08:01 +01:00
|
|
|
static NonnullRefPtr<DeprecatedFile> standard_input();
|
|
|
|
|
static NonnullRefPtr<DeprecatedFile> standard_output();
|
|
|
|
|
static NonnullRefPtr<DeprecatedFile> standard_error();
|
2020-08-17 15:58:07 -06:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
static Optional<DeprecatedString> resolve_executable_from_environment(StringView filename);
|
2022-08-20 18:31:03 +02:00
|
|
|
|
2019-04-10 20:22:23 +02:00
|
|
|
private:
|
2023-02-08 21:08:01 +01:00
|
|
|
DeprecatedFile(Object* parent = nullptr)
|
2020-02-02 12:34:39 +01:00
|
|
|
: IODevice(parent)
|
2019-09-21 20:50:06 +02:00
|
|
|
{
|
|
|
|
|
}
|
2023-02-08 21:08:01 +01:00
|
|
|
explicit DeprecatedFile(DeprecatedString filename, Object* parent = nullptr);
|
2019-09-21 20:50:06 +02:00
|
|
|
|
2021-05-12 13:56:43 +04:30
|
|
|
bool open_impl(OpenMode, mode_t);
|
2020-03-30 12:08:25 +02:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString m_filename;
|
2020-10-25 11:31:27 +00:00
|
|
|
ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
|
2019-04-10 20:22:23 +02:00
|
|
|
};
|
2020-02-02 12:34:39 +01:00
|
|
|
|
2023-02-08 21:08:01 +01:00
|
|
|
AK_ENUM_BITWISE_OPERATORS(DeprecatedFile::PreserveMode);
|
2022-07-12 23:28:10 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
}
|