summaryrefslogtreecommitdiffstats
path: root/pickles/redoxfs.pk
blob: 4fe9dd7ea56dbe0e54c610be1de9002ffe957f1e (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* redoxfs.pk - RedoxFS file system of Redox OS.  */

/* Copyright (C) 2020-2021 The poke authors */

/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* RedoxFS
 * File-system of RedoxOS (a Unix-like operating system written in Rust)
 *
 * The file-system has a header (type `RedoxFS_Header`). Header has a
 * pointer to root node. Nodes are represented by type `RedoxFS_Node`.
 * Data is stored in `extents` of each node. Extents are represented
 * by type `RedoxFS_Extents`. Each node has 238 extents. If a node needs
 * more extents, it will use the extents of the `next` node.
 * Each node can be a file or directory or symlink.
 * Extents of file nodes are point to the actual data. And extents of
 * directory nodes are point to child nodes.
 *
 * https://gitlab.redox-os.org/redox-os/redoxfs/
 */

/* set_endian(ENDIAN_LITTLE); */

load time;

/* Block size */
unit RedoxFS_BLKSZ = 4096UL * 8;  /* `4096*8` bits */

type RedoxFS_Extent =
  struct
  {
    offset<uint64,RedoxFS_BLKSZ> block;
    offset<uint64,B> length;

    method is_empty_p = int:
      {
        return block == 0#B && length == 0#B;  /* CHKME block ?= 0#B */
      }
    method get_data = uint8[]:
      {
        return uint8[length] @ block;
      }
  };

type RedoxFS_Time =
  struct
  {
    uint64 sec;
    uint32 nsec;
  };

var RedoxFS_MODE_TYPE    = 0xF000UH,
    RedoxFS_MODE_FILE    = 0x8000UH,
    RedoxFS_MODE_DIR     = 0x4000UH,
    RedoxFS_MODE_SYMLINK = 0xA000UH;

var RedoxFS_MODE_PERM  = 0x0FFFUH,
    RedoxFS_MODE_EXEC  = 0o1UH,
    RedoxFS_MODE_WRITE = 0o2UH,
    RedoxFS_MODE_READ  = 0o4UH;

/* Callback for extent visitor */
type RedoxFS_EVisitor = (RedoxFS_Extent) void;

/* A file/folder node */
type RedoxFS_Node =
  struct
  {
    uint16 mode;
    uint32 uid;
    uint32 gid;
    RedoxFS_Time ctime;
    RedoxFS_Time mtime;
    RedoxFS_Time atime;
    uint8[226] name;
    offset<uint64,RedoxFS_BLKSZ> parent;
    offset<uint64,RedoxFS_BLKSZ> next;
    RedoxFS_Extent[#RedoxFS_BLKSZ - 288#B] extents;

    method is_file_p = int:
      {
        return (mode & RedoxFS_MODE_FILE) == RedoxFS_MODE_FILE;
      }
    method is_dir_p = int:
      {
        return (mode & RedoxFS_MODE_DIR) == RedoxFS_MODE_DIR;
      }
    method is_symlink_p = int:
      {
        return (mode & RedoxFS_MODE_SYMLINK) == RedoxFS_MODE_SYMLINK;
      }
    method get_name = string:
      {
        return catos (name);
      }
    method visit_extents = (RedoxFS_EVisitor v) uint64:
      {
        var c = 0;  /* counter */

        for (e in extents where !e.is_empty_p)
          {
            ++c;
            v (e);
          }
        return c;
      }
    /* Node pretty-printer
     *
     * Prints the contents of a `RedoxFS_Node` in a readable fashion.
     * To increase readability, prints the non-empty extents before
     * the other fields.
     */
    method _print = void:
      {
        print ("#<\n  extents = [\n");

        var f = 0UL;             /* first */
        var l = extents'length;  /* last  */

        while (f != l) {
          var e = extents[f];

          if (!e.is_empty_p)
            printf ("    .[%u64d]=%v\n", f, e);
          ++f;
        }

        var mtype =
          is_symlink_p ? "FILE | SYM"
          : is_dir_p ? "DIR"
          : is_file_p ? "FILE"
          : "?";  /* This is for zero blocks */

        printf (
          "  ],\n  mode=%s | %u12o,\n  uid=%u32d,\n  gid=%u32d,\n  ctime=%v (",
          mtype, mode & RedoxFS_MODE_PERM, uid, gid, ctime);
        ptime (ctime.sec);
        printf ("),\n  mtime=%v (", mtime);
        ptime (mtime.sec);
        printf ("),\n  atime=%v (", atime);
        ptime (atime.sec);
        printf ("),\n  name=%s,\n", get_name);
        printf ("  parent=%v,\n  next=%v,\n>\n", parent, next);
      }
  };

/* Visit all extents of a node, including the nodes pointed to by `next`
 * field.
 *
 * Note that this function cannot be implemented as a method of
 * `RedoxFS_Node`; because we need to map the `RedoxFS_Node` and we cannot
 * map an incomplete type.
 */
fun redoxfs_visit_extents = (RedoxFS_Node n, RedoxFS_EVisitor v) uint64:
  {
    var counter = n.visit_extents (v);

    while (n.next != 0#B)
      {
        n = RedoxFS_Node @ n.next;
        counter += n.visit_extents (v);
      }
    return counter;
  }

type RedoxFS_Header =
  struct
  {
    string signature = "RedoxFS";
    uint64 version = 4UL;

    /* Disk ID, a 128-bit unique identifier */
    uint8[16] uuid;

    offset<uint64,B> size;

    offset<uint64,RedoxFS_BLKSZ> root;  /* Block of root node */
    offset<uint64,RedoxFS_BLKSZ> free;  /* Block of free space node */

    /* Padding */
    uint8[#RedoxFS_BLKSZ - 56#B];

    method _print = void:
      {
        printf "#<\n  signature=%s,\n  version=%u64d,\n  uuid=%v,\n  \
size=%v,\n  root=%v,\n  free=%v\n>", signature, version, uuid, size, root, free;
      }
  };

assert ((RedoxFS_Node {})'size == #RedoxFS_BLKSZ);
assert ((RedoxFS_Header {})'size == #RedoxFS_BLKSZ);

/* Callback for file-system walker
 *
 * FTYPE  'f' for files, 'd' for directories, 's' for symlinks
 * NAME   Name of the entity
 * PATH   Path of the parent directory
 * ES     Array of extents corresponding to the entity
 */
type RedoxFS_Visitor = (char /*ftype*/,
                        string /*name*/,
                        string /*path*/,
                        RedoxFS_Extent[] /*es*/) void;

type RedoxFS_Filesystem =
  struct
  {
    RedoxFS_Header header;
    RedoxFS_Node root @ header.root;

    /* File-system walker
     *
     * `redoxfs_walk` traverses the file-system hierarchy and call the visitor
     * callback function for each entity (file, directory or symlink).
     */
    method walk = (RedoxFS_Visitor v, RedoxFS_Node n = root) void:
      {
        fun nothing = (RedoxFS_Extent e) void: {};
        fun filev = (RedoxFS_Node n, string curdir) void:
          {
            /* TODO(mnabipoor) Measure performance of array construction */
            var es_count = redoxfs_visit_extents (n, nothing);
            var es = RedoxFS_Extent[es_count] ();
            var i = 0UL;
            fun cp = (RedoxFS_Extent e) void: { es[i++] = e; };

            redoxfs_visit_extents (n, cp);
            v('f', n.get_name, curdir, es);
          }
        fun visit = (RedoxFS_Node n, string curdir) void:
          {
            if (n.is_symlink_p)
              v('s', catos (n.extents[0].get_data), curdir, RedoxFS_Extent[] ());
            else if (n.is_dir_p)
              {
                var name = n.get_name;
                var path = (curdir == "/" ? "" :  curdir) + "/" + name;
                var es_count = redoxfs_visit_extents (n, nothing);
                var es = RedoxFS_Extent[es_count] ();
                var i = 0UL;
                fun ev = (RedoxFS_Extent e) void:
                  {
                    es[i++] = e;
                    visit (RedoxFS_Node @ e.block, path);
                  };

                redoxfs_visit_extents (n, ev);
                v('d', name, curdir, es);
              }
            else if (n.is_file_p)
              filev (n, curdir);
          }
        var nxt = n.next;

        visit (n, "/");
        while (nxt != 0#B)
          {
            n = RedoxFS_Node @ nxt;
            nxt = n.next;
            visit (n, "/");
          }
      }
  };
Generated by cgit on ageinghacker.net.
I am Luca Saiu. If you have reason to request commit access to one of these repositories please contact me. You may also send me patches by email.