From 4ef75249df459cd06003f770a707f8d7cd9b3a9a Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 22 Nov 2005 16:26:39 +0000 Subject: [PATCH] * file.c (test_identical): test if two files are identical. * file.c (rb_f_test): support DOSISH systems where st_ino is not reliable. fixed: [ruby-core:06672] * win32.h, win32.c (rb_w32_osid): check the running platform. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9590 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 9 +++++ file.c | 100 +++++++++++++++++++++++++++++++++++++++++++++----- win32/win32.c | 28 +++----------- win32/win32.h | 15 ++++++++ 4 files changed, 121 insertions(+), 31 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3bcf5d179c..19182ef5ff 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Wed Nov 23 01:22:57 2005 Nobuyoshi Nakada + + * file.c (test_identical): test if two files are identical. + + * file.c (rb_f_test): support DOSISH systems where st_ino is not + reliable. fixed: [ruby-core:06672] + + * win32.h, win32.c (rb_w32_osid): check the running platform. + Tue Nov 22 23:52:06 2005 Nobuyoshi Nakada * lib/optparse.rb: match incomplete (in current enconding) multibyte diff --git a/file.c b/file.c index 873087c590..674121d022 100644 --- a/file.c +++ b/file.c @@ -636,6 +636,32 @@ rb_stat(VALUE file, struct stat *st) return stat(StringValueCStr(file), st); } +#ifdef _WIN32 +static HANDLE +w32_io_info(VALUE *file, BY_HANDLE_FILE_INFORMATION *st) +{ + VALUE tmp; + HANDLE f, ret = 0; + + tmp = rb_check_convert_type(*file, T_FILE, "IO", "to_io"); + if (!NIL_P(tmp)) { + OpenFile *fptr; + + GetOpenFile(tmp, fptr); + f = (HANDLE)rb_w32_get_osfhandle(fptr->fd); + } + else { + FilePathValue(*file); + f = CreateFile(StringValueCStr(*file), 0, 0, NULL, + OPEN_EXISTING, 0, NULL); + if (f == INVALID_HANDLE_VALUE) return f; + ret = f; + } + if (GetFileInformationByHandle(f, st)) return ret; + return INVALID_HANDLE_VALUE; +} +#endif + /* * call-seq: * File.stat(file_name) => stat @@ -1315,7 +1341,7 @@ check3rdbyte(VALUE fname, int mode) * call-seq: * File.setuid?(file_name) => true or false * - * Returns true if the named file is a has the setuid bit set. + * Returns true if the named file has the setuid bit set. */ static VALUE @@ -1332,7 +1358,7 @@ test_suid(VALUE obj, VALUE fname) * call-seq: * File.setgid?(file_name) => true or false * - * Returns true if the named file is a has the setgid bit set. + * Returns true if the named file has the setgid bit set. */ static VALUE @@ -1349,7 +1375,7 @@ test_sgid(VALUE obj, VALUE fname) * call-seq: * File.sticky?(file_name) => true or false * - * Returns true if the named file is a has the sticky bit set. + * Returns true if the named file has the sticky bit set. */ static VALUE @@ -1362,6 +1388,60 @@ test_sticky(VALUE obj, VALUE fname) #endif } +/* + * call-seq: + * File.identical?(file_1, file_2) => true or false + * + * Returns true if the named files are identical. + */ + +static VALUE +test_identical(VALUE obj, VALUE fname1, VALUE fname2) +{ +#ifndef DOSISH + struct stat st1, st2; + + if (rb_stat(fname1, &st1) < 0) return Qfalse; + if (rb_stat(fname2, &st2) < 0) return Qfalse; + if (st1.st_dev != st2.st_dev) return Qfalse; + if (st1.st_ino != st2.st_ino) return Qfalse; +#else +#ifdef _WIN32 + BY_HANDLE_FILE_INFORMATION st1, st2; + HANDLE f1 = 0, f2 = 0; +#endif + + rb_secure(2); +#ifdef _WIN32 + f1 = w32_io_info(&fname1, &st1); + if (f1 == INVALID_HANDLE_VALUE) return Qfalse; + f2 = w32_io_info(&fname2, &st2); + if (f1) CloseHandle(f1); + if (f2 == INVALID_HANDLE_VALUE) return Qfalse; + if (f2) CloseHandle(f2); + + if (st1.dwVolumeSerialNumber == st2.dwVolumeSerialNumber && + st1.nFileIndexHigh == st2.nFileIndexHigh && + st1.nFileIndexLow == st2.nFileIndexLow) + return Qtrue; + if (!f1 || !f2) return Qfalse; + if (rb_w32_iswin95()) return Qfalse; +#else + FilePathValue(fname1); + fname1 = rb_str_new4(fname1); + FilePathValue(fname2); + if (access(RSTRING(fname1)->ptr, 0)) return Qfalse; + if (access(RSTRING(fname2)->ptr, 0)) return Qfalse; +#endif + fname1 = rb_file_expand_path(fname1, Qnil); + fname2 = rb_file_expand_path(fname2, Qnil); + if (RSTRING(fname1)->len != RSTRING(fname2)->len) return Qfalse; + if (rb_memcicmp(RSTRING(fname1)->ptr, RSTRING(fname2)->ptr, RSTRING(fname1)->len)) + return Qfalse; +#endif + return Qtrue; +} + /* * call-seq: * File.size(file_name) => integer @@ -3221,7 +3301,12 @@ rb_f_test(int argc, VALUE *argv) } } - if (strchr("-=<>", cmd)) { + if (cmd == '-') { + CHECK(2); + return test_identical(0, argv[1], argv[2]); + } + + if (strchr("=<>", cmd)) { struct stat st1, st2; CHECK(2); @@ -3229,11 +3314,6 @@ rb_f_test(int argc, VALUE *argv) if (rb_stat(argv[2], &st2) < 0) return Qfalse; switch (cmd) { - case '-': - if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) - return Qtrue; - return Qfalse; - case '=': if (st1.st_mtime == st2.st_mtime) return Qtrue; return Qfalse; @@ -4200,6 +4280,8 @@ Init_File(void) define_filetest_function("setgid?", test_sgid, 1); define_filetest_function("sticky?", test_sticky, 1); + define_filetest_function("identical?", test_identical, 2); + rb_define_singleton_method(rb_cFile, "stat", rb_file_s_stat, 1); rb_define_singleton_method(rb_cFile, "lstat", rb_file_s_lstat, 1); rb_define_singleton_method(rb_cFile, "ftype", rb_file_s_ftype, 1); diff --git a/win32/win32.c b/win32/win32.c index 07130a46a9..d9ab68a902 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -44,12 +44,6 @@ #undef close #undef setsockopt -#ifdef _M_IX86 -# define WIN95 1 -#else -# undef WIN95 -#endif - #if defined __BORLANDC__ || defined _WIN32_WCE # define _filbuf _fgetc # define _flsbuf _fputc @@ -204,10 +198,10 @@ map_errno(DWORD winerr) static char *NTLoginName; #ifdef WIN95 -DWORD Win32System = (DWORD)-1; +static DWORD Win32System = (DWORD)-1; -static DWORD -IdOS(void) +DWORD +rb_w32_osid(void) { static OSVERSIONINFO osver; @@ -219,21 +213,11 @@ IdOS(void) } return (Win32System); } - -static int -IsWin95(void) { - return (IdOS() == VER_PLATFORM_WIN32_WINDOWS); -} - -static int -IsWinNT(void) { - return (IdOS() == VER_PLATFORM_WIN32_NT); -} -#else -# define IsWinNT() TRUE -# define IsWin95() FALSE #endif +#define IsWinNT() rb_w32_iswinnt() +#define IsWin95() rb_w32_iswin95() + /* main thread constants */ static struct { HANDLE handle; diff --git a/win32/win32.h b/win32/win32.h index f3efa17940..05e61885aa 100644 --- a/win32/win32.h +++ b/win32/win32.h @@ -68,6 +68,21 @@ extern "C++" { } #endif +#ifdef _M_IX86 +# define WIN95 1 +#else +# undef WIN95 +#endif + +#ifdef WIN95 +extern DWORD rb_w32_osid(void); +#define rb_w32_iswinnt() (rb_w32_osid() == VER_PLATFORM_WIN32_NT) +#define rb_w32_iswin95() (rb_w32_osid() == VER_PLATFORM_WIN32_WINDOWS) +#else +#define rb_w32_iswinnt() TRUE +#define rb_w32_iswin95() FALSE +#endif + #define WNOHANG -1 #undef getc