[macruby-changes] [4081] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Wed May 12 17:44:29 PDT 2010


Revision: 4081
          http://trac.macosforge.org/projects/ruby/changeset/4081
Author:   martinlagardette at apple.com
Date:     2010-05-12 17:44:27 -0700 (Wed, 12 May 2010)
Log Message:
-----------
Improve core/file pass rate

 - Implement `#absolute_path` and `#size` (the instance method)
 - Various fixes here and there

Modified Paths:
--------------
    MacRuby/trunk/file.c
    MacRuby/trunk/include/ruby/intern.h
    MacRuby/trunk/objc.m
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/new_tags.txt

Removed Paths:
-------------
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/absolute_path_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/chmod_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/delete_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/join_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/lchmod_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/lstat_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/readlink_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/size_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/stat/
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/stat_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/symlink_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/to_path_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/unlink_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/utime_tags.txt
    MacRuby/trunk/spec/frozen/tags/macruby/core/file/world_writable_tags.txt

Modified: MacRuby/trunk/file.c
===================================================================
--- MacRuby/trunk/file.c	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/file.c	2010-05-13 00:44:27 UTC (rev 4081)
@@ -141,14 +141,9 @@
     volatile VALUE path;
 
     rb_secure(4);
-    for (i=0; i<RARRAY_LEN(vargs); i++) {
-		path = rb_check_string_type(RARRAY_AT(vargs, i));
-		if (NIL_P(path))
-		{
-			// should we check for to_path too? i find it to be a hideous idiom.
-			rb_raise(rb_eTypeError, "paths must be strings or coerceable into strings");
-		}
-		(*func)(StringValueCStr(path), arg);
+    for (i = 0; i < RARRAY_LEN(vargs); i++) {
+	path = rb_get_path(RARRAY_AT(vargs, i));
+	(*func)(StringValueCStr(path), arg);
     }
 
     return RARRAY_LEN(vargs);
@@ -1268,14 +1263,14 @@
 static VALUE
 rb_file_world_writable_p(VALUE obj, SEL sel, VALUE fname)
 {
-#ifdef S_IWOTH
     struct stat st;
 
-    if (rb_stat(fname, &st) < 0) return Qfalse;
+    if (rb_stat(fname, &st) < 0) {
+	return Qnil;
+    }
     if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
-	return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
+	return UINT2NUM(st.st_mode & (S_IRUGO | S_IWUGO | S_IXUGO));
     }
-#endif
     return Qnil;
 }
 
@@ -1533,6 +1528,17 @@
 }
 
 static VALUE
+rb_file_size(VALUE obj, SEL sel)
+{
+    struct stat st;
+    struct rb_io_t *io = ExtractIOStruct(obj);
+    if (fstat(io->fd, &st) == -1) {
+	rb_sys_fail(RSTRING_PTR(io->path));
+    }
+    return OFFT2NUM(st.st_size);
+}
+
+static VALUE
 rb_file_ftype(const struct stat *st)
 {
     const char *t;
@@ -1759,11 +1765,10 @@
 
     rb_secure(2);
     rb_scan_args(argc, argv, "1*", &vmode, &rest);
-	vmode = rb_check_to_integer(vmode, "to_int");
-	if (NIL_P(vmode))
-	{
-		rb_raise(rb_eTypeError, "chmod() takes a numeric argument");
-	}
+    vmode = rb_check_to_integer(vmode, "to_int");
+    if (NIL_P(vmode)) {
+	rb_raise(rb_eTypeError, "chmod() takes a numeric argument");
+    }
     mode = NUM2INT(vmode);
 
     n = apply2files(chmod_internal, rest, &mode);
@@ -2376,6 +2381,32 @@
     return rb_file_expand_path(fname, dname);
 }
 
+/*
+ *  call-seq:
+ *     File.absolute_path(file_name [, dir_string] ) -> abs_file_name
+ *  
+ *  Converts a pathname to an absolute pathname. Relative paths are
+ *  referenced from the current working directory of the process unless
+ *  <i>dir_string</i> is given, in which case it will be used as the
+ *  starting point. If the given pathname starts with a ``<code>~</code>''
+ *  it is NOT expanded, it is treated as a normal directory name.
+ *     
+ *     File.absolute_path("~oracle/bin")       #=> "<relative_path>/~oracle/bin"
+ */
+
+VALUE
+rb_file_s_absolute_path(VALUE rcv, SEL sel, int argc, VALUE *argv)
+{
+    VALUE fname, dname;
+
+    if (argc == 1) {
+	return rb_file_absolute_path(argv[0], Qnil);
+    }
+    rb_scan_args(argc, argv, "11", &fname, &dname);
+
+    return rb_file_absolute_path(fname, dname);
+}
+
 static int
 rmext(const char *p, int l1, const char *e)
 {
@@ -2589,6 +2620,9 @@
 		break;
 
 	    case T_ARRAY:
+		if (ary == tmp) {
+		    rb_raise(rb_eArgError, "recursive array");
+		}
 		tmp = rb_file_join(tmp, sep);
 		break;
 
@@ -2596,14 +2630,19 @@
 		FilePathStringValue(tmp);
 	}
 
+#define uchar_at_is_sep(str, pos) (rb_str_get_uchar(str, pos) == sep_char)
 	if (i > 0 && !NIL_P(sep)) {
 	    const long res_len = rb_str_chars_len(res);
 	    const long tmp_len = rb_str_chars_len(tmp);
 
-	    if (res_len > 0
-		    && rb_str_get_uchar(res, res_len - 1) == sep_char) {
-		if (tmp_len > 0 && rb_str_get_uchar(tmp, 0) == sep_char) {
-		    rb_str_delete(res, res_len - 1, 1);
+	    if (res_len > 0 && uchar_at_is_sep(res, res_len - 1)) {
+		if (tmp_len > 0 && uchar_at_is_sep(tmp, 0)) {
+		    long nb_sep_char = 1;
+		    while (nb_sep_char < res_len
+			&& uchar_at_is_sep(res, res_len - nb_sep_char - 1)) {
+			nb_sep_char++;
+		    }
+		    rb_str_delete(res, res_len - nb_sep_char, nb_sep_char);
 		}
 	    }
 	    else if (tmp_len == 0
@@ -2612,6 +2651,7 @@
 	    } 
 	}
 	rb_str_concat(res, tmp);
+#undef uchar_at_is_sep
     }
     return res;
 }
@@ -3968,6 +4008,7 @@
     rb_objc_define_method(rb_ccFile, "umask", rb_file_s_umask, -1);
     rb_objc_define_method(rb_ccFile, "truncate", rb_file_s_truncate, 2);
     rb_objc_define_method(rb_ccFile, "expand_path", rb_file_s_expand_path, -1);
+    rb_objc_define_method(rb_ccFile, "absolute_path", rb_file_s_absolute_path, -1);
     rb_objc_define_method(rb_ccFile, "basename", rb_file_s_basename, -1);
     rb_objc_define_method(rb_ccFile, "dirname", rb_file_s_dirname, 1);
     rb_objc_define_method(rb_ccFile, "extname", rb_file_s_extname, 1);
@@ -3988,6 +4029,7 @@
     rb_objc_define_method(rb_cFile, "atime", rb_file_atime, 0);
     rb_objc_define_method(rb_cFile, "mtime", rb_file_mtime, 0);
     rb_objc_define_method(rb_cFile, "ctime", rb_file_ctime, 0);
+    rb_objc_define_method(rb_cFile, "size", rb_file_size, 0);
 
     rb_objc_define_method(rb_cFile, "chmod", rb_file_chmod, 1);
     rb_objc_define_method(rb_cFile, "chown", rb_file_chown, 2);

Modified: MacRuby/trunk/include/ruby/intern.h
===================================================================
--- MacRuby/trunk/include/ruby/intern.h	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/include/ruby/intern.h	2010-05-13 00:44:27 UTC (rev 4081)
@@ -317,6 +317,7 @@
 VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int),VALUE,VALUE);
 /* file.c */
 VALUE rb_file_expand_path(VALUE, VALUE);
+VALUE rb_file_absolute_path(VALUE, VALUE);
 void rb_file_const(const char*, VALUE);
 int rb_find_file_ext(VALUE*, const char* const*);
 VALUE rb_find_file(VALUE);

Modified: MacRuby/trunk/objc.m
===================================================================
--- MacRuby/trunk/objc.m	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/objc.m	2010-05-13 00:44:27 UTC (rev 4081)
@@ -224,12 +224,21 @@
     return rb_str_new2([home_dir fileSystemRepresentation]);
 }
 
-VALUE
-rb_file_expand_path(VALUE fname, VALUE dname)
+static BOOL
+is_absolute_path(NSString *path, int expand_tilde)
 {
+    if (!expand_tilde && [path isAbsolutePath] && [path hasPrefix:@"~"]) {
+	return NO;
+    }
+    return [path isAbsolutePath];    
+}
+
+static VALUE
+file_expand_path(VALUE fname, VALUE dname, int absolute)
+{
     NSString *res = (NSString *)FilePathValue(fname);
 
-    if ([res isAbsolutePath]) {
+    if (is_absolute_path(res, !absolute)) {
 	NSString *tmp = [res stringByResolvingSymlinksInPath];
 	// Make sure we don't have an invalid user path.
 	if ([res hasPrefix:@"~"] && [tmp isEqualTo:res]) {
@@ -244,8 +253,8 @@
 	    ? (NSString *)FilePathValue(dname)
 	    : [[NSFileManager defaultManager] currentDirectoryPath];
 
-	if (![dir isAbsolutePath]) {
-	    dir = (NSString *)rb_file_expand_path((VALUE)dir, Qnil);
+	if (!is_absolute_path(dir, !absolute)) {
+	    dir = (NSString *)file_expand_path((VALUE)dir, Qnil, 0);
 	}
 
 	// stringByStandardizingPath does not expand "/." to "/".
@@ -261,6 +270,18 @@
     return rb_str_new2([res fileSystemRepresentation]);
 }
 
+VALUE
+rb_file_expand_path(VALUE fname, VALUE dname)
+{
+    return file_expand_path(fname, dname, 0);
+}
+
+VALUE
+rb_file_absolute_path(VALUE fname, VALUE dname)
+{
+    return file_expand_path(fname, dname, 1);
+}
+
 static VALUE
 rb_objc_load_bs(VALUE recv, SEL sel, VALUE path)
 {

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/file/absolute_path_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/absolute_path_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/absolute_path_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1,5 +0,0 @@
-fails:File.absolute_path returns the argument if it's an absolute pathname
-fails:File.absolute_path resolves paths relative to the current working directory
-fails:File.absolute_path doesn't expand '~'
-fails:File.absolute_path accepts a second argument of a directory from which to resolve the path
-fails:File.absolute_path calls #to_path on its argument

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/file/chmod_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/chmod_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/chmod_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1 +0,0 @@
-fails:File.chmod accepts an object that has a #to_path method

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/file/delete_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/delete_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/delete_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1 +0,0 @@
-fails:File.delete accepts an object that has a #to_path method

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/file/join_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/join_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/join_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1,2 +0,0 @@
-critical:File.join raises an ArgumentError if passed a recursive array
-fails:File.join gives priority to existing separators in the rightmost argument

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/file/lchmod_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/lchmod_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/lchmod_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1 +0,0 @@
-fails:File.lchmod changes the file mode of the link and not of the file

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/file/lstat_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/lstat_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/lstat_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1 +0,0 @@
-fails:File.lstat returns a File::Stat object with symlink properties for a symlink

Modified: MacRuby/trunk/spec/frozen/tags/macruby/core/file/new_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/new_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/new_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1,7 +1,3 @@
-fails:File.new return a new File with modus fd 
-fails:File.new raises an Errno::EINVAL error with File::APPEND
-fails:File.new raises an Errno::EINVAL error with File::RDONLY|File::APPEND
-fails:File.new expected errors 
 fails:File.new can't alter mode or permissions when opening a file
 fails:File.new returns a new File with modus fd 
 fails:File.new raises an Errno::EBADF if the first parameter is an invalid file descriptor

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/file/readlink_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/readlink_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/readlink_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1,2 +0,0 @@
-fails:File.readlink raises an Errno::ENOENT if called with an invalid argument
-fails:File.readlink return the name of the file referenced by the given link

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/file/size_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/size_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/size_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1,7 +0,0 @@
-fails:File#size is an instance method
-fails:File#size returns the file's size as a Fixnum
-fails:File#size returns the file's size in bytes
-fails:File#size returns the cached size of the file if subsequently deleted
-fails:File#size returns the file's current size even if modified
-fails:File#size returns 0 for an empty file
-fails:File#size follows symlinks if necessary

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/file/stat_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/stat_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/stat_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1,2 +0,0 @@
-fails:File.stat returns information for a file that has been deleted but is still open
-fails:File.stat returns a File::Stat object with file properties for a symlink

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/file/symlink_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/symlink_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/symlink_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1,6 +0,0 @@
-fails:File.symlink create a symbolic link
-fails:File.symlink accepts args that have #to_path methods
-fails:File.symlink raises an Errno::EEXIST if the target already exists
-fails:File.symlink? returns true if the file is a link
-fails:File.symlink? accepts an object that has a #to_path method
-fails:File.symlink create a symlink between a source and target file

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/file/to_path_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/to_path_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/to_path_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1,6 +0,0 @@
-fails:File#to_path preserves the encoding of the path
-fails:File#to_path does not preserve the encoding of the path
-fails:File#to_path returns a String
-fails:File#to_path does not normalise the path it returns
-fails:File#to_path does not canonicalize the path it returns
-fails:File#to_path does not absolute-ise the path it returns

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/file/unlink_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/unlink_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/unlink_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1 +0,0 @@
-fails:File.unlink accepts an object that has a #to_path method

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/file/utime_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/utime_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/utime_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1 +0,0 @@
-fails:File.utime accepts an object that has a #to_path method

Deleted: MacRuby/trunk/spec/frozen/tags/macruby/core/file/world_writable_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/file/world_writable_tags.txt	2010-05-12 22:23:08 UTC (rev 4080)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/file/world_writable_tags.txt	2010-05-13 00:44:27 UTC (rev 4081)
@@ -1 +0,0 @@
-fails:File.world_writable? returns nil if the file does not exist
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100512/bd61bb0c/attachment-0001.html>


More information about the macruby-changes mailing list