AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
unlink.c
Go to the documentation of this file.
1
4
5#include <core/system.h>
6#include <fs/vfs.h>
7
11static int vfs_remove(const char *path, struct uio *uio, int rmdir)
12{
13 int ret = 0;
14 struct vnode *dir = NULL, *vnode = NULL;
15 char *name = NULL;
16 struct dirent dirent;
17
18 if ((ret = vfs_lookup_parent(path, uio, &dir, &name)))
19 return ret;
20
21 /* removing an entry needs write and search permission on the directory */
22 if ((ret = vfs_access(dir, uio, VFS_W | VFS_X)))
23 goto done;
24
25 if ((ret = vfs_finddir(dir, name, &dirent)))
26 goto done;
27
28 if ((ret = vfs_vget(dir, dirent.d_ino, &vnode)))
29 goto done;
30
31 if (rmdir) {
32 if (!S_ISDIR(vnode->mode))
33 ret = -ENOTDIR;
34 else if (!vfs_dir_empty(vnode))
35 ret = -ENOTEMPTY;
36 } else if (S_ISDIR(vnode->mode)) {
37 ret = -EISDIR;
38 }
39
40 if (!ret)
41 ret = vfs_vunlink(dir, name, uio);
42
43done:
44 /* the vnode is freed here if that was its last link and nothing else
45 * has it open */
46 if (vnode)
48
49 vfs_close(dir);
50 kfree(name);
51 return ret;
52}
53
62int vfs_unlink(const char *path, struct uio *uio)
63{
64 return vfs_remove(path, uio, 0);
65}
66
74int vfs_rmdir(const char *path, struct uio *uio)
75{
76 return vfs_remove(path, uio, 1);
77}
int vfs_close(struct vnode *vnode)
Drops a reference to a vnode.
Definition close.c:18
void kfree(void *)
Frees an object that kmalloc() gave.
Definition kvmem.c:146
int vfs_lookup_parent(const char *path, struct uio *uio, struct vnode **dir, char **name)
Finds the directory of a path's last component, and the component's name, to make or remove an entry ...
Definition lookup.c:414
int vfs_finddir(struct vnode *dir, const char *name, struct dirent *dirent)
Finds the entry of a directory that has a name.
Definition readdir.c:46
int vfs_dir_empty(struct vnode *dir)
Says whether a directory is empty, so that it can be removed.
Definition readdir.c:69
#define NULL
Definition stddef.h:27
Who does a filesystem operation: where its paths start, and its credentials and umask.
Definition vfs.h:60
A file in memory (a vnode): what its inode says, and the kernel's own state.
Definition vfs.h:113
mode_t mode
Definition vfs.h:118
int vfs_access(struct vnode *vnode, struct uio *uio, int mode)
Checks whether a user may access a vnode in a way: the owner's permissions apply to its owner,...
Definition vfs.c:114
#define VFS_W
Definition vfs.h:296
#define VFS_X
Definition vfs.h:297
int vfs_vget(struct vnode *super, ino_t ino, struct vnode **ref)
Gets the vnode of an inode, by its number.
Definition vops.c:176
int vfs_vunlink(struct vnode *dir, const char *fn, struct uio *uio)
Removes a name from a directory.
Definition vops.c:79