diff --git a/libc/Makefile b/libc/Makefile index 6ee8c199..97b335d0 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -190,6 +190,7 @@ fstatat.o \ fstat.o \ fsync.o \ ftruncate.o \ +futimens.o \ getc.o \ getcwd.o \ getdtablesize.o \ @@ -270,6 +271,8 @@ unlinkat.o \ unlink.o \ uptime.o \ usleep.o \ +utimensat.o \ +utimens.o \ utime.o \ vscanf.o \ wait.o \ diff --git a/libc/futimens.cpp b/libc/futimens.cpp new file mode 100644 index 00000000..ceac2670 --- /dev/null +++ b/libc/futimens.cpp @@ -0,0 +1,34 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + 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 . + + futimens.cpp + Change file last access and modification times. + +*******************************************************************************/ + +#include +#include + +// TODO: You cannot currently pass array types to the DEFN_SYSCALL* family. +DEFN_SYSCALL2(int, sys_futimens, SYSCALL_FUTIMENS, int, const struct timespec*); + +extern "C" int futimens(int fd, const struct timespec times[2]) +{ + return sys_futimens(fd, times); +} diff --git a/libc/include/sys/stat.h b/libc/include/sys/stat.h index bc53134c..68fb7b89 100644 --- a/libc/include/sys/stat.h +++ b/libc/include/sys/stat.h @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. This file is part of the Sortix C Library. @@ -22,10 +22,8 @@ *******************************************************************************/ -/* TODO: Make this header comply with POSIX-1.2008, if it makes sense. */ - -#ifndef _SYS_STAT_H -#define _SYS_STAT_H 1 +#ifndef INCLUDE_SYS_STAT_H +#define INCLUDE_SYS_STAT_H #include @@ -42,6 +40,7 @@ __BEGIN_DECLS @include(time_t.h) __END_DECLS +#include #include __BEGIN_DECLS @@ -61,11 +60,19 @@ int fchmod(int fd, mode_t mode); int fchmodat(int dirfd, const char* path, mode_t mode, int flags); int fstat(int fd, struct stat* st); int fstatat(int dirfd, const char* path, struct stat* buf, int flags); +int futimens(int fd, const struct timespec times[2]); int lstat(const char* restrict path, struct stat* restrict st); int mkdir(const char* path, mode_t mode); int mkdirat(int dirfd, const char* path, mode_t mode); +/* TODO: mkfifo */ +/* TODO: mkfifoat */ +/* TODO: mknod? */ +/* TODO: mknodat? */ int stat(const char* restrict path, struct stat* restrict st); mode_t umask(mode_t mask); +int utimens(const char* path, const struct timespec times[2]); +int utimensat(int dirfd, const char* path, const struct timespec times[2], + int flags); __END_DECLS diff --git a/libc/include/utime.h b/libc/include/utime.h new file mode 100644 index 00000000..b316311e --- /dev/null +++ b/libc/include/utime.h @@ -0,0 +1,44 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013 + + 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 . + + utime.h + Deprecated interface to change the file access and moficiation times. + +*******************************************************************************/ + +#ifndef INCLUDE_UTIME_H +#define INCLUDE_UTIME_H + +#include + +__BEGIN_DECLS + +@include(time_t.h) + +struct utimbuf +{ + time_t actime; + time_t modtime; +}; + +int utime(const char* filename, const struct utimbuf* times); + +__END_DECLS + +#endif diff --git a/libc/utime.cpp b/libc/utime.cpp index e9ad09ef..1367435c 100644 --- a/libc/utime.cpp +++ b/libc/utime.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2013. This file is part of the Sortix C Library. @@ -22,14 +22,20 @@ *******************************************************************************/ +#include #include -//#include -#include +#include +#include +#include + +// TODO: This interface has been deprecated by utimens (although that's a +// non-standard Sortix extension, in which case by utimensat) - it'd be +// nice to remove this at some point. extern "C" int utime(const char* filepath, const struct utimbuf* times) { - (void) filepath; - (void) times; - // TODO: Sure, I did that! There is no kernel support for this yet. - return 0; + struct timespec ts_times[2]; + ts_times[0] = timespec_make(times->actime, 0); + ts_times[1] = timespec_make(times->modtime, 0); + return utimens(filepath, ts_times); } diff --git a/libc/utimens.cpp b/libc/utimens.cpp new file mode 100644 index 00000000..b9351246 --- /dev/null +++ b/libc/utimens.cpp @@ -0,0 +1,33 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + 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 . + + utimens.cpp + Change file last access and modification times. + +*******************************************************************************/ + +#include + +#include + +extern "C" +int utimens(const char* path, const struct timespec times[2]) +{ + return utimensat(AT_FDCWD, path, times, 0); +} diff --git a/libc/utimensat.cpp b/libc/utimensat.cpp new file mode 100644 index 00000000..d48c7e72 --- /dev/null +++ b/libc/utimensat.cpp @@ -0,0 +1,39 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + 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 . + + utimensat.cpp + Change file last access and modification times. + +*******************************************************************************/ + +#include +#include + +#include + +// TODO: You cannot currently pass array types to the DEFN_SYSCALL* family. +DEFN_SYSCALL4(int, sys_utimensat, SYSCALL_UTIMENSAT, int, const char*, + const struct timespec*, int); + +extern "C" +int utimensat(int dirfd, const char* path, const struct timespec times[2], + int flags) +{ + return sys_utimensat(dirfd, path, times, flags); +} diff --git a/sortix/include/sortix/syscallnum.h b/sortix/include/sortix/syscallnum.h index 494003d8..9274885d 100644 --- a/sortix/include/sortix/syscallnum.h +++ b/sortix/include/sortix/syscallnum.h @@ -106,6 +106,8 @@ #define SYSCALL_SETEUID 82 #define SYSCALL_SETEGID 83 #define SYSCALL_IOCTL 84 -#define SYSCALL_MAX_NUM 85 /* index of highest constant + 1 */ +#define SYSCALL_UTIMENSAT 85 +#define SYSCALL_FUTIMENS 86 +#define SYSCALL_MAX_NUM 87 /* index of highest constant + 1 */ #endif diff --git a/sortix/io.cpp b/sortix/io.cpp index cd1e50ba..5e576e7d 100644 --- a/sortix/io.cpp +++ b/sortix/io.cpp @@ -450,6 +450,41 @@ static int sys_chmod(const char* path, mode_t mode) return sys_fchmodat(AT_FDCWD, path, mode, 0); } +static int sys_futimens(int fd, const struct timespec user_times[2]) +{ + struct timespec times[2]; + if ( !CopyFromUser(times, user_times, sizeof(times)) ) + return -1; + Ref desc = CurrentProcess()->GetDescriptor(fd); + if ( !desc ) + return -1; + ioctx_t ctx; SetupUserIOCtx(&ctx); + return desc->utimens(&ctx, times); +} + +static int sys_utimensat(int dirfd, const char* path, + const struct timespec user_times[2], int flags) +{ + if ( flags ) + return errno = ENOTSUP, -1; + struct timespec times[2]; + if ( !CopyFromUser(times, user_times, sizeof(times)) ) + return -1; + char* pathcopy = GetStringFromUser(path); + if ( !pathcopy ) + return -1; + ioctx_t ctx; SetupUserIOCtx(&ctx); + const char* relpath = pathcopy; + Ref from = PrepareLookup(&relpath, dirfd); + if ( !from ) { delete[] pathcopy; return -1; } + Ref desc = from->open(&ctx, relpath, O_WRITE); + from.Reset(); + delete[] pathcopy; + if ( !desc ) + return -1; + return desc->utimens(&ctx, times); +} + static int sys_linkat(int olddirfd, const char* oldpath, int newdirfd, const char* newpath, int flags) @@ -608,6 +643,7 @@ void Init() Syscall::Register(SYSCALL_FSTAT, (void*) sys_fstat); Syscall::Register(SYSCALL_FSYNC, (void*) sys_fsync); Syscall::Register(SYSCALL_FTRUNCATE, (void*) sys_ftruncate); + Syscall::Register(SYSCALL_FUTIMENS, (void*) sys_futimens); Syscall::Register(SYSCALL_GETTERMMODE, (void*) sys_gettermmode); Syscall::Register(SYSCALL_IOCTL, (void*) sys_ioctl); Syscall::Register(SYSCALL_ISATTY, (void*) sys_isatty); @@ -632,6 +668,7 @@ void Init() Syscall::Register(SYSCALL_TRUNCATE, (void*) sys_truncate); Syscall::Register(SYSCALL_UNLINKAT, (void*) sys_unlinkat); Syscall::Register(SYSCALL_UNLINK, (void*) sys_unlink); + Syscall::Register(SYSCALL_UTIMENSAT, (void*) sys_utimensat); Syscall::Register(SYSCALL_WRITE, (void*) sys_write); }