|
Ruby
2.0.0p481(2014-05-08revision45883)
|
00001 /********************************************************************** 00002 00003 file.c - 00004 00005 $Author: nagachika $ 00006 created at: Mon Nov 15 12:24:34 JST 1993 00007 00008 Copyright (C) 1993-2007 Yukihiro Matsumoto 00009 Copyright (C) 2000 Network Applied Communication Laboratory, Inc. 00010 Copyright (C) 2000 Information-technology Promotion Agency, Japan 00011 00012 **********************************************************************/ 00013 00014 #ifdef _WIN32 00015 #include "missing/file.h" 00016 #endif 00017 #ifdef __CYGWIN__ 00018 #include <windows.h> 00019 #include <sys/cygwin.h> 00020 #include <wchar.h> 00021 #endif 00022 00023 #include "ruby/ruby.h" 00024 #include "ruby/io.h" 00025 #include "ruby/util.h" 00026 #include "dln.h" 00027 #include "internal.h" 00028 00029 #ifdef HAVE_UNISTD_H 00030 #include <unistd.h> 00031 #endif 00032 00033 #ifdef HAVE_SYS_FILE_H 00034 # include <sys/file.h> 00035 #else 00036 int flock(int, int); 00037 #endif 00038 00039 #ifdef HAVE_SYS_PARAM_H 00040 # include <sys/param.h> 00041 #endif 00042 #ifndef MAXPATHLEN 00043 # define MAXPATHLEN 1024 00044 #endif 00045 00046 #include <ctype.h> 00047 00048 #include <time.h> 00049 00050 #ifdef HAVE_UTIME_H 00051 #include <utime.h> 00052 #elif defined HAVE_SYS_UTIME_H 00053 #include <sys/utime.h> 00054 #endif 00055 00056 #ifdef HAVE_PWD_H 00057 #include <pwd.h> 00058 #endif 00059 00060 #include <sys/types.h> 00061 #include <sys/stat.h> 00062 00063 #if defined(__native_client__) && defined(NACL_NEWLIB) 00064 # include "nacl/utime.h" 00065 # include "nacl/stat.h" 00066 # include "nacl/unistd.h" 00067 #endif 00068 00069 00070 #ifdef HAVE_SYS_MKDEV_H 00071 #include <sys/mkdev.h> 00072 #endif 00073 00074 #if defined(HAVE_FCNTL_H) 00075 #include <fcntl.h> 00076 #endif 00077 00078 #if defined(HAVE_SYS_TIME_H) 00079 #include <sys/time.h> 00080 #endif 00081 00082 #if !defined HAVE_LSTAT && !defined lstat 00083 #define lstat stat 00084 #endif 00085 00086 /* define system APIs */ 00087 #ifdef _WIN32 00088 #define STAT(p, s) rb_w32_ustati64((p), (s)) 00089 #undef lstat 00090 #define lstat(p, s) rb_w32_ustati64((p), (s)) 00091 #undef access 00092 #define access(p, m) rb_w32_uaccess((p), (m)) 00093 #undef chmod 00094 #define chmod(p, m) rb_w32_uchmod((p), (m)) 00095 #undef chown 00096 #define chown(p, o, g) rb_w32_uchown((p), (o), (g)) 00097 #undef utime 00098 #define utime(p, t) rb_w32_uutime((p), (t)) 00099 #undef link 00100 #define link(f, t) rb_w32_ulink((f), (t)) 00101 #undef unlink 00102 #define unlink(p) rb_w32_uunlink(p) 00103 #undef rename 00104 #define rename(f, t) rb_w32_urename((f), (t)) 00105 #else 00106 #define STAT(p, s) stat((p), (s)) 00107 #endif 00108 00109 #define rb_sys_fail_path(path) rb_sys_fail_str(path) 00110 00111 #if defined(__BEOS__) || defined(__HAIKU__) /* should not change ID if -1 */ 00112 static int 00113 be_chown(const char *path, uid_t owner, gid_t group) 00114 { 00115 if (owner == (uid_t)-1 || group == (gid_t)-1) { 00116 struct stat st; 00117 if (STAT(path, &st) < 0) return -1; 00118 if (owner == (uid_t)-1) owner = st.st_uid; 00119 if (group == (gid_t)-1) group = st.st_gid; 00120 } 00121 return chown(path, owner, group); 00122 } 00123 #define chown be_chown 00124 static int 00125 be_fchown(int fd, uid_t owner, gid_t group) 00126 { 00127 if (owner == (uid_t)-1 || group == (gid_t)-1) { 00128 struct stat st; 00129 if (fstat(fd, &st) < 0) return -1; 00130 if (owner == (uid_t)-1) owner = st.st_uid; 00131 if (group == (gid_t)-1) group = st.st_gid; 00132 } 00133 return fchown(fd, owner, group); 00134 } 00135 #define fchown be_fchown 00136 #endif /* __BEOS__ || __HAIKU__ */ 00137 00138 VALUE rb_cFile; 00139 VALUE rb_mFileTest; 00140 VALUE rb_cStat; 00141 00142 #define insecure_obj_p(obj, level) ((level) >= 4 || ((level) > 0 && OBJ_TAINTED(obj))) 00143 00144 static VALUE 00145 file_path_convert(VALUE name) 00146 { 00147 #ifndef _WIN32 /* non Windows == Unix */ 00148 rb_encoding *fname_encoding = rb_enc_from_index(ENCODING_GET(name)); 00149 rb_encoding *fs_encoding; 00150 if (rb_default_internal_encoding() != NULL 00151 && rb_usascii_encoding() != fname_encoding 00152 && rb_ascii8bit_encoding() != fname_encoding 00153 && (fs_encoding = rb_filesystem_encoding()) != fname_encoding 00154 && !rb_enc_str_asciionly_p(name)) { 00155 /* Don't call rb_filesystem_encoding() before US-ASCII and ASCII-8BIT */ 00156 /* fs_encoding should be ascii compatible */ 00157 name = rb_str_conv_enc(name, fname_encoding, fs_encoding); 00158 } 00159 #endif 00160 return name; 00161 } 00162 00163 static rb_encoding * 00164 check_path_encoding(VALUE str) 00165 { 00166 rb_encoding *enc = rb_enc_get(str); 00167 if (!rb_enc_asciicompat(enc)) { 00168 rb_raise(rb_eEncCompatError, "path name must be ASCII-compatible (%s): %"PRIsVALUE, 00169 rb_enc_name(enc), rb_str_inspect(str)); 00170 } 00171 return enc; 00172 } 00173 00174 VALUE 00175 rb_get_path_check_to_string(VALUE obj, int level) 00176 { 00177 VALUE tmp; 00178 ID to_path; 00179 00180 if (insecure_obj_p(obj, level)) { 00181 rb_insecure_operation(); 00182 } 00183 00184 if (RB_TYPE_P(obj, T_STRING)) { 00185 return obj; 00186 } 00187 CONST_ID(to_path, "to_path"); 00188 tmp = rb_check_funcall(obj, to_path, 0, 0); 00189 if (tmp == Qundef) { 00190 tmp = obj; 00191 } 00192 StringValue(tmp); 00193 return tmp; 00194 } 00195 00196 VALUE 00197 rb_get_path_check_convert(VALUE obj, VALUE tmp, int level) 00198 { 00199 tmp = file_path_convert(tmp); 00200 if (obj != tmp && insecure_obj_p(tmp, level)) { 00201 rb_insecure_operation(); 00202 } 00203 00204 check_path_encoding(tmp); 00205 StringValueCStr(tmp); 00206 00207 return rb_str_new4(tmp); 00208 } 00209 00210 static VALUE 00211 rb_get_path_check(VALUE obj, int level) 00212 { 00213 VALUE tmp = rb_get_path_check_to_string(obj, level); 00214 return rb_get_path_check_convert(obj, tmp, level); 00215 } 00216 00217 VALUE 00218 rb_get_path_no_checksafe(VALUE obj) 00219 { 00220 return rb_get_path_check(obj, 0); 00221 } 00222 00223 VALUE 00224 rb_get_path(VALUE obj) 00225 { 00226 return rb_get_path_check(obj, rb_safe_level()); 00227 } 00228 00229 VALUE 00230 rb_str_encode_ospath(VALUE path) 00231 { 00232 #ifdef _WIN32 00233 rb_encoding *enc = rb_enc_get(path); 00234 if (enc != rb_ascii8bit_encoding()) { 00235 rb_encoding *utf8 = rb_utf8_encoding(); 00236 if (enc != utf8) 00237 path = rb_str_encode(path, rb_enc_from_encoding(utf8), 0, Qnil); 00238 } 00239 else if (RSTRING_LEN(path) > 0) { 00240 path = rb_str_dup(path); 00241 rb_enc_associate(path, rb_filesystem_encoding()); 00242 path = rb_str_encode(path, rb_enc_from_encoding(rb_utf8_encoding()), 0, Qnil); 00243 } 00244 #endif 00245 return path; 00246 } 00247 00248 static long 00249 apply2files(void (*func)(const char *, VALUE, void *), VALUE vargs, void *arg) 00250 { 00251 long i; 00252 volatile VALUE path; 00253 00254 rb_secure(4); 00255 for (i=0; i<RARRAY_LEN(vargs); i++) { 00256 const char *s; 00257 path = rb_get_path(RARRAY_PTR(vargs)[i]); 00258 path = rb_str_encode_ospath(path); 00259 s = RSTRING_PTR(path); 00260 (*func)(s, path, arg); 00261 } 00262 00263 return RARRAY_LEN(vargs); 00264 } 00265 00266 /* 00267 * call-seq: 00268 * file.path -> filename 00269 * 00270 * Returns the pathname used to create <i>file</i> as a string. Does 00271 * not normalize the name. 00272 * 00273 * File.new("testfile").path #=> "testfile" 00274 * File.new("/tmp/../tmp/xxx", "w").path #=> "/tmp/../tmp/xxx" 00275 * 00276 */ 00277 00278 static VALUE 00279 rb_file_path(VALUE obj) 00280 { 00281 rb_io_t *fptr; 00282 00283 fptr = RFILE(rb_io_taint_check(obj))->fptr; 00284 rb_io_check_initialized(fptr); 00285 if (NIL_P(fptr->pathv)) return Qnil; 00286 return rb_obj_taint(rb_str_dup(fptr->pathv)); 00287 } 00288 00289 static size_t 00290 stat_memsize(const void *p) 00291 { 00292 return p ? sizeof(struct stat) : 0; 00293 } 00294 00295 static const rb_data_type_t stat_data_type = { 00296 "stat", 00297 {NULL, RUBY_TYPED_DEFAULT_FREE, stat_memsize,}, 00298 }; 00299 00300 static VALUE 00301 stat_new_0(VALUE klass, struct stat *st) 00302 { 00303 struct stat *nst = 0; 00304 00305 if (st) { 00306 nst = ALLOC(struct stat); 00307 *nst = *st; 00308 } 00309 return TypedData_Wrap_Struct(klass, &stat_data_type, nst); 00310 } 00311 00312 static VALUE 00313 stat_new(struct stat *st) 00314 { 00315 return stat_new_0(rb_cStat, st); 00316 } 00317 00318 static struct stat* 00319 get_stat(VALUE self) 00320 { 00321 struct stat* st; 00322 TypedData_Get_Struct(self, struct stat, &stat_data_type, st); 00323 if (!st) rb_raise(rb_eTypeError, "uninitialized File::Stat"); 00324 return st; 00325 } 00326 00327 static struct timespec stat_mtimespec(struct stat *st); 00328 00329 /* 00330 * call-seq: 00331 * stat <=> other_stat -> -1, 0, 1, nil 00332 * 00333 * Compares File::Stat objects by comparing their respective modification 00334 * times. 00335 * 00336 * +nil+ is returned if the two values are incomparable. 00337 * 00338 * f1 = File.new("f1", "w") 00339 * sleep 1 00340 * f2 = File.new("f2", "w") 00341 * f1.stat <=> f2.stat #=> -1 00342 */ 00343 00344 static VALUE 00345 rb_stat_cmp(VALUE self, VALUE other) 00346 { 00347 if (rb_obj_is_kind_of(other, rb_obj_class(self))) { 00348 struct timespec ts1 = stat_mtimespec(get_stat(self)); 00349 struct timespec ts2 = stat_mtimespec(get_stat(other)); 00350 if (ts1.tv_sec == ts2.tv_sec) { 00351 if (ts1.tv_nsec == ts2.tv_nsec) return INT2FIX(0); 00352 if (ts1.tv_nsec < ts2.tv_nsec) return INT2FIX(-1); 00353 return INT2FIX(1); 00354 } 00355 if (ts1.tv_sec < ts2.tv_sec) return INT2FIX(-1); 00356 return INT2FIX(1); 00357 } 00358 return Qnil; 00359 } 00360 00361 #define ST2UINT(val) ((val) & ~(~1UL << (sizeof(val) * CHAR_BIT - 1))) 00362 00363 #ifndef NUM2DEVT 00364 # define NUM2DEVT(v) NUM2UINT(v) 00365 #endif 00366 #ifndef DEVT2NUM 00367 # define DEVT2NUM(v) UINT2NUM(v) 00368 #endif 00369 #ifndef PRI_DEVT_PREFIX 00370 # define PRI_DEVT_PREFIX "" 00371 #endif 00372 00373 /* 00374 * call-seq: 00375 * stat.dev -> fixnum 00376 * 00377 * Returns an integer representing the device on which <i>stat</i> 00378 * resides. 00379 * 00380 * File.stat("testfile").dev #=> 774 00381 */ 00382 00383 static VALUE 00384 rb_stat_dev(VALUE self) 00385 { 00386 return DEVT2NUM(get_stat(self)->st_dev); 00387 } 00388 00389 /* 00390 * call-seq: 00391 * stat.dev_major -> fixnum 00392 * 00393 * Returns the major part of <code>File_Stat#dev</code> or 00394 * <code>nil</code>. 00395 * 00396 * File.stat("/dev/fd1").dev_major #=> 2 00397 * File.stat("/dev/tty").dev_major #=> 5 00398 */ 00399 00400 static VALUE 00401 rb_stat_dev_major(VALUE self) 00402 { 00403 #if defined(major) 00404 return INT2NUM(major(get_stat(self)->st_dev)); 00405 #else 00406 return Qnil; 00407 #endif 00408 } 00409 00410 /* 00411 * call-seq: 00412 * stat.dev_minor -> fixnum 00413 * 00414 * Returns the minor part of <code>File_Stat#dev</code> or 00415 * <code>nil</code>. 00416 * 00417 * File.stat("/dev/fd1").dev_minor #=> 1 00418 * File.stat("/dev/tty").dev_minor #=> 0 00419 */ 00420 00421 static VALUE 00422 rb_stat_dev_minor(VALUE self) 00423 { 00424 #if defined(minor) 00425 return INT2NUM(minor(get_stat(self)->st_dev)); 00426 #else 00427 return Qnil; 00428 #endif 00429 } 00430 00431 /* 00432 * call-seq: 00433 * stat.ino -> fixnum 00434 * 00435 * Returns the inode number for <i>stat</i>. 00436 * 00437 * File.stat("testfile").ino #=> 1083669 00438 * 00439 */ 00440 00441 static VALUE 00442 rb_stat_ino(VALUE self) 00443 { 00444 #if SIZEOF_STRUCT_STAT_ST_INO > SIZEOF_LONG 00445 return ULL2NUM(get_stat(self)->st_ino); 00446 #else 00447 return ULONG2NUM(get_stat(self)->st_ino); 00448 #endif 00449 } 00450 00451 /* 00452 * call-seq: 00453 * stat.mode -> fixnum 00454 * 00455 * Returns an integer representing the permission bits of 00456 * <i>stat</i>. The meaning of the bits is platform dependent; on 00457 * Unix systems, see <code>stat(2)</code>. 00458 * 00459 * File.chmod(0644, "testfile") #=> 1 00460 * s = File.stat("testfile") 00461 * sprintf("%o", s.mode) #=> "100644" 00462 */ 00463 00464 static VALUE 00465 rb_stat_mode(VALUE self) 00466 { 00467 return UINT2NUM(ST2UINT(get_stat(self)->st_mode)); 00468 } 00469 00470 /* 00471 * call-seq: 00472 * stat.nlink -> fixnum 00473 * 00474 * Returns the number of hard links to <i>stat</i>. 00475 * 00476 * File.stat("testfile").nlink #=> 1 00477 * File.link("testfile", "testfile.bak") #=> 0 00478 * File.stat("testfile").nlink #=> 2 00479 * 00480 */ 00481 00482 static VALUE 00483 rb_stat_nlink(VALUE self) 00484 { 00485 return UINT2NUM(get_stat(self)->st_nlink); 00486 } 00487 00488 /* 00489 * call-seq: 00490 * stat.uid -> fixnum 00491 * 00492 * Returns the numeric user id of the owner of <i>stat</i>. 00493 * 00494 * File.stat("testfile").uid #=> 501 00495 * 00496 */ 00497 00498 static VALUE 00499 rb_stat_uid(VALUE self) 00500 { 00501 return UIDT2NUM(get_stat(self)->st_uid); 00502 } 00503 00504 /* 00505 * call-seq: 00506 * stat.gid -> fixnum 00507 * 00508 * Returns the numeric group id of the owner of <i>stat</i>. 00509 * 00510 * File.stat("testfile").gid #=> 500 00511 * 00512 */ 00513 00514 static VALUE 00515 rb_stat_gid(VALUE self) 00516 { 00517 return GIDT2NUM(get_stat(self)->st_gid); 00518 } 00519 00520 /* 00521 * call-seq: 00522 * stat.rdev -> fixnum or nil 00523 * 00524 * Returns an integer representing the device type on which 00525 * <i>stat</i> resides. Returns <code>nil</code> if the operating 00526 * system doesn't support this feature. 00527 * 00528 * File.stat("/dev/fd1").rdev #=> 513 00529 * File.stat("/dev/tty").rdev #=> 1280 00530 */ 00531 00532 static VALUE 00533 rb_stat_rdev(VALUE self) 00534 { 00535 #ifdef HAVE_ST_RDEV 00536 return DEVT2NUM(get_stat(self)->st_rdev); 00537 #else 00538 return Qnil; 00539 #endif 00540 } 00541 00542 /* 00543 * call-seq: 00544 * stat.rdev_major -> fixnum 00545 * 00546 * Returns the major part of <code>File_Stat#rdev</code> or 00547 * <code>nil</code>. 00548 * 00549 * File.stat("/dev/fd1").rdev_major #=> 2 00550 * File.stat("/dev/tty").rdev_major #=> 5 00551 */ 00552 00553 static VALUE 00554 rb_stat_rdev_major(VALUE self) 00555 { 00556 #if defined(HAVE_ST_RDEV) && defined(major) 00557 return DEVT2NUM(major(get_stat(self)->st_rdev)); 00558 #else 00559 return Qnil; 00560 #endif 00561 } 00562 00563 /* 00564 * call-seq: 00565 * stat.rdev_minor -> fixnum 00566 * 00567 * Returns the minor part of <code>File_Stat#rdev</code> or 00568 * <code>nil</code>. 00569 * 00570 * File.stat("/dev/fd1").rdev_minor #=> 1 00571 * File.stat("/dev/tty").rdev_minor #=> 0 00572 */ 00573 00574 static VALUE 00575 rb_stat_rdev_minor(VALUE self) 00576 { 00577 #if defined(HAVE_ST_RDEV) && defined(minor) 00578 return DEVT2NUM(minor(get_stat(self)->st_rdev)); 00579 #else 00580 return Qnil; 00581 #endif 00582 } 00583 00584 /* 00585 * call-seq: 00586 * stat.size -> fixnum 00587 * 00588 * Returns the size of <i>stat</i> in bytes. 00589 * 00590 * File.stat("testfile").size #=> 66 00591 */ 00592 00593 static VALUE 00594 rb_stat_size(VALUE self) 00595 { 00596 return OFFT2NUM(get_stat(self)->st_size); 00597 } 00598 00599 /* 00600 * call-seq: 00601 * stat.blksize -> integer or nil 00602 * 00603 * Returns the native file system's block size. Will return <code>nil</code> 00604 * on platforms that don't support this information. 00605 * 00606 * File.stat("testfile").blksize #=> 4096 00607 * 00608 */ 00609 00610 static VALUE 00611 rb_stat_blksize(VALUE self) 00612 { 00613 #ifdef HAVE_ST_BLKSIZE 00614 return ULONG2NUM(get_stat(self)->st_blksize); 00615 #else 00616 return Qnil; 00617 #endif 00618 } 00619 00620 /* 00621 * call-seq: 00622 * stat.blocks -> integer or nil 00623 * 00624 * Returns the number of native file system blocks allocated for this 00625 * file, or <code>nil</code> if the operating system doesn't 00626 * support this feature. 00627 * 00628 * File.stat("testfile").blocks #=> 2 00629 */ 00630 00631 static VALUE 00632 rb_stat_blocks(VALUE self) 00633 { 00634 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS 00635 # if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG 00636 return ULL2NUM(get_stat(self)->st_blocks); 00637 # else 00638 return ULONG2NUM(get_stat(self)->st_blocks); 00639 # endif 00640 #else 00641 return Qnil; 00642 #endif 00643 } 00644 00645 static struct timespec 00646 stat_atimespec(struct stat *st) 00647 { 00648 struct timespec ts; 00649 ts.tv_sec = st->st_atime; 00650 #if defined(HAVE_STRUCT_STAT_ST_ATIM) 00651 ts.tv_nsec = st->st_atim.tv_nsec; 00652 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC) 00653 ts.tv_nsec = st->st_atimespec.tv_nsec; 00654 #elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC) 00655 ts.tv_nsec = st->st_atimensec; 00656 #else 00657 ts.tv_nsec = 0; 00658 #endif 00659 return ts; 00660 } 00661 00662 static VALUE 00663 stat_atime(struct stat *st) 00664 { 00665 struct timespec ts = stat_atimespec(st); 00666 return rb_time_nano_new(ts.tv_sec, ts.tv_nsec); 00667 } 00668 00669 static struct timespec 00670 stat_mtimespec(struct stat *st) 00671 { 00672 struct timespec ts; 00673 ts.tv_sec = st->st_mtime; 00674 #if defined(HAVE_STRUCT_STAT_ST_MTIM) 00675 ts.tv_nsec = st->st_mtim.tv_nsec; 00676 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC) 00677 ts.tv_nsec = st->st_mtimespec.tv_nsec; 00678 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC) 00679 ts.tv_nsec = st->st_mtimensec; 00680 #else 00681 ts.tv_nsec = 0; 00682 #endif 00683 return ts; 00684 } 00685 00686 static VALUE 00687 stat_mtime(struct stat *st) 00688 { 00689 struct timespec ts = stat_mtimespec(st); 00690 return rb_time_nano_new(ts.tv_sec, ts.tv_nsec); 00691 } 00692 00693 static struct timespec 00694 stat_ctimespec(struct stat *st) 00695 { 00696 struct timespec ts; 00697 ts.tv_sec = st->st_ctime; 00698 #if defined(HAVE_STRUCT_STAT_ST_CTIM) 00699 ts.tv_nsec = st->st_ctim.tv_nsec; 00700 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC) 00701 ts.tv_nsec = st->st_ctimespec.tv_nsec; 00702 #elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC) 00703 ts.tv_nsec = st->st_ctimensec; 00704 #else 00705 ts.tv_nsec = 0; 00706 #endif 00707 return ts; 00708 } 00709 00710 static VALUE 00711 stat_ctime(struct stat *st) 00712 { 00713 struct timespec ts = stat_ctimespec(st); 00714 return rb_time_nano_new(ts.tv_sec, ts.tv_nsec); 00715 } 00716 00717 /* 00718 * call-seq: 00719 * stat.atime -> time 00720 * 00721 * Returns the last access time for this file as an object of class 00722 * <code>Time</code>. 00723 * 00724 * File.stat("testfile").atime #=> Wed Dec 31 18:00:00 CST 1969 00725 * 00726 */ 00727 00728 static VALUE 00729 rb_stat_atime(VALUE self) 00730 { 00731 return stat_atime(get_stat(self)); 00732 } 00733 00734 /* 00735 * call-seq: 00736 * stat.mtime -> aTime 00737 * 00738 * Returns the modification time of <i>stat</i>. 00739 * 00740 * File.stat("testfile").mtime #=> Wed Apr 09 08:53:14 CDT 2003 00741 * 00742 */ 00743 00744 static VALUE 00745 rb_stat_mtime(VALUE self) 00746 { 00747 return stat_mtime(get_stat(self)); 00748 } 00749 00750 /* 00751 * call-seq: 00752 * stat.ctime -> aTime 00753 * 00754 * Returns the change time for <i>stat</i> (that is, the time 00755 * directory information about the file was changed, not the file 00756 * itself). 00757 * 00758 * Note that on Windows (NTFS), returns creation time (birth time). 00759 * 00760 * File.stat("testfile").ctime #=> Wed Apr 09 08:53:14 CDT 2003 00761 * 00762 */ 00763 00764 static VALUE 00765 rb_stat_ctime(VALUE self) 00766 { 00767 return stat_ctime(get_stat(self)); 00768 } 00769 00770 /* 00771 * call-seq: 00772 * stat.inspect -> string 00773 * 00774 * Produce a nicely formatted description of <i>stat</i>. 00775 * 00776 * File.stat("/etc/passwd").inspect 00777 * #=> "#<File::Stat dev=0xe000005, ino=1078078, mode=0100644, 00778 * # nlink=1, uid=0, gid=0, rdev=0x0, size=1374, blksize=4096, 00779 * # blocks=8, atime=Wed Dec 10 10:16:12 CST 2003, 00780 * # mtime=Fri Sep 12 15:41:41 CDT 2003, 00781 * # ctime=Mon Oct 27 11:20:27 CST 2003>" 00782 */ 00783 00784 static VALUE 00785 rb_stat_inspect(VALUE self) 00786 { 00787 VALUE str; 00788 size_t i; 00789 static const struct { 00790 const char *name; 00791 VALUE (*func)(VALUE); 00792 } member[] = { 00793 {"dev", rb_stat_dev}, 00794 {"ino", rb_stat_ino}, 00795 {"mode", rb_stat_mode}, 00796 {"nlink", rb_stat_nlink}, 00797 {"uid", rb_stat_uid}, 00798 {"gid", rb_stat_gid}, 00799 {"rdev", rb_stat_rdev}, 00800 {"size", rb_stat_size}, 00801 {"blksize", rb_stat_blksize}, 00802 {"blocks", rb_stat_blocks}, 00803 {"atime", rb_stat_atime}, 00804 {"mtime", rb_stat_mtime}, 00805 {"ctime", rb_stat_ctime}, 00806 }; 00807 00808 struct stat* st; 00809 TypedData_Get_Struct(self, struct stat, &stat_data_type, st); 00810 if (!st) { 00811 return rb_sprintf("#<%s: uninitialized>", rb_obj_classname(self)); 00812 } 00813 00814 str = rb_str_buf_new2("#<"); 00815 rb_str_buf_cat2(str, rb_obj_classname(self)); 00816 rb_str_buf_cat2(str, " "); 00817 00818 for (i = 0; i < sizeof(member)/sizeof(member[0]); i++) { 00819 VALUE v; 00820 00821 if (i > 0) { 00822 rb_str_buf_cat2(str, ", "); 00823 } 00824 rb_str_buf_cat2(str, member[i].name); 00825 rb_str_buf_cat2(str, "="); 00826 v = (*member[i].func)(self); 00827 if (i == 2) { /* mode */ 00828 rb_str_catf(str, "0%lo", (unsigned long)NUM2ULONG(v)); 00829 } 00830 else if (i == 0 || i == 6) { /* dev/rdev */ 00831 rb_str_catf(str, "0x%"PRI_DEVT_PREFIX"x", NUM2DEVT(v)); 00832 } 00833 else { 00834 rb_str_append(str, rb_inspect(v)); 00835 } 00836 } 00837 rb_str_buf_cat2(str, ">"); 00838 OBJ_INFECT(str, self); 00839 00840 return str; 00841 } 00842 00843 static int 00844 rb_stat(VALUE file, struct stat *st) 00845 { 00846 VALUE tmp; 00847 00848 rb_secure(2); 00849 tmp = rb_check_convert_type(file, T_FILE, "IO", "to_io"); 00850 if (!NIL_P(tmp)) { 00851 rb_io_t *fptr; 00852 00853 GetOpenFile(tmp, fptr); 00854 return fstat(fptr->fd, st); 00855 } 00856 FilePathValue(file); 00857 file = rb_str_encode_ospath(file); 00858 return STAT(StringValueCStr(file), st); 00859 } 00860 00861 #ifdef _WIN32 00862 static HANDLE 00863 w32_io_info(VALUE *file, BY_HANDLE_FILE_INFORMATION *st) 00864 { 00865 VALUE tmp; 00866 HANDLE f, ret = 0; 00867 00868 tmp = rb_check_convert_type(*file, T_FILE, "IO", "to_io"); 00869 if (!NIL_P(tmp)) { 00870 rb_io_t *fptr; 00871 00872 GetOpenFile(tmp, fptr); 00873 f = (HANDLE)rb_w32_get_osfhandle(fptr->fd); 00874 if (f == (HANDLE)-1) return INVALID_HANDLE_VALUE; 00875 } 00876 else { 00877 VALUE tmp; 00878 WCHAR *ptr; 00879 int len; 00880 VALUE v; 00881 00882 FilePathValue(*file); 00883 tmp = rb_str_encode_ospath(*file); 00884 len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, NULL, 0); 00885 ptr = ALLOCV_N(WCHAR, v, len); 00886 MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, ptr, len); 00887 f = CreateFileW(ptr, 0, 00888 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 00889 FILE_FLAG_BACKUP_SEMANTICS, NULL); 00890 ALLOCV_END(v); 00891 if (f == INVALID_HANDLE_VALUE) return f; 00892 ret = f; 00893 } 00894 if (GetFileType(f) == FILE_TYPE_DISK) { 00895 ZeroMemory(st, sizeof(*st)); 00896 if (GetFileInformationByHandle(f, st)) return ret; 00897 } 00898 if (ret) CloseHandle(ret); 00899 return INVALID_HANDLE_VALUE; 00900 } 00901 #endif 00902 00903 /* 00904 * call-seq: 00905 * File.stat(file_name) -> stat 00906 * 00907 * Returns a <code>File::Stat</code> object for the named file (see 00908 * <code>File::Stat</code>). 00909 * 00910 * File.stat("testfile").mtime #=> Tue Apr 08 12:58:04 CDT 2003 00911 * 00912 */ 00913 00914 static VALUE 00915 rb_file_s_stat(VALUE klass, VALUE fname) 00916 { 00917 struct stat st; 00918 00919 rb_secure(4); 00920 FilePathValue(fname); 00921 if (rb_stat(fname, &st) < 0) { 00922 rb_sys_fail_path(fname); 00923 } 00924 return stat_new(&st); 00925 } 00926 00927 /* 00928 * call-seq: 00929 * ios.stat -> stat 00930 * 00931 * Returns status information for <em>ios</em> as an object of type 00932 * <code>File::Stat</code>. 00933 * 00934 * f = File.new("testfile") 00935 * s = f.stat 00936 * "%o" % s.mode #=> "100644" 00937 * s.blksize #=> 4096 00938 * s.atime #=> Wed Apr 09 08:53:54 CDT 2003 00939 * 00940 */ 00941 00942 static VALUE 00943 rb_io_stat(VALUE obj) 00944 { 00945 rb_io_t *fptr; 00946 struct stat st; 00947 00948 GetOpenFile(obj, fptr); 00949 if (fstat(fptr->fd, &st) == -1) { 00950 rb_sys_fail_path(fptr->pathv); 00951 } 00952 return stat_new(&st); 00953 } 00954 00955 /* 00956 * call-seq: 00957 * File.lstat(file_name) -> stat 00958 * 00959 * Same as <code>File::stat</code>, but does not follow the last symbolic 00960 * link. Instead, reports on the link itself. 00961 * 00962 * File.symlink("testfile", "link2test") #=> 0 00963 * File.stat("testfile").size #=> 66 00964 * File.lstat("link2test").size #=> 8 00965 * File.stat("link2test").size #=> 66 00966 * 00967 */ 00968 00969 static VALUE 00970 rb_file_s_lstat(VALUE klass, VALUE fname) 00971 { 00972 #ifdef HAVE_LSTAT 00973 struct stat st; 00974 00975 rb_secure(2); 00976 FilePathValue(fname); 00977 fname = rb_str_encode_ospath(fname); 00978 if (lstat(StringValueCStr(fname), &st) == -1) { 00979 rb_sys_fail_path(fname); 00980 } 00981 return stat_new(&st); 00982 #else 00983 return rb_file_s_stat(klass, fname); 00984 #endif 00985 } 00986 00987 /* 00988 * call-seq: 00989 * file.lstat -> stat 00990 * 00991 * Same as <code>IO#stat</code>, but does not follow the last symbolic 00992 * link. Instead, reports on the link itself. 00993 * 00994 * File.symlink("testfile", "link2test") #=> 0 00995 * File.stat("testfile").size #=> 66 00996 * f = File.new("link2test") 00997 * f.lstat.size #=> 8 00998 * f.stat.size #=> 66 00999 */ 01000 01001 static VALUE 01002 rb_file_lstat(VALUE obj) 01003 { 01004 #ifdef HAVE_LSTAT 01005 rb_io_t *fptr; 01006 struct stat st; 01007 VALUE path; 01008 01009 rb_secure(2); 01010 GetOpenFile(obj, fptr); 01011 if (NIL_P(fptr->pathv)) return Qnil; 01012 path = rb_str_encode_ospath(fptr->pathv); 01013 if (lstat(RSTRING_PTR(path), &st) == -1) { 01014 rb_sys_fail_path(fptr->pathv); 01015 } 01016 return stat_new(&st); 01017 #else 01018 return rb_io_stat(obj); 01019 #endif 01020 } 01021 01022 static int 01023 rb_group_member(GETGROUPS_T gid) 01024 { 01025 #ifdef _WIN32 01026 return FALSE; 01027 #else 01028 int rv = FALSE; 01029 int groups = 16; 01030 VALUE v = 0; 01031 GETGROUPS_T *gary; 01032 int anum; 01033 01034 if (getgid() == gid || getegid() == gid) 01035 return TRUE; 01036 01037 /* 01038 * On Mac OS X (Mountain Lion), NGROUPS is 16. But libc and kernel 01039 * accept more larger value. 01040 * So we don't trunk NGROUPS anymore. 01041 */ 01042 while (groups <= RB_MAX_GROUPS) { 01043 gary = ALLOCV_N(GETGROUPS_T, v, groups); 01044 anum = getgroups(groups, gary); 01045 if (anum != -1 && anum != groups) 01046 break; 01047 groups *= 2; 01048 if (v) { 01049 ALLOCV_END(v); 01050 v = 0; 01051 } 01052 } 01053 if (anum == -1) 01054 return FALSE; 01055 01056 while (--anum >= 0) { 01057 if (gary[anum] == gid) { 01058 rv = TRUE; 01059 break; 01060 } 01061 } 01062 if (v) 01063 ALLOCV_END(v); 01064 01065 return rv; 01066 #endif 01067 } 01068 01069 #ifndef S_IXUGO 01070 # define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH) 01071 #endif 01072 01073 #if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__) 01074 #define USE_GETEUID 1 01075 #endif 01076 01077 #ifndef HAVE_EACCESS 01078 int 01079 eaccess(const char *path, int mode) 01080 { 01081 #ifdef USE_GETEUID 01082 struct stat st; 01083 rb_uid_t euid; 01084 01085 if (STAT(path, &st) < 0) 01086 return -1; 01087 01088 euid = geteuid(); 01089 01090 if (euid == 0) { 01091 /* Root can read or write any file. */ 01092 if (!(mode & X_OK)) 01093 return 0; 01094 01095 /* Root can execute any file that has any one of the execute 01096 bits set. */ 01097 if (st.st_mode & S_IXUGO) 01098 return 0; 01099 01100 return -1; 01101 } 01102 01103 if (st.st_uid == euid) /* owner */ 01104 mode <<= 6; 01105 else if (rb_group_member(st.st_gid)) 01106 mode <<= 3; 01107 01108 if ((int)(st.st_mode & mode) == mode) return 0; 01109 01110 return -1; 01111 #else 01112 return access(path, mode); 01113 #endif 01114 } 01115 #endif 01116 01117 01118 /* 01119 * Document-class: FileTest 01120 * 01121 * <code>FileTest</code> implements file test operations similar to 01122 * those used in <code>File::Stat</code>. It exists as a standalone 01123 * module, and its methods are also insinuated into the <code>File</code> 01124 * class. (Note that this is not done by inclusion: the interpreter cheats). 01125 * 01126 */ 01127 01128 /* 01129 * Document-method: directory? 01130 * 01131 * call-seq: 01132 * File.directory?(file_name) -> true or false 01133 * 01134 * Returns <code>true</code> if the named file is a directory, 01135 * or a symlink that points at a directory, and <code>false</code> 01136 * otherwise. 01137 * 01138 * _file_name_ can be an IO object. 01139 * 01140 * File.directory?(".") 01141 */ 01142 01143 VALUE 01144 rb_file_directory_p(VALUE obj, VALUE fname) 01145 { 01146 #ifndef S_ISDIR 01147 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) 01148 #endif 01149 01150 struct stat st; 01151 01152 if (rb_stat(fname, &st) < 0) return Qfalse; 01153 if (S_ISDIR(st.st_mode)) return Qtrue; 01154 return Qfalse; 01155 } 01156 01157 /* 01158 * call-seq: 01159 * File.pipe?(file_name) -> true or false 01160 * 01161 * Returns <code>true</code> if the named file is a pipe. 01162 * 01163 * _file_name_ can be an IO object. 01164 */ 01165 01166 static VALUE 01167 rb_file_pipe_p(VALUE obj, VALUE fname) 01168 { 01169 #ifdef S_IFIFO 01170 # ifndef S_ISFIFO 01171 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) 01172 # endif 01173 01174 struct stat st; 01175 01176 if (rb_stat(fname, &st) < 0) return Qfalse; 01177 if (S_ISFIFO(st.st_mode)) return Qtrue; 01178 01179 #endif 01180 return Qfalse; 01181 } 01182 01183 /* 01184 * call-seq: 01185 * File.symlink?(file_name) -> true or false 01186 * 01187 * Returns <code>true</code> if the named file is a symbolic link. 01188 */ 01189 01190 static VALUE 01191 rb_file_symlink_p(VALUE obj, VALUE fname) 01192 { 01193 #ifndef S_ISLNK 01194 # ifdef _S_ISLNK 01195 # define S_ISLNK(m) _S_ISLNK(m) 01196 # else 01197 # ifdef _S_IFLNK 01198 # define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK) 01199 # else 01200 # ifdef S_IFLNK 01201 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) 01202 # endif 01203 # endif 01204 # endif 01205 #endif 01206 01207 #ifdef S_ISLNK 01208 struct stat st; 01209 01210 rb_secure(2); 01211 FilePathValue(fname); 01212 fname = rb_str_encode_ospath(fname); 01213 if (lstat(StringValueCStr(fname), &st) < 0) return Qfalse; 01214 if (S_ISLNK(st.st_mode)) return Qtrue; 01215 #endif 01216 01217 return Qfalse; 01218 } 01219 01220 /* 01221 * call-seq: 01222 * File.socket?(file_name) -> true or false 01223 * 01224 * Returns <code>true</code> if the named file is a socket. 01225 * 01226 * _file_name_ can be an IO object. 01227 */ 01228 01229 static VALUE 01230 rb_file_socket_p(VALUE obj, VALUE fname) 01231 { 01232 #ifndef S_ISSOCK 01233 # ifdef _S_ISSOCK 01234 # define S_ISSOCK(m) _S_ISSOCK(m) 01235 # else 01236 # ifdef _S_IFSOCK 01237 # define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK) 01238 # else 01239 # ifdef S_IFSOCK 01240 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) 01241 # endif 01242 # endif 01243 # endif 01244 #endif 01245 01246 #ifdef S_ISSOCK 01247 struct stat st; 01248 01249 if (rb_stat(fname, &st) < 0) return Qfalse; 01250 if (S_ISSOCK(st.st_mode)) return Qtrue; 01251 01252 #endif 01253 return Qfalse; 01254 } 01255 01256 /* 01257 * call-seq: 01258 * File.blockdev?(file_name) -> true or false 01259 * 01260 * Returns <code>true</code> if the named file is a block device. 01261 * 01262 * _file_name_ can be an IO object. 01263 */ 01264 01265 static VALUE 01266 rb_file_blockdev_p(VALUE obj, VALUE fname) 01267 { 01268 #ifndef S_ISBLK 01269 # ifdef S_IFBLK 01270 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) 01271 # else 01272 # define S_ISBLK(m) (0) /* anytime false */ 01273 # endif 01274 #endif 01275 01276 #ifdef S_ISBLK 01277 struct stat st; 01278 01279 if (rb_stat(fname, &st) < 0) return Qfalse; 01280 if (S_ISBLK(st.st_mode)) return Qtrue; 01281 01282 #endif 01283 return Qfalse; 01284 } 01285 01286 /* 01287 * call-seq: 01288 * File.chardev?(file_name) -> true or false 01289 * 01290 * Returns <code>true</code> if the named file is a character device. 01291 * 01292 * _file_name_ can be an IO object. 01293 */ 01294 static VALUE 01295 rb_file_chardev_p(VALUE obj, VALUE fname) 01296 { 01297 #ifndef S_ISCHR 01298 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) 01299 #endif 01300 01301 struct stat st; 01302 01303 if (rb_stat(fname, &st) < 0) return Qfalse; 01304 if (S_ISCHR(st.st_mode)) return Qtrue; 01305 01306 return Qfalse; 01307 } 01308 01309 /* 01310 * call-seq: 01311 * File.exist?(file_name) -> true or false 01312 * File.exists?(file_name) -> true or false 01313 * 01314 * Return <code>true</code> if the named file exists. 01315 * 01316 * _file_name_ can be an IO object. 01317 * 01318 * "file exists" means that stat() or fstat() system call is successful. 01319 */ 01320 01321 static VALUE 01322 rb_file_exist_p(VALUE obj, VALUE fname) 01323 { 01324 struct stat st; 01325 01326 if (rb_stat(fname, &st) < 0) return Qfalse; 01327 return Qtrue; 01328 } 01329 01330 /* 01331 * call-seq: 01332 * File.readable?(file_name) -> true or false 01333 * 01334 * Returns <code>true</code> if the named file is readable by the effective 01335 * user id of this process. 01336 */ 01337 01338 static VALUE 01339 rb_file_readable_p(VALUE obj, VALUE fname) 01340 { 01341 rb_secure(2); 01342 FilePathValue(fname); 01343 fname = rb_str_encode_ospath(fname); 01344 if (eaccess(StringValueCStr(fname), R_OK) < 0) return Qfalse; 01345 return Qtrue; 01346 } 01347 01348 /* 01349 * call-seq: 01350 * File.readable_real?(file_name) -> true or false 01351 * 01352 * Returns <code>true</code> if the named file is readable by the real 01353 * user id of this process. 01354 */ 01355 01356 static VALUE 01357 rb_file_readable_real_p(VALUE obj, VALUE fname) 01358 { 01359 rb_secure(2); 01360 FilePathValue(fname); 01361 fname = rb_str_encode_ospath(fname); 01362 if (access(StringValueCStr(fname), R_OK) < 0) return Qfalse; 01363 return Qtrue; 01364 } 01365 01366 #ifndef S_IRUGO 01367 # define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH) 01368 #endif 01369 01370 #ifndef S_IWUGO 01371 # define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH) 01372 #endif 01373 01374 /* 01375 * call-seq: 01376 * File.world_readable?(file_name) -> fixnum or nil 01377 * 01378 * If <i>file_name</i> is readable by others, returns an integer 01379 * representing the file permission bits of <i>file_name</i>. Returns 01380 * <code>nil</code> otherwise. The meaning of the bits is platform 01381 * dependent; on Unix systems, see <code>stat(2)</code>. 01382 * 01383 * _file_name_ can be an IO object. 01384 * 01385 * File.world_readable?("/etc/passwd") #=> 420 01386 * m = File.world_readable?("/etc/passwd") 01387 * sprintf("%o", m) #=> "644" 01388 */ 01389 01390 static VALUE 01391 rb_file_world_readable_p(VALUE obj, VALUE fname) 01392 { 01393 #ifdef S_IROTH 01394 struct stat st; 01395 01396 if (rb_stat(fname, &st) < 0) return Qnil; 01397 if ((st.st_mode & (S_IROTH)) == S_IROTH) { 01398 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO)); 01399 } 01400 #endif 01401 return Qnil; 01402 } 01403 01404 /* 01405 * call-seq: 01406 * File.writable?(file_name) -> true or false 01407 * 01408 * Returns <code>true</code> if the named file is writable by the effective 01409 * user id of this process. 01410 */ 01411 01412 static VALUE 01413 rb_file_writable_p(VALUE obj, VALUE fname) 01414 { 01415 rb_secure(2); 01416 FilePathValue(fname); 01417 fname = rb_str_encode_ospath(fname); 01418 if (eaccess(StringValueCStr(fname), W_OK) < 0) return Qfalse; 01419 return Qtrue; 01420 } 01421 01422 /* 01423 * call-seq: 01424 * File.writable_real?(file_name) -> true or false 01425 * 01426 * Returns <code>true</code> if the named file is writable by the real 01427 * user id of this process. 01428 */ 01429 01430 static VALUE 01431 rb_file_writable_real_p(VALUE obj, VALUE fname) 01432 { 01433 rb_secure(2); 01434 FilePathValue(fname); 01435 fname = rb_str_encode_ospath(fname); 01436 if (access(StringValueCStr(fname), W_OK) < 0) return Qfalse; 01437 return Qtrue; 01438 } 01439 01440 /* 01441 * call-seq: 01442 * File.world_writable?(file_name) -> fixnum or nil 01443 * 01444 * If <i>file_name</i> is writable by others, returns an integer 01445 * representing the file permission bits of <i>file_name</i>. Returns 01446 * <code>nil</code> otherwise. The meaning of the bits is platform 01447 * dependent; on Unix systems, see <code>stat(2)</code>. 01448 * 01449 * _file_name_ can be an IO object. 01450 * 01451 * File.world_writable?("/tmp") #=> 511 01452 * m = File.world_writable?("/tmp") 01453 * sprintf("%o", m) #=> "777" 01454 */ 01455 01456 static VALUE 01457 rb_file_world_writable_p(VALUE obj, VALUE fname) 01458 { 01459 #ifdef S_IWOTH 01460 struct stat st; 01461 01462 if (rb_stat(fname, &st) < 0) return Qnil; 01463 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) { 01464 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO)); 01465 } 01466 #endif 01467 return Qnil; 01468 } 01469 01470 /* 01471 * call-seq: 01472 * File.executable?(file_name) -> true or false 01473 * 01474 * Returns <code>true</code> if the named file is executable by the effective 01475 * user id of this process. 01476 */ 01477 01478 static VALUE 01479 rb_file_executable_p(VALUE obj, VALUE fname) 01480 { 01481 rb_secure(2); 01482 FilePathValue(fname); 01483 fname = rb_str_encode_ospath(fname); 01484 if (eaccess(StringValueCStr(fname), X_OK) < 0) return Qfalse; 01485 return Qtrue; 01486 } 01487 01488 /* 01489 * call-seq: 01490 * File.executable_real?(file_name) -> true or false 01491 * 01492 * Returns <code>true</code> if the named file is executable by the real 01493 * user id of this process. 01494 */ 01495 01496 static VALUE 01497 rb_file_executable_real_p(VALUE obj, VALUE fname) 01498 { 01499 rb_secure(2); 01500 FilePathValue(fname); 01501 fname = rb_str_encode_ospath(fname); 01502 if (access(StringValueCStr(fname), X_OK) < 0) return Qfalse; 01503 return Qtrue; 01504 } 01505 01506 #ifndef S_ISREG 01507 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) 01508 #endif 01509 01510 /* 01511 * call-seq: 01512 * File.file?(file_name) -> true or false 01513 * 01514 * Returns <code>true</code> if the named file exists and is a 01515 * regular file. 01516 * 01517 * _file_name_ can be an IO object. 01518 */ 01519 01520 static VALUE 01521 rb_file_file_p(VALUE obj, VALUE fname) 01522 { 01523 struct stat st; 01524 01525 if (rb_stat(fname, &st) < 0) return Qfalse; 01526 if (S_ISREG(st.st_mode)) return Qtrue; 01527 return Qfalse; 01528 } 01529 01530 /* 01531 * call-seq: 01532 * File.zero?(file_name) -> true or false 01533 * 01534 * Returns <code>true</code> if the named file exists and has 01535 * a zero size. 01536 * 01537 * _file_name_ can be an IO object. 01538 */ 01539 01540 static VALUE 01541 rb_file_zero_p(VALUE obj, VALUE fname) 01542 { 01543 struct stat st; 01544 01545 if (rb_stat(fname, &st) < 0) return Qfalse; 01546 if (st.st_size == 0) return Qtrue; 01547 return Qfalse; 01548 } 01549 01550 /* 01551 * call-seq: 01552 * File.size?(file_name) -> Integer or nil 01553 * 01554 * Returns +nil+ if +file_name+ doesn't exist or has zero size, the size of the 01555 * file otherwise. 01556 * 01557 * _file_name_ can be an IO object. 01558 */ 01559 01560 static VALUE 01561 rb_file_size_p(VALUE obj, VALUE fname) 01562 { 01563 struct stat st; 01564 01565 if (rb_stat(fname, &st) < 0) return Qnil; 01566 if (st.st_size == 0) return Qnil; 01567 return OFFT2NUM(st.st_size); 01568 } 01569 01570 /* 01571 * call-seq: 01572 * File.owned?(file_name) -> true or false 01573 * 01574 * Returns <code>true</code> if the named file exists and the 01575 * effective used id of the calling process is the owner of 01576 * the file. 01577 * 01578 * _file_name_ can be an IO object. 01579 */ 01580 01581 static VALUE 01582 rb_file_owned_p(VALUE obj, VALUE fname) 01583 { 01584 struct stat st; 01585 01586 if (rb_stat(fname, &st) < 0) return Qfalse; 01587 if (st.st_uid == geteuid()) return Qtrue; 01588 return Qfalse; 01589 } 01590 01591 static VALUE 01592 rb_file_rowned_p(VALUE obj, VALUE fname) 01593 { 01594 struct stat st; 01595 01596 if (rb_stat(fname, &st) < 0) return Qfalse; 01597 if (st.st_uid == getuid()) return Qtrue; 01598 return Qfalse; 01599 } 01600 01601 /* 01602 * call-seq: 01603 * File.grpowned?(file_name) -> true or false 01604 * 01605 * Returns <code>true</code> if the named file exists and the 01606 * effective group id of the calling process is the owner of 01607 * the file. Returns <code>false</code> on Windows. 01608 * 01609 * _file_name_ can be an IO object. 01610 */ 01611 01612 static VALUE 01613 rb_file_grpowned_p(VALUE obj, VALUE fname) 01614 { 01615 #ifndef _WIN32 01616 struct stat st; 01617 01618 if (rb_stat(fname, &st) < 0) return Qfalse; 01619 if (rb_group_member(st.st_gid)) return Qtrue; 01620 #endif 01621 return Qfalse; 01622 } 01623 01624 #if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX) 01625 static VALUE 01626 check3rdbyte(VALUE fname, int mode) 01627 { 01628 struct stat st; 01629 01630 rb_secure(2); 01631 FilePathValue(fname); 01632 fname = rb_str_encode_ospath(fname); 01633 if (STAT(StringValueCStr(fname), &st) < 0) return Qfalse; 01634 if (st.st_mode & mode) return Qtrue; 01635 return Qfalse; 01636 } 01637 #endif 01638 01639 /* 01640 * call-seq: 01641 * File.setuid?(file_name) -> true or false 01642 * 01643 * Returns <code>true</code> if the named file has the setuid bit set. 01644 */ 01645 01646 static VALUE 01647 rb_file_suid_p(VALUE obj, VALUE fname) 01648 { 01649 #ifdef S_ISUID 01650 return check3rdbyte(fname, S_ISUID); 01651 #else 01652 return Qfalse; 01653 #endif 01654 } 01655 01656 /* 01657 * call-seq: 01658 * File.setgid?(file_name) -> true or false 01659 * 01660 * Returns <code>true</code> if the named file has the setgid bit set. 01661 */ 01662 01663 static VALUE 01664 rb_file_sgid_p(VALUE obj, VALUE fname) 01665 { 01666 #ifdef S_ISGID 01667 return check3rdbyte(fname, S_ISGID); 01668 #else 01669 return Qfalse; 01670 #endif 01671 } 01672 01673 /* 01674 * call-seq: 01675 * File.sticky?(file_name) -> true or false 01676 * 01677 * Returns <code>true</code> if the named file has the sticky bit set. 01678 */ 01679 01680 static VALUE 01681 rb_file_sticky_p(VALUE obj, VALUE fname) 01682 { 01683 #ifdef S_ISVTX 01684 return check3rdbyte(fname, S_ISVTX); 01685 #else 01686 return Qnil; 01687 #endif 01688 } 01689 01690 /* 01691 * call-seq: 01692 * File.identical?(file_1, file_2) -> true or false 01693 * 01694 * Returns <code>true</code> if the named files are identical. 01695 * 01696 * _file_1_ and _file_2_ can be an IO object. 01697 * 01698 * open("a", "w") {} 01699 * p File.identical?("a", "a") #=> true 01700 * p File.identical?("a", "./a") #=> true 01701 * File.link("a", "b") 01702 * p File.identical?("a", "b") #=> true 01703 * File.symlink("a", "c") 01704 * p File.identical?("a", "c") #=> true 01705 * open("d", "w") {} 01706 * p File.identical?("a", "d") #=> false 01707 */ 01708 01709 static VALUE 01710 rb_file_identical_p(VALUE obj, VALUE fname1, VALUE fname2) 01711 { 01712 #ifndef DOSISH 01713 struct stat st1, st2; 01714 01715 if (rb_stat(fname1, &st1) < 0) return Qfalse; 01716 if (rb_stat(fname2, &st2) < 0) return Qfalse; 01717 if (st1.st_dev != st2.st_dev) return Qfalse; 01718 if (st1.st_ino != st2.st_ino) return Qfalse; 01719 #else 01720 # ifdef _WIN32 01721 BY_HANDLE_FILE_INFORMATION st1, st2; 01722 HANDLE f1 = 0, f2 = 0; 01723 # endif 01724 01725 rb_secure(2); 01726 # ifdef _WIN32 01727 f1 = w32_io_info(&fname1, &st1); 01728 if (f1 == INVALID_HANDLE_VALUE) return Qfalse; 01729 f2 = w32_io_info(&fname2, &st2); 01730 if (f1) CloseHandle(f1); 01731 if (f2 == INVALID_HANDLE_VALUE) return Qfalse; 01732 if (f2) CloseHandle(f2); 01733 01734 if (st1.dwVolumeSerialNumber == st2.dwVolumeSerialNumber && 01735 st1.nFileIndexHigh == st2.nFileIndexHigh && 01736 st1.nFileIndexLow == st2.nFileIndexLow) 01737 return Qtrue; 01738 if (!f1 || !f2) return Qfalse; 01739 # else 01740 FilePathValue(fname1); 01741 fname1 = rb_str_new4(fname1); 01742 fname1 = rb_str_encode_ospath(fname1); 01743 FilePathValue(fname2); 01744 fname2 = rb_str_encode_ospath(fname2); 01745 if (access(RSTRING_PTR(fname1), 0)) return Qfalse; 01746 if (access(RSTRING_PTR(fname2), 0)) return Qfalse; 01747 # endif 01748 fname1 = rb_file_expand_path(fname1, Qnil); 01749 fname2 = rb_file_expand_path(fname2, Qnil); 01750 if (RSTRING_LEN(fname1) != RSTRING_LEN(fname2)) return Qfalse; 01751 if (rb_memcicmp(RSTRING_PTR(fname1), RSTRING_PTR(fname2), RSTRING_LEN(fname1))) 01752 return Qfalse; 01753 #endif 01754 return Qtrue; 01755 } 01756 01757 /* 01758 * call-seq: 01759 * File.size(file_name) -> integer 01760 * 01761 * Returns the size of <code>file_name</code>. 01762 * 01763 * _file_name_ can be an IO object. 01764 */ 01765 01766 static VALUE 01767 rb_file_s_size(VALUE klass, VALUE fname) 01768 { 01769 struct stat st; 01770 01771 if (rb_stat(fname, &st) < 0) { 01772 FilePathValue(fname); 01773 rb_sys_fail_path(fname); 01774 } 01775 return OFFT2NUM(st.st_size); 01776 } 01777 01778 static VALUE 01779 rb_file_ftype(const struct stat *st) 01780 { 01781 const char *t; 01782 01783 if (S_ISREG(st->st_mode)) { 01784 t = "file"; 01785 } 01786 else if (S_ISDIR(st->st_mode)) { 01787 t = "directory"; 01788 } 01789 else if (S_ISCHR(st->st_mode)) { 01790 t = "characterSpecial"; 01791 } 01792 #ifdef S_ISBLK 01793 else if (S_ISBLK(st->st_mode)) { 01794 t = "blockSpecial"; 01795 } 01796 #endif 01797 #ifdef S_ISFIFO 01798 else if (S_ISFIFO(st->st_mode)) { 01799 t = "fifo"; 01800 } 01801 #endif 01802 #ifdef S_ISLNK 01803 else if (S_ISLNK(st->st_mode)) { 01804 t = "link"; 01805 } 01806 #endif 01807 #ifdef S_ISSOCK 01808 else if (S_ISSOCK(st->st_mode)) { 01809 t = "socket"; 01810 } 01811 #endif 01812 else { 01813 t = "unknown"; 01814 } 01815 01816 return rb_usascii_str_new2(t); 01817 } 01818 01819 /* 01820 * call-seq: 01821 * File.ftype(file_name) -> string 01822 * 01823 * Identifies the type of the named file; the return string is one of 01824 * ``<code>file</code>'', ``<code>directory</code>'', 01825 * ``<code>characterSpecial</code>'', ``<code>blockSpecial</code>'', 01826 * ``<code>fifo</code>'', ``<code>link</code>'', 01827 * ``<code>socket</code>'', or ``<code>unknown</code>''. 01828 * 01829 * File.ftype("testfile") #=> "file" 01830 * File.ftype("/dev/tty") #=> "characterSpecial" 01831 * File.ftype("/tmp/.X11-unix/X0") #=> "socket" 01832 */ 01833 01834 static VALUE 01835 rb_file_s_ftype(VALUE klass, VALUE fname) 01836 { 01837 struct stat st; 01838 01839 rb_secure(2); 01840 FilePathValue(fname); 01841 fname = rb_str_encode_ospath(fname); 01842 if (lstat(StringValueCStr(fname), &st) == -1) { 01843 rb_sys_fail_path(fname); 01844 } 01845 01846 return rb_file_ftype(&st); 01847 } 01848 01849 /* 01850 * call-seq: 01851 * File.atime(file_name) -> time 01852 * 01853 * Returns the last access time for the named file as a Time object). 01854 * 01855 * _file_name_ can be an IO object. 01856 * 01857 * File.atime("testfile") #=> Wed Apr 09 08:51:48 CDT 2003 01858 * 01859 */ 01860 01861 static VALUE 01862 rb_file_s_atime(VALUE klass, VALUE fname) 01863 { 01864 struct stat st; 01865 01866 if (rb_stat(fname, &st) < 0) { 01867 FilePathValue(fname); 01868 rb_sys_fail_path(fname); 01869 } 01870 return stat_atime(&st); 01871 } 01872 01873 /* 01874 * call-seq: 01875 * file.atime -> time 01876 * 01877 * Returns the last access time (a <code>Time</code> object) 01878 * for <i>file</i>, or epoch if <i>file</i> has not been accessed. 01879 * 01880 * File.new("testfile").atime #=> Wed Dec 31 18:00:00 CST 1969 01881 * 01882 */ 01883 01884 static VALUE 01885 rb_file_atime(VALUE obj) 01886 { 01887 rb_io_t *fptr; 01888 struct stat st; 01889 01890 GetOpenFile(obj, fptr); 01891 if (fstat(fptr->fd, &st) == -1) { 01892 rb_sys_fail_path(fptr->pathv); 01893 } 01894 return stat_atime(&st); 01895 } 01896 01897 /* 01898 * call-seq: 01899 * File.mtime(file_name) -> time 01900 * 01901 * Returns the modification time for the named file as a Time object. 01902 * 01903 * _file_name_ can be an IO object. 01904 * 01905 * File.mtime("testfile") #=> Tue Apr 08 12:58:04 CDT 2003 01906 * 01907 */ 01908 01909 static VALUE 01910 rb_file_s_mtime(VALUE klass, VALUE fname) 01911 { 01912 struct stat st; 01913 01914 if (rb_stat(fname, &st) < 0) { 01915 FilePathValue(fname); 01916 rb_sys_fail_path(fname); 01917 } 01918 return stat_mtime(&st); 01919 } 01920 01921 /* 01922 * call-seq: 01923 * file.mtime -> time 01924 * 01925 * Returns the modification time for <i>file</i>. 01926 * 01927 * File.new("testfile").mtime #=> Wed Apr 09 08:53:14 CDT 2003 01928 * 01929 */ 01930 01931 static VALUE 01932 rb_file_mtime(VALUE obj) 01933 { 01934 rb_io_t *fptr; 01935 struct stat st; 01936 01937 GetOpenFile(obj, fptr); 01938 if (fstat(fptr->fd, &st) == -1) { 01939 rb_sys_fail_path(fptr->pathv); 01940 } 01941 return stat_mtime(&st); 01942 } 01943 01944 /* 01945 * call-seq: 01946 * File.ctime(file_name) -> time 01947 * 01948 * Returns the change time for the named file (the time at which 01949 * directory information about the file was changed, not the file 01950 * itself). 01951 * 01952 * _file_name_ can be an IO object. 01953 * 01954 * Note that on Windows (NTFS), returns creation time (birth time). 01955 * 01956 * File.ctime("testfile") #=> Wed Apr 09 08:53:13 CDT 2003 01957 * 01958 */ 01959 01960 static VALUE 01961 rb_file_s_ctime(VALUE klass, VALUE fname) 01962 { 01963 struct stat st; 01964 01965 if (rb_stat(fname, &st) < 0) { 01966 FilePathValue(fname); 01967 rb_sys_fail_path(fname); 01968 } 01969 return stat_ctime(&st); 01970 } 01971 01972 /* 01973 * call-seq: 01974 * file.ctime -> time 01975 * 01976 * Returns the change time for <i>file</i> (that is, the time directory 01977 * information about the file was changed, not the file itself). 01978 * 01979 * Note that on Windows (NTFS), returns creation time (birth time). 01980 * 01981 * File.new("testfile").ctime #=> Wed Apr 09 08:53:14 CDT 2003 01982 * 01983 */ 01984 01985 static VALUE 01986 rb_file_ctime(VALUE obj) 01987 { 01988 rb_io_t *fptr; 01989 struct stat st; 01990 01991 GetOpenFile(obj, fptr); 01992 if (fstat(fptr->fd, &st) == -1) { 01993 rb_sys_fail_path(fptr->pathv); 01994 } 01995 return stat_ctime(&st); 01996 } 01997 01998 /* 01999 * call-seq: 02000 * file.size -> integer 02001 * 02002 * Returns the size of <i>file</i> in bytes. 02003 * 02004 * File.new("testfile").size #=> 66 02005 * 02006 */ 02007 02008 static VALUE 02009 rb_file_size(VALUE obj) 02010 { 02011 rb_io_t *fptr; 02012 struct stat st; 02013 02014 GetOpenFile(obj, fptr); 02015 if (fptr->mode & FMODE_WRITABLE) { 02016 rb_io_flush(obj); 02017 } 02018 if (fstat(fptr->fd, &st) == -1) { 02019 rb_sys_fail_path(fptr->pathv); 02020 } 02021 return OFFT2NUM(st.st_size); 02022 } 02023 02024 static void 02025 chmod_internal(const char *path, VALUE pathv, void *mode) 02026 { 02027 if (chmod(path, *(int *)mode) < 0) 02028 rb_sys_fail_path(pathv); 02029 } 02030 02031 /* 02032 * call-seq: 02033 * File.chmod(mode_int, file_name, ... ) -> integer 02034 * 02035 * Changes permission bits on the named file(s) to the bit pattern 02036 * represented by <i>mode_int</i>. Actual effects are operating system 02037 * dependent (see the beginning of this section). On Unix systems, see 02038 * <code>chmod(2)</code> for details. Returns the number of files 02039 * processed. 02040 * 02041 * File.chmod(0644, "testfile", "out") #=> 2 02042 */ 02043 02044 static VALUE 02045 rb_file_s_chmod(int argc, VALUE *argv) 02046 { 02047 VALUE vmode; 02048 VALUE rest; 02049 int mode; 02050 long n; 02051 02052 rb_secure(2); 02053 rb_scan_args(argc, argv, "1*", &vmode, &rest); 02054 mode = NUM2INT(vmode); 02055 02056 n = apply2files(chmod_internal, rest, &mode); 02057 return LONG2FIX(n); 02058 } 02059 02060 /* 02061 * call-seq: 02062 * file.chmod(mode_int) -> 0 02063 * 02064 * Changes permission bits on <i>file</i> to the bit pattern 02065 * represented by <i>mode_int</i>. Actual effects are platform 02066 * dependent; on Unix systems, see <code>chmod(2)</code> for details. 02067 * Follows symbolic links. Also see <code>File#lchmod</code>. 02068 * 02069 * f = File.new("out", "w"); 02070 * f.chmod(0644) #=> 0 02071 */ 02072 02073 static VALUE 02074 rb_file_chmod(VALUE obj, VALUE vmode) 02075 { 02076 rb_io_t *fptr; 02077 int mode; 02078 #ifndef HAVE_FCHMOD 02079 VALUE path; 02080 #endif 02081 02082 rb_secure(2); 02083 mode = NUM2INT(vmode); 02084 02085 GetOpenFile(obj, fptr); 02086 #ifdef HAVE_FCHMOD 02087 if (fchmod(fptr->fd, mode) == -1) 02088 rb_sys_fail_path(fptr->pathv); 02089 #else 02090 if (NIL_P(fptr->pathv)) return Qnil; 02091 path = rb_str_encode_ospath(fptr->pathv); 02092 if (chmod(RSTRING_PTR(path), mode) == -1) 02093 rb_sys_fail_path(fptr->pathv); 02094 #endif 02095 02096 return INT2FIX(0); 02097 } 02098 02099 #if defined(HAVE_LCHMOD) 02100 static void 02101 lchmod_internal(const char *path, VALUE pathv, void *mode) 02102 { 02103 if (lchmod(path, (int)(VALUE)mode) < 0) 02104 rb_sys_fail_path(pathv); 02105 } 02106 02107 /* 02108 * call-seq: 02109 * File.lchmod(mode_int, file_name, ...) -> integer 02110 * 02111 * Equivalent to <code>File::chmod</code>, but does not follow symbolic 02112 * links (so it will change the permissions associated with the link, 02113 * not the file referenced by the link). Often not available. 02114 * 02115 */ 02116 02117 static VALUE 02118 rb_file_s_lchmod(int argc, VALUE *argv) 02119 { 02120 VALUE vmode; 02121 VALUE rest; 02122 long mode, n; 02123 02124 rb_secure(2); 02125 rb_scan_args(argc, argv, "1*", &vmode, &rest); 02126 mode = NUM2INT(vmode); 02127 02128 n = apply2files(lchmod_internal, rest, (void *)(long)mode); 02129 return LONG2FIX(n); 02130 } 02131 #else 02132 #define rb_file_s_lchmod rb_f_notimplement 02133 #endif 02134 02135 struct chown_args { 02136 rb_uid_t owner; 02137 rb_gid_t group; 02138 }; 02139 02140 static void 02141 chown_internal(const char *path, VALUE pathv, void *arg) 02142 { 02143 struct chown_args *args = arg; 02144 if (chown(path, args->owner, args->group) < 0) 02145 rb_sys_fail_path(pathv); 02146 } 02147 02148 /* 02149 * call-seq: 02150 * File.chown(owner_int, group_int, file_name,... ) -> integer 02151 * 02152 * Changes the owner and group of the named file(s) to the given 02153 * numeric owner and group id's. Only a process with superuser 02154 * privileges may change the owner of a file. The current owner of a 02155 * file may change the file's group to any group to which the owner 02156 * belongs. A <code>nil</code> or -1 owner or group id is ignored. 02157 * Returns the number of files processed. 02158 * 02159 * File.chown(nil, 100, "testfile") 02160 * 02161 */ 02162 02163 static VALUE 02164 rb_file_s_chown(int argc, VALUE *argv) 02165 { 02166 VALUE o, g, rest; 02167 struct chown_args arg; 02168 long n; 02169 02170 rb_secure(2); 02171 rb_scan_args(argc, argv, "2*", &o, &g, &rest); 02172 if (NIL_P(o)) { 02173 arg.owner = -1; 02174 } 02175 else { 02176 arg.owner = NUM2UIDT(o); 02177 } 02178 if (NIL_P(g)) { 02179 arg.group = -1; 02180 } 02181 else { 02182 arg.group = NUM2GIDT(g); 02183 } 02184 02185 n = apply2files(chown_internal, rest, &arg); 02186 return LONG2FIX(n); 02187 } 02188 02189 /* 02190 * call-seq: 02191 * file.chown(owner_int, group_int ) -> 0 02192 * 02193 * Changes the owner and group of <i>file</i> to the given numeric 02194 * owner and group id's. Only a process with superuser privileges may 02195 * change the owner of a file. The current owner of a file may change 02196 * the file's group to any group to which the owner belongs. A 02197 * <code>nil</code> or -1 owner or group id is ignored. Follows 02198 * symbolic links. See also <code>File#lchown</code>. 02199 * 02200 * File.new("testfile").chown(502, 1000) 02201 * 02202 */ 02203 02204 static VALUE 02205 rb_file_chown(VALUE obj, VALUE owner, VALUE group) 02206 { 02207 rb_io_t *fptr; 02208 int o, g; 02209 #ifndef HAVE_FCHOWN 02210 VALUE path; 02211 #endif 02212 02213 rb_secure(2); 02214 o = NIL_P(owner) ? -1 : NUM2INT(owner); 02215 g = NIL_P(group) ? -1 : NUM2INT(group); 02216 GetOpenFile(obj, fptr); 02217 #ifndef HAVE_FCHOWN 02218 if (NIL_P(fptr->pathv)) return Qnil; 02219 path = rb_str_encode_ospath(fptr->pathv); 02220 if (chown(RSTRING_PTR(path), o, g) == -1) 02221 rb_sys_fail_path(fptr->pathv); 02222 #else 02223 if (fchown(fptr->fd, o, g) == -1) 02224 rb_sys_fail_path(fptr->pathv); 02225 #endif 02226 02227 return INT2FIX(0); 02228 } 02229 02230 #if defined(HAVE_LCHOWN) 02231 static void 02232 lchown_internal(const char *path, VALUE pathv, void *arg) 02233 { 02234 struct chown_args *args = arg; 02235 if (lchown(path, args->owner, args->group) < 0) 02236 rb_sys_fail_path(pathv); 02237 } 02238 02239 /* 02240 * call-seq: 02241 * File.lchown(owner_int, group_int, file_name,..) -> integer 02242 * 02243 * Equivalent to <code>File::chown</code>, but does not follow symbolic 02244 * links (so it will change the owner associated with the link, not the 02245 * file referenced by the link). Often not available. Returns number 02246 * of files in the argument list. 02247 * 02248 */ 02249 02250 static VALUE 02251 rb_file_s_lchown(int argc, VALUE *argv) 02252 { 02253 VALUE o, g, rest; 02254 struct chown_args arg; 02255 long n; 02256 02257 rb_secure(2); 02258 rb_scan_args(argc, argv, "2*", &o, &g, &rest); 02259 if (NIL_P(o)) { 02260 arg.owner = -1; 02261 } 02262 else { 02263 arg.owner = NUM2UIDT(o); 02264 } 02265 if (NIL_P(g)) { 02266 arg.group = -1; 02267 } 02268 else { 02269 arg.group = NUM2GIDT(g); 02270 } 02271 02272 n = apply2files(lchown_internal, rest, &arg); 02273 return LONG2FIX(n); 02274 } 02275 #else 02276 #define rb_file_s_lchown rb_f_notimplement 02277 #endif 02278 02279 struct utime_args { 02280 const struct timespec* tsp; 02281 VALUE atime, mtime; 02282 }; 02283 02284 #if defined DOSISH || defined __CYGWIN__ 02285 NORETURN(static void utime_failed(VALUE, const struct timespec *, VALUE, VALUE)); 02286 02287 static void 02288 utime_failed(VALUE path, const struct timespec *tsp, VALUE atime, VALUE mtime) 02289 { 02290 if (tsp && errno == EINVAL) { 02291 VALUE e[2], a = Qnil, m = Qnil; 02292 int d = 0; 02293 if (!NIL_P(atime)) { 02294 a = rb_inspect(atime); 02295 } 02296 if (!NIL_P(mtime) && mtime != atime && !rb_equal(atime, mtime)) { 02297 m = rb_inspect(mtime); 02298 } 02299 if (NIL_P(a)) e[0] = m; 02300 else if (NIL_P(m) || rb_str_cmp(a, m) == 0) e[0] = a; 02301 else { 02302 e[0] = rb_str_plus(a, rb_str_new_cstr(" or ")); 02303 rb_str_append(e[0], m); 02304 d = 1; 02305 } 02306 if (!NIL_P(e[0])) { 02307 if (path) { 02308 if (!d) e[0] = rb_str_dup(e[0]); 02309 rb_str_append(rb_str_cat2(e[0], " for "), path); 02310 } 02311 e[1] = INT2FIX(EINVAL); 02312 rb_exc_raise(rb_class_new_instance(2, e, rb_eSystemCallError)); 02313 } 02314 errno = EINVAL; 02315 } 02316 rb_sys_fail_path(path); 02317 } 02318 #else 02319 #define utime_failed(path, tsp, atime, mtime) rb_sys_fail_path(path) 02320 #endif 02321 02322 #if defined(HAVE_UTIMES) 02323 02324 static void 02325 utime_internal(const char *path, VALUE pathv, void *arg) 02326 { 02327 struct utime_args *v = arg; 02328 const struct timespec *tsp = v->tsp; 02329 struct timeval tvbuf[2], *tvp = NULL; 02330 02331 #ifdef HAVE_UTIMENSAT 02332 static int try_utimensat = 1; 02333 02334 if (try_utimensat) { 02335 if (utimensat(AT_FDCWD, path, tsp, 0) < 0) { 02336 if (errno == ENOSYS) { 02337 try_utimensat = 0; 02338 goto no_utimensat; 02339 } 02340 utime_failed(pathv, tsp, v->atime, v->mtime); 02341 } 02342 return; 02343 } 02344 no_utimensat: 02345 #endif 02346 02347 if (tsp) { 02348 tvbuf[0].tv_sec = tsp[0].tv_sec; 02349 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000); 02350 tvbuf[1].tv_sec = tsp[1].tv_sec; 02351 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000); 02352 tvp = tvbuf; 02353 } 02354 if (utimes(path, tvp) < 0) 02355 utime_failed(pathv, tsp, v->atime, v->mtime); 02356 } 02357 02358 #else 02359 02360 #if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H 02361 struct utimbuf { 02362 long actime; 02363 long modtime; 02364 }; 02365 #endif 02366 02367 static void 02368 utime_internal(const char *path, VALUE pathv, void *arg) 02369 { 02370 struct utime_args *v = arg; 02371 const struct timespec *tsp = v->tsp; 02372 struct utimbuf utbuf, *utp = NULL; 02373 if (tsp) { 02374 utbuf.actime = tsp[0].tv_sec; 02375 utbuf.modtime = tsp[1].tv_sec; 02376 utp = &utbuf; 02377 } 02378 if (utime(path, utp) < 0) 02379 utime_failed(pathv, tsp, v->atime, v->mtime); 02380 } 02381 02382 #endif 02383 02384 /* 02385 * call-seq: 02386 * File.utime(atime, mtime, file_name,...) -> integer 02387 * 02388 * Sets the access and modification times of each 02389 * named file to the first two arguments. Returns 02390 * the number of file names in the argument list. 02391 */ 02392 02393 static VALUE 02394 rb_file_s_utime(int argc, VALUE *argv) 02395 { 02396 VALUE rest; 02397 struct utime_args args; 02398 struct timespec tss[2], *tsp = NULL; 02399 long n; 02400 02401 rb_secure(2); 02402 rb_scan_args(argc, argv, "2*", &args.atime, &args.mtime, &rest); 02403 02404 if (!NIL_P(args.atime) || !NIL_P(args.mtime)) { 02405 tsp = tss; 02406 tsp[0] = rb_time_timespec(args.atime); 02407 tsp[1] = rb_time_timespec(args.mtime); 02408 } 02409 args.tsp = tsp; 02410 02411 n = apply2files(utime_internal, rest, &args); 02412 return LONG2FIX(n); 02413 } 02414 02415 NORETURN(static void sys_fail2(VALUE,VALUE)); 02416 static void 02417 sys_fail2(VALUE s1, VALUE s2) 02418 { 02419 VALUE str; 02420 #ifdef MAX_PATH 02421 const int max_pathlen = MAX_PATH; 02422 #else 02423 const int max_pathlen = MAXPATHLEN; 02424 #endif 02425 02426 str = rb_str_new_cstr("("); 02427 rb_str_append(str, rb_str_ellipsize(s1, max_pathlen)); 02428 rb_str_cat2(str, ", "); 02429 rb_str_append(str, rb_str_ellipsize(s2, max_pathlen)); 02430 rb_str_cat2(str, ")"); 02431 rb_sys_fail_path(str); 02432 } 02433 02434 #ifdef HAVE_LINK 02435 /* 02436 * call-seq: 02437 * File.link(old_name, new_name) -> 0 02438 * 02439 * Creates a new name for an existing file using a hard link. Will not 02440 * overwrite <i>new_name</i> if it already exists (raising a subclass 02441 * of <code>SystemCallError</code>). Not available on all platforms. 02442 * 02443 * File.link("testfile", ".testfile") #=> 0 02444 * IO.readlines(".testfile")[0] #=> "This is line one\n" 02445 */ 02446 02447 static VALUE 02448 rb_file_s_link(VALUE klass, VALUE from, VALUE to) 02449 { 02450 rb_secure(2); 02451 FilePathValue(from); 02452 FilePathValue(to); 02453 from = rb_str_encode_ospath(from); 02454 to = rb_str_encode_ospath(to); 02455 02456 if (link(StringValueCStr(from), StringValueCStr(to)) < 0) { 02457 sys_fail2(from, to); 02458 } 02459 return INT2FIX(0); 02460 } 02461 #else 02462 #define rb_file_s_link rb_f_notimplement 02463 #endif 02464 02465 #ifdef HAVE_SYMLINK 02466 /* 02467 * call-seq: 02468 * File.symlink(old_name, new_name) -> 0 02469 * 02470 * Creates a symbolic link called <i>new_name</i> for the existing file 02471 * <i>old_name</i>. Raises a <code>NotImplemented</code> exception on 02472 * platforms that do not support symbolic links. 02473 * 02474 * File.symlink("testfile", "link2test") #=> 0 02475 * 02476 */ 02477 02478 static VALUE 02479 rb_file_s_symlink(VALUE klass, VALUE from, VALUE to) 02480 { 02481 rb_secure(2); 02482 FilePathValue(from); 02483 FilePathValue(to); 02484 from = rb_str_encode_ospath(from); 02485 to = rb_str_encode_ospath(to); 02486 02487 if (symlink(StringValueCStr(from), StringValueCStr(to)) < 0) { 02488 sys_fail2(from, to); 02489 } 02490 return INT2FIX(0); 02491 } 02492 #else 02493 #define rb_file_s_symlink rb_f_notimplement 02494 #endif 02495 02496 #ifdef HAVE_READLINK 02497 static VALUE rb_readlink(VALUE path); 02498 02499 /* 02500 * call-seq: 02501 * File.readlink(link_name) -> file_name 02502 * 02503 * Returns the name of the file referenced by the given link. 02504 * Not available on all platforms. 02505 * 02506 * File.symlink("testfile", "link2test") #=> 0 02507 * File.readlink("link2test") #=> "testfile" 02508 */ 02509 02510 static VALUE 02511 rb_file_s_readlink(VALUE klass, VALUE path) 02512 { 02513 return rb_readlink(path); 02514 } 02515 02516 static VALUE 02517 rb_readlink(VALUE path) 02518 { 02519 int size = 100; 02520 ssize_t rv; 02521 VALUE v; 02522 02523 rb_secure(2); 02524 FilePathValue(path); 02525 path = rb_str_encode_ospath(path); 02526 v = rb_enc_str_new(0, size, rb_filesystem_encoding()); 02527 while ((rv = readlink(RSTRING_PTR(path), RSTRING_PTR(v), size)) == size 02528 #ifdef _AIX 02529 || (rv < 0 && errno == ERANGE) /* quirky behavior of GPFS */ 02530 #endif 02531 ) { 02532 rb_str_modify_expand(v, size); 02533 size *= 2; 02534 rb_str_set_len(v, size); 02535 } 02536 if (rv < 0) { 02537 rb_str_resize(v, 0); 02538 rb_sys_fail_path(path); 02539 } 02540 rb_str_resize(v, rv); 02541 02542 return v; 02543 } 02544 #else 02545 #define rb_file_s_readlink rb_f_notimplement 02546 #endif 02547 02548 static void 02549 unlink_internal(const char *path, VALUE pathv, void *arg) 02550 { 02551 if (unlink(path) < 0) 02552 rb_sys_fail_path(pathv); 02553 } 02554 02555 /* 02556 * call-seq: 02557 * File.delete(file_name, ...) -> integer 02558 * File.unlink(file_name, ...) -> integer 02559 * 02560 * Deletes the named files, returning the number of names 02561 * passed as arguments. Raises an exception on any error. 02562 * See also <code>Dir::rmdir</code>. 02563 */ 02564 02565 static VALUE 02566 rb_file_s_unlink(VALUE klass, VALUE args) 02567 { 02568 long n; 02569 02570 rb_secure(2); 02571 n = apply2files(unlink_internal, args, 0); 02572 return LONG2FIX(n); 02573 } 02574 02575 /* 02576 * call-seq: 02577 * File.rename(old_name, new_name) -> 0 02578 * 02579 * Renames the given file to the new name. Raises a 02580 * <code>SystemCallError</code> if the file cannot be renamed. 02581 * 02582 * File.rename("afile", "afile.bak") #=> 0 02583 */ 02584 02585 static VALUE 02586 rb_file_s_rename(VALUE klass, VALUE from, VALUE to) 02587 { 02588 const char *src, *dst; 02589 VALUE f, t; 02590 02591 rb_secure(2); 02592 FilePathValue(from); 02593 FilePathValue(to); 02594 f = rb_str_encode_ospath(from); 02595 t = rb_str_encode_ospath(to); 02596 src = StringValueCStr(f); 02597 dst = StringValueCStr(t); 02598 #if defined __CYGWIN__ 02599 errno = 0; 02600 #endif 02601 if (rename(src, dst) < 0) { 02602 #if defined DOSISH 02603 switch (errno) { 02604 case EEXIST: 02605 #if defined (__EMX__) 02606 case EACCES: 02607 #endif 02608 if (chmod(dst, 0666) == 0 && 02609 unlink(dst) == 0 && 02610 rename(src, dst) == 0) 02611 return INT2FIX(0); 02612 } 02613 #endif 02614 sys_fail2(from, to); 02615 } 02616 02617 return INT2FIX(0); 02618 } 02619 02620 /* 02621 * call-seq: 02622 * File.umask() -> integer 02623 * File.umask(integer) -> integer 02624 * 02625 * Returns the current umask value for this process. If the optional 02626 * argument is given, set the umask to that value and return the 02627 * previous value. Umask values are <em>subtracted</em> from the 02628 * default permissions, so a umask of <code>0222</code> would make a 02629 * file read-only for everyone. 02630 * 02631 * File.umask(0006) #=> 18 02632 * File.umask #=> 6 02633 */ 02634 02635 static VALUE 02636 rb_file_s_umask(int argc, VALUE *argv) 02637 { 02638 int omask = 0; 02639 02640 rb_secure(2); 02641 if (argc == 0) { 02642 omask = umask(0); 02643 umask(omask); 02644 } 02645 else if (argc == 1) { 02646 omask = umask(NUM2INT(argv[0])); 02647 } 02648 else { 02649 rb_check_arity(argc, 0, 1); 02650 } 02651 return INT2FIX(omask); 02652 } 02653 02654 #ifdef __CYGWIN__ 02655 #undef DOSISH 02656 #endif 02657 #if defined __CYGWIN__ || defined DOSISH 02658 #define DOSISH_UNC 02659 #define DOSISH_DRIVE_LETTER 02660 #define FILE_ALT_SEPARATOR '\\' 02661 #endif 02662 #ifdef FILE_ALT_SEPARATOR 02663 #define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR) 02664 static const char file_alt_separator[] = {FILE_ALT_SEPARATOR, '\0'}; 02665 #else 02666 #define isdirsep(x) ((x) == '/') 02667 #endif 02668 02669 #ifndef USE_NTFS 02670 #if defined _WIN32 || defined __CYGWIN__ 02671 #define USE_NTFS 1 02672 #else 02673 #define USE_NTFS 0 02674 #endif 02675 #endif 02676 02677 #if USE_NTFS 02678 #define istrailinggarbage(x) ((x) == '.' || (x) == ' ') 02679 #else 02680 #define istrailinggarbage(x) 0 02681 #endif 02682 02683 #define Next(p, e, enc) ((p) + rb_enc_mbclen((p), (e), (enc))) 02684 #define Inc(p, e, enc) ((p) = Next((p), (e), (enc))) 02685 02686 #if defined(DOSISH_UNC) 02687 #define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1])) 02688 #else 02689 #define has_unc(buf) 0 02690 #endif 02691 02692 #ifdef DOSISH_DRIVE_LETTER 02693 static inline int 02694 has_drive_letter(const char *buf) 02695 { 02696 if (ISALPHA(buf[0]) && buf[1] == ':') { 02697 return 1; 02698 } 02699 else { 02700 return 0; 02701 } 02702 } 02703 02704 #ifndef _WIN32 02705 static char* 02706 getcwdofdrv(int drv) 02707 { 02708 char drive[4]; 02709 char *drvcwd, *oldcwd; 02710 02711 drive[0] = drv; 02712 drive[1] = ':'; 02713 drive[2] = '\0'; 02714 02715 /* the only way that I know to get the current directory 02716 of a particular drive is to change chdir() to that drive, 02717 so save the old cwd before chdir() 02718 */ 02719 oldcwd = my_getcwd(); 02720 if (chdir(drive) == 0) { 02721 drvcwd = my_getcwd(); 02722 chdir(oldcwd); 02723 xfree(oldcwd); 02724 } 02725 else { 02726 /* perhaps the drive is not exist. we return only drive letter */ 02727 drvcwd = strdup(drive); 02728 } 02729 return drvcwd; 02730 } 02731 #endif 02732 02733 static inline int 02734 not_same_drive(VALUE path, int drive) 02735 { 02736 const char *p = RSTRING_PTR(path); 02737 if (RSTRING_LEN(path) < 2) return 0; 02738 if (has_drive_letter(p)) { 02739 return TOLOWER(p[0]) != TOLOWER(drive); 02740 } 02741 else { 02742 return has_unc(p); 02743 } 02744 } 02745 #endif 02746 02747 static inline char * 02748 skiproot(const char *path, const char *end, rb_encoding *enc) 02749 { 02750 #ifdef DOSISH_DRIVE_LETTER 02751 if (path + 2 <= end && has_drive_letter(path)) path += 2; 02752 #endif 02753 while (path < end && isdirsep(*path)) path++; 02754 return (char *)path; 02755 } 02756 02757 #define nextdirsep rb_enc_path_next 02758 char * 02759 rb_enc_path_next(const char *s, const char *e, rb_encoding *enc) 02760 { 02761 while (s < e && !isdirsep(*s)) { 02762 Inc(s, e, enc); 02763 } 02764 return (char *)s; 02765 } 02766 02767 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER) 02768 #define skipprefix rb_enc_path_skip_prefix 02769 #else 02770 #define skipprefix(path, end, enc) (path) 02771 #endif 02772 char * 02773 rb_enc_path_skip_prefix(const char *path, const char *end, rb_encoding *enc) 02774 { 02775 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER) 02776 #ifdef DOSISH_UNC 02777 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) { 02778 path += 2; 02779 while (path < end && isdirsep(*path)) path++; 02780 if ((path = rb_enc_path_next(path, end, enc)) < end && path[0] && path[1] && !isdirsep(path[1])) 02781 path = rb_enc_path_next(path + 1, end, enc); 02782 return (char *)path; 02783 } 02784 #endif 02785 #ifdef DOSISH_DRIVE_LETTER 02786 if (has_drive_letter(path)) 02787 return (char *)(path + 2); 02788 #endif 02789 #endif 02790 return (char *)path; 02791 } 02792 02793 static inline char * 02794 skipprefixroot(const char *path, const char *end, rb_encoding *enc) 02795 { 02796 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER) 02797 char *p = skipprefix(path, end, enc); 02798 while (isdirsep(*p)) p++; 02799 return p; 02800 #else 02801 return skiproot(path, end, enc); 02802 #endif 02803 } 02804 02805 #define strrdirsep rb_enc_path_last_separator 02806 char * 02807 rb_enc_path_last_separator(const char *path, const char *end, rb_encoding *enc) 02808 { 02809 char *last = NULL; 02810 while (path < end) { 02811 if (isdirsep(*path)) { 02812 const char *tmp = path++; 02813 while (path < end && isdirsep(*path)) path++; 02814 if (path >= end) break; 02815 last = (char *)tmp; 02816 } 02817 else { 02818 Inc(path, end, enc); 02819 } 02820 } 02821 return last; 02822 } 02823 02824 static char * 02825 chompdirsep(const char *path, const char *end, rb_encoding *enc) 02826 { 02827 while (path < end) { 02828 if (isdirsep(*path)) { 02829 const char *last = path++; 02830 while (path < end && isdirsep(*path)) path++; 02831 if (path >= end) return (char *)last; 02832 } 02833 else { 02834 Inc(path, end, enc); 02835 } 02836 } 02837 return (char *)path; 02838 } 02839 02840 char * 02841 rb_enc_path_end(const char *path, const char *end, rb_encoding *enc) 02842 { 02843 if (path < end && isdirsep(*path)) path++; 02844 return chompdirsep(path, end, enc); 02845 } 02846 02847 #if USE_NTFS 02848 static char * 02849 ntfs_tail(const char *path, const char *end, rb_encoding *enc) 02850 { 02851 while (path < end && *path == '.') path++; 02852 while (path < end && *path != ':') { 02853 if (istrailinggarbage(*path)) { 02854 const char *last = path++; 02855 while (path < end && istrailinggarbage(*path)) path++; 02856 if (path >= end || *path == ':') return (char *)last; 02857 } 02858 else if (isdirsep(*path)) { 02859 const char *last = path++; 02860 while (path < end && isdirsep(*path)) path++; 02861 if (path >= end) return (char *)last; 02862 if (*path == ':') path++; 02863 } 02864 else { 02865 Inc(path, end, enc); 02866 } 02867 } 02868 return (char *)path; 02869 } 02870 #endif 02871 02872 #define BUFCHECK(cond) do {\ 02873 bdiff = p - buf;\ 02874 if (cond) {\ 02875 do {buflen *= 2;} while (cond);\ 02876 rb_str_resize(result, buflen);\ 02877 buf = RSTRING_PTR(result);\ 02878 p = buf + bdiff;\ 02879 pend = buf + buflen;\ 02880 }\ 02881 } while (0) 02882 02883 #define BUFINIT() (\ 02884 p = buf = RSTRING_PTR(result),\ 02885 buflen = RSTRING_LEN(result),\ 02886 pend = p + buflen) 02887 02888 VALUE 02889 rb_home_dir(const char *user, VALUE result) 02890 { 02891 const char *dir; 02892 char *buf; 02893 #if defined DOSISH || defined __CYGWIN__ 02894 char *p, *bend; 02895 #endif 02896 long dirlen; 02897 rb_encoding *enc; 02898 02899 if (!user || !*user) { 02900 if (!(dir = getenv("HOME"))) { 02901 rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'"); 02902 } 02903 dirlen = strlen(dir); 02904 rb_str_resize(result, dirlen); 02905 memcpy(buf = RSTRING_PTR(result), dir, dirlen); 02906 } 02907 else { 02908 #ifdef HAVE_PWD_H 02909 struct passwd *pwPtr = getpwnam(user); 02910 if (!pwPtr) { 02911 endpwent(); 02912 rb_raise(rb_eArgError, "user %s doesn't exist", user); 02913 } 02914 dirlen = strlen(pwPtr->pw_dir); 02915 rb_str_resize(result, dirlen); 02916 memcpy(buf = RSTRING_PTR(result), pwPtr->pw_dir, dirlen + 1); 02917 endpwent(); 02918 #else 02919 return Qnil; 02920 #endif 02921 } 02922 enc = rb_filesystem_encoding(); 02923 rb_enc_associate(result, enc); 02924 #if defined DOSISH || defined __CYGWIN__ 02925 for (bend = (p = buf) + dirlen; p < bend; Inc(p, bend, enc)) { 02926 if (*p == '\\') { 02927 *p = '/'; 02928 } 02929 } 02930 #endif 02931 return result; 02932 } 02933 02934 #ifndef _WIN32 02935 static char * 02936 append_fspath(VALUE result, VALUE fname, char *dir, rb_encoding **enc, rb_encoding *fsenc) 02937 { 02938 char *buf, *cwdp = dir; 02939 VALUE dirname = Qnil; 02940 size_t dirlen = strlen(dir), buflen = rb_str_capacity(result); 02941 02942 if (*enc != fsenc) { 02943 rb_encoding *direnc = rb_enc_check(fname, dirname = rb_enc_str_new(dir, dirlen, fsenc)); 02944 if (direnc != fsenc) { 02945 dirname = rb_str_conv_enc(dirname, fsenc, direnc); 02946 RSTRING_GETMEM(dirname, cwdp, dirlen); 02947 } 02948 *enc = direnc; 02949 } 02950 do {buflen *= 2;} while (dirlen > buflen); 02951 rb_str_resize(result, buflen); 02952 buf = RSTRING_PTR(result); 02953 memcpy(buf, cwdp, dirlen); 02954 xfree(dir); 02955 if (!NIL_P(dirname)) rb_str_resize(dirname, 0); 02956 rb_enc_associate(result, *enc); 02957 return buf + dirlen; 02958 } 02959 02960 VALUE 02961 rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result) 02962 { 02963 const char *s, *b, *fend; 02964 char *buf, *p, *pend, *root; 02965 size_t buflen, bdiff; 02966 int tainted; 02967 rb_encoding *enc, *fsenc = rb_filesystem_encoding(); 02968 02969 s = StringValuePtr(fname); 02970 fend = s + RSTRING_LEN(fname); 02971 enc = rb_enc_get(fname); 02972 BUFINIT(); 02973 tainted = OBJ_TAINTED(fname); 02974 02975 if (s[0] == '~' && abs_mode == 0) { /* execute only if NOT absolute_path() */ 02976 long userlen = 0; 02977 tainted = 1; 02978 if (isdirsep(s[1]) || s[1] == '\0') { 02979 buf = 0; 02980 b = 0; 02981 rb_str_set_len(result, 0); 02982 if (*++s) ++s; 02983 } 02984 else { 02985 s = nextdirsep(b = s, fend, enc); 02986 userlen = s - b; 02987 BUFCHECK(bdiff + userlen >= buflen); 02988 memcpy(p, b, userlen); 02989 rb_str_set_len(result, userlen); 02990 buf = p + 1; 02991 p += userlen; 02992 } 02993 if (NIL_P(rb_home_dir(buf, result))) { 02994 rb_raise(rb_eArgError, "can't find user %s", buf); 02995 } 02996 if (!rb_is_absolute_path(RSTRING_PTR(result))) { 02997 if (userlen) { 02998 rb_raise(rb_eArgError, "non-absolute home of %.*s", (int)userlen, b); 02999 } 03000 else { 03001 rb_raise(rb_eArgError, "non-absolute home"); 03002 } 03003 } 03004 BUFINIT(); 03005 p = pend; 03006 } 03007 #ifdef DOSISH_DRIVE_LETTER 03008 /* skip drive letter */ 03009 else if (has_drive_letter(s)) { 03010 if (isdirsep(s[2])) { 03011 /* specified drive letter, and full path */ 03012 /* skip drive letter */ 03013 BUFCHECK(bdiff + 2 >= buflen); 03014 memcpy(p, s, 2); 03015 p += 2; 03016 s += 2; 03017 rb_enc_copy(result, fname); 03018 } 03019 else { 03020 /* specified drive, but not full path */ 03021 int same = 0; 03022 if (!NIL_P(dname) && !not_same_drive(dname, s[0])) { 03023 rb_file_expand_path_internal(dname, Qnil, abs_mode, long_name, result); 03024 BUFINIT(); 03025 if (has_drive_letter(p) && TOLOWER(p[0]) == TOLOWER(s[0])) { 03026 /* ok, same drive */ 03027 same = 1; 03028 } 03029 } 03030 if (!same) { 03031 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc); 03032 tainted = 1; 03033 BUFINIT(); 03034 p = e; 03035 } 03036 else { 03037 rb_enc_associate(result, enc = rb_enc_check(result, fname)); 03038 p = pend; 03039 } 03040 p = chompdirsep(skiproot(buf, p, enc), p, enc); 03041 s += 2; 03042 } 03043 } 03044 #endif 03045 else if (!rb_is_absolute_path(s)) { 03046 if (!NIL_P(dname)) { 03047 rb_file_expand_path_internal(dname, Qnil, abs_mode, long_name, result); 03048 rb_enc_associate(result, rb_enc_check(result, fname)); 03049 BUFINIT(); 03050 p = pend; 03051 } 03052 else { 03053 char *e = append_fspath(result, fname, my_getcwd(), &enc, fsenc); 03054 tainted = 1; 03055 BUFINIT(); 03056 p = e; 03057 } 03058 #if defined DOSISH || defined __CYGWIN__ 03059 if (isdirsep(*s)) { 03060 /* specified full path, but not drive letter nor UNC */ 03061 /* we need to get the drive letter or UNC share name */ 03062 p = skipprefix(buf, p, enc); 03063 } 03064 else 03065 #endif 03066 p = chompdirsep(skiproot(buf, p, enc), p, enc); 03067 } 03068 else { 03069 size_t len; 03070 b = s; 03071 do s++; while (isdirsep(*s)); 03072 len = s - b; 03073 p = buf + len; 03074 BUFCHECK(bdiff >= buflen); 03075 memset(buf, '/', len); 03076 rb_str_set_len(result, len); 03077 rb_enc_associate(result, rb_enc_check(result, fname)); 03078 } 03079 if (p > buf && p[-1] == '/') 03080 --p; 03081 else { 03082 rb_str_set_len(result, p-buf); 03083 BUFCHECK(bdiff + 1 >= buflen); 03084 *p = '/'; 03085 } 03086 03087 rb_str_set_len(result, p-buf+1); 03088 BUFCHECK(bdiff + 1 >= buflen); 03089 p[1] = 0; 03090 root = skipprefix(buf, p+1, enc); 03091 03092 b = s; 03093 while (*s) { 03094 switch (*s) { 03095 case '.': 03096 if (b == s++) { /* beginning of path element */ 03097 switch (*s) { 03098 case '\0': 03099 b = s; 03100 break; 03101 case '.': 03102 if (*(s+1) == '\0' || isdirsep(*(s+1))) { 03103 /* We must go back to the parent */ 03104 char *n; 03105 *p = '\0'; 03106 if (!(n = strrdirsep(root, p, enc))) { 03107 *p = '/'; 03108 } 03109 else { 03110 p = n; 03111 } 03112 b = ++s; 03113 } 03114 #if USE_NTFS 03115 else { 03116 do ++s; while (istrailinggarbage(*s)); 03117 } 03118 #endif 03119 break; 03120 case '/': 03121 #if defined DOSISH || defined __CYGWIN__ 03122 case '\\': 03123 #endif 03124 b = ++s; 03125 break; 03126 default: 03127 /* ordinary path element, beginning don't move */ 03128 break; 03129 } 03130 } 03131 #if USE_NTFS 03132 else { 03133 --s; 03134 case ' ': { 03135 const char *e = s; 03136 while (s < fend && istrailinggarbage(*s)) s++; 03137 if (!*s) { 03138 s = e; 03139 goto endpath; 03140 } 03141 } 03142 } 03143 #endif 03144 break; 03145 case '/': 03146 #if defined DOSISH || defined __CYGWIN__ 03147 case '\\': 03148 #endif 03149 if (s > b) { 03150 long rootdiff = root - buf; 03151 rb_str_set_len(result, p-buf+1); 03152 BUFCHECK(bdiff + (s-b+1) >= buflen); 03153 root = buf + rootdiff; 03154 memcpy(++p, b, s-b); 03155 p += s-b; 03156 *p = '/'; 03157 } 03158 b = ++s; 03159 break; 03160 default: 03161 Inc(s, fend, enc); 03162 break; 03163 } 03164 } 03165 03166 if (s > b) { 03167 #if USE_NTFS 03168 static const char prime[] = ":$DATA"; 03169 enum {prime_len = sizeof(prime) -1}; 03170 endpath: 03171 if (s > b + prime_len && strncasecmp(s - prime_len, prime, prime_len) == 0) { 03172 /* alias of stream */ 03173 /* get rid of a bug of x64 VC++ */ 03174 if (*(s - (prime_len+1)) == ':') { 03175 s -= prime_len + 1; /* prime */ 03176 } 03177 else if (memchr(b, ':', s - prime_len - b)) { 03178 s -= prime_len; /* alternative */ 03179 } 03180 } 03181 #endif 03182 rb_str_set_len(result, p-buf+1); 03183 BUFCHECK(bdiff + (s-b) >= buflen); 03184 memcpy(++p, b, s-b); 03185 p += s-b; 03186 rb_str_set_len(result, p-buf); 03187 } 03188 if (p == skiproot(buf, p + !!*p, enc) - 1) p++; 03189 03190 #if USE_NTFS 03191 *p = '\0'; 03192 if ((s = strrdirsep(b = buf, p, enc)) != 0 && !strpbrk(s, "*?")) { 03193 VALUE tmp, v; 03194 size_t len; 03195 rb_encoding *enc; 03196 WCHAR *wstr; 03197 WIN32_FIND_DATAW wfd; 03198 HANDLE h; 03199 #ifdef __CYGWIN__ 03200 #ifdef HAVE_CYGWIN_CONV_PATH 03201 char *w32buf = NULL; 03202 const int flags = CCP_POSIX_TO_WIN_A | CCP_RELATIVE; 03203 #else 03204 char w32buf[MAXPATHLEN]; 03205 #endif 03206 const char *path; 03207 ssize_t bufsize; 03208 int lnk_added = 0, is_symlink = 0; 03209 struct stat st; 03210 p = (char *)s; 03211 len = strlen(p); 03212 if (lstat(buf, &st) == 0 && S_ISLNK(st.st_mode)) { 03213 is_symlink = 1; 03214 if (len > 4 && STRCASECMP(p + len - 4, ".lnk") != 0) { 03215 lnk_added = 1; 03216 } 03217 } 03218 path = *buf ? buf : "/"; 03219 #ifdef HAVE_CYGWIN_CONV_PATH 03220 bufsize = cygwin_conv_path(flags, path, NULL, 0); 03221 if (bufsize > 0) { 03222 bufsize += len; 03223 if (lnk_added) bufsize += 4; 03224 w32buf = ALLOCA_N(char, bufsize); 03225 if (cygwin_conv_path(flags, path, w32buf, bufsize) == 0) { 03226 b = w32buf; 03227 } 03228 } 03229 #else 03230 bufsize = MAXPATHLEN; 03231 if (cygwin_conv_to_win32_path(path, w32buf) == 0) { 03232 b = w32buf; 03233 } 03234 #endif 03235 if (is_symlink && b == w32buf) { 03236 *p = '\\'; 03237 strlcat(w32buf, p, bufsize); 03238 if (lnk_added) { 03239 strlcat(w32buf, ".lnk", bufsize); 03240 } 03241 } 03242 else { 03243 lnk_added = 0; 03244 } 03245 *p = '/'; 03246 #endif 03247 rb_str_set_len(result, p - buf + strlen(p)); 03248 enc = rb_enc_get(result); 03249 tmp = result; 03250 if (enc != rb_utf8_encoding() && rb_enc_str_coderange(result) != ENC_CODERANGE_7BIT) { 03251 tmp = rb_str_encode_ospath(result); 03252 } 03253 len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, NULL, 0); 03254 wstr = ALLOCV_N(WCHAR, v, len); 03255 MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, wstr, len); 03256 if (tmp != result) rb_str_resize(tmp, 0); 03257 h = FindFirstFileW(wstr, &wfd); 03258 ALLOCV_END(v); 03259 if (h != INVALID_HANDLE_VALUE) { 03260 size_t wlen; 03261 FindClose(h); 03262 len = lstrlenW(wfd.cFileName); 03263 #ifdef __CYGWIN__ 03264 if (lnk_added && len > 4 && 03265 wcscasecmp(wfd.cFileName + len - 4, L".lnk") == 0) { 03266 wfd.cFileName[len -= 4] = L'\0'; 03267 } 03268 #else 03269 p = (char *)s; 03270 #endif 03271 ++p; 03272 wlen = (int)len; 03273 len = WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, NULL, 0, NULL, NULL); 03274 BUFCHECK(bdiff + len >= buflen); 03275 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, p, len + 1, NULL, NULL); 03276 if (tmp != result) { 03277 rb_str_buf_cat(tmp, p, len); 03278 tmp = rb_str_encode(tmp, rb_enc_from_encoding(enc), 0, Qnil); 03279 len = RSTRING_LEN(tmp); 03280 BUFCHECK(bdiff + len >= buflen); 03281 memcpy(p, RSTRING_PTR(tmp), len); 03282 rb_str_resize(tmp, 0); 03283 } 03284 p += len; 03285 } 03286 #ifdef __CYGWIN__ 03287 else { 03288 p += strlen(p); 03289 } 03290 #endif 03291 } 03292 #endif 03293 03294 if (tainted) OBJ_TAINT(result); 03295 rb_str_set_len(result, p - buf); 03296 rb_enc_check(fname, result); 03297 ENC_CODERANGE_CLEAR(result); 03298 return result; 03299 } 03300 #endif /* _WIN32 */ 03301 03302 #define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, MAXPATHLEN + 2) 03303 03304 #define check_expand_path_args(fname, dname) \ 03305 (((fname) = rb_get_path(fname)), \ 03306 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname)))) 03307 03308 static VALUE 03309 file_expand_path_1(VALUE fname) 03310 { 03311 return rb_file_expand_path_internal(fname, Qnil, 0, 0, EXPAND_PATH_BUFFER()); 03312 } 03313 03314 VALUE 03315 rb_file_expand_path(VALUE fname, VALUE dname) 03316 { 03317 check_expand_path_args(fname, dname); 03318 return rb_file_expand_path_internal(fname, dname, 0, 1, EXPAND_PATH_BUFFER()); 03319 } 03320 03321 VALUE 03322 rb_file_expand_path_fast(VALUE fname, VALUE dname) 03323 { 03324 return rb_file_expand_path_internal(fname, dname, 0, 0, EXPAND_PATH_BUFFER()); 03325 } 03326 03327 /* 03328 * call-seq: 03329 * File.expand_path(file_name [, dir_string] ) -> abs_file_name 03330 * 03331 * Converts a pathname to an absolute pathname. Relative paths are 03332 * referenced from the current working directory of the process unless 03333 * <i>dir_string</i> is given, in which case it will be used as the 03334 * starting point. The given pathname may start with a 03335 * ``<code>~</code>'', which expands to the process owner's home 03336 * directory (the environment variable <code>HOME</code> must be set 03337 * correctly). ``<code>~</code><i>user</i>'' expands to the named 03338 * user's home directory. 03339 * 03340 * File.expand_path("~oracle/bin") #=> "/home/oracle/bin" 03341 * File.expand_path("../../bin", "/tmp/x") #=> "/bin" 03342 */ 03343 03344 VALUE 03345 rb_file_s_expand_path(int argc, VALUE *argv) 03346 { 03347 VALUE fname, dname; 03348 03349 if (argc == 1) { 03350 return rb_file_expand_path(argv[0], Qnil); 03351 } 03352 rb_scan_args(argc, argv, "11", &fname, &dname); 03353 03354 return rb_file_expand_path(fname, dname); 03355 } 03356 03357 VALUE 03358 rb_file_absolute_path(VALUE fname, VALUE dname) 03359 { 03360 check_expand_path_args(fname, dname); 03361 return rb_file_expand_path_internal(fname, dname, 1, 1, EXPAND_PATH_BUFFER()); 03362 } 03363 03364 /* 03365 * call-seq: 03366 * File.absolute_path(file_name [, dir_string] ) -> abs_file_name 03367 * 03368 * Converts a pathname to an absolute pathname. Relative paths are 03369 * referenced from the current working directory of the process unless 03370 * <i>dir_string</i> is given, in which case it will be used as the 03371 * starting point. If the given pathname starts with a ``<code>~</code>'' 03372 * it is NOT expanded, it is treated as a normal directory name. 03373 * 03374 * File.absolute_path("~oracle/bin") #=> "<relative_path>/~oracle/bin" 03375 */ 03376 03377 VALUE 03378 rb_file_s_absolute_path(int argc, VALUE *argv) 03379 { 03380 VALUE fname, dname; 03381 03382 if (argc == 1) { 03383 return rb_file_absolute_path(argv[0], Qnil); 03384 } 03385 rb_scan_args(argc, argv, "11", &fname, &dname); 03386 03387 return rb_file_absolute_path(fname, dname); 03388 } 03389 03390 static void 03391 realpath_rec(long *prefixlenp, VALUE *resolvedp, const char *unresolved, VALUE loopcheck, int strict, int last) 03392 { 03393 const char *pend = unresolved + strlen(unresolved); 03394 rb_encoding *enc = rb_enc_get(*resolvedp); 03395 ID resolving; 03396 CONST_ID(resolving, "resolving"); 03397 while (unresolved < pend) { 03398 const char *testname = unresolved; 03399 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc); 03400 long testnamelen = unresolved_firstsep - unresolved; 03401 const char *unresolved_nextname = unresolved_firstsep; 03402 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname)) 03403 unresolved_nextname++; 03404 unresolved = unresolved_nextname; 03405 if (testnamelen == 1 && testname[0] == '.') { 03406 } 03407 else if (testnamelen == 2 && testname[0] == '.' && testname[1] == '.') { 03408 if (*prefixlenp < RSTRING_LEN(*resolvedp)) { 03409 const char *resolved_str = RSTRING_PTR(*resolvedp); 03410 const char *resolved_names = resolved_str + *prefixlenp; 03411 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), enc); 03412 long len = lastsep ? lastsep - resolved_names : 0; 03413 rb_str_resize(*resolvedp, *prefixlenp + len); 03414 } 03415 } 03416 else { 03417 VALUE checkval; 03418 VALUE testpath = rb_str_dup(*resolvedp); 03419 if (*prefixlenp < RSTRING_LEN(testpath)) 03420 rb_str_cat2(testpath, "/"); 03421 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER) 03422 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) { 03423 const char *prefix = RSTRING_PTR(testpath); 03424 const char *last = rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc); 03425 if (!isdirsep(*last)) rb_str_cat2(testpath, "/"); 03426 } 03427 #endif 03428 rb_str_cat(testpath, testname, testnamelen); 03429 checkval = rb_hash_aref(loopcheck, testpath); 03430 if (!NIL_P(checkval)) { 03431 if (checkval == ID2SYM(resolving)) { 03432 errno = ELOOP; 03433 rb_sys_fail_path(testpath); 03434 } 03435 else { 03436 *resolvedp = rb_str_dup(checkval); 03437 } 03438 } 03439 else { 03440 struct stat sbuf; 03441 int ret; 03442 VALUE testpath2 = rb_str_encode_ospath(testpath); 03443 #ifdef __native_client__ 03444 ret = stat(RSTRING_PTR(testpath2), &sbuf); 03445 #else 03446 ret = lstat(RSTRING_PTR(testpath2), &sbuf); 03447 #endif 03448 if (ret == -1) { 03449 if (errno == ENOENT) { 03450 if (strict || !last || *unresolved_firstsep) 03451 rb_sys_fail_path(testpath); 03452 *resolvedp = testpath; 03453 break; 03454 } 03455 else { 03456 rb_sys_fail_path(testpath); 03457 } 03458 } 03459 #ifdef HAVE_READLINK 03460 if (S_ISLNK(sbuf.st_mode)) { 03461 VALUE link; 03462 volatile VALUE link_orig = Qnil; 03463 const char *link_prefix, *link_names; 03464 long link_prefixlen; 03465 rb_hash_aset(loopcheck, testpath, ID2SYM(resolving)); 03466 link = rb_readlink(testpath); 03467 link_prefix = RSTRING_PTR(link); 03468 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link)); 03469 link_prefixlen = link_names - link_prefix; 03470 if (link_prefixlen > 0) { 03471 rb_encoding *enc, *linkenc = rb_enc_get(link); 03472 link_orig = link; 03473 link = rb_str_subseq(link, 0, link_prefixlen); 03474 enc = rb_enc_check(*resolvedp, link); 03475 if (enc != linkenc) link = rb_str_conv_enc(link, linkenc, enc); 03476 *resolvedp = link; 03477 *prefixlenp = link_prefixlen; 03478 } 03479 realpath_rec(prefixlenp, resolvedp, link_names, loopcheck, strict, *unresolved_firstsep == '\0'); 03480 RB_GC_GUARD(link_orig); 03481 rb_hash_aset(loopcheck, testpath, rb_str_dup_frozen(*resolvedp)); 03482 } 03483 else 03484 #endif 03485 { 03486 VALUE s = rb_str_dup_frozen(testpath); 03487 rb_hash_aset(loopcheck, s, s); 03488 *resolvedp = testpath; 03489 } 03490 } 03491 } 03492 } 03493 } 03494 03495 #ifdef __native_client__ 03496 VALUE 03497 rb_realpath_internal(VALUE basedir, VALUE path, int strict) 03498 { 03499 return path; 03500 } 03501 #else 03502 VALUE 03503 rb_realpath_internal(VALUE basedir, VALUE path, int strict) 03504 { 03505 long prefixlen; 03506 VALUE resolved; 03507 volatile VALUE unresolved_path; 03508 VALUE loopcheck; 03509 volatile VALUE curdir = Qnil; 03510 03511 rb_encoding *enc; 03512 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL; 03513 char *ptr, *prefixptr = NULL, *pend; 03514 long len; 03515 03516 rb_secure(2); 03517 03518 FilePathValue(path); 03519 unresolved_path = rb_str_dup_frozen(path); 03520 03521 if (!NIL_P(basedir)) { 03522 FilePathValue(basedir); 03523 basedir = rb_str_dup_frozen(basedir); 03524 } 03525 03526 RSTRING_GETMEM(unresolved_path, ptr, len); 03527 path_names = skipprefixroot(ptr, ptr + len, rb_enc_get(unresolved_path)); 03528 if (ptr != path_names) { 03529 resolved = rb_str_subseq(unresolved_path, 0, path_names - ptr); 03530 goto root_found; 03531 } 03532 03533 if (!NIL_P(basedir)) { 03534 RSTRING_GETMEM(basedir, ptr, len); 03535 basedir_names = skipprefixroot(ptr, ptr + len, rb_enc_get(basedir)); 03536 if (ptr != basedir_names) { 03537 resolved = rb_str_subseq(basedir, 0, basedir_names - ptr); 03538 goto root_found; 03539 } 03540 } 03541 03542 curdir = rb_dir_getwd(); 03543 RSTRING_GETMEM(curdir, ptr, len); 03544 curdir_names = skipprefixroot(ptr, ptr + len, rb_enc_get(curdir)); 03545 resolved = rb_str_subseq(curdir, 0, curdir_names - ptr); 03546 03547 root_found: 03548 RSTRING_GETMEM(resolved, prefixptr, prefixlen); 03549 pend = prefixptr + prefixlen; 03550 enc = rb_enc_get(resolved); 03551 ptr = chompdirsep(prefixptr, pend, enc); 03552 if (ptr < pend) { 03553 prefixlen = ++ptr - prefixptr; 03554 rb_str_set_len(resolved, prefixlen); 03555 } 03556 #ifdef FILE_ALT_SEPARATOR 03557 while (prefixptr < ptr) { 03558 if (*prefixptr == FILE_ALT_SEPARATOR) { 03559 *prefixptr = '/'; 03560 } 03561 Inc(prefixptr, pend, enc); 03562 } 03563 #endif 03564 03565 loopcheck = rb_hash_new(); 03566 if (curdir_names) 03567 realpath_rec(&prefixlen, &resolved, curdir_names, loopcheck, 1, 0); 03568 if (basedir_names) 03569 realpath_rec(&prefixlen, &resolved, basedir_names, loopcheck, 1, 0); 03570 realpath_rec(&prefixlen, &resolved, path_names, loopcheck, strict, 1); 03571 03572 OBJ_TAINT(resolved); 03573 return resolved; 03574 } 03575 #endif 03576 03577 /* 03578 * call-seq: 03579 * File.realpath(pathname [, dir_string]) -> real_pathname 03580 * 03581 * Returns the real (absolute) pathname of _pathname_ in the actual 03582 * filesystem not containing symlinks or useless dots. 03583 * 03584 * If _dir_string_ is given, it is used as a base directory 03585 * for interpreting relative pathname instead of the current directory. 03586 * 03587 * All components of the pathname must exist when this method is 03588 * called. 03589 */ 03590 static VALUE 03591 rb_file_s_realpath(int argc, VALUE *argv, VALUE klass) 03592 { 03593 VALUE path, basedir; 03594 rb_scan_args(argc, argv, "11", &path, &basedir); 03595 return rb_realpath_internal(basedir, path, 1); 03596 } 03597 03598 /* 03599 * call-seq: 03600 * File.realdirpath(pathname [, dir_string]) -> real_pathname 03601 * 03602 * Returns the real (absolute) pathname of _pathname_ in the actual filesystem. 03603 * The real pathname doesn't contain symlinks or useless dots. 03604 * 03605 * If _dir_string_ is given, it is used as a base directory 03606 * for interpreting relative pathname instead of the current directory. 03607 * 03608 * The last component of the real pathname can be nonexistent. 03609 */ 03610 static VALUE 03611 rb_file_s_realdirpath(int argc, VALUE *argv, VALUE klass) 03612 { 03613 VALUE path, basedir; 03614 rb_scan_args(argc, argv, "11", &path, &basedir); 03615 return rb_realpath_internal(basedir, path, 0); 03616 } 03617 03618 static size_t 03619 rmext(const char *p, long l0, long l1, const char *e, long l2, rb_encoding *enc) 03620 { 03621 int len1, len2; 03622 unsigned int c; 03623 const char *s, *last; 03624 03625 if (!e || !l2) return 0; 03626 03627 c = rb_enc_codepoint_len(e, e + l2, &len1, enc); 03628 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) == '*' && len1 + len2 == l2) { 03629 if (c == '.') return l0; 03630 s = p; 03631 e = p + l1; 03632 last = e; 03633 while (s < e) { 03634 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s; 03635 s += len1; 03636 } 03637 return last - p; 03638 } 03639 if (l1 < l2) return l1; 03640 03641 s = p+l1-l2; 03642 if (rb_enc_left_char_head(p, s, p+l1, enc) != s) return 0; 03643 #if CASEFOLD_FILESYSTEM 03644 #define fncomp strncasecmp 03645 #else 03646 #define fncomp strncmp 03647 #endif 03648 if (fncomp(s, e, l2) == 0) { 03649 return l1-l2; 03650 } 03651 return 0; 03652 } 03653 03654 const char * 03655 ruby_enc_find_basename(const char *name, long *baselen, long *alllen, rb_encoding *enc) 03656 { 03657 const char *p, *q, *e, *end; 03658 #if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC 03659 const char *root; 03660 #endif 03661 long f = 0, n = -1; 03662 03663 end = name + (alllen ? (size_t)*alllen : strlen(name)); 03664 name = skipprefix(name, end, enc); 03665 #if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC 03666 root = name; 03667 #endif 03668 while (isdirsep(*name)) 03669 name++; 03670 if (!*name) { 03671 p = name - 1; 03672 f = 1; 03673 #if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC 03674 if (name != root) { 03675 /* has slashes */ 03676 } 03677 #ifdef DOSISH_DRIVE_LETTER 03678 else if (*p == ':') { 03679 p++; 03680 f = 0; 03681 } 03682 #endif 03683 #ifdef DOSISH_UNC 03684 else { 03685 p = "/"; 03686 } 03687 #endif 03688 #endif 03689 } 03690 else { 03691 if (!(p = strrdirsep(name, end, enc))) { 03692 p = name; 03693 } 03694 else { 03695 while (isdirsep(*p)) p++; /* skip last / */ 03696 } 03697 #if USE_NTFS 03698 n = ntfs_tail(p, end, enc) - p; 03699 #else 03700 n = chompdirsep(p, end, enc) - p; 03701 #endif 03702 for (q = p; q - p < n && *q == '.'; q++); 03703 for (e = 0; q - p < n; Inc(q, end, enc)) { 03704 if (*q == '.') e = q; 03705 } 03706 if (e) f = e - p; 03707 else f = n; 03708 } 03709 03710 if (baselen) 03711 *baselen = f; 03712 if (alllen) 03713 *alllen = n; 03714 return p; 03715 } 03716 03717 /* 03718 * call-seq: 03719 * File.basename(file_name [, suffix] ) -> base_name 03720 * 03721 * Returns the last component of the filename given in <i>file_name</i>, 03722 * which can be formed using both <code>File::SEPARATOR</code> and 03723 * <code>File::ALT_SEPARETOR</code> as the separator when 03724 * <code>File::ALT_SEPARATOR</code> is not <code>nil</code>. If 03725 * <i>suffix</i> is given and present at the end of <i>file_name</i>, 03726 * it is removed. 03727 * 03728 * File.basename("/home/gumby/work/ruby.rb") #=> "ruby.rb" 03729 * File.basename("/home/gumby/work/ruby.rb", ".rb") #=> "ruby" 03730 */ 03731 03732 static VALUE 03733 rb_file_s_basename(int argc, VALUE *argv) 03734 { 03735 VALUE fname, fext, basename; 03736 const char *name, *p; 03737 long f, n; 03738 rb_encoding *enc; 03739 03740 if (rb_scan_args(argc, argv, "11", &fname, &fext) == 2) { 03741 StringValue(fext); 03742 enc = check_path_encoding(fext); 03743 } 03744 FilePathStringValue(fname); 03745 if (NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) { 03746 enc = rb_enc_get(fname); 03747 fext = Qnil; 03748 } 03749 if ((n = RSTRING_LEN(fname)) == 0 || !*(name = RSTRING_PTR(fname))) 03750 return rb_str_new_shared(fname); 03751 03752 p = ruby_enc_find_basename(name, &f, &n, enc); 03753 if (n >= 0) { 03754 if (NIL_P(fext)) { 03755 f = n; 03756 } 03757 else { 03758 const char *fp; 03759 fp = StringValueCStr(fext); 03760 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) { 03761 f = n; 03762 } 03763 RB_GC_GUARD(fext); 03764 } 03765 if (f == RSTRING_LEN(fname)) return rb_str_new_shared(fname); 03766 } 03767 03768 basename = rb_str_new(p, f); 03769 rb_enc_copy(basename, fname); 03770 OBJ_INFECT(basename, fname); 03771 return basename; 03772 } 03773 03774 /* 03775 * call-seq: 03776 * File.dirname(file_name) -> dir_name 03777 * 03778 * Returns all components of the filename given in <i>file_name</i> 03779 * except the last one. The filename can be formed using both 03780 * <code>File::SEPARATOR</code> and <code>File::ALT_SEPARETOR</code> as the 03781 * separator when <code>File::ALT_SEPARATOR</code> is not <code>nil</code>. 03782 * 03783 * File.dirname("/home/gumby/work/ruby.rb") #=> "/home/gumby/work" 03784 */ 03785 03786 static VALUE 03787 rb_file_s_dirname(VALUE klass, VALUE fname) 03788 { 03789 return rb_file_dirname(fname); 03790 } 03791 03792 VALUE 03793 rb_file_dirname(VALUE fname) 03794 { 03795 const char *name, *root, *p, *end; 03796 VALUE dirname; 03797 rb_encoding *enc; 03798 03799 FilePathStringValue(fname); 03800 name = StringValueCStr(fname); 03801 end = name + RSTRING_LEN(fname); 03802 enc = rb_enc_get(fname); 03803 root = skiproot(name, end, enc); 03804 #ifdef DOSISH_UNC 03805 if (root > name + 1 && isdirsep(*name)) 03806 root = skipprefix(name = root - 2, end, enc); 03807 #else 03808 if (root > name + 1) 03809 name = root - 1; 03810 #endif 03811 p = strrdirsep(root, end, enc); 03812 if (!p) { 03813 p = root; 03814 } 03815 if (p == name) 03816 return rb_usascii_str_new2("."); 03817 #ifdef DOSISH_DRIVE_LETTER 03818 if (has_drive_letter(name) && isdirsep(*(name + 2))) { 03819 const char *top = skiproot(name + 2, end, enc); 03820 dirname = rb_str_new(name, 3); 03821 rb_str_cat(dirname, top, p - top); 03822 } 03823 else 03824 #endif 03825 dirname = rb_str_new(name, p - name); 03826 #ifdef DOSISH_DRIVE_LETTER 03827 if (has_drive_letter(name) && root == name + 2 && p - name == 2) 03828 rb_str_cat(dirname, ".", 1); 03829 #endif 03830 rb_enc_copy(dirname, fname); 03831 OBJ_INFECT(dirname, fname); 03832 return dirname; 03833 } 03834 03835 /* 03836 * accept a String, and return the pointer of the extension. 03837 * if len is passed, set the length of extension to it. 03838 * returned pointer is in ``name'' or NULL. 03839 * returns *len 03840 * no dot NULL 0 03841 * dotfile top 0 03842 * end with dot dot 1 03843 * .ext dot len of .ext 03844 * .ext:stream dot len of .ext without :stream (NT only) 03845 * 03846 */ 03847 const char * 03848 ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc) 03849 { 03850 const char *p, *e, *end = name + (len ? *len : (long)strlen(name)); 03851 03852 p = strrdirsep(name, end, enc); /* get the last path component */ 03853 if (!p) 03854 p = name; 03855 else 03856 do name = ++p; while (isdirsep(*p)); 03857 03858 e = 0; 03859 while (*p && *p == '.') p++; 03860 while (*p) { 03861 if (*p == '.' || istrailinggarbage(*p)) { 03862 #if USE_NTFS 03863 const char *last = p++, *dot = last; 03864 while (istrailinggarbage(*p)) { 03865 if (*p == '.') dot = p; 03866 p++; 03867 } 03868 if (!*p || *p == ':') { 03869 p = last; 03870 break; 03871 } 03872 if (*last == '.' || dot > last) e = dot; 03873 continue; 03874 #else 03875 e = p; /* get the last dot of the last component */ 03876 #endif 03877 } 03878 #if USE_NTFS 03879 else if (*p == ':') { 03880 break; 03881 } 03882 #endif 03883 else if (isdirsep(*p)) 03884 break; 03885 Inc(p, end, enc); 03886 } 03887 03888 if (len) { 03889 /* no dot, or the only dot is first or end? */ 03890 if (!e || e == name) 03891 *len = 0; 03892 else if (e+1 == p) 03893 *len = 1; 03894 else 03895 *len = p - e; 03896 } 03897 return e; 03898 } 03899 03900 /* 03901 * call-seq: 03902 * File.extname(path) -> string 03903 * 03904 * Returns the extension (the portion of file name in +path+ 03905 * starting from the last period). 03906 * 03907 * If +path+ is a dotfile, or starts with a period, then the starting 03908 * dot is not dealt with the start of the extension. 03909 * 03910 * An empty string will also be returned when the period is the last character 03911 * in +path+. 03912 * 03913 * File.extname("test.rb") #=> ".rb" 03914 * File.extname("a/b/d/test.rb") #=> ".rb" 03915 * File.extname("foo.") #=> "" 03916 * File.extname("test") #=> "" 03917 * File.extname(".profile") #=> "" 03918 * File.extname(".profile.sh") #=> ".sh" 03919 * 03920 */ 03921 03922 static VALUE 03923 rb_file_s_extname(VALUE klass, VALUE fname) 03924 { 03925 const char *name, *e; 03926 long len; 03927 VALUE extname; 03928 03929 FilePathStringValue(fname); 03930 name = StringValueCStr(fname); 03931 len = RSTRING_LEN(fname); 03932 e = ruby_enc_find_extname(name, &len, rb_enc_get(fname)); 03933 if (len <= 1) 03934 return rb_str_new(0, 0); 03935 extname = rb_str_subseq(fname, e - name, len); /* keep the dot, too! */ 03936 OBJ_INFECT(extname, fname); 03937 return extname; 03938 } 03939 03940 /* 03941 * call-seq: 03942 * File.path(path) -> string 03943 * 03944 * Returns the string representation of the path 03945 * 03946 * File.path("/dev/null") #=> "/dev/null" 03947 * File.path(Pathname.new("/tmp")) #=> "/tmp" 03948 * 03949 */ 03950 03951 static VALUE 03952 rb_file_s_path(VALUE klass, VALUE fname) 03953 { 03954 return rb_get_path(fname); 03955 } 03956 03957 /* 03958 * call-seq: 03959 * File.split(file_name) -> array 03960 * 03961 * Splits the given string into a directory and a file component and 03962 * returns them in a two-element array. See also 03963 * <code>File::dirname</code> and <code>File::basename</code>. 03964 * 03965 * File.split("/home/gumby/.profile") #=> ["/home/gumby", ".profile"] 03966 */ 03967 03968 static VALUE 03969 rb_file_s_split(VALUE klass, VALUE path) 03970 { 03971 FilePathStringValue(path); /* get rid of converting twice */ 03972 return rb_assoc_new(rb_file_s_dirname(Qnil, path), rb_file_s_basename(1,&path)); 03973 } 03974 03975 static VALUE separator; 03976 03977 static VALUE rb_file_join(VALUE ary, VALUE sep); 03978 03979 static VALUE 03980 file_inspect_join(VALUE ary, VALUE argp, int recur) 03981 { 03982 VALUE *arg = (VALUE *)argp; 03983 if (recur || ary == arg[0]) rb_raise(rb_eArgError, "recursive array"); 03984 return rb_file_join(arg[0], arg[1]); 03985 } 03986 03987 static VALUE 03988 rb_file_join(VALUE ary, VALUE sep) 03989 { 03990 long len, i; 03991 VALUE result, tmp; 03992 const char *name, *tail; 03993 int checked = TRUE; 03994 rb_encoding *enc; 03995 03996 if (RARRAY_LEN(ary) == 0) return rb_str_new(0, 0); 03997 03998 len = 1; 03999 for (i=0; i<RARRAY_LEN(ary); i++) { 04000 tmp = RARRAY_PTR(ary)[i]; 04001 if (RB_TYPE_P(tmp, T_STRING)) { 04002 check_path_encoding(tmp); 04003 len += RSTRING_LEN(tmp); 04004 } 04005 else { 04006 len += 10; 04007 } 04008 } 04009 if (!NIL_P(sep)) { 04010 StringValue(sep); 04011 len += RSTRING_LEN(sep) * (RARRAY_LEN(ary) - 1); 04012 } 04013 result = rb_str_buf_new(len); 04014 RBASIC(result)->klass = 0; 04015 OBJ_INFECT(result, ary); 04016 for (i=0; i<RARRAY_LEN(ary); i++) { 04017 tmp = RARRAY_PTR(ary)[i]; 04018 switch (TYPE(tmp)) { 04019 case T_STRING: 04020 if (!checked) check_path_encoding(tmp); 04021 StringValueCStr(tmp); 04022 break; 04023 case T_ARRAY: 04024 if (ary == tmp) { 04025 rb_raise(rb_eArgError, "recursive array"); 04026 } 04027 else { 04028 VALUE args[2]; 04029 04030 args[0] = tmp; 04031 args[1] = sep; 04032 tmp = rb_exec_recursive(file_inspect_join, ary, (VALUE)args); 04033 } 04034 break; 04035 default: 04036 FilePathStringValue(tmp); 04037 checked = FALSE; 04038 } 04039 RSTRING_GETMEM(result, name, len); 04040 if (i == 0) { 04041 rb_enc_copy(result, tmp); 04042 } 04043 else if (!NIL_P(sep)) { 04044 tail = chompdirsep(name, name + len, rb_enc_get(result)); 04045 if (RSTRING_PTR(tmp) && isdirsep(RSTRING_PTR(tmp)[0])) { 04046 rb_str_set_len(result, tail - name); 04047 } 04048 else if (!*tail) { 04049 enc = rb_enc_check(result, sep); 04050 rb_str_buf_append(result, sep); 04051 rb_enc_associate(result, enc); 04052 } 04053 } 04054 enc = rb_enc_check(result, tmp); 04055 rb_str_buf_append(result, tmp); 04056 rb_enc_associate(result, enc); 04057 } 04058 RBASIC(result)->klass = rb_cString; 04059 04060 return result; 04061 } 04062 04063 /* 04064 * call-seq: 04065 * File.join(string, ...) -> path 04066 * 04067 * Returns a new string formed by joining the strings using 04068 * <code>File::SEPARATOR</code>. 04069 * 04070 * File.join("usr", "mail", "gumby") #=> "usr/mail/gumby" 04071 * 04072 */ 04073 04074 static VALUE 04075 rb_file_s_join(VALUE klass, VALUE args) 04076 { 04077 return rb_file_join(args, separator); 04078 } 04079 04080 #if defined(HAVE_TRUNCATE) || defined(HAVE_CHSIZE) 04081 /* 04082 * call-seq: 04083 * File.truncate(file_name, integer) -> 0 04084 * 04085 * Truncates the file <i>file_name</i> to be at most <i>integer</i> 04086 * bytes long. Not available on all platforms. 04087 * 04088 * f = File.new("out", "w") 04089 * f.write("1234567890") #=> 10 04090 * f.close #=> nil 04091 * File.truncate("out", 5) #=> 0 04092 * File.size("out") #=> 5 04093 * 04094 */ 04095 04096 static VALUE 04097 rb_file_s_truncate(VALUE klass, VALUE path, VALUE len) 04098 { 04099 off_t pos; 04100 04101 rb_secure(2); 04102 pos = NUM2OFFT(len); 04103 FilePathValue(path); 04104 path = rb_str_encode_ospath(path); 04105 #ifdef HAVE_TRUNCATE 04106 if (truncate(StringValueCStr(path), pos) < 0) 04107 rb_sys_fail_path(path); 04108 #else /* defined(HAVE_CHSIZE) */ 04109 { 04110 int tmpfd; 04111 04112 if ((tmpfd = rb_cloexec_open(StringValueCStr(path), 0, 0)) < 0) { 04113 rb_sys_fail_path(path); 04114 } 04115 rb_update_max_fd(tmpfd); 04116 if (chsize(tmpfd, pos) < 0) { 04117 close(tmpfd); 04118 rb_sys_fail_path(path); 04119 } 04120 close(tmpfd); 04121 } 04122 #endif 04123 return INT2FIX(0); 04124 } 04125 #else 04126 #define rb_file_s_truncate rb_f_notimplement 04127 #endif 04128 04129 #if defined(HAVE_FTRUNCATE) || defined(HAVE_CHSIZE) 04130 /* 04131 * call-seq: 04132 * file.truncate(integer) -> 0 04133 * 04134 * Truncates <i>file</i> to at most <i>integer</i> bytes. The file 04135 * must be opened for writing. Not available on all platforms. 04136 * 04137 * f = File.new("out", "w") 04138 * f.syswrite("1234567890") #=> 10 04139 * f.truncate(5) #=> 0 04140 * f.close() #=> nil 04141 * File.size("out") #=> 5 04142 */ 04143 04144 static VALUE 04145 rb_file_truncate(VALUE obj, VALUE len) 04146 { 04147 rb_io_t *fptr; 04148 off_t pos; 04149 04150 rb_secure(2); 04151 pos = NUM2OFFT(len); 04152 GetOpenFile(obj, fptr); 04153 if (!(fptr->mode & FMODE_WRITABLE)) { 04154 rb_raise(rb_eIOError, "not opened for writing"); 04155 } 04156 rb_io_flush(obj); 04157 #ifdef HAVE_FTRUNCATE 04158 if (ftruncate(fptr->fd, pos) < 0) 04159 rb_sys_fail_path(fptr->pathv); 04160 #else /* defined(HAVE_CHSIZE) */ 04161 if (chsize(fptr->fd, pos) < 0) 04162 rb_sys_fail_path(fptr->pathv); 04163 #endif 04164 return INT2FIX(0); 04165 } 04166 #else 04167 #define rb_file_truncate rb_f_notimplement 04168 #endif 04169 04170 # ifndef LOCK_SH 04171 # define LOCK_SH 1 04172 # endif 04173 # ifndef LOCK_EX 04174 # define LOCK_EX 2 04175 # endif 04176 # ifndef LOCK_NB 04177 # define LOCK_NB 4 04178 # endif 04179 # ifndef LOCK_UN 04180 # define LOCK_UN 8 04181 # endif 04182 04183 #ifdef __CYGWIN__ 04184 #include <winerror.h> 04185 #endif 04186 04187 static VALUE 04188 rb_thread_flock(void *data) 04189 { 04190 #ifdef __CYGWIN__ 04191 int old_errno = errno; 04192 #endif 04193 int *op = data, ret = flock(op[0], op[1]); 04194 04195 #ifdef __CYGWIN__ 04196 if (GetLastError() == ERROR_NOT_LOCKED) { 04197 ret = 0; 04198 errno = old_errno; 04199 } 04200 #endif 04201 return (VALUE)ret; 04202 } 04203 04204 /* 04205 * call-seq: 04206 * file.flock(locking_constant) -> 0 or false 04207 * 04208 * Locks or unlocks a file according to <i>locking_constant</i> (a 04209 * logical <em>or</em> of the values in the table below). 04210 * Returns <code>false</code> if <code>File::LOCK_NB</code> is 04211 * specified and the operation would otherwise have blocked. Not 04212 * available on all platforms. 04213 * 04214 * Locking constants (in class File): 04215 * 04216 * LOCK_EX | Exclusive lock. Only one process may hold an 04217 * | exclusive lock for a given file at a time. 04218 * ----------+------------------------------------------------ 04219 * LOCK_NB | Don't block when locking. May be combined 04220 * | with other lock options using logical or. 04221 * ----------+------------------------------------------------ 04222 * LOCK_SH | Shared lock. Multiple processes may each hold a 04223 * | shared lock for a given file at the same time. 04224 * ----------+------------------------------------------------ 04225 * LOCK_UN | Unlock. 04226 * 04227 * Example: 04228 * 04229 * # update a counter using write lock 04230 * # don't use "w" because it truncates the file before lock. 04231 * File.open("counter", File::RDWR|File::CREAT, 0644) {|f| 04232 * f.flock(File::LOCK_EX) 04233 * value = f.read.to_i + 1 04234 * f.rewind 04235 * f.write("#{value}\n") 04236 * f.flush 04237 * f.truncate(f.pos) 04238 * } 04239 * 04240 * # read the counter using read lock 04241 * File.open("counter", "r") {|f| 04242 * f.flock(File::LOCK_SH) 04243 * p f.read 04244 * } 04245 * 04246 */ 04247 04248 static VALUE 04249 rb_file_flock(VALUE obj, VALUE operation) 04250 { 04251 rb_io_t *fptr; 04252 int op[2], op1; 04253 struct timeval time; 04254 04255 rb_secure(2); 04256 op[1] = op1 = NUM2INT(operation); 04257 GetOpenFile(obj, fptr); 04258 op[0] = fptr->fd; 04259 04260 if (fptr->mode & FMODE_WRITABLE) { 04261 rb_io_flush(obj); 04262 } 04263 while ((int)rb_thread_io_blocking_region(rb_thread_flock, op, fptr->fd) < 0) { 04264 switch (errno) { 04265 case EAGAIN: 04266 case EACCES: 04267 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN 04268 case EWOULDBLOCK: 04269 #endif 04270 if (op1 & LOCK_NB) return Qfalse; 04271 04272 time.tv_sec = 0; 04273 time.tv_usec = 100 * 1000; /* 0.1 sec */ 04274 rb_thread_wait_for(time); 04275 rb_io_check_closed(fptr); 04276 continue; 04277 04278 case EINTR: 04279 #if defined(ERESTART) 04280 case ERESTART: 04281 #endif 04282 break; 04283 04284 default: 04285 rb_sys_fail_path(fptr->pathv); 04286 } 04287 } 04288 return INT2FIX(0); 04289 } 04290 #undef flock 04291 04292 static void 04293 test_check(int n, int argc, VALUE *argv) 04294 { 04295 int i; 04296 04297 rb_secure(2); 04298 n+=1; 04299 rb_check_arity(argc, n, n); 04300 for (i=1; i<n; i++) { 04301 if (!RB_TYPE_P(argv[i], T_FILE)) { 04302 FilePathValue(argv[i]); 04303 } 04304 } 04305 } 04306 04307 #define CHECK(n) test_check((n), argc, argv) 04308 04309 /* 04310 * call-seq: 04311 * test(cmd, file1 [, file2] ) -> obj 04312 * 04313 * Uses the integer +cmd+ to perform various tests on +file1+ (first 04314 * table below) or on +file1+ and +file2+ (second table). 04315 * 04316 * File tests on a single file: 04317 * 04318 * Cmd Returns Meaning 04319 * "A" | Time | Last access time for file1 04320 * "b" | boolean | True if file1 is a block device 04321 * "c" | boolean | True if file1 is a character device 04322 * "C" | Time | Last change time for file1 04323 * "d" | boolean | True if file1 exists and is a directory 04324 * "e" | boolean | True if file1 exists 04325 * "f" | boolean | True if file1 exists and is a regular file 04326 * "g" | boolean | True if file1 has the \CF{setgid} bit 04327 * | | set (false under NT) 04328 * "G" | boolean | True if file1 exists and has a group 04329 * | | ownership equal to the caller's group 04330 * "k" | boolean | True if file1 exists and has the sticky bit set 04331 * "l" | boolean | True if file1 exists and is a symbolic link 04332 * "M" | Time | Last modification time for file1 04333 * "o" | boolean | True if file1 exists and is owned by 04334 * | | the caller's effective uid 04335 * "O" | boolean | True if file1 exists and is owned by 04336 * | | the caller's real uid 04337 * "p" | boolean | True if file1 exists and is a fifo 04338 * "r" | boolean | True if file1 is readable by the effective 04339 * | | uid/gid of the caller 04340 * "R" | boolean | True if file is readable by the real 04341 * | | uid/gid of the caller 04342 * "s" | int/nil | If file1 has nonzero size, return the size, 04343 * | | otherwise return nil 04344 * "S" | boolean | True if file1 exists and is a socket 04345 * "u" | boolean | True if file1 has the setuid bit set 04346 * "w" | boolean | True if file1 exists and is writable by 04347 * | | the effective uid/gid 04348 * "W" | boolean | True if file1 exists and is writable by 04349 * | | the real uid/gid 04350 * "x" | boolean | True if file1 exists and is executable by 04351 * | | the effective uid/gid 04352 * "X" | boolean | True if file1 exists and is executable by 04353 * | | the real uid/gid 04354 * "z" | boolean | True if file1 exists and has a zero length 04355 * 04356 * Tests that take two files: 04357 * 04358 * "-" | boolean | True if file1 and file2 are identical 04359 * "=" | boolean | True if the modification times of file1 04360 * | | and file2 are equal 04361 * "<" | boolean | True if the modification time of file1 04362 * | | is prior to that of file2 04363 * ">" | boolean | True if the modification time of file1 04364 * | | is after that of file2 04365 */ 04366 04367 static VALUE 04368 rb_f_test(int argc, VALUE *argv) 04369 { 04370 int cmd; 04371 04372 if (argc == 0) rb_check_arity(argc, 2, 3); 04373 cmd = NUM2CHR(argv[0]); 04374 if (cmd == 0) { 04375 unknown: 04376 /* unknown command */ 04377 if (ISPRINT(cmd)) { 04378 rb_raise(rb_eArgError, "unknown command '%s%c'", cmd == '\'' || cmd == '\\' ? "\\" : "", cmd); 04379 } 04380 else { 04381 rb_raise(rb_eArgError, "unknown command \"\\x%02X\"", cmd); 04382 } 04383 } 04384 if (strchr("bcdefgGkloOprRsSuwWxXz", cmd)) { 04385 CHECK(1); 04386 switch (cmd) { 04387 case 'b': 04388 return rb_file_blockdev_p(0, argv[1]); 04389 04390 case 'c': 04391 return rb_file_chardev_p(0, argv[1]); 04392 04393 case 'd': 04394 return rb_file_directory_p(0, argv[1]); 04395 04396 case 'a': 04397 case 'e': 04398 return rb_file_exist_p(0, argv[1]); 04399 04400 case 'f': 04401 return rb_file_file_p(0, argv[1]); 04402 04403 case 'g': 04404 return rb_file_sgid_p(0, argv[1]); 04405 04406 case 'G': 04407 return rb_file_grpowned_p(0, argv[1]); 04408 04409 case 'k': 04410 return rb_file_sticky_p(0, argv[1]); 04411 04412 case 'l': 04413 return rb_file_symlink_p(0, argv[1]); 04414 04415 case 'o': 04416 return rb_file_owned_p(0, argv[1]); 04417 04418 case 'O': 04419 return rb_file_rowned_p(0, argv[1]); 04420 04421 case 'p': 04422 return rb_file_pipe_p(0, argv[1]); 04423 04424 case 'r': 04425 return rb_file_readable_p(0, argv[1]); 04426 04427 case 'R': 04428 return rb_file_readable_real_p(0, argv[1]); 04429 04430 case 's': 04431 return rb_file_size_p(0, argv[1]); 04432 04433 case 'S': 04434 return rb_file_socket_p(0, argv[1]); 04435 04436 case 'u': 04437 return rb_file_suid_p(0, argv[1]); 04438 04439 case 'w': 04440 return rb_file_writable_p(0, argv[1]); 04441 04442 case 'W': 04443 return rb_file_writable_real_p(0, argv[1]); 04444 04445 case 'x': 04446 return rb_file_executable_p(0, argv[1]); 04447 04448 case 'X': 04449 return rb_file_executable_real_p(0, argv[1]); 04450 04451 case 'z': 04452 return rb_file_zero_p(0, argv[1]); 04453 } 04454 } 04455 04456 if (strchr("MAC", cmd)) { 04457 struct stat st; 04458 VALUE fname = argv[1]; 04459 04460 CHECK(1); 04461 if (rb_stat(fname, &st) == -1) { 04462 FilePathValue(fname); 04463 rb_sys_fail_path(fname); 04464 } 04465 04466 switch (cmd) { 04467 case 'A': 04468 return stat_atime(&st); 04469 case 'M': 04470 return stat_mtime(&st); 04471 case 'C': 04472 return stat_ctime(&st); 04473 } 04474 } 04475 04476 if (cmd == '-') { 04477 CHECK(2); 04478 return rb_file_identical_p(0, argv[1], argv[2]); 04479 } 04480 04481 if (strchr("=<>", cmd)) { 04482 struct stat st1, st2; 04483 04484 CHECK(2); 04485 if (rb_stat(argv[1], &st1) < 0) return Qfalse; 04486 if (rb_stat(argv[2], &st2) < 0) return Qfalse; 04487 04488 switch (cmd) { 04489 case '=': 04490 if (st1.st_mtime == st2.st_mtime) return Qtrue; 04491 return Qfalse; 04492 04493 case '>': 04494 if (st1.st_mtime > st2.st_mtime) return Qtrue; 04495 return Qfalse; 04496 04497 case '<': 04498 if (st1.st_mtime < st2.st_mtime) return Qtrue; 04499 return Qfalse; 04500 } 04501 } 04502 goto unknown; 04503 } 04504 04505 04506 /* 04507 * Document-class: File::Stat 04508 * 04509 * Objects of class <code>File::Stat</code> encapsulate common status 04510 * information for <code>File</code> objects. The information is 04511 * recorded at the moment the <code>File::Stat</code> object is 04512 * created; changes made to the file after that point will not be 04513 * reflected. <code>File::Stat</code> objects are returned by 04514 * <code>IO#stat</code>, <code>File::stat</code>, 04515 * <code>File#lstat</code>, and <code>File::lstat</code>. Many of these 04516 * methods return platform-specific values, and not all values are 04517 * meaningful on all systems. See also <code>Kernel#test</code>. 04518 */ 04519 04520 static VALUE 04521 rb_stat_s_alloc(VALUE klass) 04522 { 04523 return stat_new_0(klass, 0); 04524 } 04525 04526 /* 04527 * call-seq: 04528 * 04529 * File::Stat.new(file_name) -> stat 04530 * 04531 * Create a File::Stat object for the given file name (raising an 04532 * exception if the file doesn't exist). 04533 */ 04534 04535 static VALUE 04536 rb_stat_init(VALUE obj, VALUE fname) 04537 { 04538 struct stat st, *nst; 04539 04540 rb_secure(2); 04541 FilePathValue(fname); 04542 fname = rb_str_encode_ospath(fname); 04543 if (STAT(StringValueCStr(fname), &st) == -1) { 04544 rb_sys_fail_path(fname); 04545 } 04546 if (DATA_PTR(obj)) { 04547 xfree(DATA_PTR(obj)); 04548 DATA_PTR(obj) = NULL; 04549 } 04550 nst = ALLOC(struct stat); 04551 *nst = st; 04552 DATA_PTR(obj) = nst; 04553 04554 return Qnil; 04555 } 04556 04557 /* :nodoc: */ 04558 static VALUE 04559 rb_stat_init_copy(VALUE copy, VALUE orig) 04560 { 04561 struct stat *nst; 04562 04563 if (!OBJ_INIT_COPY(copy, orig)) return copy; 04564 if (DATA_PTR(copy)) { 04565 xfree(DATA_PTR(copy)); 04566 DATA_PTR(copy) = 0; 04567 } 04568 if (DATA_PTR(orig)) { 04569 nst = ALLOC(struct stat); 04570 *nst = *(struct stat*)DATA_PTR(orig); 04571 DATA_PTR(copy) = nst; 04572 } 04573 04574 return copy; 04575 } 04576 04577 /* 04578 * call-seq: 04579 * stat.ftype -> string 04580 * 04581 * Identifies the type of <i>stat</i>. The return string is one of: 04582 * ``<code>file</code>'', ``<code>directory</code>'', 04583 * ``<code>characterSpecial</code>'', ``<code>blockSpecial</code>'', 04584 * ``<code>fifo</code>'', ``<code>link</code>'', 04585 * ``<code>socket</code>'', or ``<code>unknown</code>''. 04586 * 04587 * File.stat("/dev/tty").ftype #=> "characterSpecial" 04588 * 04589 */ 04590 04591 static VALUE 04592 rb_stat_ftype(VALUE obj) 04593 { 04594 return rb_file_ftype(get_stat(obj)); 04595 } 04596 04597 /* 04598 * call-seq: 04599 * stat.directory? -> true or false 04600 * 04601 * Returns <code>true</code> if <i>stat</i> is a directory, 04602 * <code>false</code> otherwise. 04603 * 04604 * File.stat("testfile").directory? #=> false 04605 * File.stat(".").directory? #=> true 04606 */ 04607 04608 static VALUE 04609 rb_stat_d(VALUE obj) 04610 { 04611 if (S_ISDIR(get_stat(obj)->st_mode)) return Qtrue; 04612 return Qfalse; 04613 } 04614 04615 /* 04616 * call-seq: 04617 * stat.pipe? -> true or false 04618 * 04619 * Returns <code>true</code> if the operating system supports pipes and 04620 * <i>stat</i> is a pipe; <code>false</code> otherwise. 04621 */ 04622 04623 static VALUE 04624 rb_stat_p(VALUE obj) 04625 { 04626 #ifdef S_IFIFO 04627 if (S_ISFIFO(get_stat(obj)->st_mode)) return Qtrue; 04628 04629 #endif 04630 return Qfalse; 04631 } 04632 04633 /* 04634 * call-seq: 04635 * stat.symlink? -> true or false 04636 * 04637 * Returns <code>true</code> if <i>stat</i> is a symbolic link, 04638 * <code>false</code> if it isn't or if the operating system doesn't 04639 * support this feature. As <code>File::stat</code> automatically 04640 * follows symbolic links, <code>symlink?</code> will always be 04641 * <code>false</code> for an object returned by 04642 * <code>File::stat</code>. 04643 * 04644 * File.symlink("testfile", "alink") #=> 0 04645 * File.stat("alink").symlink? #=> false 04646 * File.lstat("alink").symlink? #=> true 04647 * 04648 */ 04649 04650 static VALUE 04651 rb_stat_l(VALUE obj) 04652 { 04653 #ifdef S_ISLNK 04654 if (S_ISLNK(get_stat(obj)->st_mode)) return Qtrue; 04655 #endif 04656 return Qfalse; 04657 } 04658 04659 /* 04660 * call-seq: 04661 * stat.socket? -> true or false 04662 * 04663 * Returns <code>true</code> if <i>stat</i> is a socket, 04664 * <code>false</code> if it isn't or if the operating system doesn't 04665 * support this feature. 04666 * 04667 * File.stat("testfile").socket? #=> false 04668 * 04669 */ 04670 04671 static VALUE 04672 rb_stat_S(VALUE obj) 04673 { 04674 #ifdef S_ISSOCK 04675 if (S_ISSOCK(get_stat(obj)->st_mode)) return Qtrue; 04676 04677 #endif 04678 return Qfalse; 04679 } 04680 04681 /* 04682 * call-seq: 04683 * stat.blockdev? -> true or false 04684 * 04685 * Returns <code>true</code> if the file is a block device, 04686 * <code>false</code> if it isn't or if the operating system doesn't 04687 * support this feature. 04688 * 04689 * File.stat("testfile").blockdev? #=> false 04690 * File.stat("/dev/hda1").blockdev? #=> true 04691 * 04692 */ 04693 04694 static VALUE 04695 rb_stat_b(VALUE obj) 04696 { 04697 #ifdef S_ISBLK 04698 if (S_ISBLK(get_stat(obj)->st_mode)) return Qtrue; 04699 04700 #endif 04701 return Qfalse; 04702 } 04703 04704 /* 04705 * call-seq: 04706 * stat.chardev? -> true or false 04707 * 04708 * Returns <code>true</code> if the file is a character device, 04709 * <code>false</code> if it isn't or if the operating system doesn't 04710 * support this feature. 04711 * 04712 * File.stat("/dev/tty").chardev? #=> true 04713 * 04714 */ 04715 04716 static VALUE 04717 rb_stat_c(VALUE obj) 04718 { 04719 if (S_ISCHR(get_stat(obj)->st_mode)) return Qtrue; 04720 04721 return Qfalse; 04722 } 04723 04724 /* 04725 * call-seq: 04726 * stat.owned? -> true or false 04727 * 04728 * Returns <code>true</code> if the effective user id of the process is 04729 * the same as the owner of <i>stat</i>. 04730 * 04731 * File.stat("testfile").owned? #=> true 04732 * File.stat("/etc/passwd").owned? #=> false 04733 * 04734 */ 04735 04736 static VALUE 04737 rb_stat_owned(VALUE obj) 04738 { 04739 if (get_stat(obj)->st_uid == geteuid()) return Qtrue; 04740 return Qfalse; 04741 } 04742 04743 static VALUE 04744 rb_stat_rowned(VALUE obj) 04745 { 04746 if (get_stat(obj)->st_uid == getuid()) return Qtrue; 04747 return Qfalse; 04748 } 04749 04750 /* 04751 * call-seq: 04752 * stat.grpowned? -> true or false 04753 * 04754 * Returns true if the effective group id of the process is the same as 04755 * the group id of <i>stat</i>. On Windows NT, returns <code>false</code>. 04756 * 04757 * File.stat("testfile").grpowned? #=> true 04758 * File.stat("/etc/passwd").grpowned? #=> false 04759 * 04760 */ 04761 04762 static VALUE 04763 rb_stat_grpowned(VALUE obj) 04764 { 04765 #ifndef _WIN32 04766 if (rb_group_member(get_stat(obj)->st_gid)) return Qtrue; 04767 #endif 04768 return Qfalse; 04769 } 04770 04771 /* 04772 * call-seq: 04773 * stat.readable? -> true or false 04774 * 04775 * Returns <code>true</code> if <i>stat</i> is readable by the 04776 * effective user id of this process. 04777 * 04778 * File.stat("testfile").readable? #=> true 04779 * 04780 */ 04781 04782 static VALUE 04783 rb_stat_r(VALUE obj) 04784 { 04785 struct stat *st = get_stat(obj); 04786 04787 #ifdef USE_GETEUID 04788 if (geteuid() == 0) return Qtrue; 04789 #endif 04790 #ifdef S_IRUSR 04791 if (rb_stat_owned(obj)) 04792 return st->st_mode & S_IRUSR ? Qtrue : Qfalse; 04793 #endif 04794 #ifdef S_IRGRP 04795 if (rb_stat_grpowned(obj)) 04796 return st->st_mode & S_IRGRP ? Qtrue : Qfalse; 04797 #endif 04798 #ifdef S_IROTH 04799 if (!(st->st_mode & S_IROTH)) return Qfalse; 04800 #endif 04801 return Qtrue; 04802 } 04803 04804 /* 04805 * call-seq: 04806 * stat.readable_real? -> true or false 04807 * 04808 * Returns <code>true</code> if <i>stat</i> is readable by the real 04809 * user id of this process. 04810 * 04811 * File.stat("testfile").readable_real? #=> true 04812 * 04813 */ 04814 04815 static VALUE 04816 rb_stat_R(VALUE obj) 04817 { 04818 struct stat *st = get_stat(obj); 04819 04820 #ifdef USE_GETEUID 04821 if (getuid() == 0) return Qtrue; 04822 #endif 04823 #ifdef S_IRUSR 04824 if (rb_stat_rowned(obj)) 04825 return st->st_mode & S_IRUSR ? Qtrue : Qfalse; 04826 #endif 04827 #ifdef S_IRGRP 04828 if (rb_group_member(get_stat(obj)->st_gid)) 04829 return st->st_mode & S_IRGRP ? Qtrue : Qfalse; 04830 #endif 04831 #ifdef S_IROTH 04832 if (!(st->st_mode & S_IROTH)) return Qfalse; 04833 #endif 04834 return Qtrue; 04835 } 04836 04837 /* 04838 * call-seq: 04839 * stat.world_readable? -> fixnum or nil 04840 * 04841 * If <i>stat</i> is readable by others, returns an integer 04842 * representing the file permission bits of <i>stat</i>. Returns 04843 * <code>nil</code> otherwise. The meaning of the bits is platform 04844 * dependent; on Unix systems, see <code>stat(2)</code>. 04845 * 04846 * m = File.stat("/etc/passwd").world_readable? #=> 420 04847 * sprintf("%o", m) #=> "644" 04848 */ 04849 04850 static VALUE 04851 rb_stat_wr(VALUE obj) 04852 { 04853 #ifdef S_IROTH 04854 if ((get_stat(obj)->st_mode & (S_IROTH)) == S_IROTH) { 04855 return UINT2NUM(get_stat(obj)->st_mode & (S_IRUGO|S_IWUGO|S_IXUGO)); 04856 } 04857 else { 04858 return Qnil; 04859 } 04860 #endif 04861 } 04862 04863 /* 04864 * call-seq: 04865 * stat.writable? -> true or false 04866 * 04867 * Returns <code>true</code> if <i>stat</i> is writable by the 04868 * effective user id of this process. 04869 * 04870 * File.stat("testfile").writable? #=> true 04871 * 04872 */ 04873 04874 static VALUE 04875 rb_stat_w(VALUE obj) 04876 { 04877 struct stat *st = get_stat(obj); 04878 04879 #ifdef USE_GETEUID 04880 if (geteuid() == 0) return Qtrue; 04881 #endif 04882 #ifdef S_IWUSR 04883 if (rb_stat_owned(obj)) 04884 return st->st_mode & S_IWUSR ? Qtrue : Qfalse; 04885 #endif 04886 #ifdef S_IWGRP 04887 if (rb_stat_grpowned(obj)) 04888 return st->st_mode & S_IWGRP ? Qtrue : Qfalse; 04889 #endif 04890 #ifdef S_IWOTH 04891 if (!(st->st_mode & S_IWOTH)) return Qfalse; 04892 #endif 04893 return Qtrue; 04894 } 04895 04896 /* 04897 * call-seq: 04898 * stat.writable_real? -> true or false 04899 * 04900 * Returns <code>true</code> if <i>stat</i> is writable by the real 04901 * user id of this process. 04902 * 04903 * File.stat("testfile").writable_real? #=> true 04904 * 04905 */ 04906 04907 static VALUE 04908 rb_stat_W(VALUE obj) 04909 { 04910 struct stat *st = get_stat(obj); 04911 04912 #ifdef USE_GETEUID 04913 if (getuid() == 0) return Qtrue; 04914 #endif 04915 #ifdef S_IWUSR 04916 if (rb_stat_rowned(obj)) 04917 return st->st_mode & S_IWUSR ? Qtrue : Qfalse; 04918 #endif 04919 #ifdef S_IWGRP 04920 if (rb_group_member(get_stat(obj)->st_gid)) 04921 return st->st_mode & S_IWGRP ? Qtrue : Qfalse; 04922 #endif 04923 #ifdef S_IWOTH 04924 if (!(st->st_mode & S_IWOTH)) return Qfalse; 04925 #endif 04926 return Qtrue; 04927 } 04928 04929 /* 04930 * call-seq: 04931 * stat.world_writable? -> fixnum or nil 04932 * 04933 * If <i>stat</i> is writable by others, returns an integer 04934 * representing the file permission bits of <i>stat</i>. Returns 04935 * <code>nil</code> otherwise. The meaning of the bits is platform 04936 * dependent; on Unix systems, see <code>stat(2)</code>. 04937 * 04938 * m = File.stat("/tmp").world_writable? #=> 511 04939 * sprintf("%o", m) #=> "777" 04940 */ 04941 04942 static VALUE 04943 rb_stat_ww(VALUE obj) 04944 { 04945 #ifdef S_IROTH 04946 if ((get_stat(obj)->st_mode & (S_IWOTH)) == S_IWOTH) { 04947 return UINT2NUM(get_stat(obj)->st_mode & (S_IRUGO|S_IWUGO|S_IXUGO)); 04948 } 04949 else { 04950 return Qnil; 04951 } 04952 #endif 04953 } 04954 04955 /* 04956 * call-seq: 04957 * stat.executable? -> true or false 04958 * 04959 * Returns <code>true</code> if <i>stat</i> is executable or if the 04960 * operating system doesn't distinguish executable files from 04961 * nonexecutable files. The tests are made using the effective owner of 04962 * the process. 04963 * 04964 * File.stat("testfile").executable? #=> false 04965 * 04966 */ 04967 04968 static VALUE 04969 rb_stat_x(VALUE obj) 04970 { 04971 struct stat *st = get_stat(obj); 04972 04973 #ifdef USE_GETEUID 04974 if (geteuid() == 0) { 04975 return st->st_mode & S_IXUGO ? Qtrue : Qfalse; 04976 } 04977 #endif 04978 #ifdef S_IXUSR 04979 if (rb_stat_owned(obj)) 04980 return st->st_mode & S_IXUSR ? Qtrue : Qfalse; 04981 #endif 04982 #ifdef S_IXGRP 04983 if (rb_stat_grpowned(obj)) 04984 return st->st_mode & S_IXGRP ? Qtrue : Qfalse; 04985 #endif 04986 #ifdef S_IXOTH 04987 if (!(st->st_mode & S_IXOTH)) return Qfalse; 04988 #endif 04989 return Qtrue; 04990 } 04991 04992 /* 04993 * call-seq: 04994 * stat.executable_real? -> true or false 04995 * 04996 * Same as <code>executable?</code>, but tests using the real owner of 04997 * the process. 04998 */ 04999 05000 static VALUE 05001 rb_stat_X(VALUE obj) 05002 { 05003 struct stat *st = get_stat(obj); 05004 05005 #ifdef USE_GETEUID 05006 if (getuid() == 0) { 05007 return st->st_mode & S_IXUGO ? Qtrue : Qfalse; 05008 } 05009 #endif 05010 #ifdef S_IXUSR 05011 if (rb_stat_rowned(obj)) 05012 return st->st_mode & S_IXUSR ? Qtrue : Qfalse; 05013 #endif 05014 #ifdef S_IXGRP 05015 if (rb_group_member(get_stat(obj)->st_gid)) 05016 return st->st_mode & S_IXGRP ? Qtrue : Qfalse; 05017 #endif 05018 #ifdef S_IXOTH 05019 if (!(st->st_mode & S_IXOTH)) return Qfalse; 05020 #endif 05021 return Qtrue; 05022 } 05023 05024 /* 05025 * call-seq: 05026 * stat.file? -> true or false 05027 * 05028 * Returns <code>true</code> if <i>stat</i> is a regular file (not 05029 * a device file, pipe, socket, etc.). 05030 * 05031 * File.stat("testfile").file? #=> true 05032 * 05033 */ 05034 05035 static VALUE 05036 rb_stat_f(VALUE obj) 05037 { 05038 if (S_ISREG(get_stat(obj)->st_mode)) return Qtrue; 05039 return Qfalse; 05040 } 05041 05042 /* 05043 * call-seq: 05044 * stat.zero? -> true or false 05045 * 05046 * Returns <code>true</code> if <i>stat</i> is a zero-length file; 05047 * <code>false</code> otherwise. 05048 * 05049 * File.stat("testfile").zero? #=> false 05050 * 05051 */ 05052 05053 static VALUE 05054 rb_stat_z(VALUE obj) 05055 { 05056 if (get_stat(obj)->st_size == 0) return Qtrue; 05057 return Qfalse; 05058 } 05059 05060 /* 05061 * call-seq: 05062 * state.size -> integer 05063 * 05064 * Returns the size of <i>stat</i> in bytes. 05065 * 05066 * File.stat("testfile").size #=> 66 05067 * 05068 */ 05069 05070 static VALUE 05071 rb_stat_s(VALUE obj) 05072 { 05073 off_t size = get_stat(obj)->st_size; 05074 05075 if (size == 0) return Qnil; 05076 return OFFT2NUM(size); 05077 } 05078 05079 /* 05080 * call-seq: 05081 * stat.setuid? -> true or false 05082 * 05083 * Returns <code>true</code> if <i>stat</i> has the set-user-id 05084 * permission bit set, <code>false</code> if it doesn't or if the 05085 * operating system doesn't support this feature. 05086 * 05087 * File.stat("/bin/su").setuid? #=> true 05088 */ 05089 05090 static VALUE 05091 rb_stat_suid(VALUE obj) 05092 { 05093 #ifdef S_ISUID 05094 if (get_stat(obj)->st_mode & S_ISUID) return Qtrue; 05095 #endif 05096 return Qfalse; 05097 } 05098 05099 /* 05100 * call-seq: 05101 * stat.setgid? -> true or false 05102 * 05103 * Returns <code>true</code> if <i>stat</i> has the set-group-id 05104 * permission bit set, <code>false</code> if it doesn't or if the 05105 * operating system doesn't support this feature. 05106 * 05107 * File.stat("/usr/sbin/lpc").setgid? #=> true 05108 * 05109 */ 05110 05111 static VALUE 05112 rb_stat_sgid(VALUE obj) 05113 { 05114 #ifdef S_ISGID 05115 if (get_stat(obj)->st_mode & S_ISGID) return Qtrue; 05116 #endif 05117 return Qfalse; 05118 } 05119 05120 /* 05121 * call-seq: 05122 * stat.sticky? -> true or false 05123 * 05124 * Returns <code>true</code> if <i>stat</i> has its sticky bit set, 05125 * <code>false</code> if it doesn't or if the operating system doesn't 05126 * support this feature. 05127 * 05128 * File.stat("testfile").sticky? #=> false 05129 * 05130 */ 05131 05132 static VALUE 05133 rb_stat_sticky(VALUE obj) 05134 { 05135 #ifdef S_ISVTX 05136 if (get_stat(obj)->st_mode & S_ISVTX) return Qtrue; 05137 #endif 05138 return Qfalse; 05139 } 05140 05141 VALUE rb_mFConst; 05142 05143 void 05144 rb_file_const(const char *name, VALUE value) 05145 { 05146 rb_define_const(rb_mFConst, name, value); 05147 } 05148 05149 int 05150 rb_is_absolute_path(const char *path) 05151 { 05152 #ifdef DOSISH_DRIVE_LETTER 05153 if (has_drive_letter(path) && isdirsep(path[2])) return 1; 05154 #endif 05155 #ifdef DOSISH_UNC 05156 if (isdirsep(path[0]) && isdirsep(path[1])) return 1; 05157 #endif 05158 #ifndef DOSISH 05159 if (path[0] == '/') return 1; 05160 #endif 05161 return 0; 05162 } 05163 05164 #ifndef ENABLE_PATH_CHECK 05165 # if defined DOSISH || defined __CYGWIN__ 05166 # define ENABLE_PATH_CHECK 0 05167 # else 05168 # define ENABLE_PATH_CHECK 1 05169 # endif 05170 #endif 05171 05172 #if ENABLE_PATH_CHECK 05173 static int 05174 path_check_0(VALUE path, int execpath) 05175 { 05176 struct stat st; 05177 const char *p0 = StringValueCStr(path); 05178 const char *e0; 05179 rb_encoding *enc; 05180 char *p = 0, *s; 05181 05182 if (!rb_is_absolute_path(p0)) { 05183 char *buf = my_getcwd(); 05184 VALUE newpath; 05185 05186 newpath = rb_str_new2(buf); 05187 xfree(buf); 05188 05189 rb_str_cat2(newpath, "/"); 05190 rb_str_cat2(newpath, p0); 05191 path = newpath; 05192 p0 = RSTRING_PTR(path); 05193 } 05194 e0 = p0 + RSTRING_LEN(path); 05195 enc = rb_enc_get(path); 05196 for (;;) { 05197 #ifndef S_IWOTH 05198 # define S_IWOTH 002 05199 #endif 05200 if (STAT(p0, &st) == 0 && S_ISDIR(st.st_mode) && (st.st_mode & S_IWOTH) 05201 #ifdef S_ISVTX 05202 && !(p && execpath && (st.st_mode & S_ISVTX)) 05203 #endif 05204 && !access(p0, W_OK)) { 05205 rb_warn("Insecure world writable dir %s in %sPATH, mode 0%" 05206 PRI_MODET_PREFIX"o", 05207 p0, (execpath ? "" : "LOAD_"), st.st_mode); 05208 if (p) *p = '/'; 05209 RB_GC_GUARD(path); 05210 return 0; 05211 } 05212 s = strrdirsep(p0, e0, enc); 05213 if (p) *p = '/'; 05214 if (!s || s == p0) return 1; 05215 p = s; 05216 e0 = p; 05217 *p = '\0'; 05218 } 05219 } 05220 #endif 05221 05222 #if ENABLE_PATH_CHECK 05223 #define fpath_check(path) path_check_0((path), FALSE) 05224 #else 05225 #define fpath_check(path) 1 05226 #endif 05227 05228 int 05229 rb_path_check(const char *path) 05230 { 05231 #if ENABLE_PATH_CHECK 05232 const char *p0, *p, *pend; 05233 const char sep = PATH_SEP_CHAR; 05234 05235 if (!path) return 1; 05236 05237 pend = path + strlen(path); 05238 p0 = path; 05239 p = strchr(path, sep); 05240 if (!p) p = pend; 05241 05242 for (;;) { 05243 if (!path_check_0(rb_str_new(p0, p - p0), TRUE)) { 05244 return 0; /* not safe */ 05245 } 05246 p0 = p + 1; 05247 if (p0 > pend) break; 05248 p = strchr(p0, sep); 05249 if (!p) p = pend; 05250 } 05251 #endif 05252 return 1; 05253 } 05254 05255 #ifndef _WIN32 05256 #ifdef __native_client__ 05257 __attribute__((noinline)) 05258 #endif 05259 int 05260 rb_file_load_ok(const char *path) 05261 { 05262 int ret = 1; 05263 int fd = rb_cloexec_open(path, O_RDONLY, 0); 05264 if (fd == -1) return 0; 05265 rb_update_max_fd(fd); 05266 #if !defined DOSISH 05267 { 05268 struct stat st; 05269 if (fstat(fd, &st) || !S_ISREG(st.st_mode)) { 05270 ret = 0; 05271 } 05272 } 05273 #endif 05274 (void)close(fd); 05275 return ret; 05276 } 05277 #endif 05278 05279 static int 05280 is_explicit_relative(const char *path) 05281 { 05282 if (*path++ != '.') return 0; 05283 if (*path == '.') path++; 05284 return isdirsep(*path); 05285 } 05286 05287 static VALUE 05288 copy_path_class(VALUE path, VALUE orig) 05289 { 05290 RBASIC(path)->klass = rb_obj_class(orig); 05291 OBJ_FREEZE(path); 05292 return path; 05293 } 05294 05295 int 05296 rb_find_file_ext(VALUE *filep, const char *const *ext) 05297 { 05298 return rb_find_file_ext_safe(filep, ext, rb_safe_level()); 05299 } 05300 05301 int 05302 rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level) 05303 { 05304 const char *f = StringValueCStr(*filep); 05305 VALUE fname = *filep, load_path, tmp; 05306 long i, j, fnlen; 05307 int expanded = 0; 05308 05309 if (!ext[0]) return 0; 05310 05311 if (f[0] == '~') { 05312 fname = file_expand_path_1(fname); 05313 if (safe_level >= 1 && OBJ_TAINTED(fname)) { 05314 rb_raise(rb_eSecurityError, "loading from unsafe file %s", f); 05315 } 05316 f = RSTRING_PTR(fname); 05317 *filep = fname; 05318 expanded = 1; 05319 } 05320 05321 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) { 05322 if (safe_level >= 1 && !fpath_check(fname)) { 05323 rb_raise(rb_eSecurityError, "loading from unsafe path %s", f); 05324 } 05325 if (!expanded) fname = file_expand_path_1(fname); 05326 fnlen = RSTRING_LEN(fname); 05327 for (i=0; ext[i]; i++) { 05328 rb_str_cat2(fname, ext[i]); 05329 if (rb_file_load_ok(RSTRING_PTR(fname))) { 05330 *filep = copy_path_class(fname, *filep); 05331 return (int)(i+1); 05332 } 05333 rb_str_set_len(fname, fnlen); 05334 } 05335 return 0; 05336 } 05337 05338 if (safe_level >= 4) { 05339 rb_raise(rb_eSecurityError, "loading from non-absolute path %s", f); 05340 } 05341 05342 RB_GC_GUARD(load_path) = rb_get_expanded_load_path(); 05343 if (!load_path) return 0; 05344 05345 fname = rb_str_dup(*filep); 05346 RBASIC(fname)->klass = 0; 05347 fnlen = RSTRING_LEN(fname); 05348 tmp = rb_str_tmp_new(MAXPATHLEN + 2); 05349 rb_enc_associate_index(tmp, rb_usascii_encindex()); 05350 for (j=0; ext[j]; j++) { 05351 rb_str_cat2(fname, ext[j]); 05352 for (i = 0; i < RARRAY_LEN(load_path); i++) { 05353 VALUE str = RARRAY_PTR(load_path)[i]; 05354 05355 RB_GC_GUARD(str) = rb_get_path_check(str, safe_level); 05356 if (RSTRING_LEN(str) == 0) continue; 05357 rb_file_expand_path_internal(fname, str, 0, 0, tmp); 05358 if (rb_file_load_ok(RSTRING_PTR(tmp))) { 05359 *filep = copy_path_class(tmp, *filep); 05360 return (int)(j+1); 05361 } 05362 FL_UNSET(tmp, FL_TAINT | FL_UNTRUSTED); 05363 } 05364 rb_str_set_len(fname, fnlen); 05365 } 05366 RB_GC_GUARD(load_path); 05367 return 0; 05368 } 05369 05370 VALUE 05371 rb_find_file(VALUE path) 05372 { 05373 return rb_find_file_safe(path, rb_safe_level()); 05374 } 05375 05376 VALUE 05377 rb_find_file_safe(VALUE path, int safe_level) 05378 { 05379 VALUE tmp, load_path; 05380 const char *f = StringValueCStr(path); 05381 int expanded = 0; 05382 05383 if (f[0] == '~') { 05384 tmp = file_expand_path_1(path); 05385 if (safe_level >= 1 && OBJ_TAINTED(tmp)) { 05386 rb_raise(rb_eSecurityError, "loading from unsafe file %s", f); 05387 } 05388 path = copy_path_class(tmp, path); 05389 f = RSTRING_PTR(path); 05390 expanded = 1; 05391 } 05392 05393 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) { 05394 if (safe_level >= 1 && !fpath_check(path)) { 05395 rb_raise(rb_eSecurityError, "loading from unsafe path %s", f); 05396 } 05397 if (!rb_file_load_ok(f)) return 0; 05398 if (!expanded) 05399 path = copy_path_class(file_expand_path_1(path), path); 05400 return path; 05401 } 05402 05403 if (safe_level >= 4) { 05404 rb_raise(rb_eSecurityError, "loading from non-absolute path %s", f); 05405 } 05406 05407 RB_GC_GUARD(load_path) = rb_get_expanded_load_path(); 05408 if (load_path) { 05409 long i; 05410 05411 tmp = rb_str_tmp_new(MAXPATHLEN + 2); 05412 rb_enc_associate_index(tmp, rb_usascii_encindex()); 05413 for (i = 0; i < RARRAY_LEN(load_path); i++) { 05414 VALUE str = RARRAY_PTR(load_path)[i]; 05415 RB_GC_GUARD(str) = rb_get_path_check(str, safe_level); 05416 if (RSTRING_LEN(str) > 0) { 05417 rb_file_expand_path_internal(path, str, 0, 0, tmp); 05418 f = RSTRING_PTR(tmp); 05419 if (rb_file_load_ok(f)) goto found; 05420 } 05421 } 05422 return 0; 05423 } 05424 else { 05425 return 0; /* no path, no load */ 05426 } 05427 05428 found: 05429 if (safe_level >= 1 && !fpath_check(tmp)) { 05430 rb_raise(rb_eSecurityError, "loading from unsafe file %s", f); 05431 } 05432 05433 return copy_path_class(tmp, path); 05434 } 05435 05436 static void 05437 define_filetest_function(const char *name, VALUE (*func)(ANYARGS), int argc) 05438 { 05439 rb_define_module_function(rb_mFileTest, name, func, argc); 05440 rb_define_singleton_method(rb_cFile, name, func, argc); 05441 } 05442 05443 static const char null_device[] = 05444 #if defined DOSISH 05445 "NUL" 05446 #elif defined AMIGA || defined __amigaos__ 05447 "NIL" 05448 #elif defined __VMS 05449 "NL:" 05450 #else 05451 "/dev/null" 05452 #endif 05453 ; 05454 05455 /* 05456 * A <code>File</code> is an abstraction of any file object accessible 05457 * by the program and is closely associated with class <code>IO</code> 05458 * <code>File</code> includes the methods of module 05459 * <code>FileTest</code> as class methods, allowing you to write (for 05460 * example) <code>File.exist?("foo")</code>. 05461 * 05462 * In the description of File methods, 05463 * <em>permission bits</em> are a platform-specific 05464 * set of bits that indicate permissions of a file. On Unix-based 05465 * systems, permissions are viewed as a set of three octets, for the 05466 * owner, the group, and the rest of the world. For each of these 05467 * entities, permissions may be set to read, write, or execute the 05468 * file: 05469 * 05470 * The permission bits <code>0644</code> (in octal) would thus be 05471 * interpreted as read/write for owner, and read-only for group and 05472 * other. Higher-order bits may also be used to indicate the type of 05473 * file (plain, directory, pipe, socket, and so on) and various other 05474 * special features. If the permissions are for a directory, the 05475 * meaning of the execute bit changes; when set the directory can be 05476 * searched. 05477 * 05478 * On non-Posix operating systems, there may be only the ability to 05479 * make a file read-only or read-write. In this case, the remaining 05480 * permission bits will be synthesized to resemble typical values. For 05481 * instance, on Windows NT the default permission bits are 05482 * <code>0644</code>, which means read/write for owner, read-only for 05483 * all others. The only change that can be made is to make the file 05484 * read-only, which is reported as <code>0444</code>. 05485 */ 05486 05487 void 05488 Init_File(void) 05489 { 05490 rb_mFileTest = rb_define_module("FileTest"); 05491 rb_cFile = rb_define_class("File", rb_cIO); 05492 05493 define_filetest_function("directory?", rb_file_directory_p, 1); 05494 define_filetest_function("exist?", rb_file_exist_p, 1); 05495 define_filetest_function("exists?", rb_file_exist_p, 1); 05496 define_filetest_function("readable?", rb_file_readable_p, 1); 05497 define_filetest_function("readable_real?", rb_file_readable_real_p, 1); 05498 define_filetest_function("world_readable?", rb_file_world_readable_p, 1); 05499 define_filetest_function("writable?", rb_file_writable_p, 1); 05500 define_filetest_function("writable_real?", rb_file_writable_real_p, 1); 05501 define_filetest_function("world_writable?", rb_file_world_writable_p, 1); 05502 define_filetest_function("executable?", rb_file_executable_p, 1); 05503 define_filetest_function("executable_real?", rb_file_executable_real_p, 1); 05504 define_filetest_function("file?", rb_file_file_p, 1); 05505 define_filetest_function("zero?", rb_file_zero_p, 1); 05506 define_filetest_function("size?", rb_file_size_p, 1); 05507 define_filetest_function("size", rb_file_s_size, 1); 05508 define_filetest_function("owned?", rb_file_owned_p, 1); 05509 define_filetest_function("grpowned?", rb_file_grpowned_p, 1); 05510 05511 define_filetest_function("pipe?", rb_file_pipe_p, 1); 05512 define_filetest_function("symlink?", rb_file_symlink_p, 1); 05513 define_filetest_function("socket?", rb_file_socket_p, 1); 05514 05515 define_filetest_function("blockdev?", rb_file_blockdev_p, 1); 05516 define_filetest_function("chardev?", rb_file_chardev_p, 1); 05517 05518 define_filetest_function("setuid?", rb_file_suid_p, 1); 05519 define_filetest_function("setgid?", rb_file_sgid_p, 1); 05520 define_filetest_function("sticky?", rb_file_sticky_p, 1); 05521 05522 define_filetest_function("identical?", rb_file_identical_p, 2); 05523 05524 rb_define_singleton_method(rb_cFile, "stat", rb_file_s_stat, 1); 05525 rb_define_singleton_method(rb_cFile, "lstat", rb_file_s_lstat, 1); 05526 rb_define_singleton_method(rb_cFile, "ftype", rb_file_s_ftype, 1); 05527 05528 rb_define_singleton_method(rb_cFile, "atime", rb_file_s_atime, 1); 05529 rb_define_singleton_method(rb_cFile, "mtime", rb_file_s_mtime, 1); 05530 rb_define_singleton_method(rb_cFile, "ctime", rb_file_s_ctime, 1); 05531 05532 rb_define_singleton_method(rb_cFile, "utime", rb_file_s_utime, -1); 05533 rb_define_singleton_method(rb_cFile, "chmod", rb_file_s_chmod, -1); 05534 rb_define_singleton_method(rb_cFile, "chown", rb_file_s_chown, -1); 05535 rb_define_singleton_method(rb_cFile, "lchmod", rb_file_s_lchmod, -1); 05536 rb_define_singleton_method(rb_cFile, "lchown", rb_file_s_lchown, -1); 05537 05538 rb_define_singleton_method(rb_cFile, "link", rb_file_s_link, 2); 05539 rb_define_singleton_method(rb_cFile, "symlink", rb_file_s_symlink, 2); 05540 rb_define_singleton_method(rb_cFile, "readlink", rb_file_s_readlink, 1); 05541 05542 rb_define_singleton_method(rb_cFile, "unlink", rb_file_s_unlink, -2); 05543 rb_define_singleton_method(rb_cFile, "delete", rb_file_s_unlink, -2); 05544 rb_define_singleton_method(rb_cFile, "rename", rb_file_s_rename, 2); 05545 rb_define_singleton_method(rb_cFile, "umask", rb_file_s_umask, -1); 05546 rb_define_singleton_method(rb_cFile, "truncate", rb_file_s_truncate, 2); 05547 rb_define_singleton_method(rb_cFile, "expand_path", rb_file_s_expand_path, -1); 05548 rb_define_singleton_method(rb_cFile, "absolute_path", rb_file_s_absolute_path, -1); 05549 rb_define_singleton_method(rb_cFile, "realpath", rb_file_s_realpath, -1); 05550 rb_define_singleton_method(rb_cFile, "realdirpath", rb_file_s_realdirpath, -1); 05551 rb_define_singleton_method(rb_cFile, "basename", rb_file_s_basename, -1); 05552 rb_define_singleton_method(rb_cFile, "dirname", rb_file_s_dirname, 1); 05553 rb_define_singleton_method(rb_cFile, "extname", rb_file_s_extname, 1); 05554 rb_define_singleton_method(rb_cFile, "path", rb_file_s_path, 1); 05555 05556 separator = rb_obj_freeze(rb_usascii_str_new2("/")); 05557 rb_define_const(rb_cFile, "Separator", separator); 05558 rb_define_const(rb_cFile, "SEPARATOR", separator); 05559 rb_define_singleton_method(rb_cFile, "split", rb_file_s_split, 1); 05560 rb_define_singleton_method(rb_cFile, "join", rb_file_s_join, -2); 05561 05562 #ifdef DOSISH 05563 rb_define_const(rb_cFile, "ALT_SEPARATOR", rb_obj_freeze(rb_usascii_str_new2(file_alt_separator))); 05564 #else 05565 rb_define_const(rb_cFile, "ALT_SEPARATOR", Qnil); 05566 #endif 05567 rb_define_const(rb_cFile, "PATH_SEPARATOR", rb_obj_freeze(rb_str_new2(PATH_SEP))); 05568 05569 rb_define_method(rb_cIO, "stat", rb_io_stat, 0); /* this is IO's method */ 05570 rb_define_method(rb_cFile, "lstat", rb_file_lstat, 0); 05571 05572 rb_define_method(rb_cFile, "atime", rb_file_atime, 0); 05573 rb_define_method(rb_cFile, "mtime", rb_file_mtime, 0); 05574 rb_define_method(rb_cFile, "ctime", rb_file_ctime, 0); 05575 rb_define_method(rb_cFile, "size", rb_file_size, 0); 05576 05577 rb_define_method(rb_cFile, "chmod", rb_file_chmod, 1); 05578 rb_define_method(rb_cFile, "chown", rb_file_chown, 2); 05579 rb_define_method(rb_cFile, "truncate", rb_file_truncate, 1); 05580 05581 rb_define_method(rb_cFile, "flock", rb_file_flock, 1); 05582 05583 /* 05584 * Document-module: File::Constants 05585 * 05586 * File::Constants provides file-related constants. All possible 05587 * file constants are listed in the documentation but they may not all 05588 * be present on your platform. 05589 * 05590 * If the underlying platform doesn't define a constant the corresponding 05591 * Ruby constant is not defined. 05592 * 05593 * Your platform documentations (e.g. man open(2)) may describe more 05594 * detailed information. 05595 */ 05596 rb_mFConst = rb_define_module_under(rb_cFile, "Constants"); 05597 rb_include_module(rb_cIO, rb_mFConst); 05598 05599 /* open for reading only */ 05600 rb_define_const(rb_mFConst, "RDONLY", INT2FIX(O_RDONLY)); 05601 /* open for writing only */ 05602 rb_define_const(rb_mFConst, "WRONLY", INT2FIX(O_WRONLY)); 05603 /* open for reading and writing */ 05604 rb_define_const(rb_mFConst, "RDWR", INT2FIX(O_RDWR)); 05605 /* append on each write */ 05606 rb_define_const(rb_mFConst, "APPEND", INT2FIX(O_APPEND)); 05607 /* create file if it does not exist */ 05608 rb_define_const(rb_mFConst, "CREAT", INT2FIX(O_CREAT)); 05609 /* error if CREAT and the file exists */ 05610 rb_define_const(rb_mFConst, "EXCL", INT2FIX(O_EXCL)); 05611 #if defined(O_NDELAY) || defined(O_NONBLOCK) 05612 # ifndef O_NONBLOCK 05613 # define O_NONBLOCK O_NDELAY 05614 # endif 05615 /* do not block on open or for data to become available */ 05616 rb_define_const(rb_mFConst, "NONBLOCK", INT2FIX(O_NONBLOCK)); 05617 #endif 05618 /* truncate size to 0 */ 05619 rb_define_const(rb_mFConst, "TRUNC", INT2FIX(O_TRUNC)); 05620 #ifdef O_NOCTTY 05621 /* not to make opened IO the controlling terminal device */ 05622 rb_define_const(rb_mFConst, "NOCTTY", INT2FIX(O_NOCTTY)); 05623 #endif 05624 #ifndef O_BINARY 05625 # define O_BINARY 0 05626 #endif 05627 /* disable line code conversion */ 05628 rb_define_const(rb_mFConst, "BINARY", INT2FIX(O_BINARY)); 05629 #ifdef O_SYNC 05630 /* any write operation perform synchronously */ 05631 rb_define_const(rb_mFConst, "SYNC", INT2FIX(O_SYNC)); 05632 #endif 05633 #ifdef O_DSYNC 05634 /* any write operation perform synchronously except some meta data */ 05635 rb_define_const(rb_mFConst, "DSYNC", INT2FIX(O_DSYNC)); 05636 #endif 05637 #ifdef O_RSYNC 05638 /* any read operation perform synchronously. used with SYNC or DSYNC. */ 05639 rb_define_const(rb_mFConst, "RSYNC", INT2FIX(O_RSYNC)); 05640 #endif 05641 #ifdef O_NOFOLLOW 05642 /* do not follow symlinks */ 05643 rb_define_const(rb_mFConst, "NOFOLLOW", INT2FIX(O_NOFOLLOW)); /* FreeBSD, Linux */ 05644 #endif 05645 #ifdef O_NOATIME 05646 /* do not change atime */ 05647 rb_define_const(rb_mFConst, "NOATIME", INT2FIX(O_NOATIME)); /* Linux */ 05648 #endif 05649 #ifdef O_DIRECT 05650 /* Try to minimize cache effects of the I/O to and from this file. */ 05651 rb_define_const(rb_mFConst, "DIRECT", INT2FIX(O_DIRECT)); 05652 #endif 05653 05654 /* shared lock. see File#flock */ 05655 rb_define_const(rb_mFConst, "LOCK_SH", INT2FIX(LOCK_SH)); 05656 /* exclusive lock. see File#flock */ 05657 rb_define_const(rb_mFConst, "LOCK_EX", INT2FIX(LOCK_EX)); 05658 /* unlock. see File#flock */ 05659 rb_define_const(rb_mFConst, "LOCK_UN", INT2FIX(LOCK_UN)); 05660 /* non-blocking lock. used with LOCK_SH or LOCK_EX. see File#flock */ 05661 rb_define_const(rb_mFConst, "LOCK_NB", INT2FIX(LOCK_NB)); 05662 05663 /* Name of the null device */ 05664 rb_define_const(rb_mFConst, "NULL", rb_obj_freeze(rb_usascii_str_new2(null_device))); 05665 05666 rb_define_method(rb_cFile, "path", rb_file_path, 0); 05667 rb_define_method(rb_cFile, "to_path", rb_file_path, 0); 05668 rb_define_global_function("test", rb_f_test, -1); 05669 05670 rb_cStat = rb_define_class_under(rb_cFile, "Stat", rb_cObject); 05671 rb_define_alloc_func(rb_cStat, rb_stat_s_alloc); 05672 rb_define_method(rb_cStat, "initialize", rb_stat_init, 1); 05673 rb_define_method(rb_cStat, "initialize_copy", rb_stat_init_copy, 1); 05674 05675 rb_include_module(rb_cStat, rb_mComparable); 05676 05677 rb_define_method(rb_cStat, "<=>", rb_stat_cmp, 1); 05678 05679 rb_define_method(rb_cStat, "dev", rb_stat_dev, 0); 05680 rb_define_method(rb_cStat, "dev_major", rb_stat_dev_major, 0); 05681 rb_define_method(rb_cStat, "dev_minor", rb_stat_dev_minor, 0); 05682 rb_define_method(rb_cStat, "ino", rb_stat_ino, 0); 05683 rb_define_method(rb_cStat, "mode", rb_stat_mode, 0); 05684 rb_define_method(rb_cStat, "nlink", rb_stat_nlink, 0); 05685 rb_define_method(rb_cStat, "uid", rb_stat_uid, 0); 05686 rb_define_method(rb_cStat, "gid", rb_stat_gid, 0); 05687 rb_define_method(rb_cStat, "rdev", rb_stat_rdev, 0); 05688 rb_define_method(rb_cStat, "rdev_major", rb_stat_rdev_major, 0); 05689 rb_define_method(rb_cStat, "rdev_minor", rb_stat_rdev_minor, 0); 05690 rb_define_method(rb_cStat, "size", rb_stat_size, 0); 05691 rb_define_method(rb_cStat, "blksize", rb_stat_blksize, 0); 05692 rb_define_method(rb_cStat, "blocks", rb_stat_blocks, 0); 05693 rb_define_method(rb_cStat, "atime", rb_stat_atime, 0); 05694 rb_define_method(rb_cStat, "mtime", rb_stat_mtime, 0); 05695 rb_define_method(rb_cStat, "ctime", rb_stat_ctime, 0); 05696 05697 rb_define_method(rb_cStat, "inspect", rb_stat_inspect, 0); 05698 05699 rb_define_method(rb_cStat, "ftype", rb_stat_ftype, 0); 05700 05701 rb_define_method(rb_cStat, "directory?", rb_stat_d, 0); 05702 rb_define_method(rb_cStat, "readable?", rb_stat_r, 0); 05703 rb_define_method(rb_cStat, "readable_real?", rb_stat_R, 0); 05704 rb_define_method(rb_cStat, "world_readable?", rb_stat_wr, 0); 05705 rb_define_method(rb_cStat, "writable?", rb_stat_w, 0); 05706 rb_define_method(rb_cStat, "writable_real?", rb_stat_W, 0); 05707 rb_define_method(rb_cStat, "world_writable?", rb_stat_ww, 0); 05708 rb_define_method(rb_cStat, "executable?", rb_stat_x, 0); 05709 rb_define_method(rb_cStat, "executable_real?", rb_stat_X, 0); 05710 rb_define_method(rb_cStat, "file?", rb_stat_f, 0); 05711 rb_define_method(rb_cStat, "zero?", rb_stat_z, 0); 05712 rb_define_method(rb_cStat, "size?", rb_stat_s, 0); 05713 rb_define_method(rb_cStat, "owned?", rb_stat_owned, 0); 05714 rb_define_method(rb_cStat, "grpowned?", rb_stat_grpowned, 0); 05715 05716 rb_define_method(rb_cStat, "pipe?", rb_stat_p, 0); 05717 rb_define_method(rb_cStat, "symlink?", rb_stat_l, 0); 05718 rb_define_method(rb_cStat, "socket?", rb_stat_S, 0); 05719 05720 rb_define_method(rb_cStat, "blockdev?", rb_stat_b, 0); 05721 rb_define_method(rb_cStat, "chardev?", rb_stat_c, 0); 05722 05723 rb_define_method(rb_cStat, "setuid?", rb_stat_suid, 0); 05724 rb_define_method(rb_cStat, "setgid?", rb_stat_sgid, 0); 05725 rb_define_method(rb_cStat, "sticky?", rb_stat_sticky, 0); 05726 05727 #ifdef _WIN32 05728 rb_w32_init_file(); 05729 #endif 05730 } 05731
1.7.6.1