summaryrefslogtreecommitdiff
path: root/core/fs/readdir.c
blob: 8acdd55813961d77d06325e825ddb2380b4a80e4 (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
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/dirent.h>
#include "fs.h"
#include "core.h"
#include "multifs.h"

/* 
 * Open a directory
 */
__export DIR *opendir(const char *path)
{
    int rv;
    struct file *file;

    if (multifs_switch_fs(&path))
	return NULL;

    rv = searchdir(path, O_RDONLY|O_DIRECTORY);
    if (rv < 0)
	return NULL;

    file = handle_to_file(rv);

    if (file->inode->mode != DT_DIR) {
	_close_file(file);
	return NULL;
    }

    multifs_restore_fs();

    return (DIR *)file;
}

/*
 * Read one directory entry at one time. 
 */
__export struct dirent *readdir(DIR *dir)
{
    static struct dirent buf;
    struct file *dd_dir = (struct file *)dir;
    int rv = -1;

    if (dd_dir) {
        if (dd_dir->fs->fs_ops->readdir) {
	    rv = dd_dir->fs->fs_ops->readdir(dd_dir, &buf);
        }
    }

    return rv < 0 ? NULL : &buf;
}

/*
 * Close a directory
 */
__export int closedir(DIR *dir)
{
    struct file *dd_dir = (struct file *)dir;
    _close_file(dd_dir);
    return 0;
}