summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore/Directory.h
blob: 8518c991e31f50b6ccf0862f460aca81d57df4f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Error.h>
#include <AK/Format.h>
#include <AK/LexicalPath.h>
#include <AK/Noncopyable.h>
#include <AK/Optional.h>
#include <LibCore/Stream.h>
#include <dirent.h>
#include <sys/stat.h>

namespace Core {

class DirIterator;

// Deal with real system directories. Any Directory instance always refers to a valid existing directory.
class Directory {
    AK_MAKE_NONCOPYABLE(Directory);

public:
    Directory(Directory&&);
    ~Directory();

    // When this flag is set, both the directory attempted to instantiate as well as all of its parents are created with mode 0755 if necessary.
    enum class CreateDirectories : bool {
        No,
        Yes,
    };

    static ErrorOr<Directory> create(LexicalPath path, CreateDirectories, mode_t creation_mode = 0755);
    static ErrorOr<Directory> create(DeprecatedString path, CreateDirectories, mode_t creation_mode = 0755);
    static ErrorOr<Directory> adopt_fd(int fd, Optional<LexicalPath> path = {});

    ErrorOr<NonnullOwnPtr<Stream::File>> open(StringView filename, Stream::OpenMode mode) const;
    ErrorOr<struct stat> stat() const;
    ErrorOr<DirIterator> create_iterator() const;

    ErrorOr<LexicalPath> path() const;

    ErrorOr<void> chown(uid_t, gid_t);

    static ErrorOr<bool> is_valid_directory(int fd);

private:
    Directory(int directory_fd, Optional<LexicalPath> path);
    static ErrorOr<void> ensure_directory(LexicalPath const& path, mode_t creation_mode = 0755);

    Optional<LexicalPath> m_path;
    int m_directory_fd;
};

}

namespace AK {
template<>
struct Formatter<Core::Directory> : Formatter<StringView> {
    ErrorOr<void> format(FormatBuilder& builder, Core::Directory const& directory)
    {
        auto path = directory.path();
        if (path.is_error())
            TRY(builder.put_string("<unknown>"sv));
        TRY(builder.put_string(path.release_value().string()));
        return {};
    }
};

}