summaryrefslogtreecommitdiff
path: root/file.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-11-22 16:26:39 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-11-22 16:26:39 +0000
commit4ef75249df459cd06003f770a707f8d7cd9b3a9a (patch)
treea4603a252478b9eb84b443473e2f236a106652ed /file.c
parente8a83b54cb2321c3b6de9ea2f3df81064a1b18f7 (diff)
* 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
Diffstat (limited to 'file.c')
-rw-r--r--file.c100
1 files changed, 91 insertions, 9 deletions
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 <code>true</code> if the named file is a has the setuid bit set.
+ * Returns <code>true</code> 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 <code>true</code> if the named file is a has the setgid bit set.
+ * Returns <code>true</code> 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 <code>true</code> if the named file is a has the sticky bit set.
+ * Returns <code>true</code> if the named file has the sticky bit set.
*/
static VALUE
@@ -1364,6 +1390,60 @@ test_sticky(VALUE obj, VALUE fname)
/*
* call-seq:
+ * File.identical?(file_1, file_2) => true or false
+ *
+ * Returns <code>true</code> 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
*
* Returns the size of <code>file_name</code>.
@@ -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);