[PATCH] utime: replaced existing (broken) utime(2) wrappers with a common function
Enrico Scholz <[email protected]> Sun, 20 Feb 2011 16:16:38 +0100
| Newsgroups | gmane.linux.lib.dietlibc |
|---|---|
| Message-ID | <1298215004-28546-2-git-send-email-enrico.scholz@informatik.tu-chemnitz.de> |
utime(2) is not available on every architecture. The alpha and ia64 provided already replacements, but these were wrong (they made utime(2) an alias for utimes(2), which is wrong). Patch adds a C implementation and a testprogram for this syscall. It corrects also the type of the 2nd argument in the prototype which expects a pointer to a *constant* buffer. Signed-off-by: Enrico Scholz <[email protected]> --- alpha/utime.S | 3 --- ia64/utime.S | 3 --- include/utime.h | 2 +- lib/__utime.c | 18 ++++++++++++++++++ syscalls.s/utime.S | 2 ++ test/Makefile | 2 +- test/runtests.sh | 2 +- test/utime.c | 37 +++++++++++++++++++++++++++++++++++++ 8 files changed, 60 insertions(+), 9 deletions(-) delete mode 100644 alpha/utime.S delete mode 100644 ia64/utime.S create mode 100644 lib/__utime.c create mode 100644 test/utime.c diff --git a/alpha/utime.S b/alpha/utime.S deleted file mode 100644 index a9a8aad..0000000 --- a/alpha/utime.S +++ /dev/null @@ -1,3 +0,0 @@ -#include "syscalls.h" - -syscall(utimes,utime) diff --git a/ia64/utime.S b/ia64/utime.S deleted file mode 100644 index a9a8aad..0000000 --- a/ia64/utime.S +++ /dev/null @@ -1,3 +0,0 @@ -#include "syscalls.h" - -syscall(utimes,utime) diff --git a/include/utime.h b/include/utime.h index eebc4da..ce98bff 100644 --- a/include/utime.h +++ b/include/utime.h @@ -12,7 +12,7 @@ struct utimbuf { time_t modtime; /* modification time */ }; -int utime(const char* filename, struct utimbuf* buf) __THROW; +int utime(const char* filename, const struct utimbuf* buf) __THROW; __END_DECLS diff --git a/lib/__utime.c b/lib/__utime.c new file mode 100644 index 0000000..e013265 --- /dev/null +++ b/lib/__utime.c @@ -0,0 +1,18 @@ +#include <utime.h> +#include <syscalls.h> + +#ifndef __NR_utime +int utime(const char *filename, const struct utimbuf *times) +{ + if (times == NULL) + return utimes(filename, NULL); + else { + struct timeval tvs[2]; + tvs[0].tv_sec = times->actime; + tvs[0].tv_usec = 0; + tvs[1].tv_sec = times->modtime; + tvs[1].tv_usec = 0; + return utimes(filename, tvs); + } +} +#endif diff --git a/syscalls.s/utime.S b/syscalls.s/utime.S index 08cd221..9878b8f 100644 --- a/syscalls.s/utime.S +++ b/syscalls.s/utime.S @@ -1,3 +1,5 @@ #include "syscalls.h" +#ifdef __NR_utime syscall(utime,utime) +#endif diff --git a/test/Makefile b/test/Makefile index 949c517..8f5b964 100644 --- a/test/Makefile +++ b/test/Makefile @@ -14,7 +14,7 @@ glob grent hasmntopt hello iconv if_nameindex ltostr malloc-debugger math md5_te memccpy memchr memcmp memrchr memusage mktime mmap_test pipe printf printftest \ protoent prototypes putenv pwent rand48 read1 readdir regex select sendfile servent siglist \ speed spent sprintf sscanf stdarg strcasecmp strcmp strncat strncpy strptime strrchr \ -strstr strtol sysconf sysenter ungetc waitpid +strstr strtol sysconf sysenter ungetc utime waitpid test: $(TESTPROGRAMS) diff --git a/test/runtests.sh b/test/runtests.sh index 6fb6bdc..4703888 100644 --- a/test/runtests.sh +++ b/test/runtests.sh @@ -1,6 +1,6 @@ SUBDIRS="dirent inet stdio string stdlib time" -TESTPROGRAMS="adjtime alarm argv atexit bsearch byteswap calloc confstr empty fadvise flush fputc ffs fnmatch ftw fwrite getaddrinfo getenv getdelim getgrnam gethostbyaddr gethostbyname gethostbyname_r getmntent getopt getpwnam getservbyname getservbyport getusershell glob grent hasmntopt hello iconv if_nameindex ltostr malloc-debugger math md5_testharness memccpy memchr memcmp memrchr memusage mktime mmap_test pipe printf printftest protoent prototypes putenv pwent rand48 readdir regex select sendfile servent siglist speed spent sprintf sscanf stdarg strcasecmp strcmp strncat strncpy strptime strrchr strstr strtol sysconf sysenter ungetc waitpid" +TESTPROGRAMS="adjtime alarm argv atexit bsearch byteswap calloc confstr empty fadvise flush fputc ffs fnmatch ftw fwrite getaddrinfo getenv getdelim getgrnam gethostbyaddr gethostbyname gethostbyname_r getmntent getopt getpwnam getservbyname getservbyport getusershell glob grent hasmntopt hello iconv if_nameindex ltostr malloc-debugger math md5_testharness memccpy memchr memcmp memrchr memusage mktime mmap_test pipe printf printftest protoent prototypes putenv pwent rand48 readdir regex select sendfile servent siglist speed spent sprintf sscanf stdarg strcasecmp strcmp strncat strncpy strptime strrchr strstr strtol sysconf sysenter ungetc utime waitpid" STDIN="read1" PASS="getpass" diff --git a/test/utime.c b/test/utime.c new file mode 100644 index 0000000..955ac6f --- /dev/null +++ b/test/utime.c @@ -0,0 +1,37 @@ +#include <assert.h> +#include <stdlib.h> +#include <utime.h> +#include <unistd.h> +#include <sys/stat.h> + +int main(void) +{ + char file[] = "/tmp/utime-test.XXXXXX"; + int tmp_fd; + struct utimbuf utm = { + .actime = 23, + .modtime = 42, + }; + struct stat st; + time_t now; + + tmp_fd = mkstemp(file); + close(tmp_fd); + + assert(utime(file, &utm) == 0); + assert(stat(file, &st) == 0); + assert(st.st_atime == utm.actime); + assert(st.st_mtime == utm.modtime); + + now = time(NULL); + assert(utime(file, NULL) == 0); + assert(stat(file, &st) == 0); + + assert(st.st_atime == st.st_mtime); + assert(st.st_atime >= now); + assert(st.st_atime - now < 10); + + unlink(file); + + return EXIT_SUCCESS; +} -- 1.7.4