From aec6d6f5c88955d6e967c7a64d7a63fb0c413e2a Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 19 Mar 2008 23:29:19 -0500 Subject: [PATCH] Replace (stat) with (exists?) --- core/bootstrap/primitives.factor | 5 ++- core/io/files/files-docs.factor | 14 +------ core/io/files/files.factor | 11 ++---- vm/io.h | 2 +- vm/os-unix.c | 18 +-------- vm/os-windows.c | 65 +++++++++++--------------------- vm/primitives.c | 2 +- 7 files changed, 36 insertions(+), 81 deletions(-) diff --git a/core/bootstrap/primitives.factor b/core/bootstrap/primitives.factor index e407bfd143..354ea672eb 100755 --- a/core/bootstrap/primitives.factor +++ b/core/bootstrap/primitives.factor @@ -91,8 +91,9 @@ call } [ create-vocab drop ] each H{ } clone source-files set -H{ } clone class } " and " { $link } ", to prepare a pathname before passing it to underlying code." } ; +{ $description "Called by words such as " { $link } " and " { $link } " to prepare a pathname before passing it to underlying code." } ; HELP: ( str -- pathname ) { $values { "str" "a pathname string" } { "pathname" pathname } } diff --git a/core/io/files/files.factor b/core/io/files/files.factor index 0d00197415..3de7559303 100755 --- a/core/io/files/files.factor +++ b/core/io/files/files.factor @@ -86,14 +86,11 @@ SYMBOL: +socket+ SYMBOL: +unknown+ ! File metadata -: stat ( path -- directory? permissions length modified ) - normalize-pathname (stat) ; +: exists? ( path -- ? ) + normalize-pathname (exists?) ; -: file-modified ( path -- n ) stat >r 3drop r> ; - -: exists? ( path -- ? ) file-modified >boolean ; - -: directory? ( path -- ? ) file-info file-info-type +directory+ = ; +: directory? ( path -- ? ) + file-info file-info-type +directory+ = ; ! Current working directory HOOK: cd io-backend ( path -- ) diff --git a/vm/io.h b/vm/io.h index a19da3887c..6291db50ee 100755 --- a/vm/io.h +++ b/vm/io.h @@ -12,5 +12,5 @@ DECLARE_PRIMITIVE(fclose); /* Platform specific primitives */ DECLARE_PRIMITIVE(open_file); -DECLARE_PRIMITIVE(stat); +DECLARE_PRIMITIVE(existsp); DECLARE_PRIMITIVE(read_dir); diff --git a/vm/os-unix.c b/vm/os-unix.c index 37dceb0d37..29d53487a3 100755 --- a/vm/os-unix.c +++ b/vm/os-unix.c @@ -41,24 +41,10 @@ void ffi_dlclose(F_DLL *dll) dll->dll = NULL; } -DEFINE_PRIMITIVE(stat) +DEFINE_PRIMITIVE(existsp) { struct stat sb; - - if(stat(unbox_char_string(),&sb) < 0) - { - dpush(F); - dpush(F); - dpush(F); - dpush(F); - } - else - { - box_boolean(S_ISDIR(sb.st_mode)); - box_signed_4(sb.st_mode & ~S_IFMT); - box_unsigned_8(sb.st_size); - box_unsigned_8(sb.st_mtime); - } + box_boolean(stat(unbox_char_string(),&sb) < 0); } /* Allocates memory */ diff --git a/vm/os-windows.c b/vm/os-windows.c index f9b80ea32a..1be41f8b57 100755 --- a/vm/os-windows.c +++ b/vm/os-windows.c @@ -87,14 +87,6 @@ const F_CHAR *vm_executable_path(void) return safe_strdup(full_path); } -void stat_not_found(void) -{ - dpush(F); - dpush(F); - dpush(F); - dpush(F); -} - void find_file_stat(F_CHAR *path) { // FindFirstFile is the only call that can stat c:\pagefile.sys @@ -102,56 +94,45 @@ void find_file_stat(F_CHAR *path) HANDLE h; if(INVALID_HANDLE_VALUE == (h = FindFirstFile(path, &st))) - stat_not_found(); + dpush(F); else { - box_boolean(st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); - dpush(tag_fixnum(0)); - box_unsigned_8( - (u64)st.nFileSizeLow | (u64)st.nFileSizeHigh << 32); - - u64 lo = st.ftLastWriteTime.dwLowDateTime; - u64 hi = st.ftLastWriteTime.dwHighDateTime; - u64 modTime = (hi << 32) + lo; - - box_unsigned_8((modTime - EPOCH_OFFSET) / 10000000); FindClose(h); + dpush(T); } } -DEFINE_PRIMITIVE(stat) +DEFINE_PRIMITIVE(existsp) { - HANDLE h; BY_HANDLE_FILE_INFORMATION bhfi; F_CHAR *path = unbox_u16_string(); //wprintf(L"path = %s\n", path); - h = CreateFileW(path, - GENERIC_READ, - FILE_SHARE_READ, - NULL, - OPEN_EXISTING, - FILE_FLAG_BACKUP_SEMANTICS, - NULL); + HANDLE h = CreateFileW(path, + GENERIC_READ, + FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS, + NULL); + if(h == INVALID_HANDLE_VALUE) { - find_file_stat(path); + // FindFirstFile is the only call that can stat c:\pagefile.sys + WIN32_FIND_DATA st; + HANDLE h; + + if(INVALID_HANDLE_VALUE == (h = FindFirstFile(path, &st))) + dpush(F); + else + { + FindClose(h); + dpush(T); + } return; } - if(!GetFileInformationByHandle(h, &bhfi)) - stat_not_found(); - else { - box_boolean(bhfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); - dpush(tag_fixnum(0)); - box_unsigned_8( - (u64)bhfi.nFileSizeLow | (u64)bhfi.nFileSizeHigh << 32); - u64 lo = bhfi.ftLastWriteTime.dwLowDateTime; - u64 hi = bhfi.ftLastWriteTime.dwHighDateTime; - u64 modTime = (hi << 32) + lo; - - box_unsigned_8((modTime - EPOCH_OFFSET) / 10000000); - } + box_boolean(GetFileInformationByHandle(h, &bhfi)); CloseHandle(h); } diff --git a/vm/primitives.c b/vm/primitives.c index d1d956dca0..ce26c20f63 100755 --- a/vm/primitives.c +++ b/vm/primitives.c @@ -88,7 +88,7 @@ void *primitives[] = { primitive_eq, primitive_getenv, primitive_setenv, - primitive_stat, + primitive_existsp, primitive_read_dir, primitive_data_gc, primitive_code_gc,