1/** \file
2 *  \brief Test FAT32 file system implementation
3 */
4
5/*
6 * Copyright (c) 2013, ETH Zurich.
7 * All rights reserved.
8 *
9 * This file is distributed under the terms in the attached LICENSE file.
10 * If you do not find this file, copies can be found by writing to:
11 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
12 */
13
14#include <stdio.h>
15#include <string.h>
16#include <barrelfish/barrelfish.h>
17#include <vfs/vfs.h>
18
19
20static void walk_dir(char* path)
21{
22    printf("%s:%d: path=%s\n", __FUNCTION__, __LINE__, path);
23    vfs_handle_t dhandle;
24    struct vfs_fileinfo info;
25
26    errval_t err = vfs_opendir(path, &dhandle);
27    assert(err_is_ok(err));
28
29    char* name;
30    while(err_is_ok(vfs_dir_read_next(dhandle, &name, &info))) {
31
32        if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
33            continue;
34        char* newpath = malloc(snprintf(NULL, 0, "%s/%s", path, name) + 1);
35        sprintf(newpath, "%s/%s", path, name);
36
37        switch (info.type) {
38        case VFS_DIRECTORY:
39            printf("%s:%d: Found directory %s\n", __FUNCTION__, __LINE__, newpath);
40            walk_dir(newpath);
41            break;
42
43        case VFS_FILE:
44            printf("%s:%d: Found file %s\n", __FUNCTION__, __LINE__, newpath);
45            vfs_handle_t fhandle;
46
47            err = vfs_open(newpath, &fhandle);
48            char* buf = malloc(info.size);
49            size_t bytes_read = 0;
50            err = vfs_read(fhandle, buf, info.size, &bytes_read);
51            assert(err_is_ok(err));
52            assert(bytes_read == info.size);
53            printf("%s:%d: File content is (bytes=%zu):\n%s\n",
54                   __FUNCTION__, __LINE__, bytes_read, buf);
55
56            free(buf);
57            break;
58        }
59        free(name);
60        free(newpath);
61    }
62
63    err = vfs_closedir(dhandle);
64    assert(err_is_ok(err));
65}
66
67int main(int argc, char *argv[])
68{
69    vfs_init();
70    vfs_mkdir("/fat");
71
72    errval_t err = vfs_mount("/fat", "fat32://0+0");
73    if (err_is_fail(err)) {
74        USER_PANIC_ERR(err, "vfs_fat_mount failed");
75    }
76
77    walk_dir("/fat");
78
79    return 0;
80}
81