Add link(2).

This commit is contained in:
Jonas 'Sortie' Termansen 2012-10-23 20:09:43 +02:00
parent 2389a834b5
commit 49fe4aa51f
5 changed files with 69 additions and 2 deletions

View File

@ -161,6 +161,7 @@ ioleast.o \
isatty.o \
kernelinfo.o \
kill.o \
link.o \
localeconv.o \
lseek.o \
memstat.o \

View File

@ -115,7 +115,6 @@ pid_t getpgrp(void);
pid_t getsid(pid_t);
uid_t getuid(void);
int lchown(const char*, uid_t, gid_t);
int link(const char*, const char*);
int linkat(int, const char*, int, const char*, int);
int lockf(int, int, off_t);
int nice(int);
@ -169,6 +168,7 @@ char* get_current_dir_name(void);
pid_t getpid(void);
pid_t getppid(void);
int isatty(int);
int link(const char*, const char*);
off_t lseek(int, off_t, int);
int pipe(int [2]);
ssize_t pread(int, void*, size_t, off_t);

33
libc/link.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/>.
link.cpp
Give a new name to a file.
*******************************************************************************/
#include <sys/syscall.h>
#include <unistd.h>
DEFN_SYSCALL2(int, sys_link, SYSCALL_LINK, const char*, const char*);
extern "C" int link(const char* oldpath, const char* newpath)
{
return sys_link(oldpath, newpath);
}

View File

@ -80,6 +80,7 @@
#define SYSCALL_FSTATAT 56
#define SYSCALL_CHMOD 57
#define SYSCALL_CHOWN 58
#define SYSCALL_MAX_NUM 59 /* index of highest constant + 1 */
#define SYSCALL_LINK 59
#define SYSCALL_MAX_NUM 60 /* index of highest constant + 1 */
#endif

View File

@ -349,6 +349,37 @@ static int sys_chmod(const char* path, mode_t mode)
return desc->chmod(&ctx, mode);
}
static int sys_link(const char* oldpath, const char* newpath)
{
ioctx_t ctx; SetupUserIOCtx(&ctx);
char* newpathcopy = GetStringFromUser(newpath);
if ( !newpathcopy )
return -1;
const char* newrelpath = newpathcopy;
Ref<Descriptor> newfrom(PrepareLookup(&newrelpath));
char* final_elem;
Ref<Descriptor> dir = OpenDirContainingPath(&ctx, newfrom, newpathcopy,
&final_elem);
delete[] newpathcopy;
if ( !dir )
return -1;
char* oldpathcopy = GetStringFromUser(oldpath);
if ( !oldpathcopy ) { delete[] final_elem; return -1; }
const char* oldrelpath = oldpathcopy;
Ref<Descriptor> oldfrom(PrepareLookup(&oldrelpath));
Ref<Descriptor> file = oldfrom->open(&ctx, oldrelpath, O_RDONLY);
delete[] oldpathcopy;
if ( !file ) { delete[] final_elem; return -1; }
int ret = dir->link(&ctx, final_elem, file);
delete[] final_elem;
return ret;
}
static int sys_settermmode(int fd, unsigned mode)
{
Ref<Descriptor> desc = CurrentProcess()->GetDescriptor(fd);
@ -399,6 +430,7 @@ void Init()
Syscall::Register(SYSCALL_FTRUNCATE, (void*) sys_ftruncate);
Syscall::Register(SYSCALL_GETTERMMODE, (void*) sys_gettermmode);
Syscall::Register(SYSCALL_ISATTY, (void*) sys_isatty);
Syscall::Register(SYSCALL_LINK, (void*) sys_link);
Syscall::Register(SYSCALL_MKDIR, (void*) sys_mkdir);
Syscall::Register(SYSCALL_OPENAT, (void*) sys_openat);
Syscall::Register(SYSCALL_OPEN, (void*) sys_open);