Raymond Toy pushed to branch issue-125-unix-stat-wrong at cmucl / cmucl
Commits: 50da071d by Raymond Toy at 2022-08-03T12:38:09-07:00 Clean up impl more
* Move the stat functions from unix.c to os-common.c and remove unix.c. * Update Linux config files appropriately. * Use the same unix_stat function everywhere, so we can remove all of the different OS-specific definitions of struct stat that we no longer need.
- - - - -
6 changed files:
- src/code/unix.lisp - src/i18n/locale/cmucl-unix.pot - src/lisp/Config.x86_linux - src/lisp/Config.x86_linux_clang - src/lisp/os-common.c - − src/lisp/unix.c
Changes:
===================================== src/code/unix.lisp ===================================== @@ -1346,232 +1346,6 @@ (d-name (array char 256)))) ; name must be no longer than this
-#+(and bsd (not netbsd)) -(def-alien-type nil - (struct stat - (st-dev dev-t) - (st-ino ino-t) - (st-mode mode-t) - (st-nlink nlink-t) - (st-uid uid-t) - (st-gid gid-t) - (st-rdev dev-t) - (st-atime (struct timespec-t)) - (st-mtime (struct timespec-t)) - (st-ctime (struct timespec-t)) - (st-size off-t) - (st-blocks off-t) - (st-blksize unsigned-long) - (st-flags unsigned-long) - (st-gen unsigned-long) - (st-lspare long) - (st-qspare (array long 4)))) - -#+svr4 -(def-alien-type nil - (struct stat - (st-dev dev-t) - (st-pad1 #-linux (array long 3) #+linux unsigned-short) - (st-ino ino-t) - (st-mode #-linux unsigned-long #+linux unsigned-short) - (st-nlink #-linux short #+linux unsigned-short) - (st-uid #-linux uid-t #+linux unsigned-short) - (st-gid #-linux gid-t #+linux unsigned-short) - (st-rdev dev-t) - (st-pad2 #-linux (array long 2) #+linux unsigned-short) - (st-size off-t) - #-linux (st-pad3 long) - #+linux (st-blksize unsigned-long) - #+linux (st-blocks unsigned-long) - #-linux (st-atime (struct timestruc-t)) - #+linux (st-atime unsigned-long) - #+linux (unused-1 unsigned-long) - #-linux (st-mtime (struct timestruc-t)) - #+linux (st-mtime unsigned-long) - #+linux (unused-2 unsigned-long) - #-linux (st-ctime (struct timestruc-t)) - #+linux (st-ctime unsigned-long) - #+linux (unused-3 unsigned-long) - #+linux (unused-4 unsigned-long) - #+linux (unused-5 unsigned-long) - #-linux(st-blksize long) - #-linux (st-blocks long) - #-linux (st-fstype (array char 16)) - #-linux (st-pad4 (array long 8)))) - -#+(and nil linux) -(def-alien-type nil - (struct stat - (st-dev dev-t) - #-(or alpha amd64) (st-pad1 unsigned-short) - (st-ino ino64-t) - #+alpha (st-pad1 unsigned-int) - #-amd64 (st-mode mode-t) - (st-nlink nlink-t) - #+amd64 (st-mode mode-t) - (st-uid uid-t) - (st-gid gid-t) - (st-rdev dev-t) - #-alpha (st-pad2 unsigned-short) - (st-size off-t) - #-alpha (st-blksize unsigned-long) - #-alpha (st-blocks blkcnt-t) - (st-atime time-t) - #-alpha (unused-1 unsigned-long) - (st-mtime time-t) - #-alpha (unused-2 unsigned-long) - (st-ctime time-t) - #+alpha (st-blocks int) - #+alpha (st-pad2 unsigned-int) - #+alpha (st-blksize unsigned-int) - #+alpha (st-flags unsigned-int) - #+alpha (st-gen unsigned-int) - #+alpha (st-pad3 unsigned-int) - #+alpha (unused-1 unsigned-long) - #+alpha (unused-2 unsigned-long) - (unused-3 unsigned-long) - (unused-4 unsigned-long) - #-alpha (unused-5 unsigned-long))) - -;;; 64-bit stat for Solaris -#+solaris -(def-alien-type nil - (struct stat64 - (st-dev dev-t) - (st-pad1 (array long 3)) ; Pad so ino is 64-bit aligned - (st-ino ino64-t) - (st-mode unsigned-long) - (st-nlink short) - (st-uid uid-t) - (st-gid gid-t) - (st-rdev dev-t) - (st-pad2 (array long 3)) ; Pad so size is 64-bit aligned - (st-size off64-t) - (st-atime (struct timestruc-t)) - (st-mtime (struct timestruc-t)) - (st-ctime (struct timestruc-t)) - (st-blksize long) - (st-pad3 (array long 1)) ; Pad so blocks is 64-bit aligned - (st-blocks blkcnt64-t) - (st-fstype (array char 16)) - (st-pad4 (array long 8)))) - -#+netbsd -(def-alien-type nil - (struct stat - (st-dev dev-t) - (st-mode mode-t) - (st-ino ino-t) - (st-nlink nlink-t) - (st-uid uid-t) - (st-gid gid-t) - (st-rdev dev-t) - (st-atime (struct timespec-t)) - (st-mtime (struct timespec-t)) - (st-ctime (struct timespec-t)) - (st-birthtime (struct timespec-t)) - (st-size off-t) - (st-blocks off-t) - (st-blksize long) - (st-flags unsigned-long) - (st-gen unsigned-long) - (st-spare (array unsigned-long 2)))) - -#-linux -(defmacro extract-stat-results (buf) - `(values T - (slot ,buf 'st-dev) - (slot ,buf 'st-ino) - (slot ,buf 'st-mode) - (slot ,buf 'st-nlink) - (slot ,buf 'st-uid) - (slot ,buf 'st-gid) - (slot ,buf 'st-rdev) - (slot ,buf 'st-size) - #-(or svr4 BSD) (slot ,buf 'st-atime) - #+svr4 (slot (slot ,buf 'st-atime) 'tv-sec) - #+BSD (slot (slot ,buf 'st-atime) 'ts-sec) - #-(or svr4 BSD)(slot ,buf 'st-mtime) - #+svr4 (slot (slot ,buf 'st-mtime) 'tv-sec) - #+BSD(slot (slot ,buf 'st-mtime) 'ts-sec) - #-(or svr4 BSD) (slot ,buf 'st-ctime) - #+svr4 (slot (slot ,buf 'st-ctime) 'tv-sec) - #+BSD(slot (slot ,buf 'st-ctime) 'ts-sec) - #+netbsd (slot (slot ,buf 'st-birthtime) 'ts-sec) - (slot ,buf 'st-blksize) - (slot ,buf 'st-blocks))) - -#+linux -(defmacro extract-stat-results (buf) - `(values T - #+(or alpha amd64) - (slot ,buf 'st-dev) - #-(or alpha amd64) - (+ (deref (slot ,buf 'st-dev) 0) - (* (+ +max-u-long+ 1) - (deref (slot ,buf 'st-dev) 1))) ;;; let's hope this works.. - (slot ,buf 'st-ino) - (slot ,buf 'st-mode) - (slot ,buf 'st-nlink) - (slot ,buf 'st-uid) - (slot ,buf 'st-gid) - #+(or alpha amd64) - (slot ,buf 'st-rdev) - #-(or alpha amd64) - (+ (deref (slot ,buf 'st-rdev) 0) - (* (+ +max-u-long+ 1) - (deref (slot ,buf 'st-rdev) 1))) ;;; let's hope this works.. - (slot ,buf 'st-size) - (slot ,buf 'st-atime) - (slot ,buf 'st-mtime) - (slot ,buf 'st-ctime) - (slot ,buf 'st-blksize) - (slot ,buf 'st-blocks))) - -#-(or linux solaris) -(progn -(defun unix-stat (name) - _N"Unix-stat retrieves information about the specified - file returning them in the form of multiple values. - See the UNIX Programmer's Manual for a description - of the values returned. If the call fails, then NIL - and an error number is returned instead." - (declare (type unix-pathname name)) - (when (string= name "") - (setf name ".")) - (with-alien ((buf (struct stat))) - (syscall (#+linux "stat64" #+netbsd "__stat50" #-(or linux netbsd) "stat" - c-string (* (struct stat))) - (extract-stat-results buf) - (%name->file name) (addr buf)))) - -(defun unix-lstat (name) - _N"Unix-lstat is similar to unix-stat except the specified - file must be a symbolic link." - (declare (type unix-pathname name)) - (with-alien ((buf (struct stat))) - (syscall (#+linux "lstat64" #+netbsd "__lstat50" #-(or linux netbsd) "lstat" - c-string (* (struct stat))) - (extract-stat-results buf) - (%name->file name) (addr buf)))) - -(defun unix-fstat (fd) - _N"Unix-fstat is similar to unix-stat except the file is specified - by the file descriptor fd." - (declare (type unix-fd fd)) - (with-alien ((buf (struct stat))) - (syscall (#+linux "fstat64" #+netbsd "__fstat50" #-(or linux netbsd) "fstat" - int (* (struct stat))) - (extract-stat-results buf) - fd (addr buf)))) -) - -;; On linux we call out to our own C routine to return the appropriate -;; parts so that we don't have to mess around with making sure the -;; struct stat is consistent with the library. -;; -;; This should be updated so that all OSes do this. -#+linux (macrolet ((call-stat (c-func-name first-arg-type first-arg) ;; Call the stat function named C-FUNC-NAME. The type of the
===================================== src/i18n/locale/cmucl-unix.pot ===================================== @@ -497,14 +497,14 @@ msgstr ""
#: src/code/unix.lisp msgid "" -"Unix-lstat is similar to unix-stat except the specified\n" -" file must be a symbolic link." +"Unix-fstat is similar to unix-stat except the file is specified\n" +" by the file descriptor fd." msgstr ""
#: src/code/unix.lisp msgid "" -"Unix-fstat is similar to unix-stat except the file is specified\n" -" by the file descriptor fd." +"Unix-lstat is similar to unix-stat except the specified\n" +" file must be a symbolic link." msgstr ""
#: src/code/unix.lisp
===================================== src/lisp/Config.x86_linux ===================================== @@ -7,7 +7,7 @@ CFLAGS += -rdynamic -march=pentium4 -mfpmath=sse -mtune=generic
UNDEFSYMPATTERN = -Xlinker -u -Xlinker & ASSEM_SRC += linux-stubs.S -OS_SRC += Linux-os.c elf.c unix.c +OS_SRC += Linux-os.c elf.c OS_LIBS = -ldl OS_LINK_FLAGS = -m32 -rdynamic -Xlinker --export-dynamic -Xlinker -Map -Xlinker foo OS_LINK_FLAGS += -Wl,-z,noexecstack
===================================== src/lisp/Config.x86_linux_clang ===================================== @@ -12,7 +12,7 @@ CFLAGS += -msse2 -mtune=pentium4 -ftrapping-math
UNDEFSYMPATTERN = -Xlinker -u -Xlinker & ASSEM_SRC += linux-stubs.S -OS_SRC += Linux-os.c elf.c unix.c +OS_SRC += Linux-os.c elf.c OS_LIBS = -ldl OS_LINK_FLAGS = -m32 -rdynamic -Xlinker --export-dynamic -Xlinker -Map -Xlinker foo OS_LINK_FLAGS += -Wl,-z,noexecstack
===================================== src/lisp/os-common.c ===================================== @@ -5,12 +5,17 @@
*/
+#define _LARGEFILE_SOURCE +#define _FILE_OFFSET_BITS 64 + +#include <stdio.h> #include <errno.h> #include <math.h> #include <netdb.h> #include <stdio.h> #include <string.h> #include <time.h> +#include <sys/stat.h>
#include "os.h" #include "internals.h" @@ -589,3 +594,98 @@ os_sleep(double seconds) requested = remaining; } } + +int unix_stat(const char* path, dev_t *dev, ino_t *ino, mode_t *mode, nlink_t *nlink, + uid_t *uid, gid_t *gid, dev_t *rdev, off_t *size, + time_t *atime, time_t *mtime, time_t *ctime, + long *blksize, off_t *blocks) +{ + int rc; + struct stat buf; + + rc = stat(path, &buf); + + fprintf(stderr, "size dev %d\n", sizeof(buf.st_dev)); + fprintf(stderr, "size ino %d\n", sizeof(buf.st_ino)); + fprintf(stderr, "size mode %d\n", sizeof(buf.st_mode)); + fprintf(stderr, "size nlink %d\n", sizeof(buf.st_nlink)); + fprintf(stderr, "size uid %d\n", sizeof(buf.st_uid)); + fprintf(stderr, "size gid %d\n", sizeof(buf.st_gid)); + fprintf(stderr, "size rdev %d\n", sizeof(buf.st_rdev)); + fprintf(stderr, "size size %d\n", sizeof(buf.st_size)); + fprintf(stderr, "size atime %d\n", sizeof(buf.st_atime)); + fprintf(stderr, "size mtime %d\n", sizeof(buf.st_mtime)); + fprintf(stderr, "size ctime %d\n", sizeof(buf.st_ctime)); + fprintf(stderr, "size blksize %d\n", sizeof(buf.st_blksize)); + fprintf(stderr, "size blocks %d\n", sizeof(buf.st_blocks)); + + *dev = buf.st_dev; + *ino = buf.st_ino; + *mode = buf.st_mode; + *nlink = buf.st_nlink; + *uid = buf.st_uid; + *gid = buf.st_gid; + *rdev = buf.st_rdev; + *size = buf.st_size; + *atime = buf.st_atime; + *mtime = buf.st_mtime; + *ctime = buf.st_ctime; + *blksize = buf.st_blksize; + *blocks = buf.st_blocks; + + return rc; +} + +int unix_fstat(int fd, dev_t *dev, ino_t *ino, mode_t *mode, nlink_t *nlink, + uid_t *uid, gid_t *gid, dev_t *rdev, off_t *size, + time_t *atime, time_t *mtime, time_t *ctime, + long *blksize, off_t *blocks) +{ + int rc; + struct stat buf; + + rc = fstat(fd, &buf); + + *dev = buf.st_dev; + *ino = buf.st_ino; + *mode = buf.st_mode; + *nlink = buf.st_nlink; + *uid = buf.st_uid; + *gid = buf.st_gid; + *rdev = buf.st_rdev; + *size = buf.st_size; + *atime = buf.st_atime; + *mtime = buf.st_mtime; + *ctime = buf.st_ctime; + *blksize = buf.st_blksize; + *blocks = buf.st_blocks; + + return rc; +} + +int unix_lstat(const char* path, dev_t *dev, ino_t *ino, mode_t *mode, nlink_t *nlink, + uid_t *uid, gid_t *gid, dev_t *rdev, off_t *size, + time_t *atime, time_t *mtime, time_t *ctime, + long *blksize, off_t *blocks) +{ + int rc; + struct stat buf; + + rc = lstat(path, &buf); + + *dev = buf.st_dev; + *ino = buf.st_ino; + *mode = buf.st_mode; + *nlink = buf.st_nlink; + *uid = buf.st_uid; + *gid = buf.st_gid; + *rdev = buf.st_rdev; + *size = buf.st_size; + *atime = buf.st_atime; + *mtime = buf.st_mtime; + *ctime = buf.st_ctime; + *blksize = buf.st_blksize; + *blocks = buf.st_blocks; + + return rc; +}
===================================== src/lisp/unix.c deleted ===================================== @@ -1,110 +0,0 @@ -/* - * C interfaces to unix syscalls - */ - -/* We want to support large files */ - -#define _LARGEFILE_SOURCE -#define _FILE_OFFSET_BITS 64 - -#include <stdio.h> -#include <sys/stat.h> - -int unix_stat(const char* path, dev_t *dev, ino_t *ino, mode_t *mode, nlink_t *nlink, - uid_t *uid, gid_t *gid, dev_t *rdev, off_t *size, - time_t *atime, time_t *mtime, time_t *ctime, - long *blksize, off_t *blocks) -{ - int rc; - struct stat buf; - - rc = stat(path, &buf); - - fprintf(stderr, "size dev %d\n", sizeof(buf.st_dev)); - fprintf(stderr, "size ino %d\n", sizeof(buf.st_ino)); - fprintf(stderr, "size mode %d\n", sizeof(buf.st_mode)); - fprintf(stderr, "size nlink %d\n", sizeof(buf.st_nlink)); - fprintf(stderr, "size uid %d\n", sizeof(buf.st_uid)); - fprintf(stderr, "size gid %d\n", sizeof(buf.st_gid)); - fprintf(stderr, "size rdev %d\n", sizeof(buf.st_rdev)); - fprintf(stderr, "size size %d\n", sizeof(buf.st_size)); - fprintf(stderr, "size atime %d\n", sizeof(buf.st_atime)); - fprintf(stderr, "size mtime %d\n", sizeof(buf.st_mtime)); - fprintf(stderr, "size ctime %d\n", sizeof(buf.st_ctime)); - fprintf(stderr, "size blksize %d\n", sizeof(buf.st_blksize)); - fprintf(stderr, "size blocks %d\n", sizeof(buf.st_blocks)); - - - *dev = buf.st_dev; - *ino = buf.st_ino; - *mode = buf.st_mode; - *nlink = buf.st_nlink; - *uid = buf.st_uid; - *gid = buf.st_gid; - *rdev = buf.st_rdev; - *size = buf.st_size; - *atime = buf.st_atime; - *mtime = buf.st_mtime; - *ctime = buf.st_ctime; - *blksize = buf.st_blksize; - *blocks = buf.st_blocks; - - return rc; -} - -int unix_fstat(int fd, dev_t *dev, ino_t *ino, mode_t *mode, nlink_t *nlink, - uid_t *uid, gid_t *gid, dev_t *rdev, off_t *size, - time_t *atime, time_t *mtime, time_t *ctime, - long *blksize, off_t *blocks) -{ - int rc; - struct stat buf; - - rc = fstat(fd, &buf); - - *dev = buf.st_dev; - *ino = buf.st_ino; - *mode = buf.st_mode; - *nlink = buf.st_nlink; - *uid = buf.st_uid; - *gid = buf.st_gid; - *rdev = buf.st_rdev; - *size = buf.st_size; - *atime = buf.st_atime; - *mtime = buf.st_mtime; - *ctime = buf.st_ctime; - *blksize = buf.st_blksize; - *blocks = buf.st_blocks; - - return rc; -} - -int unix_lstat(const char* path, dev_t *dev, ino_t *ino, mode_t *mode, nlink_t *nlink, - uid_t *uid, gid_t *gid, dev_t *rdev, off_t *size, - time_t *atime, time_t *mtime, time_t *ctime, - long *blksize, off_t *blocks) -{ - int rc; - struct stat buf; - - rc = lstat(path, &buf); - - *dev = buf.st_dev; - *ino = buf.st_ino; - *mode = buf.st_mode; - *nlink = buf.st_nlink; - *uid = buf.st_uid; - *gid = buf.st_gid; - *rdev = buf.st_rdev; - *size = buf.st_size; - *atime = buf.st_atime; - *mtime = buf.st_mtime; - *ctime = buf.st_ctime; - *blksize = buf.st_blksize; - *blocks = buf.st_blocks; - - return rc; -} - - -
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/commit/50da071db677ed03e8054221...