vfs: remove opendir, readdir, closedir
This commit is contained in:
parent
4aefedf276
commit
285086d77d
3 changed files with 3 additions and 74 deletions
59
fs/vfs.c
59
fs/vfs.c
|
|
@ -213,65 +213,6 @@ int unlink(const char *pathname)
|
|||
return rootfs->ops->unlink(rootfs, pathname);
|
||||
}
|
||||
|
||||
DIR *opendir(const char *name)
|
||||
{
|
||||
int err;
|
||||
struct file *file = vfs_open(name, &err);
|
||||
if (!file) {
|
||||
kprintf(LOG_ERROR "Failed to open directory %s: %s\n", name, strerror(-err));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fs *fs = file->vnode->fs;
|
||||
|
||||
if ((file->vnode->flags & VFS_VTYPE_MASK) != VFS_VNO_DIR) {
|
||||
vfs_close(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DIR *dir = kzalloc(sizeof(DIR), ALLOC_KERN);
|
||||
dir->file = file;
|
||||
dir->fs = fs;
|
||||
dir->pos = 0;
|
||||
dir->dirents = NULL;
|
||||
|
||||
return dir;
|
||||
}
|
||||
|
||||
struct dirent *readdir(DIR *dir)
|
||||
{
|
||||
if (!dir)
|
||||
return NULL;
|
||||
|
||||
struct fs *fs = dir->fs;
|
||||
|
||||
if ((dir->file->vnode->flags & VFS_VTYPE_MASK) != VFS_VNO_DIR)
|
||||
return NULL;
|
||||
|
||||
if (dir->dirents == NULL)
|
||||
dir->num_dirents = fs->ops->readdir(dir->file->vnode, &dir->dirents);
|
||||
|
||||
if (dir->dirents == NULL)
|
||||
return NULL;
|
||||
|
||||
if (dir->pos >= dir->num_dirents)
|
||||
return NULL;
|
||||
|
||||
return &dir->dirents[dir->pos++];
|
||||
}
|
||||
|
||||
int closedir(DIR *dir)
|
||||
{
|
||||
if (!dir)
|
||||
return -EBADF;
|
||||
|
||||
int ret = vfs_close(dir->file);
|
||||
kfree(dir->dirents);
|
||||
kfree(dir);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *basename(char *path)
|
||||
{
|
||||
char *ret = path;
|
||||
|
|
|
|||
|
|
@ -88,14 +88,6 @@ struct dirent {
|
|||
struct vnode *vnode; /* we don't want duplicate vnodes */
|
||||
};
|
||||
|
||||
typedef struct _DIR {
|
||||
struct fs *fs;
|
||||
struct dirent *dirents;
|
||||
size_t num_dirents;
|
||||
struct file *file;
|
||||
uint64_t pos;
|
||||
} DIR;
|
||||
|
||||
struct file {
|
||||
struct vnode *vnode;
|
||||
|
||||
|
|
@ -148,10 +140,6 @@ int vfs_close(struct file *file);
|
|||
int unlink(const char *pathname);
|
||||
int statfd(struct file_descriptor *fdesc, struct statbuf *statbuf);
|
||||
|
||||
DIR *opendir(const char *name);
|
||||
struct dirent *readdir(DIR *dir);
|
||||
int closedir(DIR *dir);
|
||||
|
||||
char *basename(char *path);
|
||||
char *dirname(char *path);
|
||||
|
||||
|
|
|
|||
|
|
@ -129,17 +129,17 @@ int kcmd_ls(int argc, char **argv)
|
|||
else
|
||||
path = argv[1];
|
||||
|
||||
DIR *dir = opendir(path);
|
||||
struct file *dir = vfs_open(path, NULL);
|
||||
if (!dir) {
|
||||
kprintf("Failed to open directory %s\n", path);
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct dirent *ent;
|
||||
while ((ent = readdir(dir))) {
|
||||
for (size_t i = 0; i < dir->vnode->num_dirents; i++) {
|
||||
ent = &dir->vnode->dirents[i];
|
||||
if (ent->name[0] == '.')
|
||||
continue;
|
||||
|
||||
kprintf("%s\n", ent->name);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue