2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2021-08-20 06:16:42 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-04-10 15:22:23 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2021-02-20 21:51:55 -03:00
|
|
|
#include <AK/OSError.h>
|
2020-04-21 08:29:36 -03:00
|
|
|
#include <AK/Result.h>
|
2019-09-06 10:34:26 -03:00
|
|
|
#include <AK/String.h>
|
2020-02-06 11:04:03 -03:00
|
|
|
#include <LibCore/IODevice.h>
|
2021-02-20 21:51:55 -03:00
|
|
|
#include <sys/stat.h>
|
2019-04-10 15:22:23 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
|
|
class File final : public IODevice {
|
|
|
|
|
C_OBJECT(File)
|
2019-04-10 15:22:23 -03:00
|
|
|
public:
|
2020-02-02 08:34:39 -03:00
|
|
|
virtual ~File() override;
|
2019-04-10 15:22:23 -03:00
|
|
|
|
2021-08-20 06:16:42 -03:00
|
|
|
static Result<NonnullRefPtr<File>, OSError> open(String filename, OpenMode, mode_t = 0644);
|
2020-03-30 07:08:25 -03:00
|
|
|
|
2019-04-10 15:22:23 -03:00
|
|
|
String filename() const { return m_filename; }
|
2021-04-16 19:48:31 -03:00
|
|
|
void set_filename(const String filename) { m_filename = move(filename); }
|
2019-04-10 15:22:23 -03:00
|
|
|
|
2020-02-09 07:50:18 -03:00
|
|
|
bool is_directory() const;
|
2021-07-09 12:41:07 -03:00
|
|
|
static bool is_directory(String const& filename);
|
2020-02-09 07:50:18 -03:00
|
|
|
|
2021-03-29 18:36:19 -03:00
|
|
|
bool is_device() const;
|
2021-07-09 12:41:07 -03:00
|
|
|
static bool is_device(String const& filename);
|
2021-03-29 18:36:19 -03:00
|
|
|
|
2021-07-11 17:38:55 -03:00
|
|
|
bool is_link() const;
|
2021-07-09 12:41:07 -03:00
|
|
|
static bool is_link(String const& filename);
|
2021-07-11 17:38:55 -03:00
|
|
|
|
2021-07-09 12:41:07 -03:00
|
|
|
static bool exists(String const& filename);
|
|
|
|
|
static bool ensure_parent_directories(String const& path);
|
2021-07-09 12:29:07 -03:00
|
|
|
static String current_working_directory();
|
2021-07-09 12:35:30 -03:00
|
|
|
static String absolute_path(String const& path);
|
2021-02-20 21:51:55 -03:00
|
|
|
|
|
|
|
|
enum class RecursionMode {
|
|
|
|
|
Allowed,
|
|
|
|
|
Disallowed
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class LinkMode {
|
|
|
|
|
Allowed,
|
|
|
|
|
Disallowed
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class AddDuplicateFileMarker {
|
|
|
|
|
Yes,
|
|
|
|
|
No,
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-15 07:24:00 -03:00
|
|
|
enum class PreserveMode {
|
|
|
|
|
Nothing,
|
|
|
|
|
PermissionsOwnershipTimestamps,
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-20 21:51:55 -03:00
|
|
|
struct CopyError {
|
|
|
|
|
OSError error_code;
|
|
|
|
|
bool tried_recursing;
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-15 07:24:00 -03:00
|
|
|
static Result<void, CopyError> copy_file(String const& dst_path, struct stat const& src_stat, File& source, PreserveMode = PreserveMode::Nothing);
|
|
|
|
|
static Result<void, CopyError> copy_directory(String const& dst_path, String const& src_path, struct stat const& src_stat, LinkMode = LinkMode::Disallowed, PreserveMode = PreserveMode::Nothing);
|
|
|
|
|
static Result<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);
|
2021-02-20 21:51:55 -03:00
|
|
|
|
2021-07-09 12:41:07 -03:00
|
|
|
static String real_path_for(String const& filename);
|
2021-04-16 19:48:31 -03:00
|
|
|
static String read_link(String const& link_path);
|
2021-07-09 12:41:07 -03:00
|
|
|
static Result<void, OSError> link_file(String const& dst_path, String const& src_path);
|
2021-02-20 21:51:55 -03:00
|
|
|
|
|
|
|
|
struct RemoveError {
|
|
|
|
|
String file;
|
|
|
|
|
OSError error_code;
|
|
|
|
|
};
|
2021-07-09 12:41:07 -03:00
|
|
|
static Result<void, RemoveError> remove(String const& path, RecursionMode, bool force);
|
2020-02-12 17:17:00 -03:00
|
|
|
|
2021-05-12 06:26:43 -03:00
|
|
|
virtual bool open(OpenMode) override;
|
2019-04-10 15:22:23 -03:00
|
|
|
|
2020-10-25 08:31:27 -03:00
|
|
|
enum class ShouldCloseFileDescriptor {
|
2019-05-28 06:53:16 -03:00
|
|
|
No = 0,
|
|
|
|
|
Yes
|
|
|
|
|
};
|
2021-05-12 06:26:43 -03:00
|
|
|
bool open(int fd, OpenMode, ShouldCloseFileDescriptor);
|
2021-07-04 01:42:51 -03:00
|
|
|
[[nodiscard]] int leak_fd();
|
2019-04-25 21:22:21 -03:00
|
|
|
|
2020-12-22 19:37:11 -03:00
|
|
|
static NonnullRefPtr<File> standard_input();
|
|
|
|
|
static NonnullRefPtr<File> standard_output();
|
|
|
|
|
static NonnullRefPtr<File> standard_error();
|
2020-08-17 18:58:07 -03:00
|
|
|
|
2019-04-10 15:22:23 -03:00
|
|
|
private:
|
2020-02-02 08:34:39 -03:00
|
|
|
File(Object* parent = nullptr)
|
|
|
|
|
: IODevice(parent)
|
2019-09-21 15:50:06 -03:00
|
|
|
{
|
|
|
|
|
}
|
2021-04-16 19:48:31 -03:00
|
|
|
explicit File(String filename, Object* parent = nullptr);
|
2019-09-21 15:50:06 -03:00
|
|
|
|
2021-05-12 06:26:43 -03:00
|
|
|
bool open_impl(OpenMode, mode_t);
|
2020-03-30 07:08:25 -03:00
|
|
|
|
2019-04-10 15:22:23 -03:00
|
|
|
String m_filename;
|
2020-10-25 08:31:27 -03:00
|
|
|
ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
|
2019-04-10 15:22:23 -03:00
|
|
|
};
|
2020-02-02 08:34:39 -03:00
|
|
|
|
|
|
|
|
}
|