summaryrefslogtreecommitdiff
path: root/Kernel/InodeMetadata.h
blob: ee24330f9d4151f5002f87d52cc69b8340ed4613 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#pragma once

#include "InodeIdentifier.h"
#include "UnixTypes.h"
#include <AK/HashTable.h>

class Process;

inline bool is_directory(mode_t mode) { return (mode & 0170000) == 0040000; }
inline bool is_character_device(mode_t mode) { return (mode & 0170000) == 0020000; }
inline bool is_block_device(mode_t mode) { return (mode & 0170000) == 0060000; }
inline bool is_regular_file(mode_t mode) { return (mode & 0170000) == 0100000; }
inline bool is_fifo(mode_t mode) { return (mode & 0170000) == 0010000; }
inline bool is_symlink(mode_t mode) { return (mode & 0170000) == 0120000; }
inline bool is_socket(mode_t mode) { return (mode & 0170000) == 0140000; }
inline bool is_sticky(mode_t mode) { return mode & 01000; }
inline bool is_setuid(mode_t mode) { return mode & 04000; }
inline bool is_setgid(mode_t mode) { return mode & 02000; }

struct InodeMetadata {
    bool is_valid() const { return inode.is_valid(); }

    bool may_read(Process&) const;
    bool may_write(Process&) const;
    bool may_execute(Process&) const;

    bool may_read(uid_t u, const HashTable<gid_t>& g) const
    {
        if (u == 0)
            return true;
        if (uid == u)
            return mode & 0400;
        if (g.contains(gid))
            return mode & 0040;
        return mode & 0004;
    }

    bool may_write(uid_t u, const HashTable<gid_t>& g) const
    {
        if (u == 0)
            return true;
        if (uid == u)
            return mode & 0200;
        if (g.contains(gid))
            return mode & 0020;
        return mode & 0002;
    }

    bool may_execute(uid_t u, const HashTable<gid_t>& g) const
    {
        if (u == 0)
            return true;
        if (uid == u)
            return mode & 0100;
        if (g.contains(gid))
            return mode & 0010;
        return mode & 0001;
    }

    bool is_directory() const { return ::is_directory(mode); }
    bool is_character_device() const { return ::is_character_device(mode); }
    bool is_block_device() const { return ::is_block_device(mode); }
    bool is_device() const { return is_character_device() || is_block_device(); }
    bool is_regular_file() const { return ::is_regular_file(mode); }
    bool is_fifo() const { return ::is_fifo(mode); }
    bool is_symlink() const { return ::is_symlink(mode); }
    bool is_socket() const { return ::is_socket(mode); }
    bool is_sticky() const { return ::is_sticky(mode); }
    bool is_setuid() const { return ::is_setuid(mode); }
    bool is_setgid() const { return ::is_setgid(mode); }

    InodeIdentifier inode;
    off_t size { 0 };
    mode_t mode { 0 };
    uid_t uid { 0 };
    gid_t gid { 0 };
    nlink_t link_count { 0 };
    time_t atime { 0 };
    time_t ctime { 0 };
    time_t mtime { 0 };
    time_t dtime { 0 };
    blkcnt_t block_count { 0 };
    blksize_t block_size { 0 };
    unsigned major_device { 0 };
    unsigned minor_device { 0 };
};