ruby-changes:7263
From: akr <ko1@a...>
Date: Sat, 23 Aug 2008 09:49:51 +0900 (JST)
Subject: [ruby-changes:7263] Ruby:r18782 (trunk): * include/ruby/io.h (rb_io_t): remove path field and add pathv field.
akr 2008-08-23 09:47:54 +0900 (Sat, 23 Aug 2008) New Revision: 18782 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=18782 Log: * include/ruby/io.h (rb_io_t): remove path field and add pathv field. (MakeOpenFile): initialize pathv as Qnil. * gc.c: mark pathv field in T_FILE. * io.c: follow the rb_io_t field change. * file.c: ditto. * ext/socket/socket.c: ditto. Modified files: trunk/ChangeLog trunk/ext/socket/socket.c trunk/file.c trunk/gc.c trunk/include/ruby/io.h trunk/io.c Index: include/ruby/io.h =================================================================== --- include/ruby/io.h (revision 18781) +++ include/ruby/io.h (revision 18782) @@ -33,7 +33,7 @@ int mode; /* mode flags */ rb_pid_t pid; /* child's pid (for pipes) */ int lineno; /* number of lines read */ - char *path; /* pathname for file */ + VALUE pathv; /* pathname for file */ void (*finalize)(struct rb_io_t*,int); /* finalize proc */ long refcnt; @@ -103,7 +103,7 @@ fp->mode = 0;\ fp->pid = 0;\ fp->lineno = 0;\ - fp->path = NULL;\ + fp->pathv = Qnil;\ fp->finalize = 0;\ fp->refcnt = 1;\ fp->wbuf = NULL;\ Index: ChangeLog =================================================================== --- ChangeLog (revision 18781) +++ ChangeLog (revision 18782) @@ -1,3 +1,16 @@ +Sat Aug 23 09:45:35 2008 Tanaka Akira <akr@f...> + + * include/ruby/io.h (rb_io_t): remove path field and add pathv field. + (MakeOpenFile): initialize pathv as Qnil. + + * gc.c: mark pathv field in T_FILE. + + * io.c: follow the rb_io_t field change. + + * file.c: ditto. + + * ext/socket/socket.c: ditto. + Sat Aug 23 01:42:22 2008 Tanaka Akira <akr@f...> * include/ruby/io.h (FMODE_TEXTMODE): defined. Index: io.c =================================================================== --- io.c (revision 18781) +++ io.c (revision 18782) @@ -202,17 +202,19 @@ #define shutdown(a,b) 0 #endif +#define rb_sys_fail_path(path) rb_sys_fail(NIL_P(path) ? 0 : RSTRING_PTR(path)) + #if defined(_WIN32) #define is_socket(fd, path) rb_w32_is_socket(fd) #elif !defined(S_ISSOCK) #define is_socket(fd, path) 0 #else static int -is_socket(int fd, const char *path) +is_socket(int fd, VALUE path) { struct stat sbuf; if (fstat(fd, &sbuf) < 0) - rb_sys_fail(path); + rb_sys_fail_path(path); return S_ISSOCK(sbuf.st_mode); } #endif @@ -820,7 +822,7 @@ of.fd = fileno(f); of.stdio_file = f; of.mode = FMODE_WRITABLE; - of.path = NULL; + of.pathv = Qnil; return io_fwrite(rb_str_new(ptr, len), &of); } @@ -864,7 +866,7 @@ rb_io_check_writable(fptr); n = io_fwrite(str, fptr); - if (n == -1L) rb_sys_fail(fptr->path); + if (n == -1L) rb_sys_fail_path(fptr->pathv); return LONG2FIX(n); } @@ -957,7 +959,7 @@ GetOpenFile(io, fptr); pos = io_tell(fptr); - if (pos < 0 && errno) rb_sys_fail(fptr->path); + if (pos < 0 && errno) rb_sys_fail_path(fptr->pathv); return OFFT2NUM(pos); } @@ -970,7 +972,7 @@ pos = NUM2OFFT(offset); GetOpenFile(io, fptr); pos = io_seek(fptr, pos, whence); - if (pos < 0 && errno) rb_sys_fail(fptr->path); + if (pos < 0 && errno) rb_sys_fail_path(fptr->pathv); return INT2FIX(0); } @@ -1029,7 +1031,7 @@ pos = NUM2OFFT(offset); GetOpenFile(io, fptr); pos = io_seek(fptr, pos, SEEK_SET); - if (pos < 0) rb_sys_fail(fptr->path); + if (pos < 0) rb_sys_fail_path(fptr->pathv); return OFFT2NUM(pos); } @@ -1054,7 +1056,7 @@ rb_io_t *fptr; GetOpenFile(io, fptr); - if (io_seek(fptr, 0L, 0) < 0) rb_sys_fail(fptr->path); + if (io_seek(fptr, 0L, 0) < 0) rb_sys_fail_path(fptr->pathv); if (io == ARGF.current_file) { ARGF.gets_lineno -= fptr->lineno; } @@ -1082,7 +1084,7 @@ if (r < 0) { if (rb_io_wait_readable(fptr->fd)) goto retry; - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } fptr->rbuf_off = 0; fptr->rbuf_len = r; @@ -1217,7 +1219,7 @@ io_fflush(fptr); if (fsync(fptr->fd) < 0) - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); return INT2FIX(0); #else rb_notimplement(); @@ -1296,12 +1298,12 @@ const char *st = ""; fptr = RFILE(rb_io_taint_check(obj))->fptr; - if (!fptr || !fptr->path) return rb_any_to_s(obj); + if (!fptr || NIL_P(fptr->pathv)) return rb_any_to_s(obj); cname = rb_obj_classname(obj); if (fptr->fd < 0) { st = " (closed)"; } - return rb_sprintf("#<%s:%s%s>", cname, fptr->path, st); + return rb_sprintf("#<%s:%s%s>", cname, RSTRING_PTR(fptr->pathv), st); } /* @@ -1344,7 +1346,7 @@ c = rb_read_internal(fptr->fd, RSTRING_PTR(str)+offset, n); if (c == 0) break; if (c < 0) { - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } offset += c; if ((n -= c) <= 0) break; @@ -1599,7 +1601,7 @@ #ifdef F_GETFL flags = fcntl(fptr->fd, F_GETFL); if (flags == -1) { - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } #else flags = 0; @@ -1607,7 +1609,7 @@ if ((flags & O_NONBLOCK) == 0) { flags |= O_NONBLOCK; if (fcntl(fptr->fd, F_SETFL, flags) == -1) { - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } } } @@ -1658,7 +1660,7 @@ if (n < 0) { if (!nonblock && rb_io_wait_readable(fptr->fd)) goto again; - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } } rb_str_resize(str, n); @@ -1810,7 +1812,7 @@ rb_io_set_nonblock(fptr); n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str)); - if (n == -1) rb_sys_fail(fptr->path); + if (n == -1) rb_sys_fail_path(fptr->pathv); return LONG2FIX(n); } @@ -1992,7 +1994,7 @@ i = cnt; while (--i && *++p == term); if (!read_buffered_data(buf, cnt - i, fptr)) /* must not fail */ - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } rb_thread_wait_fd(fptr->fd); rb_io_check_closed(fptr); @@ -2873,14 +2875,14 @@ if (io != write_io) { GetOpenFile(write_io, fptr); if (fptr && 0 <= (fd = fptr->fd)) { - if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail(fptr->path); + if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail_path(fptr->pathv); if (!(ret & FD_CLOEXEC)) return Qfalse; } } GetOpenFile(io, fptr); if (fptr && 0 <= (fd = fptr->fd)) { - if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail(fptr->path); + if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail_path(fptr->pathv); if (!(ret & FD_CLOEXEC)) return Qfalse; } return Qtrue; @@ -2915,11 +2917,11 @@ if (io != write_io) { GetOpenFile(write_io, fptr); if (fptr && 0 <= (fd = fptr->fd)) { - if ((ret = fcntl(fptr->fd, F_GETFD)) == -1) rb_sys_fail(fptr->path); + if ((ret = fcntl(fptr->fd, F_GETFD)) == -1) rb_sys_fail_path(fptr->pathv); if ((ret & FD_CLOEXEC) != flag) { ret = (ret & ~FD_CLOEXEC) | flag; ret = fcntl(fd, F_SETFD, ret); - if (ret == -1) rb_sys_fail(fptr->path); + if (ret == -1) rb_sys_fail_path(fptr->pathv); } } @@ -2927,11 +2929,11 @@ GetOpenFile(io, fptr); if (fptr && 0 <= (fd = fptr->fd)) { - if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail(fptr->path); + if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail_path(fptr->pathv); if ((ret & FD_CLOEXEC) != flag) { ret = (ret & ~FD_CLOEXEC) | flag; ret = fcntl(fd, F_SETFD, ret); - if (ret == -1) rb_sys_fail(fptr->path); + if (ret == -1) rb_sys_fail_path(fptr->pathv); } } #else @@ -2942,7 +2944,7 @@ #define FMODE_PREP (1<<16) #define IS_PREP_STDIO(f) ((f)->mode & FMODE_PREP) -#define PREP_STDIO_NAME(f) ((f)->path) +#define PREP_STDIO_NAME(f) (RSTRING_PTR((f)->pathv)) static void finish_writeconv(rb_io_t *fptr, int noraise) @@ -3028,14 +3030,14 @@ /* fptr->stdio_file is deallocated anyway */ fptr->stdio_file = 0; fptr->fd = -1; - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } } else if (0 <= fptr->fd) { if (close(fptr->fd) < 0 && !noraise) { if (errno != EBADF) { /* fptr->fd is still not closed */ - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } else { /* fptr->fd is already closed. */ @@ -3047,7 +3049,7 @@ fptr->stdio_file = 0; fptr->mode &= ~(FMODE_READABLE|FMODE_WRITABLE); if (ebadf) { - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } } @@ -3097,10 +3099,7 @@ { if (!fptr) return 0; if (fptr->refcnt <= 0 || --fptr->refcnt) return 0; - if (fptr->path) { - free(fptr->path); - fptr->path = 0; - } + fptr->pathv = Qnil; if (0 <= fptr->fd) rb_io_fptr_cleanup(fptr, Qtrue); if (fptr->rbuf) { @@ -3252,12 +3251,12 @@ rb_raise(rb_eSecurityError, "Insecure: can't close"); } GetOpenFile(io, fptr); - if (is_socket(fptr->fd, fptr->path)) { + if (is_socket(fptr->fd, fptr->pathv)) { #ifndef SHUT_RD # define SHUT_RD 0 #endif if (shutdown(fptr->fd, SHUT_RD) < 0) - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); fptr->mode &= ~FMODE_READABLE; if (!(fptr->mode & FMODE_WRITABLE)) return rb_io_close(io); @@ -3313,12 +3312,12 @@ } write_io = GetWriteIO(io); GetOpenFile(write_io, fptr); - if (is_socket(fptr->fd, fptr->path)) { + if (is_socket(fptr->fd, fptr->pathv)) { #ifndef SHUT_WR # define SHUT_WR 1 #endif if (shutdown(fptr->fd, SHUT_WR) < 0) - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); fptr->mode &= ~FMODE_WRITABLE; if (!(fptr->mode & FMODE_READABLE)) return rb_io_close(write_io); @@ -3371,7 +3370,7 @@ rb_warn("sysseek for buffered IO"); } pos = lseek(fptr->fd, pos, whence); - if (pos == -1) rb_sys_fail(fptr->path); + if (pos == -1) rb_sys_fail_path(fptr->pathv); return OFFT2NUM(pos); } @@ -3413,7 +3412,7 @@ n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str)); TRAP_END; - if (n == -1) rb_sys_fail(fptr->path); + if (n == -1) rb_sys_fail_path(fptr->pathv); return LONG2FIX(n); } @@ -3471,7 +3470,7 @@ n = rb_read_internal(fptr->fd, RSTRING_PTR(str), ilen); if (n == -1) { - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } rb_str_set_len(str, n); if (n == 0 && ilen > 0) { @@ -3991,8 +3990,8 @@ fptr->enc = NULL; fptr->enc2 = NULL; } - fptr->path = strdup(RSTRING_PTR(filename)); - fptr->fd = rb_sysopen(fptr->path, modenum, perm); + fptr->pathv = rb_str_new_frozen(filename); + fptr->fd = rb_sysopen(RSTRING_PTR(fptr->pathv), modenum, perm); io_check_tty(fptr); return io; @@ -4891,9 +4890,8 @@ fptr->mode = orig->mode | (fptr->mode & FMODE_PREP); fptr->pid = orig->pid; fptr->lineno = orig->lineno; - if (fptr->path) free(fptr->path); - if (orig->path) fptr->path = strdup(orig->path); - else fptr->path = 0; + if (orig->pathv) fptr->pathv = orig->pathv; + else fptr->pathv = Qnil; fptr->finalize = orig->finalize; fd = fptr->fd; @@ -4902,7 +4900,7 @@ if (IS_PREP_STDIO(fptr)) { /* need to keep stdio objects */ if (dup2(fd2, fd) < 0) - rb_sys_fail(orig->path); + rb_sys_fail_path(orig->pathv); } else { if (fptr->stdio_file) @@ -4912,16 +4910,16 @@ fptr->stdio_file = 0; fptr->fd = -1; if (dup2(fd2, fd) < 0) - rb_sys_fail(orig->path); + rb_sys_fail_path(orig->pathv); fptr->fd = fd; } rb_thread_fd_close(fd); if ((orig->mode & FMODE_READABLE) && pos >= 0) { if (io_seek(fptr, pos, SEEK_SET) < 0) { - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } if (io_seek(orig, pos, SEEK_SET) < 0) { - rb_sys_fail(orig->path); + rb_sys_fail_path(orig->pathv); } } } @@ -4987,15 +4985,10 @@ rb_io_mode_enc(fptr, StringValueCStr(nmode)); } - if (fptr->path) { - free(fptr->path); - fptr->path = 0; - } - - fptr->path = strdup(StringValueCStr(fname)); + fptr->pathv = rb_str_new_frozen(fname); modenum = rb_io_flags_modenum(fptr->mode); if (fptr->fd < 0) { - fptr->fd = rb_sysopen(fptr->path, modenum, 0666); + fptr->fd = rb_sysopen(RSTRING_PTR(fptr->pathv), modenum, 0666); fptr->stdio_file = 0; return file; } @@ -5006,20 +4999,20 @@ fptr->rbuf_off = fptr->rbuf_len = 0; if (fptr->stdio_file) { - if (freopen(fptr->path, rb_io_modenum_mode(modenum), fptr->stdio_file) == 0) { - rb_sys_fail(fptr->path); + if (freopen(RSTRING_PTR(fptr->pathv), rb_io_modenum_mode(modenum), fptr->stdio_file) == 0) { + rb_sys_fail_path(fptr->pathv); } fptr->fd = fileno(fptr->stdio_file); #ifdef USE_SETVBUF if (setvbuf(fptr->stdio_file, NULL, _IOFBF, 0) != 0) - rb_warn("setvbuf() can't be honoured for %s", fptr->path); + rb_warn("setvbuf() can't be honoured for %s", RSTRING_PTR(fptr->pathv)); #endif } else { if (close(fptr->fd) < 0) - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); fptr->fd = -1; - fptr->fd = rb_sysopen(fptr->path, modenum, 0666); + fptr->fd = rb_sysopen(RSTRING_PTR(fptr->pathv), modenum, 0666); } return file; @@ -5044,7 +5037,7 @@ fptr->mode = orig->mode & ~FMODE_PREP; fptr->pid = orig->pid; fptr->lineno = orig->lineno; - if (orig->path) fptr->path = strdup(orig->path); + if (!NIL_P(orig->pathv)) fptr->pathv = orig->pathv; fptr->finalize = orig->finalize; fd = ruby_dup(orig->fd); @@ -5449,7 +5442,7 @@ #endif fp->mode = flags; io_check_tty(fp); - if (path) fp->path = strdup(path); + if (path) fp->pathv = rb_obj_freeze(rb_str_new_cstr(path)); return io; } @@ -6398,7 +6391,7 @@ } GetOpenFile(io, fptr); retval = io_cntl(fptr->fd, cmd, narg, io_p); - if (retval < 0) rb_sys_fail(fptr->path); + if (retval < 0) rb_sys_fail_path(fptr->pathv); if (TYPE(arg) == T_STRING && RSTRING_PTR(arg)[len] != 17) { rb_raise(rb_eArgError, "return value overflowed string"); } Index: gc.c =================================================================== --- gc.c (revision 18781) +++ gc.c (revision 18782) @@ -1508,6 +1508,7 @@ case T_FILE: if (obj->as.file.fptr) { + gc_mark(objspace, obj->as.file.fptr->pathv, lev); gc_mark(objspace, obj->as.file.fptr->tied_io_for_writing, lev); gc_mark(objspace, obj->as.file.fptr->writeconv_stateless, lev); } Index: ext/socket/socket.c =================================================================== --- ext/socket/socket.c (revision 18781) +++ ext/socket/socket.c (revision 18782) @@ -403,9 +403,11 @@ break; } +#define rb_sys_fail_path(path) rb_sys_fail(NIL_P(path) ? 0 : RSTRING_PTR(path)) + GetOpenFile(sock, fptr); if (setsockopt(fptr->fd, level, option, v, vlen) < 0) - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); return INT2FIX(0); } @@ -466,7 +468,7 @@ GetOpenFile(sock, fptr); if (getsockopt(fptr->fd, level, option, buf, &len) < 0) - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); return rb_str_new(buf, len); #else @@ -1700,7 +1702,7 @@ init_sock(sock, fd); if (server) { GetOpenFile(sock, fptr); - fptr->path = strdup(RSTRING_PTR(path)); + fptr->pathv = rb_str_new_frozen(path); } return sock; @@ -1938,14 +1940,14 @@ rb_io_t *fptr; GetOpenFile(sock, fptr); - if (fptr->path == 0) { + if (NIL_P(fptr->pathv)) { struct sockaddr_un addr; socklen_t len = sizeof(addr); if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0) rb_sys_fail(0); - fptr->path = strdup(unixpath(&addr, len)); + fptr->pathv = rb_obj_freeze(rb_str_new_cstr(unixpath(&addr, len))); } - return rb_str_new2(fptr->path); + return rb_str_dup(fptr->pathv); } static VALUE Index: file.c =================================================================== --- file.c (revision 18781) +++ file.c (revision 18782) @@ -170,8 +170,8 @@ fptr = RFILE(rb_io_taint_check(obj))->fptr; rb_io_check_initialized(fptr); - if (!fptr->path) return Qnil; - return rb_tainted_str_new2(fptr->path); + if (NIL_P(fptr->pathv)) return Qnil; + return rb_obj_taint(rb_str_dup(fptr->pathv)); } static VALUE @@ -800,9 +800,10 @@ rb_io_t *fptr; struct stat st; +#define rb_sys_fail_path(path) rb_sys_fail(NIL_P(path) ? 0 : RSTRING_PTR(path)) GetOpenFile(obj, fptr); if (fstat(fptr->fd, &st) == -1) { - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } return stat_new(&st); } @@ -862,9 +863,9 @@ rb_secure(2); GetOpenFile(obj, fptr); - if (!fptr->path) return Qnil; - if (lstat(fptr->path, &st) == -1) { - rb_sys_fail(fptr->path); + if (NIL_P(fptr->pathv)) return Qnil; + if (lstat(RSTRING_PTR(fptr->pathv), &st) == -1) { + rb_sys_fail_path(fptr->pathv); } return stat_new(&st); #else @@ -1704,7 +1705,7 @@ GetOpenFile(obj, fptr); if (fstat(fptr->fd, &st) == -1) { - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } return stat_atime(&st); } @@ -1747,7 +1748,7 @@ GetOpenFile(obj, fptr); if (fstat(fptr->fd, &st) == -1) { - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } return stat_mtime(&st); } @@ -1793,7 +1794,7 @@ GetOpenFile(obj, fptr); if (fstat(fptr->fd, &st) == -1) { - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } return stat_ctime(&st); } @@ -1859,11 +1860,11 @@ GetOpenFile(obj, fptr); #ifdef HAVE_FCHMOD if (fchmod(fptr->fd, mode) == -1) - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); #else - if (!fptr->path) return Qnil; - if (chmod(fptr->path, mode) == -1) - rb_sys_fail(fptr->path); + if (NIL_P(fptr->pathv)) return Qnil; + if (chmod(RSTRING_PTR(fptr->pathv), mode) == -1) + rb_sys_fail_path(fptr->pathv); #endif return INT2FIX(0); @@ -1990,12 +1991,12 @@ g = NIL_P(group) ? -1 : NUM2INT(group); GetOpenFile(obj, fptr); #if defined(DJGPP) || defined(__CYGWIN32__) || defined(_WIN32) || defined(__EMX__) - if (!fptr->path) return Qnil; - if (chown(fptr->path, o, g) == -1) - rb_sys_fail(fptr->path); + if (NIL_P(fptr->pathv)) return Qnil; + if (chown(RSTRING_PTR(fptr->pathv), o, g) == -1) + rb_sys_fail_path(fptr->pathv); #else if (fchown(fptr->fd, o, g) == -1) - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); #endif return INT2FIX(0); @@ -2274,7 +2275,7 @@ } if (rv < 0) { xfree(buf); - rb_sys_fail(RSTRING_PTR(path)); + rb_sys_fail_path(path); } v = rb_tainted_str_new(buf, rv); xfree(buf); @@ -3347,11 +3348,11 @@ rb_io_flush(obj); #ifdef HAVE_FTRUNCATE if (ftruncate(fptr->fd, pos) < 0) - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); #else # ifdef HAVE_CHSIZE if (chsize(fptr->fd, pos) < 0) - rb_sys_fail(fptr->path); + rb_sys_fail(fptr->pathv); # else rb_notimplement(); # endif @@ -3457,7 +3458,7 @@ break; default: - rb_sys_fail(fptr->path); + rb_sys_fail_path(fptr->pathv); } } #endif -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/