1/*
2 * Copyright (c) 2007, 2008, 2009, 2011, ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
8 */
9
10#include <barrelfish/barrelfish.h>
11#include <vfs/vfs.h>
12#include <vfs/vfs_path.h>
13#include <dirent.h>
14#include <unistd.h>
15#include <string.h>
16#include <errno.h>
17#include "posixcompat.h"
18
19DIR *opendir(const char *pathname)
20{
21    vfs_handle_t vh;
22    errval_t err;
23
24    char *path = vfs_path_mkabs(pathname);
25    assert(path != NULL);
26
27    err = vfs_opendir(path, &vh);
28    free(path);
29    if (err_is_fail(err)) {
30        if(err_no(err) == FS_ERR_NOTFOUND) {
31            errno = ENOENT;
32        }
33        POSIXCOMPAT_DEBUG("opendir(%s) not found\n", pathname);
34        return NULL;
35    }
36
37    POSIXCOMPAT_DEBUG("opendir(%s)\n", pathname);
38
39    DIR *ret = malloc(sizeof(DIR));
40    assert(ret != NULL);
41
42    ret->vh = vh;
43    return ret;
44}
45
46struct dirent *readdir(DIR* dir)
47{
48    char *name;
49    errval_t err;
50    err = vfs_dir_read_next(dir->vh, &name, NULL);
51    if (err_is_fail(err)) {
52        if (err_no(err) != FS_ERR_INDEX_BOUNDS) {
53        }
54        return NULL;
55    }
56
57    strncpy(dir->dirent.d_name, name, sizeof(dir->dirent.d_name));
58    dir->dirent.d_name[sizeof(dir->dirent.d_name) - 1] = '\0';
59    return &dir->dirent;
60}
61
62int closedir(DIR *dir)
63{
64    vfs_closedir(dir->vh);
65    free(dir);
66    return(0);
67}
68