syscalls: make the *at syscalls use the dirfd_enter and dirfd_leave functions

This commit is contained in:
Matheus
2023-07-06 19:59:19 -03:00
parent c769735068
commit fd9f9b65fe
6 changed files with 25 additions and 115 deletions
+5 -22
View File
@@ -23,24 +23,9 @@ syscallret_t syscall_faccessat(context_t *, int dirfd, const char *upath, int mo
vnode_t *dirnode = NULL; vnode_t *dirnode = NULL;
file_t *file = NULL; file_t *file = NULL;
if (*path == '/') { ret.errno = dirfd_enter(path, dirfd, &file, &dirnode);
dirnode = sched_getroot(); if (ret.errno)
} else if (dirfd == AT_FDCWD) { goto cleanup;
dirnode = sched_getcwd();
} else {
file = fd_get(dirfd);
if (file == NULL) {
ret.errno = EBADF;
goto cleanup;
}
dirnode = file->vnode;
if (dirnode->type != V_TYPE_DIR) {
ret.errno = ENOTDIR;
goto cleanup;
}
}
vnode_t *node = NULL; vnode_t *node = NULL;
ret.errno = vfs_lookup(&node, dirnode, path, NULL, flags & AT_SYMLINK_NOFOLLOW ? VFS_LOOKUP_NOLINK : 0); ret.errno = vfs_lookup(&node, dirnode, path, NULL, flags & AT_SYMLINK_NOFOLLOW ? VFS_LOOKUP_NOLINK : 0);
@@ -54,10 +39,8 @@ syscallret_t syscall_faccessat(context_t *, int dirfd, const char *upath, int mo
if (node) if (node)
VOP_RELEASE(node); VOP_RELEASE(node);
if (file) if (dirnode)
fd_release(file); dirfd_leave(dirnode, file);
else
VOP_RELEASE(dirnode);
free(path); free(path);
+5 -22
View File
@@ -21,24 +21,9 @@ syscallret_t syscall_mkdirat(context_t *, int dirfd, const char *upath, mode_t m
vnode_t *dirnode = NULL; vnode_t *dirnode = NULL;
file_t *file = NULL; file_t *file = NULL;
if (*path == '/') { ret.errno = dirfd_enter(path, dirfd, &file, &dirnode);
dirnode = sched_getroot(); if (ret.errno)
} else if (dirfd == AT_FDCWD) { goto cleanup;
dirnode = sched_getcwd();
} else {
file = fd_get(dirfd);
if (file == NULL) {
ret.errno = EBADF;
goto cleanup;
}
dirnode = file->vnode;
if (dirnode->type != V_TYPE_DIR) {
ret.errno = ENOTDIR;
goto cleanup;
}
}
vattr_t attr = { vattr_t attr = {
.mode = mode, .mode = mode,
@@ -51,10 +36,8 @@ syscallret_t syscall_mkdirat(context_t *, int dirfd, const char *upath, mode_t m
cleanup: cleanup:
if (file) if (dirnode)
fd_release(file); dirfd_leave(dirnode, file);
else
VOP_RELEASE(dirnode);
free(path); free(path);
+4 -25
View File
@@ -15,31 +15,13 @@ syscallret_t syscall_openat(context_t *context, int dirfd, const char *path, int
++flags; ++flags;
vnode_t *dirnode = NULL; vnode_t *dirnode = NULL;
file_t *dirfile; file_t *dirfile = NULL;
size_t pathsize = strlen(path); // TODO u_strlen size_t pathsize = strlen(path); // TODO u_strlen
char *pathbuf = alloc(pathsize + 1); char *pathbuf = alloc(pathsize + 1);
strcpy(pathbuf, path); // TODO u_strcpy strcpy(pathbuf, path); // TODO u_strcpy
if (*pathbuf == '/') { ret.errno = dirfd_enter(pathbuf, dirfd, &dirfile, &dirnode);
dirnode = sched_getroot();
dirfile = NULL;
} else if (dirfd == AT_FDCWD) {
dirnode = sched_getcwd();
dirfile = NULL;
} else {
dirfile = fd_get(dirfd);
if (dirfile == NULL) {
ret.errno = EBADF;
goto cleanup;
}
dirnode = dirfile->vnode;
if (dirnode->type != V_TYPE_DIR) {
ret.errno = ENOTDIR;
goto cleanup;
}
}
// TODO pass CLOEXEC to fd // TODO pass CLOEXEC to fd
file_t *newfile = NULL; file_t *newfile = NULL;
@@ -96,11 +78,8 @@ syscallret_t syscall_openat(context_t *context, int dirfd, const char *path, int
if (pathbuf) if (pathbuf)
free(pathbuf); free(pathbuf);
if (dirfile) { if (dirnode)
fd_release(dirfile); dirfd_leave(dirnode, dirfile);
} else if (dirnode){
VOP_RELEASE(dirnode);
}
return ret; return ret;
} }
+1 -2
View File
@@ -9,8 +9,7 @@ syscallret_t syscall_readlinkat(context_t *, int dirfd, const char *upath, char
.ret = -1 .ret = -1
}; };
char *path = alloc(strlen(upath + 1)); char *path = alloc(strlen(upath) + 1);
if (path == NULL) { if (path == NULL) {
ret.errno = ENOMEM; ret.errno = ENOMEM;
return ret; return ret;
+5 -22
View File
@@ -68,24 +68,9 @@ syscallret_t syscall_fstatat(context_t *ctx, int dirfd, char *upath, stat_t *ust
vnode_t *dirnode = NULL; vnode_t *dirnode = NULL;
file_t *file = NULL; file_t *file = NULL;
if (*path == '/') { ret.errno = dirfd_enter(path, dirfd, &file, &dirnode);
dirnode = sched_getroot(); if (ret.errno)
} else if (dirfd == AT_FDCWD) { goto cleanup;
dirnode = sched_getcwd();
} else {
file = fd_get(dirfd);
if (file == NULL) {
ret.errno = EBADF;
goto cleanup;
}
dirnode = file->vnode;
if (dirnode->type != V_TYPE_DIR) {
ret.errno = ENOTDIR;
goto cleanup;
}
}
vnode_t *node = NULL; vnode_t *node = NULL;
ret.errno = vfs_lookup(&node, dirnode, path, NULL, flags & AT_SYMLINK_NOFOLLOW ? VFS_LOOKUP_NOLINK : 0); ret.errno = vfs_lookup(&node, dirnode, path, NULL, flags & AT_SYMLINK_NOFOLLOW ? VFS_LOOKUP_NOLINK : 0);
@@ -105,10 +90,8 @@ syscallret_t syscall_fstatat(context_t *ctx, int dirfd, char *upath, stat_t *ust
if (node) if (node)
VOP_RELEASE(node); VOP_RELEASE(node);
if (file) if (dirnode)
fd_release(file); dirfd_leave(dirnode, file);
else
VOP_RELEASE(dirnode);
free(path); free(path);
+5 -22
View File
@@ -31,24 +31,9 @@ syscallret_t syscall_unlinkat(context_t *, int dirfd, const char *upath, int fla
goto cleanup; goto cleanup;
} }
if (*path == '/') { ret.errno = dirfd_enter(path, dirfd, &file, &dirnode);
dirnode = sched_getroot(); if (ret.errno)
} else if (dirfd == AT_FDCWD) { goto cleanup;
dirnode = sched_getcwd();
} else {
file = fd_get(dirfd);
if (file == NULL) {
ret.errno = EBADF;
goto cleanup;
}
dirnode = file->vnode;
if (dirnode->type != V_TYPE_DIR) {
ret.errno = ENOTDIR;
goto cleanup;
}
}
// TODO rmdir flag // TODO rmdir flag
@@ -67,10 +52,8 @@ syscallret_t syscall_unlinkat(context_t *, int dirfd, const char *upath, int fla
if (node) if (node)
VOP_RELEASE(node); VOP_RELEASE(node);
if (file) if (dirnode)
fd_release(file); dirfd_leave(dirnode, file);
else
VOP_RELEASE(dirnode);
free(path); free(path);