Hi hackers, Here are two small patches. The first just removes a helper procedure that I think is no longer necessary, `##sys#platform-fixup-pathname`, which only affects MinGW and doesn't seem to be needed anymore (see the commit message for details about that). The second patch fixes a TOCTTOU problem with `delete-file*`, brings its return value in line with the documentation, and also speeds it up by about 2x judging by my testing.
Let me know if you have any questions about these. Happy Friday, Evan
>From 83948be1f7014c81d8064250e2ba467f3b13ed3d Mon Sep 17 00:00:00 2001 From: Evan Hanson <[email protected]> Date: Fri, 15 Sep 2017 18:16:45 +1200 Subject: [PATCH 1/2] Drop `##sys#platform-fixup-pathname` This was used in a couple of places to trim a single trailing slash from pathnames on MinGW, but it wasn't used consistently and it isn't necessary to access paths with *one* trailing slash on modern Windows distributions. Since accessing "/foo/" will work fine without it, and since it doesn't solve the problem of trailing slashes in general (i.e. accessing "/foo///" will fail even with it), we simply remove it. --- file.scm | 5 ++--- library.scm | 24 ++---------------------- posix-common.scm | 5 +---- 3 files changed, 5 insertions(+), 29 deletions(-) diff --git a/file.scm b/file.scm index 0f8f2ea3..31f122b5 100644 --- a/file.scm +++ b/file.scm @@ -115,9 +115,8 @@ EOF ;;; Like `delete-file', but does nothing if the file doesn't exist: -(define delete-file* - (lambda (file) - (and (file-exists? file) (delete-file file)))) +(define (delete-file* filename) + (and (file-exists? filename) (delete-file filename))) (define (rename-file old new) (##sys#check-string old 'rename-file) diff --git a/library.scm b/library.scm index d75635a9..584e539c 100644 --- a/library.scm +++ b/library.scm @@ -2749,20 +2749,6 @@ EOF (define (##sys#port-data port) (##sys#slot port 9)) (define (##sys#set-port-data! port data) (##sys#setslot port 9 data)) -(define ##sys#platform-fixup-pathname - (let* ([bp (string->symbol ((##core#primitive "C_build_platform")))] - [fixsuffix (eq? bp 'mingw32)]) - (lambda (name) - (if fixsuffix - (let ([end (fx- (##sys#size name) 1)]) - (if (fx>= end 0) - (let ([c (##core#inline "C_subchar" name end)]) - (if (or (eq? c #\\) (eq? c #\/)) - (##sys#substring name 0 end) - name) ) - name) ) - name) ) ) ) - (define open-input-file) (define open-output-file) (define close-input-port) @@ -2858,17 +2844,11 @@ EOF (define (file-exists? name) (##sys#check-string name 'file-exists?) - (and (##sys#file-exists? - (##sys#platform-fixup-pathname name) - #f #f 'file-exists?) - name) ) + (and (##sys#file-exists? name #f #f 'file-exists?) name)) (define (directory-exists? name) (##sys#check-string name 'directory-exists?) - (and (##sys#file-exists? - (##sys#platform-fixup-pathname name) - #f #t 'directory-exists?) - name) ) + (and (##sys#file-exists? name #f #t 'directory-exists?) name)) (define (##sys#flush-output port) ((##sys#slot (##sys#slot port 2) 5) port) ; flush-output diff --git a/posix-common.scm b/posix-common.scm index b0a8b5be..ca2a8ee0 100644 --- a/posix-common.scm +++ b/posix-common.scm @@ -251,10 +251,7 @@ EOF (let ((r (cond ((fixnum? file) (##core#inline "C_fstat" file)) ((port? file) (##core#inline "C_fstat" (port->fileno file))) ((string? file) - (let ((path (##sys#make-c-string - (##sys#platform-fixup-pathname - file) - loc))) + (let ((path (##sys#make-c-string file loc))) (if link (##core#inline "C_lstat" path) (##core#inline "C_stat" path) ) ) ) -- 2.11.0
>From ecd163fec7b3fed0bbc5f7d4f8b3ab354175e34e Mon Sep 17 00:00:00 2001 From: Evan Hanson <[email protected]> Date: Fri, 15 Sep 2017 18:21:22 +1200 Subject: [PATCH 2/2] Fix TOCTTOU and speed up `delete-file*` In addition to checking for the file's existence, we also ignore errors from rename(3) in case the file is created after the existence test but before the deletion attempt. Additionally, make the procedure always return a boolean indicating whether the file was deleted (as it is currently documented to do) and update its types.db entry accordingly. --- file.scm | 4 +++- types.db | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/file.scm b/file.scm index 31f122b5..3c2b4e61 100644 --- a/file.scm +++ b/file.scm @@ -116,7 +116,9 @@ EOF ;;; Like `delete-file', but does nothing if the file doesn't exist: (define (delete-file* filename) - (and (file-exists? filename) (delete-file filename))) + (##sys#check-string filename 'delete-file*) + (and (##sys#file-exists? filename #f #f 'delete-file*) + (eq? 0 (##core#inline "C_delete_file" (##sys#make-c-string filename 'delete-file*))))) (define (rename-file old new) (##sys#check-string old 'rename-file) diff --git a/types.db b/types.db index 717b60fd..8f8b45c9 100644 --- a/types.db +++ b/types.db @@ -1561,7 +1561,7 @@ (chicken.file#create-temporary-file (#(procedure #:clean #:enforce) chicken.file#create-temporary-file (#!optional string) string)) (chicken.file#delete-directory (#(procedure #:clean #:enforce) chicken.file#delete-directory (string #!optional *) string)) (chicken.file#delete-file (#(procedure #:clean #:enforce) chicken.file#delete-file (string) string)) -(chicken.file#delete-file* (#(procedure #:clean #:enforce) chicken.file#delete-file* (string) *)) +(chicken.file#delete-file* (#(procedure #:clean #:enforce) chicken.file#delete-file* (string) boolean)) (chicken.file#file-copy (#(procedure #:clean #:enforce) chicken.file#file-copy (string string #!optional * fixnum) fixnum)) (chicken.file#file-move (#(procedure #:clean #:enforce) chicken.file#file-move (string string #!optional * fixnum) fixnum)) (chicken.file#find-files (#(procedure #:enforce) chicken.file#find-files (string #!rest) list)) -- 2.11.0
_______________________________________________ Chicken-hackers mailing list [email protected] https://lists.nongnu.org/mailman/listinfo/chicken-hackers
