[Git][cmucl/cmucl][issue-125-unix-stat-wrong] Don't use dev_t and friends in stat interface definition.

Raymond Toy (@rtoy) gitlab at common-lisp.net
Thu Aug 4 02:01:46 UTC 2022



Raymond Toy pushed to branch issue-125-unix-stat-wrong at cmucl / cmucl


Commits:
b726c5a4 by Raymond Toy at 2022-08-03T19:01:31-07:00
Don't use dev_t and friends in stat interface definition.

To simplify things, don't use dev_t and friends in the C interface.
Instead, use the largest integer type that any OS would want to use.
So dev_t becomes u_int64_t because Linux can return 64-bit integers
here.  This would work just fine for macos which uses 32-bit integers.

We are depending on the compiler to produce a warning or error if we
try to store a larger integer type into a smaller type.

- - - - -


2 changed files:

- src/code/unix.lisp
- src/lisp/os-common.c


Changes:

=====================================
src/code/unix.lisp
=====================================
@@ -69,6 +69,7 @@
     #+(and bsd netbsd) int64-t
     #+alpha unsigned-int)
 
+#+nil
 (def-alien-type dev-t
     #-(or alpha svr4 bsd linux) short
     #+linux u-int64-t
@@ -1351,37 +1352,37 @@
        ;; to the stat function.  fstat is different from stat and
        ;; lstat since it takes an fd for the first arg instead of
        ;; string.
-       `(with-alien ((dev dev-t)
-		     (ino ino-t)
-		     (mode mode-t)
-		     (nlink nlink-t)
-		     (uid uid-t)
-		     (gid gid-t)
-		     (rdev dev-t)
-		     (size off-t)
-		     (atime time-t)
-		     (mtime time-t)
-		     (ctime time-t)
+       `(with-alien ((dev u-int64-t)
+		     (ino u-int64-t)
+		     (mode c-call:unsigned-int)
+		     (nlink u-int64-t)
+		     (uid c-call:unsigned-int)
+		     (gid c-call:unsigned-int)
+		     (rdev u-int64-t)
+		     (size int64-t)
+		     (atime int64-t)
+		     (mtime int64-t)
+		     (ctime int64-t)
 		     (blksize c-call:long)
-		     (blocks off-t))
+		     (blocks int64-t))
 	  (let ((result
 		  (alien-funcall
 		   (extern-alien ,c-func-name
 				 (function int
 					   ,first-arg-type
-					   (* dev-t)
-					   (* ino-t)
-					   (* mode-t)
-					   (* nlink-t)
-					   (* uid-t)
-					   (* gid-t)
-					   (* dev-t)
-					   (* off-t)
-					   (* time-t)
-					   (* time-t)
-					   (* time-t)
+					   (* u-int64-t)
+					   (* u-int64-t)
+					   (* c-call:unsigned-int)
+					   (* u-int64-t)
+					   (* c-call:unsigned-int)
+					   (* c-call:unsigned-int)
+					   (* u-int64-t)
+					   (* int64-t)
+					   (* int64-t)
+					   (* int64-t)
+					   (* int64-t)
 					   (* c-call:long)
-					   (* off-t)))
+					   (* int64-t)))
 		   ,first-arg
 		   (addr dev)
 		   (addr ino)


=====================================
src/lisp/os-common.c
=====================================
@@ -597,11 +597,19 @@ os_sleep(double seconds)
     }
 }
 
+/*
+ * Interface to stat/fstat/lstat.
+ *
+ * The arg types are chosen such that they can hold the largest
+ * possible value that any OS would use for the particular slot in the
+ * stat structure.  That way we can just use one OS-independent
+ * function that works across all OSes.
+ */
 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)
+unix_stat(const char* path, u_int64_t *dev, u_int64_t *ino, unsigned int *mode, u_int64_t *nlink,
+          unsigned int *uid, unsigned int *gid, u_int64_t *rdev, int64_t *size,
+          int64_t *atime, int64_t *mtime, int64_t *ctime,
+          long *blksize, int64_t *blocks)
 {
     int rc;
     struct stat buf;
@@ -647,10 +655,10 @@ unix_stat(const char* path, dev_t *dev, ino_t *ino, mode_t *mode, nlink_t *nlink
 }
 
 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)
+unix_fstat(int fd, u_int64_t *dev, u_int64_t *ino, unsigned int *mode, u_int64_t *nlink,
+           unsigned int *uid, unsigned int *gid, u_int64_t *rdev, int64_t *size,
+           int64_t *atime, int64_t *mtime, int64_t *ctime,
+           long *blksize, int64_t *blocks)
 {
     int rc;
     struct stat buf;
@@ -675,10 +683,10 @@ unix_fstat(int fd, dev_t *dev, ino_t *ino, mode_t *mode, nlink_t *nlink,
 }
 
 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)
+unix_lstat(const char* path, u_int64_t *dev, u_int64_t *ino, unsigned int *mode, u_int64_t *nlink,
+           unsigned int *uid, unsigned int *gid, u_int64_t *rdev, int64_t *size,
+           int64_t *atime, int64_t *mtime, int64_t *ctime,
+           long *blksize, int64_t *blocks)
 {
     int rc;
     struct stat buf;



View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/commit/b726c5a416dd05fc6fbd8486db8ab33bd18c2a71

-- 
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/commit/b726c5a416dd05fc6fbd8486db8ab33bd18c2a71
You're receiving this email because of your account on gitlab.common-lisp.net.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cmucl-cvs/attachments/20220804/8717cd80/attachment-0001.html>


More information about the cmucl-cvs mailing list