Raymond Toy pushed to branch issue-135-unix-namestring-dot at cmucl / cmucl

Commits:

5 changed files:

Changes:

  • .gitlab-ci.yml
    ... ... @@ -167,7 +167,8 @@ osx:ansi-test:
    167 167
       script:
    
    168 168
         - cd ansi-test
    
    169 169
         - make LISP="../dist/bin/lisp -batch -noinit -nositeinit"
    
    170
    -    - grep 'No unexpected \(successes\|failures\)' test.out 
    
    170
    +    # There should be no unexpected successes or failures; check these separately.
    
    171
    +    - grep -a 'No unexpected successes' test.out && grep -a 'No unexpected failures' test.out
    
    171 172
       
    
    172 173
     osx:benchmark:
    
    173 174
       stage: benchmark
    

  • src/code/filesys.lisp
    ... ... @@ -786,7 +786,6 @@
    786 786
       (let ((name (%pathname-name pathname))
    
    787 787
     	(type (%pathname-type pathname))
    
    788 788
     	(version (%pathname-version pathname)))
    
    789
    -    (format t "name type version = ~S ~S ~S~%" name type version)
    
    790 789
         (cond ((member name '(nil :unspecific))
    
    791 790
     	   (when (or (not verify-existence)
    
    792 791
     		     (unix:unix-file-kind directory))
    
    ... ... @@ -1081,13 +1080,21 @@ optionally keeping some of the most recent old versions."
    1081 1080
     		 :pathname file
    
    1082 1081
     		 :format-control (intl:gettext "~S doesn't exist.")
    
    1083 1082
     		 :format-arguments (list file)))
    
    1084
    -	(multiple-value-bind (winp dev ino mode nlink uid)
    
    1085
    -			     (unix:unix-stat name)
    
    1086
    -	  (declare (ignore dev ino mode nlink))
    
    1087
    -	  (when winp
    
    1088
    -            (let ((user-info (unix:unix-getpwuid uid)))
    
    1089
    -              (when user-info
    
    1090
    -                (unix:user-info-name user-info))))))))
    
    1083
    +	;; unix-namestring converts "." to "".  Convert it back to
    
    1084
    +	;; "." so we can stat the current directory.  (Perhaps
    
    1085
    +	;; that's a bug in unix-namestring?)
    
    1086
    +	(when (zerop (length name))
    
    1087
    +	  (setf name "."))
    
    1088
    +	(let (author)
    
    1089
    +	  (unwind-protect
    
    1090
    +	       (progn
    
    1091
    +		 (setf author (alien:alien-funcall
    
    1092
    +			       (alien:extern-alien "os_file_author"
    
    1093
    +						   (function (alien:* c-call:c-string) c-call:c-string))
    
    1094
    +			       (unix::%name->file name)))
    
    1095
    +		 (unless (alien:null-alien author)
    
    1096
    +		   (alien:cast author c-call:c-string)))
    
    1097
    +	    (alien:free-alien author))))))
    
    1091 1098
     
    
    1092 1099
     
    
    1093 1100
     ;;;; DIRECTORY.
    

  • src/lisp/os-common.c
    ... ... @@ -5,12 +5,16 @@
    5 5
     
    
    6 6
     */
    
    7 7
     
    
    8
    +#include <assert.h>
    
    8 9
     #include <errno.h>
    
    9 10
     #include <math.h>
    
    10 11
     #include <netdb.h>
    
    12
    +#include <pwd.h>
    
    11 13
     #include <stdio.h>
    
    14
    +#include <stdlib.h>
    
    12 15
     #include <string.h>
    
    13 16
     #include <sys/stat.h>
    
    17
    +#include <unistd.h>
    
    14 18
     #include <time.h>
    
    15 19
     
    
    16 20
     #include "os.h"
    
    ... ... @@ -715,3 +719,57 @@ os_lstat(const char* path, u_int64_t *dev, u_int64_t *ino, unsigned int *mode, u
    715 719
     
    
    716 720
         return rc;
    
    717 721
     }
    
    722
    +
    
    723
    +/*
    
    724
    + * Interface for file-author.  Given a pathname, returns a new string
    
    725
    + * holding the author of the file or NULL if some error occurred.  The
    
    726
    + * caller is responsible for freeing the memory used by the string.
    
    727
    + */
    
    728
    +char *
    
    729
    +os_file_author(const char *path)
    
    730
    +{
    
    731
    +    struct stat sb;
    
    732
    +    char initial[1024];
    
    733
    +    char *buffer, *obuffer;
    
    734
    +    size_t size;
    
    735
    +    struct passwd pwd;
    
    736
    +    struct passwd *ppwd;
    
    737
    +    char *result;
    
    738
    +
    
    739
    +    if (stat(path, &sb) != 0) {
    
    740
    +        return NULL;
    
    741
    +    }
    
    742
    +
    
    743
    +    result = NULL;
    
    744
    +    buffer = initial;
    
    745
    +    obuffer = NULL;
    
    746
    +    size = sizeof(initial) / sizeof(initial[0]);
    
    747
    +
    
    748
    +    /*
    
    749
    +     * Keep trying with larger buffers until a maximum is reached.  We
    
    750
    +     * assume (1 << 20) is large enough for any OS.
    
    751
    +     */
    
    752
    +    while (size <= (1 << 20)) {
    
    753
    +        switch (getpwuid_r(sb.st_uid, &pwd, buffer, size, &ppwd)) {
    
    754
    +          case 0:
    
    755
    +              /* Success, though we might not have a matching entry */
    
    756
    +              result = (ppwd == NULL) ? NULL : strdup(pwd.pw_name);
    
    757
    +              goto exit;
    
    758
    +          case ERANGE:
    
    759
    +              /* Buffer is too small, double its size and try again */
    
    760
    +              size *= 2;
    
    761
    +              obuffer = (buffer == initial) ? NULL : buffer;
    
    762
    +              if ((buffer = realloc(obuffer, size)) == NULL) {
    
    763
    +                  goto exit;
    
    764
    +              }
    
    765
    +              continue;
    
    766
    +          default:
    
    767
    +              /* All other errors */
    
    768
    +              goto exit;
    
    769
    +        }
    
    770
    +    }
    
    771
    +exit:
    
    772
    +    free(obuffer);
    
    773
    +    
    
    774
    +    return result;
    
    775
    +}

  • tests/issues.lisp
    ... ... @@ -670,8 +670,37 @@
    670 670
     		 (err (relerr value answer)))
    
    671 671
     	    (assert-true (<= err eps) base err eps)))))))
    
    672 672
     
    
    673
    +(define-test issue.130
    
    674
    +    (:tag :issues)
    
    675
    +  ;; Just verify that file-author works.  In particular "." should
    
    676
    +  ;; work and not return NIL.
    
    677
    +  (assert-true (file-author "."))
    
    678
    +  (assert-true (file-author "bin/build.sh"))
    
    679
    +  (assert-true (file-author "tests/안녕하십니까.txt")))
    
    680
    +
    
    673 681
     (define-test issue.135
    
    674 682
         (:tag :issues)
    
    675
    -  (assert-equalp "." (ext:unix-namestring "."))
    
    676
    -  (assert-equalp "./abc.txt" (ext:unix-namestring "abc.txt"))
    
    677
    -  (assert-equalp "./abc/def/foo.txt" (ext:unix-namestring "abc/def/foo.txt")))
    683
    +  (assert-equalp "./" (ext:unix-namestring "."))
    
    684
    +  (unwind-protect
    
    685
    +       (progn
    
    686
    +	 ;; Create a test file in the current directory.
    
    687
    +	 ;; unix-namestring requires files to exist to be able to
    
    688
    +	 ;; return the namestring.
    
    689
    +	 (with-open-file (f1 "foo.txt"
    
    690
    +			     :direction :output
    
    691
    +			     :if-exists :supersede)
    
    692
    +	   (print "foo" f1))
    
    693
    +	 ;; Check unix-namestring and verify that converting the
    
    694
    +	 ;; namestring to a pathname results in the same pathname
    
    695
    +	 ;; object as expected.
    
    696
    +	 (let ((foo (ext:unix-namestring "foo.txt")))
    
    697
    +	   (assert-equalp "foo.txt" foo)
    
    698
    +	   (assert-equalp (make-pathname :name "foo" :type "txt")
    
    699
    +			  (pathname foo)))
    
    700
    +	 (let ((bar (ext:unix-namestring "src/code/filesys.lisp")))
    
    701
    +	   (assert-equalp "./src/code/filesys.lisp" bar)
    
    702
    +	   (assert-equalp (make-pathname :directory '(:relative "src" "code")
    
    703
    +					 :name "filesys"
    
    704
    +					 :type "lisp")
    
    705
    +			  (pathname bar))))
    
    706
    +    (assert-true (delete-file "foo.txt"))))

  • tests/안녕하십니까.txt
    1
    +The file name of this file is "안녕하십니까.txt" ("Hello" in Korean.)
    
    2
    +
    
    3
    +