summaryrefslogtreecommitdiff
path: root/VirtualFileSystem/test.cpp
blob: f24a302eee9a857db5ad27c4760d9e370fa9712f (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
#include "Ext2FileSystem.h"
#include "FileBackedDiskDevice.h"
#include "VirtualFileSystem.h"
#include "FileDescriptor.h"
#include "SyntheticFileSystem.h"
#include "ZeroDevice.h"
#include "NullDevice.h"
#include "FullDevice.h"
#include "RandomDevice.h"
#include <cstring>
#include <AK/FileSystemPath.h>
#include <AK/SimpleMalloc.h>
#include <AK/StdLib.h>
#include <AK/kmalloc.h>
#include <AK/ktime.h>

static RetainPtr<FS> makeFileSystem(const char* imagePath);

int main(int c, char** v)
{
    const char* filename = "small.fs";
    if (c >= 2)
        filename = v[1];

    VFS::initialize_globals();

    VFS vfs;

    auto zero = make<ZeroDevice>();
    vfs.register_character_device(*zero);

    auto null = make<NullDevice>();
    vfs.register_character_device(*null);

    auto full = make<FullDevice>();
    vfs.register_character_device(*full);

    auto random = make<RandomDevice>();
    vfs.register_character_device(*random);

    if (!vfs.mount_root(makeFileSystem(filename))) {
        printf("Failed to mount root :(\n");
        return 1;
    }

#if 0
    auto newFile = vfs.create("/empty");
    printf("vfs.create: %p\n", newFile.ptr());
#endif
#if 1
    auto newDir = vfs.mkdir("/mydir");
    printf("vfs.mkdir: %p\n", newDir.ptr());
#endif
    //return 0;

    if (!strcmp(v[0], "./vcat")) {
        int error;
        auto descriptor = vfs.open(v[2], error);
        if (!descriptor) {
            printf("failed to open %s inside fs image\n", v[2]);
            return 1;
        }
        auto contents = descriptor->read_entire_file();

        FILE* fout = fopen(v[3], "w");
        if (!fout) {
            printf("failed to open %s for output\n", v[3]);
            return 1;
        }
        fwrite(contents.pointer(), sizeof(char), contents.size(), fout);
        fclose(fout);
        return 0;
    }

    auto synthfs = SynthFS::create();
    bool success = static_cast<FS&>(*synthfs).initialize();
    printf("synth->initialize(): returned %u\n", success);

    vfs.mount(std::move(synthfs), "/syn");

    vfs.listDirectory(".", vfs.root()->inode);
    printf("list /syn:\n");
    vfs.listDirectory("/syn", vfs.root()->inode);

#if 0
    auto descriptor = vfs.open("/home/andreas/../../home/./andreas/./file2");
    printf("descriptor = %p\n", handle.ptr());
    ASSERT(descriptor);

    auto contents = descriptor->readEntireFile();
    ASSERT(contents);

    printf("contents: '%s'\n", contents->pointer());
#endif

    String currentDirectory = "/";

    auto cwd = vfs.root()->inode;

    while (true) {
        char cmdbuf[256];
        printf("::>");
        fflush(stdout);
        fgets(cmdbuf, sizeof(cmdbuf), stdin);

        if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
            cmdbuf[strlen(cmdbuf) - 1] = '\0';

        String command = cmdbuf;
        auto parts = command.split(' ');

        if (parts.is_empty())
            continue;

        String cmd = parts[0];

        if (cmd == "q")
            break;

        if (cmd == "pwd") {
            printf("%s\n", currentDirectory.characters());
            continue;
        }

        if (cmd == "ls") {
            vfs.listDirectory(".", cwd);
            continue;
        }

        if (cmd == "lr") {
            vfs.listDirectoryRecursively(".", cwd);
            continue;
        }

        if (cmd == "cd" && parts.size() > 1) {
            char buf[4096];
            sprintf(buf, "%s/%s", currentDirectory.characters(), parts[1].characters());
            FileSystemPath new_path(buf);
            if (new_path.string() == "/") {
                cwd = vfs.root()->inode;
                continue;
            }
            int error;
            auto new_cwd = vfs.open(new_path.string(), error, 0, cwd);
            if (new_cwd && new_cwd->is_directory()) {
                currentDirectory = new_path.string();
                cwd = new_cwd->metadata().inode;
            } else {
                printf("No such directory: %s\n", parts[1].characters());
            }
            continue;
        }

        if (cmd == "mt" && parts.size() > 1) {
            char buf[1024];
            sprintf(buf, "%s/%s", currentDirectory.characters(), parts[1].characters());
            vfs.touch(buf);
            continue;
        }

        if (cmd == "stat" && parts.size() > 1) {
            char buf[1024];
            sprintf(buf, "%s/%s", currentDirectory.characters(), parts[1].characters());
            int error;
            auto descriptor = vfs.open(buf, error);
            if (!descriptor) {
                printf("Can't open '%s' :(\n", buf);
                continue;
            }
            Unix::stat st;
            int rc = descriptor->stat(&st);
            if (rc < 0) {
                printf("stat failed: %d\n", rc);
                continue;
            }
            printf("st_dev:     %u\n", st.st_dev);
            printf("st_ino:     %u\n", st.st_ino);
            printf("st_mode:    %o\n", st.st_mode);
            printf("st_nlink:   %u\n", st.st_nlink);
            printf("st_uid:     %u\n", st.st_uid);
            printf("st_gid:     %u\n", st.st_gid);
            printf("st_rdev:    %u\n", st.st_rdev);
            printf("st_size:    %lld\n", st.st_size);
            printf("st_blksize: %u\n", st.st_blksize);
            printf("st_blocks:  %u\n", st.st_blocks);
            printf("st_atime:   %u - %s", st.st_atime, ctime(&st.st_atime));
            printf("st_mtime:   %u - %s", st.st_mtime, ctime(&st.st_mtime));
            printf("st_ctime:   %u - %s", st.st_ctime, ctime(&st.st_ctime));
            continue;
        }

        if (cmd == "cat" && parts.size() > 1) {
            char pathbuf[1024];
            sprintf(pathbuf, "%s/%s", currentDirectory.characters(), parts[1].characters());
            int error;
            auto descriptor = vfs.open(pathbuf, error);
            if (!descriptor) {
                printf("failed to open %s\n", pathbuf);
                continue;
            }
            auto contents = descriptor->read_entire_file();
            fwrite(contents.pointer(), sizeof(char), contents.size(), stdout);
            continue;
        }

        if (cmd == "kat" && parts.size() > 1) {
            char pathbuf[1024];
            sprintf(pathbuf, "%s/%s", currentDirectory.characters(), parts[1].characters());
            int error;
            auto descriptor = vfs.open(pathbuf, error);
            if (!descriptor) {
                printf("failed to open %s\n", pathbuf);
                continue;
            }
            ssize_t nread;
            byte buffer[512];
            for (;;) {
                nread = descriptor->read(buffer, sizeof(buffer));
                if (nread <= 0)
                    break;
                fwrite(buffer, 1, nread, stdout);
            }
            if (nread < 0)
                printf("ERROR: %d\n", nread);
            continue;
        }

        if (cmd == "ma") {
            SimpleMalloc::dump();
            continue;
        }
    }

    return 0;
}

RetainPtr<FS> makeFileSystem(const char* imagePath)
{
    auto fsImage = FileBackedDiskDevice::create(imagePath, 512);
    if (!fsImage->is_valid()) {
        fprintf(stderr, "Failed to open fs image file '%s'\n", imagePath);
        exit(1);
    }
    auto ext2 = Ext2FS::create(std::move(fsImage));

    bool success = static_cast<FS&>(*ext2).initialize();
    printf("ext2->initialize(): returned %u\n", success);
    return ext2;
}