Exemplo n.º 1
0
static int ls(int argc, char *argv[])
{
    errval_t err;
    int ret = 0;

    // XXX: cheat and assume we have some extra space wherever argv lives
    if (argc <= 1) {
        argv[1] = cwd;
        argc = 2;
    }

    for (int i = 1; i < argc; i++) {
        vfs_handle_t vh;
        char *path = vfs_path_mkabsolute(cwd, argv[i]);
        err = vfs_opendir(path, &vh);
        free(path);
        if (err_is_fail(err)) {
            DEBUG_ERR(err, "in vfs_opendir %s", argv[i]);
            printf("%s: not found\n", argv[i]);
            ret = 1;
            continue;
        }

        if (i > 1) {
            printf("\n");
        }
        printf("%s:\n", argv[i]);

        do {
            struct vfs_fileinfo info;
            char *name;
            err = vfs_dir_read_next(vh, &name, &info);
            if (err_is_ok(err)) {
                printf("%8zu %c %s\n", info.size, vfs_type_char(info.type),
                       name);
                free(name);
            }
        } while(err_is_ok(err));

        err = vfs_closedir(vh);
        if (err_is_fail(err)) {
            DEBUG_ERR(err, "in vfs_closedir");
        }
    }

    return ret;
}
Exemplo n.º 2
0
static void walk_dir(char* path)
{
    printf("%s:%d: path=%s\n", __FUNCTION__, __LINE__, path);
    vfs_handle_t dhandle;
    struct vfs_fileinfo info;
    
    errval_t err = vfs_opendir(path, &dhandle);
    assert(err_is_ok(err));

    char* name;
    while(err_is_ok(vfs_dir_read_next(dhandle, &name, &info))) {

        if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
            continue;
        char* newpath = malloc(snprintf(NULL, 0, "%s/%s", path, name) + 1);
        sprintf(newpath, "%s/%s", path, name);

        switch (info.type) {
        case VFS_DIRECTORY:
            printf("%s:%d: Found directory %s\n", __FUNCTION__, __LINE__, newpath);
            walk_dir(newpath);
            break;
        
        case VFS_FILE:
            printf("%s:%d: Found file %s\n", __FUNCTION__, __LINE__, newpath);
            vfs_handle_t fhandle;

            err = vfs_open(newpath, &fhandle);
            char* buf = malloc(info.size);
            size_t bytes_read = 0;
            err = vfs_read(fhandle, buf, info.size, &bytes_read);
            assert(err_is_ok(err));
            assert(bytes_read == info.size);
            printf("%s:%d: File content is (bytes=%d):\n%s\n", 
                   __FUNCTION__, __LINE__, bytes_read, buf);

            free(buf);
            break;
        }
        free(name);
        free(newpath);
    }

    err = vfs_closedir(dhandle);
    assert(err_is_ok(err));   
}