Add unlinkat(2).

This commit is contained in:
Jonas 'Sortie' Termansen 2012-10-23 13:50:33 +02:00
parent 8e50f3d76b
commit 835f0d5fbc
6 changed files with 52 additions and 17 deletions

View File

@ -196,6 +196,7 @@ tfork.o \
time.o \
truncate.o \
umask.o \
unlinkat.o \
unlink.o \
uptime.o \
usleep.o \

View File

@ -138,7 +138,6 @@ pid_t tcgetpgrp(int);
int tcsetpgrp(int, pid_t);
char* ttyname(int);
int ttyname_r(int, char*, size_t);
int unlinkat(int, const char*, int);
#if __POSIX_OBSOLETE <= 200801
pid_t setpgrp(void);
@ -180,6 +179,7 @@ int truncate(const char*, off_t);
#if __POSIX_OBSOLETE <= 200112 || defined(_SORTIX_SOURCE)
int usleep(useconds_t useconds);
#endif
int unlinkat(int, const char*, int);
int unlink(const char*);
ssize_t write(int, const void*, size_t);

33
libc/unlinkat.cpp Normal file
View File

@ -0,0 +1,33 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
unlinkat.cpp
Removes a directory entry relative to directory.
*******************************************************************************/
#include <sys/syscall.h>
#include <fcntl.h>
DEFN_SYSCALL3(int, sys_unlinkat, SYSCALL_UNLINKAT, int, const char*, int);
extern "C" int unlinkat(int dirfd, const char* pathname, int flags)
{
return sys_unlinkat(dirfd, pathname, flags);
}

View File

@ -55,6 +55,7 @@ __BEGIN_DECLS
#define F_GETFL 3
#define AT_FDCWD (-100)
#define AT_REMOVEDIR (1<<0)
__END_DECLS

View File

@ -82,6 +82,7 @@
#define SYSCALL_CHOWN 58
#define SYSCALL_LINK 59
#define SYSCALL_DUP2 60
#define SYSCALL_MAX_NUM 61 /* index of highest constant + 1 */
#define SYSCALL_UNLINKAT 61
#define SYSCALL_MAX_NUM 62 /* index of highest constant + 1 */
#endif

View File

@ -171,20 +171,28 @@ static int sys_access(const char* path, int /*mode*/)
return desc ? 0 : -1;
}
static int sys_unlink(const char* path)
static int sys_unlinkat(int dirfd, const char* path, int flags)
{
char* pathcopy = GetStringFromUser(path);
if ( !pathcopy )
return -1;
ioctx_t ctx; SetupUserIOCtx(&ctx);
const char* relpath = pathcopy;
Ref<Descriptor> from = PrepareLookup(&relpath);
int ret = from->unlink(&ctx, relpath);
Ref<Descriptor> from = PrepareLookup(&relpath, dirfd);
if ( !from ) { delete[] pathcopy; return -1; }
int ret;
if ( flags & AT_REMOVEDIR )
ret = from->rmdir(&ctx, relpath);
else
ret = from->unlink(&ctx, relpath);
delete[] pathcopy;
return ret;
}
// TODO: unlinkat
static int sys_unlink(const char* path)
{
return sys_unlinkat(AT_FDCWD, path, 0);
}
static int sys_mkdir(const char* path, mode_t mode)
{
@ -203,19 +211,9 @@ static int sys_mkdir(const char* path, mode_t mode)
static int sys_rmdir(const char* path)
{
char* pathcopy = GetStringFromUser(path);
if ( !pathcopy )
return -1;
ioctx_t ctx; SetupUserIOCtx(&ctx);
const char* relpath = pathcopy;
Ref<Descriptor> from = PrepareLookup(&relpath);
int ret = from->rmdir(&ctx, relpath);
delete[] pathcopy;
return ret;
return sys_unlinkat(AT_FDCWD, path, AT_REMOVEDIR);
}
// TODO: unlinkat(AT_REMOVEDIR)
static int sys_truncate(const char* path, off_t length)
{
char* pathcopy = GetStringFromUser(path);
@ -451,6 +449,7 @@ void Init()
Syscall::Register(SYSCALL_STAT, (void*) sys_stat);
Syscall::Register(SYSCALL_TCGETWINSIZE, (void*) sys_tcgetwinsize);
Syscall::Register(SYSCALL_TRUNCATE, (void*) sys_truncate);
Syscall::Register(SYSCALL_UNLINKAT, (void*) sys_unlinkat);
Syscall::Register(SYSCALL_UNLINK, (void*) sys_unlink);
Syscall::Register(SYSCALL_WRITE, (void*) sys_write);
}