Ruby
2.0.0p247(2013-06-27revision41674)
|
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 } 02535 if (rv < 0) { 02536 rb_str_resize(v, 0); 02537 rb_sys_fail_path(path); 02538 } 02539 rb_str_resize(v, rv); 02540 02541 return v; 02542 } 02543 #else 02544 #define rb_file_s_readlink rb_f_notimplement 02545 #endif 02546 02547 static void 02548 unlink_internal(const char *path, VALUE pathv, void *arg) 02549 { 02550 if (unlink(path) < 0) 02551 rb_sys_fail_path(pathv); 02552 } 02553 02554 /* 02555 * call-seq: 02556 * File.delete(file_name, ...) -> integer 02557 * File.unlink(file_name, ...) -> integer 02558 * 02559 * Deletes the named files, returning the number of names 02560 * passed as arguments. Raises an exception on any error. 02561 * See also <code>Dir::rmdir</code>. 02562 */ 02563 02564 static VALUE 02565 rb_file_s_unlink(VALUE klass, VALUE args) 02566 { 02567 long n; 02568 02569 rb_secure(2); 02570 n = apply2files(unlink_internal, args, 0); 02571 return LONG2FIX(n); 02572 } 02573 02574 /* 02575 * call-seq: 02576 * File.rename(old_name, new_name) -> 0 02577 * 02578 * Renames the given file to the new name. Raises a 02579 * <code>SystemCallError</code> if the file cannot be renamed. 02580 * 02581 * File.rename("afile", "afile.bak") #=> 0 02582 */ 02583 02584 static VALUE 02585 rb_file_s_rename(VALUE klass, VALUE from, VALUE to) 02586 { 02587 const char *src, *dst; 02588 VALUE f, t; 02589 02590 rb_secure(2); 02591 FilePathValue(from); 02592 FilePathValue(to); 02593 f = rb_str_encode_ospath(from); 02594 t = rb_str_encode_ospath(to); 02595 src = StringValueCStr(f); 02596 dst = StringValueCStr(t); 02597 #if defined __CYGWIN__ 02598 errno = 0; 02599 #endif 02600 if (rename(src, dst) < 0) { 02601 #if defined DOSISH 02602 switch (errno) { 02603 case EEXIST: 02604 #if defined (__EMX__) 02605 case EACCES: 02606 #endif 02607 if (chmod(dst, 0666) == 0 && 02608 unlink(dst) == 0 && 02609 rename(src, dst) == 0) 02610 return INT2FIX(0); 02611 } 02612 #endif 02613 sys_fail2(from, to); 02614 } 02615 02616 return INT2FIX(0); 02617 } 02618 02619 /* 02620 * call-seq: 02621 * File.umask() -> integer 02622 * File.umask(integer) -> integer 02623 * 02624 * Returns the current umask value for this process. If the optional 02625 * argument is given, set the umask to that value and return the 02626 * previous value. Umask values are <em>subtracted</em> from the 02627 * default permissions, so a umask of <code>0222</code> would make a 02628 * file read-only for everyone. 02629 * 02630 * File.umask(0006) #=> 18 02631 * File.umask #=> 6 02632 */ 02633 02634 static VALUE 02635 rb_file_s_umask(int argc, VALUE *argv) 02636 { 02637 int omask = 0; 02638 02639 rb_secure(2); 02640 if (argc == 0) { 02641 omask = umask(0); 02642 umask(omask); 02643 } 02644 else if (argc == 1) { 02645 omask = umask(NUM2INT(argv[0])); 02646 } 02647 else { 02648 rb_check_arity(argc, 0, 1); 02649 } 02650 return INT2FIX(omask); 02651 } 02652 02653 #ifdef __CYGWIN__ 02654 #undef DOSISH 02655 #endif 02656 #if defined __CYGWIN__ || defined DOSISH 02657 #define DOSISH_UNC 02658 #define DOSISH_DRIVE_LETTER 02659 #define FILE_ALT_SEPARATOR '\\' 02660 #endif 02661 #ifdef FILE_ALT_SEPARATOR 02662 #define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR) 02663 static const char file_alt_separator[] = {FILE_ALT_SEPARATOR, '\0'}; 02664 #else 02665 #define isdirsep(x) ((x) == '/') 02666 #endif 02667 02668 #ifndef USE_NTFS 02669 #if defined _WIN32 || defined __CYGWIN__ 02670 #define USE_NTFS 1 02671 #else 02672 #define USE_NTFS 0 02673 #endif 02674 #endif 02675 02676 #if USE_NTFS 02677 #define istrailinggarbage(x) ((x) == '.' || (x) == ' ') 02678 #else 02679 #define istrailinggarbage(x) 0 02680 #endif 02681 02682 #define Next(p, e, enc) ((p) + rb_enc_mbclen((p), (e), (enc))) 02683 #define Inc(p, e, enc) ((p) = Next((p), (e), (enc))) 02684 02685 #if defined(DOSISH_UNC) 02686 #define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1])) 02687 #else 02688 #define has_unc(buf) 0 02689 #endif 02690 02691 #ifdef DOSISH_DRIVE_LETTER 02692 static inline int 02693 has_drive_letter(const char *buf) 02694 { 02695 if (ISALPHA(buf[0]) && buf[1] == ':') { 02696 return 1; 02697 } 02698 else { 02699 return 0; 02700 } 02701 } 02702 02703 #ifndef _WIN32 02704 static char* 02705 getcwdofdrv(int drv) 02706 { 02707 char drive[4]; 02708 char *drvcwd, *oldcwd; 02709 02710 drive[0] = drv; 02711 drive[1] = ':'; 02712 drive[2] = '\0'; 02713 02714 /* the only way that I know to get the current directory 02715 of a particular drive is to change chdir() to that drive, 02716 so save the old cwd before chdir() 02717 */ 02718 oldcwd = my_getcwd(); 02719 if (chdir(drive) == 0) { 02720 drvcwd = my_getcwd(); 02721 chdir(oldcwd); 02722 xfree(oldcwd); 02723 } 02724 else { 02725 /* perhaps the drive is not exist. we return only drive letter */ 02726 drvcwd = strdup(drive); 02727 } 02728 return drvcwd; 02729 } 02730 #endif 02731 02732 static inline int 02733 not_same_drive(VALUE path, int drive) 02734 { 02735 const char *p = RSTRING_PTR(path); 02736 if (RSTRING_LEN(path) < 2) return 0; 02737 if (has_drive_letter(p)) { 02738 return TOLOWER(p[0]) != TOLOWER(drive); 02739 } 02740 else { 02741 return has_unc(p); 02742 } 02743 } 02744 #endif 02745 02746 static inline char * 02747 skiproot(const char *path, const char *end, rb_encoding *enc) 02748 { 02749 #ifdef DOSISH_DRIVE_LETTER 02750 if (path + 2 <= end && has_drive_letter(path)) path += 2; 02751 #endif 02752 while (path < end && isdirsep(*path)) path++; 02753 return (char *)path; 02754 } 02755 02756 #define nextdirsep rb_enc_path_next 02757 char * 02758 rb_enc_path_next(const char *s, const char *e, rb_encoding *enc) 02759 { 02760 while (s < e && !isdirsep(*s)) { 02761 Inc(s, e, enc); 02762 } 02763 return (char *)s; 02764 } 02765 02766 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER) 02767 #define skipprefix rb_enc_path_skip_prefix 02768 #else 02769 #define skipprefix(path, end, enc) (path) 02770 #endif 02771 char * 02772 rb_enc_path_skip_prefix(const char *path, const char *end, rb_encoding *enc) 02773 { 02774 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER) 02775 #ifdef DOSISH_UNC 02776 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) { 02777 path += 2; 02778 while (path < end && isdirsep(*path)) path++; 02779 if ((path = rb_enc_path_next(path, end, enc)) < end && path[0] && path[1] && !isdirsep(path[1])) 02780 path = rb_enc_path_next(path + 1, end, enc); 02781 return (char *)path; 02782 } 02783 #endif 02784 #ifdef DOSISH_DRIVE_LETTER 02785 if (has_drive_letter(path)) 02786 return (char *)(path + 2); 02787 #endif 02788 #endif 02789 return (char *)path; 02790 } 02791 02792 static inline char * 02793 skipprefixroot(const char *path, const char *end, rb_encoding *enc) 02794 { 02795 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER) 02796 char *p = skipprefix(path, end, enc); 02797 while (isdirsep(*p)) p++; 02798 return p; 02799 #else 02800 return skiproot(path, end, enc); 02801 #endif 02802 } 02803 02804 #define strrdirsep rb_enc_path_last_separator 02805 char * 02806 rb_enc_path_last_separator(const char *path, const char *end, rb_encoding *enc) 02807 { 02808 char *last = NULL; 02809 while (path < end) { 02810 if (isdirsep(*path)) { 02811 const char *tmp = path++; 02812 while (path < end && isdirsep(*path)) path++; 02813 if (path >= end) break; 02814 last = (char *)tmp; 02815 } 02816 else { 02817 Inc(path, end, enc); 02818 } 02819 } 02820 return last; 02821 } 02822 02823 static char * 02824 chompdirsep(const char *path, const char *end, rb_encoding *enc) 02825 { 02826 while (path < end) { 02827 if (isdirsep(*path)) { 02828 const char *last = path++; 02829 while (path < end && isdirsep(*path)) path++; 02830 if (path >= end) return (char *)last; 02831 } 02832 else { 02833 Inc(path, end, enc); 02834 } 02835 } 02836 return (char *)path; 02837 } 02838 02839 char * 02840 rb_enc_path_end(const char *path, const char *end, rb_encoding *enc) 02841 { 02842 if (path < end && isdirsep(*path)) path++; 02843 return chompdirsep(path, end, enc); 02844 } 02845 02846 #if USE_NTFS 02847 static char * 02848 ntfs_tail(const char *path, const char *end, rb_encoding *enc) 02849 { 02850 while (path < end && *path == '.') path++; 02851 while (path < end && *path != ':') { 02852 if (istrailinggarbage(*path)) { 02853 const char *last = path++; 02854 while (path < end && istrailinggarbage(*path)) path++; 02855 if (path >= end || *path == ':') return (char *)last; 02856 } 02857 else if (isdirsep(*path)) { 02858 const char *last = path++; 02859 while (path < end && isdirsep(*path)) path++; 02860 if (path >= end) return (char *)last; 02861 if (*path == ':') path++; 02862 } 02863 else { 02864 Inc(path, end, enc); 02865 } 02866 } 02867 return (char *)path; 02868 } 02869 #endif 02870 02871 #define BUFCHECK(cond) do {\ 02872 bdiff = p - buf;\ 02873 if (cond) {\ 02874 do {buflen *= 2;} while (cond);\ 02875 rb_str_resize(result, buflen);\ 02876 buf = RSTRING_PTR(result);\ 02877 p = buf + bdiff;\ 02878 pend = buf + buflen;\ 02879 }\ 02880 } while (0) 02881 02882 #define BUFINIT() (\ 02883 p = buf = RSTRING_PTR(result),\ 02884 buflen = RSTRING_LEN(result),\ 02885 pend = p + buflen) 02886 02887 VALUE 02888 rb_home_dir(const char *user, VALUE result) 02889 { 02890 const char *dir; 02891 char *buf; 02892 #if defined DOSISH || defined __CYGWIN__ 02893 char *p, *bend; 02894 #endif 02895 long dirlen; 02896 rb_encoding *enc; 02897 02898 if (!user || !*user) { 02899 if (!(dir = getenv("HOME"))) { 02900 rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'"); 02901 } 02902 dirlen = strlen(dir); 02903 rb_str_resize(result, dirlen); 02904 memcpy(buf = RSTRING_PTR(result), dir, dirlen); 02905 } 02906 else { 02907 #ifdef HAVE_PWD_H 02908 struct passwd *pwPtr = getpwnam(user); 02909 if (!pwPtr) { 02910 endpwent(); 02911 rb_raise(rb_eArgError, "user %s doesn't exist", user); 02912 } 02913 dirlen = strlen(pwPtr->pw_dir); 02914 rb_str_resize(result, dirlen); 02915 memcpy(buf = RSTRING_PTR(result), pwPtr->pw_dir, dirlen + 1); 02916 endpwent(); 02917 #else 02918 return Qnil; 02919 #endif 02920 } 02921 enc = rb_filesystem_encoding(); 02922 rb_enc_associate(result, enc); 02923 #if defined DOSISH || defined __CYGWIN__ 02924 for (bend = (p = buf) + dirlen; p < bend; Inc(p, bend, enc)) { 02925 if (*p == '\\') { 02926 *p = '/'; 02927 } 02928 } 02929 #endif 02930 return result; 02931 } 02932 02933 #ifndef _WIN32 02934 static char * 02935 append_fspath(VALUE result, VALUE fname, char *dir, rb_encoding **enc, rb_encoding *fsenc) 02936 { 02937 char *buf, *cwdp = dir; 02938 VALUE dirname = Qnil; 02939 size_t dirlen = strlen(dir), buflen = rb_str_capacity(result); 02940 02941 if (*enc != fsenc) { 02942 rb_encoding *direnc = rb_enc_check(fname, dirname = rb_enc_str_new(dir, dirlen, fsenc)); 02943 if (direnc != fsenc) { 02944 dirname = rb_str_conv_enc(dirname, fsenc, direnc); 02945 RSTRING_GETMEM(dirname, cwdp, dirlen); 02946 } 02947 *enc = direnc; 02948 } 02949 do {buflen *= 2;} while (dirlen > buflen); 02950 rb_str_resize(result, buflen); 02951 buf = RSTRING_PTR(result); 02952 memcpy(buf, cwdp, dirlen); 02953 xfree(dir); 02954 if (!NIL_P(dirname)) rb_str_resize(dirname, 0); 02955 rb_enc_associate(result, *enc); 02956 return buf + dirlen; 02957 } 02958 02959 VALUE 02960 rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result) 02961 { 02962 const char *s, *b, *fend; 02963 char *buf, *p, *pend, *root; 02964 size_t buflen, bdiff; 02965 int tainted; 02966 rb_encoding *enc, *fsenc = rb_filesystem_encoding(); 02967 02968 s = StringValuePtr(fname); 02969 fend = s + RSTRING_LEN(fname); 02970 enc = rb_enc_get(fname); 02971 BUFINIT(); 02972 tainted = OBJ_TAINTED(fname); 02973 02974 if (s[0] == '~' && abs_mode == 0) { /* execute only if NOT absolute_path() */ 02975 long userlen = 0; 02976 tainted = 1; 02977 if (isdirsep(s[1]) || s[1] == '\0') { 02978 buf = 0; 02979 b = 0; 02980 rb_str_set_len(result, 0); 02981 if (*++s) ++s; 02982 } 02983 else { 02984 s = nextdirsep(b = s, fend, enc); 02985 userlen = s - b; 02986 BUFCHECK(bdiff + userlen >= buflen); 02987 memcpy(p, b, userlen); 02988 rb_str_set_len(result, userlen); 02989 buf = p + 1; 02990 p += userlen; 02991 } 02992 if (NIL_P(rb_home_dir(buf, result))) { 02993 rb_raise(rb_eArgError, "can't find user %s", buf); 02994 } 02995 if (!rb_is_absolute_path(RSTRING_PTR(result))) { 02996 if (userlen) { 02997 rb_raise(rb_eArgError, "non-absolute home of %.*s", (int)userlen, b); 02998 } 02999 else { 03000 rb_raise(rb_eArgError, "non-absolute home"); 03001 } 03002 } 03003 BUFINIT(); 03004 p = pend; 03005 } 03006 #ifdef DOSISH_DRIVE_LETTER 03007 /* skip drive letter */ 03008 else if (has_drive_letter(s)) { 03009 if (isdirsep(s[2])) { 03010 /* specified drive letter, and full path */ 03011 /* skip drive letter */ 03012 BUFCHECK(bdiff + 2 >= buflen); 03013 memcpy(p, s, 2); 03014 p += 2; 03015 s += 2; 03016 rb_enc_copy(result, fname); 03017 } 03018 else { 03019 /* specified drive, but not full path */ 03020 int same = 0; 03021 if (!NIL_P(dname) && !not_same_drive(dname, s[0])) { 03022 rb_file_expand_path_internal(dname, Qnil, abs_mode, long_name, result); 03023 BUFINIT(); 03024 if (has_drive_letter(p) && TOLOWER(p[0]) == TOLOWER(s[0])) { 03025 /* ok, same drive */ 03026 same = 1; 03027 } 03028 } 03029 if (!same) { 03030 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc); 03031 tainted = 1; 03032 BUFINIT(); 03033 p = e; 03034 } 03035 else { 03036 rb_enc_associate(result, enc = rb_enc_check(result, fname)); 03037 p = pend; 03038 } 03039 p = chompdirsep(skiproot(buf, p, enc), p, enc); 03040 s += 2; 03041 } 03042 } 03043 #endif 03044 else if (!rb_is_absolute_path(s)) { 03045 if (!NIL_P(dname)) { 03046 rb_file_expand_path_internal(dname, Qnil, abs_mode, long_name, result); 03047 rb_enc_associate(result, rb_enc_check(result, fname)); 03048 BUFINIT(); 03049 p = pend; 03050 } 03051 else { 03052 char *e = append_fspath(result, fname, my_getcwd(), &enc, fsenc); 03053 tainted = 1; 03054 BUFINIT(); 03055 p = e; 03056 } 03057 #if defined DOSISH || defined __CYGWIN__ 03058 if (isdirsep(*s)) { 03059 /* specified full path, but not drive letter nor UNC */ 03060 /* we need to get the drive letter or UNC share name */ 03061 p = skipprefix(buf, p, enc); 03062 } 03063 else 03064 #endif 03065 p = chompdirsep(skiproot(buf, p, enc), p, enc); 03066 } 03067 else { 03068 size_t len; 03069 b = s; 03070 do s++; while (isdirsep(*s)); 03071 len = s - b; 03072 p = buf + len; 03073 BUFCHECK(bdiff >= buflen); 03074 memset(buf, '/', len); 03075 rb_str_set_len(result, len); 03076 rb_enc_associate(result, rb_enc_check(result, fname)); 03077 } 03078 if (p > buf && p[-1] == '/') 03079 --p; 03080 else { 03081 rb_str_set_len(result, p-buf); 03082 BUFCHECK(bdiff + 1 >= buflen); 03083 *p = '/'; 03084 } 03085 03086 rb_str_set_len(result, p-buf+1); 03087 BUFCHECK(bdiff + 1 >= buflen); 03088 p[1] = 0; 03089 root = skipprefix(buf, p+1, enc); 03090 03091 b = s; 03092 while (*s) { 03093 switch (*s) { 03094 case '.': 03095 if (b == s++) { /* beginning of path element */ 03096 switch (*s) { 03097 case '\0': 03098 b = s; 03099 break; 03100 case '.': 03101 if (*(s+1) == '\0' || isdirsep(*(s+1))) { 03102 /* We must go back to the parent */ 03103 char *n; 03104 *p = '\0'; 03105 if (!(n = strrdirsep(root, p, enc))) { 03106 *p = '/'; 03107 } 03108 else { 03109 p = n; 03110 } 03111 b = ++s; 03112 } 03113 #if USE_NTFS 03114 else { 03115 do ++s; while (istrailinggarbage(*s)); 03116 } 03117 #endif 03118 break; 03119 case '/': 03120 #if defined DOSISH || defined __CYGWIN__ 03121 case '\\': 03122 #endif 03123 b = ++s; 03124 break; 03125 default: 03126 /* ordinary path element, beginning don't move */ 03127 break; 03128 } 03129 } 03130 #if USE_NTFS 03131 else { 03132 --s; 03133 case ' ': { 03134 const char *e = s; 03135 while (s < fend && istrailinggarbage(*s)) s++; 03136 if (!*s) { 03137 s = e; 03138 goto endpath; 03139 } 03140 } 03141 } 03142 #endif 03143 break; 03144 case '/': 03145 #if defined DOSISH || defined __CYGWIN__ 03146 case '\\': 03147 #endif 03148 if (s > b) { 03149 long rootdiff = root - buf; 03150 rb_str_set_len(result, p-buf+1); 03151 BUFCHECK(bdiff + (s-b+1) >= buflen); 03152 root = buf + rootdiff; 03153 memcpy(++p, b, s-b); 03154 p += s-b; 03155 *p = '/'; 03156 } 03157 b = ++s; 03158 break; 03159 default: 03160 Inc(s, fend, enc); 03161 break; 03162 } 03163 } 03164 03165 if (s > b) { 03166 #if USE_NTFS 03167 static const char prime[] = ":$DATA"; 03168 enum {prime_len = sizeof(prime) -1}; 03169 endpath: 03170 if (s > b + prime_len && strncasecmp(s - prime_len, prime, prime_len) == 0) { 03171 /* alias of stream */ 03172 /* get rid of a bug of x64 VC++ */ 03173 if (*(s - (prime_len+1)) == ':') { 03174 s -= prime_len + 1; /* prime */ 03175 } 03176 else if (memchr(b, ':', s - prime_len - b)) { 03177 s -= prime_len; /* alternative */ 03178 } 03179 } 03180 #endif 03181 rb_str_set_len(result, p-buf+1); 03182 BUFCHECK(bdiff + (s-b) >= buflen); 03183 memcpy(++p, b, s-b); 03184 p += s-b; 03185 rb_str_set_len(result, p-buf); 03186 } 03187 if (p == skiproot(buf, p + !!*p, enc) - 1) p++; 03188 03189 #if USE_NTFS 03190 *p = '\0'; 03191 if ((s = strrdirsep(b = buf, p, enc)) != 0 && !strpbrk(s, "*?")) { 03192 VALUE tmp, v; 03193 size_t len; 03194 rb_encoding *enc; 03195 WCHAR *wstr; 03196 WIN32_FIND_DATAW wfd; 03197 HANDLE h; 03198 #ifdef __CYGWIN__ 03199 #ifdef HAVE_CYGWIN_CONV_PATH 03200 char *w32buf = NULL; 03201 const int flags = CCP_POSIX_TO_WIN_A | CCP_RELATIVE; 03202 #else 03203 char w32buf[MAXPATHLEN]; 03204 #endif 03205 const char *path; 03206 ssize_t bufsize; 03207 int lnk_added = 0, is_symlink = 0; 03208 struct stat st; 03209 p = (char *)s; 03210 len = strlen(p); 03211 if (lstat(buf, &st) == 0 && S_ISLNK(st.st_mode)) { 03212 is_symlink = 1; 03213 if (len > 4 && STRCASECMP(p + len - 4, ".lnk") != 0) { 03214 lnk_added = 1; 03215 } 03216 } 03217 path = *buf ? buf : "/"; 03218 #ifdef HAVE_CYGWIN_CONV_PATH 03219 bufsize = cygwin_conv_path(flags, path, NULL, 0); 03220 if (bufsize > 0) { 03221 bufsize += len; 03222 if (lnk_added) bufsize += 4; 03223 w32buf = ALLOCA_N(char, bufsize); 03224 if (cygwin_conv_path(flags, path, w32buf, bufsize) == 0) { 03225 b = w32buf; 03226 } 03227 } 03228 #else 03229 bufsize = MAXPATHLEN; 03230 if (cygwin_conv_to_win32_path(path, w32buf) == 0) { 03231 b = w32buf; 03232 } 03233 #endif 03234 if (is_symlink && b == w32buf) { 03235 *p = '\\'; 03236 strlcat(w32buf, p, bufsize); 03237 if (lnk_added) { 03238 strlcat(w32buf, ".lnk", bufsize); 03239 } 03240 } 03241 else { 03242 lnk_added = 0; 03243 } 03244 *p = '/'; 03245 #endif 03246 rb_str_set_len(result, p - buf + strlen(p)); 03247 enc = rb_enc_get(result); 03248 tmp = result; 03249 if (enc != rb_utf8_encoding() && rb_enc_str_coderange(result) != ENC_CODERANGE_7BIT) { 03250 tmp = rb_str_encode_ospath(result); 03251 } 03252 len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, NULL, 0); 03253 wstr = ALLOCV_N(WCHAR, v, len); 03254 MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, wstr, len); 03255 if (tmp != result) rb_str_resize(tmp, 0); 03256 h = FindFirstFileW(wstr, &wfd); 03257 ALLOCV_END(v); 03258 if (h != INVALID_HANDLE_VALUE) { 03259 size_t wlen; 03260 FindClose(h); 03261 len = lstrlenW(wfd.cFileName); 03262 #ifdef __CYGWIN__ 03263 if (lnk_added && len > 4 && 03264 wcscasecmp(wfd.cFileName + len - 4, L".lnk") == 0) { 03265 wfd.cFileName[len -= 4] = L'\0'; 03266 } 03267 #else 03268 p = (char *)s; 03269 #endif 03270 ++p; 03271 wlen = (int)len; 03272 len = WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, NULL, 0, NULL, NULL); 03273 BUFCHECK(bdiff + len >= buflen); 03274 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, p, len + 1, NULL, NULL); 03275 if (tmp != result) { 03276 rb_str_buf_cat(tmp, p, len); 03277 tmp = rb_str_encode(tmp, rb_enc_from_encoding(enc), 0, Qnil); 03278 len = RSTRING_LEN(tmp); 03279 BUFCHECK(bdiff + len >= buflen); 03280 memcpy(p, RSTRING_PTR(tmp), len); 03281 rb_str_resize(tmp, 0); 03282 } 03283 p += len; 03284 } 03285 #ifdef __CYGWIN__ 03286 else { 03287 p += strlen(p); 03288 } 03289 #endif 03290 } 03291 #endif 03292 03293 if (tainted) OBJ_TAINT(result); 03294 rb_str_set_len(result, p - buf); 03295 rb_enc_check(fname, result); 03296 ENC_CODERANGE_CLEAR(result); 03297 return result; 03298 } 03299 #endif /* _WIN32 */ 03300 03301 #define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, MAXPATHLEN + 2) 03302 03303 #define check_expand_path_args(fname, dname) \ 03304 (((fname) = rb_get_path(fname)), \ 03305 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname)))) 03306 03307 static VALUE 03308 file_expand_path_1(VALUE fname) 03309 { 03310 return rb_file_expand_path_internal(fname, Qnil, 0, 0, EXPAND_PATH_BUFFER()); 03311 } 03312 03313 VALUE 03314 rb_file_expand_path(VALUE fname, VALUE dname) 03315 { 03316 check_expand_path_args(fname, dname); 03317 return rb_file_expand_path_internal(fname, dname, 0, 1, EXPAND_PATH_BUFFER()); 03318 } 03319 03320 VALUE 03321 rb_file_expand_path_fast(VALUE fname, VALUE dname) 03322 { 03323 return rb_file_expand_path_internal(fname, dname, 0, 0, EXPAND_PATH_BUFFER()); 03324 } 03325 03326 /* 03327 * call-seq: 03328 * File.expand_path(file_name [, dir_string] ) -> abs_file_name 03329 * 03330 * Converts a pathname to an absolute pathname. Relative paths are 03331 * referenced from the current working directory of the process unless 03332 * <i>dir_string</i> is given, in which case it will be used as the 03333 * starting point. The given pathname may start with a 03334 * ``<code>~</code>'', which expands to the process owner's home 03335 * directory (the environment variable <code>HOME</code> must be set 03336 * correctly). ``<code>~</code><i>user</i>'' expands to the named 03337 * user's home directory. 03338 * 03339 * File.expand_path("~oracle/bin") #=> "/home/oracle/bin" 03340 * File.expand_path("../../bin", "/tmp/x") #=> "/bin" 03341 */ 03342 03343 VALUE 03344 rb_file_s_expand_path(int argc, VALUE *argv) 03345 { 03346 VALUE fname, dname; 03347 03348 if (argc == 1) { 03349 return rb_file_expand_path(argv[0], Qnil); 03350 } 03351 rb_scan_args(argc, argv, "11", &fname, &dname); 03352 03353 return rb_file_expand_path(fname, dname); 03354 } 03355 03356 VALUE 03357 rb_file_absolute_path(VALUE fname, VALUE dname) 03358 { 03359 check_expand_path_args(fname, dname); 03360 return rb_file_expand_path_internal(fname, dname, 1, 1, EXPAND_PATH_BUFFER()); 03361 } 03362 03363 /* 03364 * call-seq: 03365 * File.absolute_path(file_name [, dir_string] ) -> abs_file_name 03366 * 03367 * Converts a pathname to an absolute pathname. Relative paths are 03368 * referenced from the current working directory of the process unless 03369 * <i>dir_string</i> is given, in which case it will be used as the 03370 * starting point. If the given pathname starts with a ``<code>~</code>'' 03371 * it is NOT expanded, it is treated as a normal directory name. 03372 * 03373 * File.absolute_path("~oracle/bin") #=> "<relative_path>/~oracle/bin" 03374 */ 03375 03376 VALUE 03377 rb_file_s_absolute_path(int argc, VALUE *argv) 03378 { 03379 VALUE fname, dname; 03380 03381 if (argc == 1) { 03382 return rb_file_absolute_path(argv[0], Qnil); 03383 } 03384 rb_scan_args(argc, argv, "11", &fname, &dname); 03385 03386 return rb_file_absolute_path(fname, dname); 03387 } 03388 03389 static void 03390 realpath_rec(long *prefixlenp, VALUE *resolvedp, const char *unresolved, VALUE loopcheck, int strict, int last) 03391 { 03392 const char *pend = unresolved + strlen(unresolved); 03393 rb_encoding *enc = rb_enc_get(*resolvedp); 03394 ID resolving; 03395 CONST_ID(resolving, "resolving"); 03396 while (unresolved < pend) { 03397 const char *testname = unresolved; 03398 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc); 03399 long testnamelen = unresolved_firstsep - unresolved; 03400 const char *unresolved_nextname = unresolved_firstsep; 03401 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname)) 03402 unresolved_nextname++; 03403 unresolved = unresolved_nextname; 03404 if (testnamelen == 1 && testname[0] == '.') { 03405 } 03406 else if (testnamelen == 2 && testname[0] == '.' && testname[1] == '.') { 03407 if (*prefixlenp < RSTRING_LEN(*resolvedp)) { 03408 const char *resolved_str = RSTRING_PTR(*resolvedp); 03409 const char *resolved_names = resolved_str + *prefixlenp; 03410 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), enc); 03411 long len = lastsep ? lastsep - resolved_names : 0; 03412 rb_str_resize(*resolvedp, *prefixlenp + len); 03413 } 03414 } 03415 else { 03416 VALUE checkval; 03417 VALUE testpath = rb_str_dup(*resolvedp); 03418 if (*prefixlenp < RSTRING_LEN(testpath)) 03419 rb_str_cat2(testpath, "/"); 03420 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER) 03421 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) { 03422 const char *prefix = RSTRING_PTR(testpath); 03423 const char *last = rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc); 03424 if (!isdirsep(*last)) rb_str_cat2(testpath, "/"); 03425 } 03426 #endif 03427 rb_str_cat(testpath, testname, testnamelen); 03428 checkval = rb_hash_aref(loopcheck, testpath); 03429 if (!NIL_P(checkval)) { 03430 if (checkval == ID2SYM(resolving)) { 03431 errno = ELOOP; 03432 rb_sys_fail_path(testpath); 03433 } 03434 else { 03435 *resolvedp = rb_str_dup(checkval); 03436 } 03437 } 03438 else { 03439 struct stat sbuf; 03440 int ret; 03441 VALUE testpath2 = rb_str_encode_ospath(testpath); 03442 #ifdef __native_client__ 03443 ret = stat(RSTRING_PTR(testpath2), &sbuf); 03444 #else 03445 ret = lstat(RSTRING_PTR(testpath2), &sbuf); 03446 #endif 03447 if (ret == -1) { 03448 if (errno == ENOENT) { 03449 if (strict || !last || *unresolved_firstsep) 03450 rb_sys_fail_path(testpath); 03451 *resolvedp = testpath; 03452 break; 03453 } 03454 else { 03455 rb_sys_fail_path(testpath); 03456 } 03457 } 03458 #ifdef HAVE_READLINK 03459 if (S_ISLNK(sbuf.st_mode)) { 03460 VALUE link; 03461 volatile VALUE link_orig = Qnil; 03462 const char *link_prefix, *link_names; 03463 long link_prefixlen; 03464 rb_hash_aset(loopcheck, testpath, ID2SYM(resolving)); 03465 link = rb_readlink(testpath); 03466 link_prefix = RSTRING_PTR(link); 03467 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link)); 03468 link_prefixlen = link_names - link_prefix; 03469 if (link_prefixlen > 0) { 03470 rb_encoding *enc, *linkenc = rb_enc_get(link); 03471 link_orig = link; 03472 link = rb_str_subseq(link, 0, link_prefixlen); 03473 enc = rb_enc_check(*resolvedp, link); 03474 if (enc != linkenc) link = rb_str_conv_enc(link, linkenc, enc); 03475 *resolvedp = link; 03476 *prefixlenp = link_prefixlen; 03477 } 03478 realpath_rec(prefixlenp, resolvedp, link_names, loopcheck, strict, *unresolved_firstsep == '\0'); 03479 RB_GC_GUARD(link_orig); 03480 rb_hash_aset(loopcheck, testpath, rb_str_dup_frozen(*resolvedp)); 03481 } 03482 else 03483 #endif 03484 { 03485 VALUE s = rb_str_dup_frozen(testpath); 03486 rb_hash_aset(loopcheck, s, s); 03487 *resolvedp = testpath; 03488 } 03489 } 03490 } 03491 } 03492 } 03493 03494 #ifdef __native_client__ 03495 VALUE 03496 rb_realpath_internal(VALUE basedir, VALUE path, int strict) 03497 { 03498 return path; 03499 } 03500 #else 03501 VALUE 03502 rb_realpath_internal(VALUE basedir, VALUE path, int strict) 03503 { 03504 long prefixlen; 03505 VALUE resolved; 03506 volatile VALUE unresolved_path; 03507 VALUE loopcheck; 03508 volatile VALUE curdir = Qnil; 03509 03510 rb_encoding *enc; 03511 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL; 03512 char *ptr, *prefixptr = NULL, *pend; 03513 long len; 03514 03515 rb_secure(2); 03516 03517 FilePathValue(path); 03518 unresolved_path = rb_str_dup_frozen(path); 03519 03520 if (!NIL_P(basedir)) { 03521 FilePathValue(basedir); 03522 basedir = rb_str_dup_frozen(basedir); 03523 } 03524 03525 RSTRING_GETMEM(unresolved_path, ptr, len); 03526 path_names = skipprefixroot(ptr, ptr + len, rb_enc_get(unresolved_path)); 03527 if (ptr != path_names) { 03528 resolved = rb_str_subseq(unresolved_path, 0, path_names - ptr); 03529 goto root_found; 03530 } 03531 03532 if (!NIL_P(basedir)) { 03533 RSTRING_GETMEM(basedir, ptr, len); 03534 basedir_names = skipprefixroot(ptr, ptr + len, rb_enc_get(basedir)); 03535 if (ptr != basedir_names) { 03536 resolved = rb_str_subseq(basedir, 0, basedir_names - ptr); 03537 goto root_found; 03538 } 03539 } 03540 03541 curdir = rb_dir_getwd(); 03542 RSTRING_GETMEM(curdir, ptr, len); 03543 curdir_names = skipprefixroot(ptr, ptr + len, rb_enc_get(curdir)); 03544 resolved = rb_str_subseq(curdir, 0, curdir_names - ptr); 03545 03546 root_found: 03547 RSTRING_GETMEM(resolved, prefixptr, prefixlen); 03548 pend = prefixptr + prefixlen; 03549 enc = rb_enc_get(resolved); 03550 ptr = chompdirsep(prefixptr, pend, enc); 03551 if (ptr < pend) { 03552 prefixlen = ++ptr - prefixptr; 03553 rb_str_set_len(resolved, prefixlen); 03554 } 03555 #ifdef FILE_ALT_SEPARATOR 03556 while (prefixptr < ptr) { 03557 if (*prefixptr == FILE_ALT_SEPARATOR) { 03558 *prefixptr = '/'; 03559 } 03560 Inc(prefixptr, pend, enc); 03561 } 03562 #endif 03563 03564 loopcheck = rb_hash_new(); 03565 if (curdir_names) 03566 realpath_rec(&prefixlen, &resolved, curdir_names, loopcheck, 1, 0); 03567 if (basedir_names) 03568 realpath_rec(&prefixlen, &resolved, basedir_names, loopcheck, 1, 0); 03569 realpath_rec(&prefixlen, &resolved, path_names, loopcheck, strict, 1); 03570 03571 OBJ_TAINT(resolved); 03572 return resolved; 03573 } 03574 #endif 03575 03576 /* 03577 * call-seq: 03578 * File.realpath(pathname [, dir_string]) -> real_pathname 03579 * 03580 * Returns the real (absolute) pathname of _pathname_ in the actual 03581 * filesystem not containing symlinks or useless dots. 03582 * 03583 * If _dir_string_ is given, it is used as a base directory 03584 * for interpreting relative pathname instead of the current directory. 03585 * 03586 * All components of the pathname must exist when this method is 03587 * called. 03588 */ 03589 static VALUE 03590 rb_file_s_realpath(int argc, VALUE *argv, VALUE klass) 03591 { 03592 VALUE path, basedir; 03593 rb_scan_args(argc, argv, "11", &path, &basedir); 03594 return rb_realpath_internal(basedir, path, 1); 03595 } 03596 03597 /* 03598 * call-seq: 03599 * File.realdirpath(pathname [, dir_string]) -> real_pathname 03600 * 03601 * Returns the real (absolute) pathname of _pathname_ in the actual filesystem. 03602 * The real pathname doesn't contain symlinks or useless dots. 03603 * 03604 * If _dir_string_ is given, it is used as a base directory 03605 * for interpreting relative pathname instead of the current directory. 03606 * 03607 * The last component of the real pathname can be nonexistent. 03608 */ 03609 static VALUE 03610 rb_file_s_realdirpath(int argc, VALUE *argv, VALUE klass) 03611 { 03612 VALUE path, basedir; 03613 rb_scan_args(argc, argv, "11", &path, &basedir); 03614 return rb_realpath_internal(basedir, path, 0); 03615 } 03616 03617 static size_t 03618 rmext(const char *p, long l0, long l1, const char *e, long l2, rb_encoding *enc) 03619 { 03620 int len1, len2; 03621 unsigned int c; 03622 const char *s, *last; 03623 03624 if (!e || !l2) return 0; 03625 03626 c = rb_enc_codepoint_len(e, e + l2, &len1, enc); 03627 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) == '*' && len1 + len2 == l2) { 03628 if (c == '.') return l0; 03629 s = p; 03630 e = p + l1; 03631 last = e; 03632 while (s < e) { 03633 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s; 03634 s += len1; 03635 } 03636 return last - p; 03637 } 03638 if (l1 < l2) return l1; 03639 03640 s = p+l1-l2; 03641 if (rb_enc_left_char_head(p, s, p+l1, enc) != s) return 0; 03642 #if CASEFOLD_FILESYSTEM 03643 #define fncomp strncasecmp 03644 #else 03645 #define fncomp strncmp 03646 #endif 03647 if (fncomp(s, e, l2) == 0) { 03648 return l1-l2; 03649 } 03650 return 0; 03651 } 03652 03653 const char * 03654 ruby_enc_find_basename(const char *name, long *baselen, long *alllen, rb_encoding *enc) 03655 { 03656 const char *p, *q, *e, *end; 03657 #if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC 03658 const char *root; 03659 #endif 03660 long f = 0, n = -1; 03661 03662 end = name + (alllen ? (size_t)*alllen : strlen(name)); 03663 name = skipprefix(name, end, enc); 03664 #if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC 03665 root = name; 03666 #endif 03667 while (isdirsep(*name)) 03668 name++; 03669 if (!*name) { 03670 p = name - 1; 03671 f = 1; 03672 #if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC 03673 if (name != root) { 03674 /* has slashes */ 03675 } 03676 #ifdef DOSISH_DRIVE_LETTER 03677 else if (*p == ':') { 03678 p++; 03679 f = 0; 03680 } 03681 #endif 03682 #ifdef DOSISH_UNC 03683 else { 03684 p = "/"; 03685 } 03686 #endif 03687 #endif 03688 } 03689 else { 03690 if (!(p = strrdirsep(name, end, enc))) { 03691 p = name; 03692 } 03693 else { 03694 while (isdirsep(*p)) p++; /* skip last / */ 03695 } 03696 #if USE_NTFS 03697 n = ntfs_tail(p, end, enc) - p; 03698 #else 03699 n = chompdirsep(p, end, enc) - p; 03700 #endif 03701 for (q = p; q - p < n && *q == '.'; q++); 03702 for (e = 0; q - p < n; Inc(q, end, enc)) { 03703 if (*q == '.') e = q; 03704 } 03705 if (e) f = e - p; 03706 else f = n; 03707 } 03708 03709 if (baselen) 03710 *baselen = f; 03711 if (alllen) 03712 *alllen = n; 03713 return p; 03714 } 03715 03716 /* 03717 * call-seq: 03718 * File.basename(file_name [, suffix] ) -> base_name 03719 * 03720 * Returns the last component of the filename given in <i>file_name</i>, 03721 * which can be formed using both <code>File::SEPARATOR</code> and 03722 * <code>File::ALT_SEPARETOR</code> as the separator when 03723 * <code>File::ALT_SEPARATOR</code> is not <code>nil</code>. If 03724 * <i>suffix</i> is given and present at the end of <i>file_name</i>, 03725 * it is removed. 03726 * 03727 * File.basename("/home/gumby/work/ruby.rb") #=> "ruby.rb" 03728 * File.basename("/home/gumby/work/ruby.rb", ".rb") #=> "ruby" 03729 */ 03730 03731 static VALUE 03732 rb_file_s_basename(int argc, VALUE *argv) 03733 { 03734 VALUE fname, fext, basename; 03735 const char *name, *p; 03736 long f, n; 03737 rb_encoding *enc; 03738 03739 if (rb_scan_args(argc, argv, "11", &fname, &fext) == 2) { 03740 StringValue(fext); 03741 enc = check_path_encoding(fext); 03742 } 03743 FilePathStringValue(fname); 03744 if (NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) { 03745 enc = rb_enc_get(fname); 03746 fext = Qnil; 03747 } 03748 if ((n = RSTRING_LEN(fname)) == 0 || !*(name = RSTRING_PTR(fname))) 03749 return rb_str_new_shared(fname); 03750 03751 p = ruby_enc_find_basename(name, &f, &n, enc); 03752 if (n >= 0) { 03753 if (NIL_P(fext)) { 03754 f = n; 03755 } 03756 else { 03757 const char *fp; 03758 fp = StringValueCStr(fext); 03759 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) { 03760 f = n; 03761 } 03762 RB_GC_GUARD(fext); 03763 } 03764 if (f == RSTRING_LEN(fname)) return rb_str_new_shared(fname); 03765 } 03766 03767 basename = rb_str_new(p, f); 03768 rb_enc_copy(basename, fname); 03769 OBJ_INFECT(basename, fname); 03770 return basename; 03771 } 03772 03773 /* 03774 * call-seq: 03775 * File.dirname(file_name) -> dir_name 03776 * 03777 * Returns all components of the filename given in <i>file_name</i> 03778 * except the last one. The filename can be formed using both 03779 * <code>File::SEPARATOR</code> and <code>File::ALT_SEPARETOR</code> as the 03780 * separator when <code>File::ALT_SEPARATOR</code> is not <code>nil</code>. 03781 * 03782 * File.dirname("/home/gumby/work/ruby.rb") #=> "/home/gumby/work" 03783 */ 03784 03785 static VALUE 03786 rb_file_s_dirname(VALUE klass, VALUE fname) 03787 { 03788 return rb_file_dirname(fname); 03789 } 03790 03791 VALUE 03792 rb_file_dirname(VALUE fname) 03793 { 03794 const char *name, *root, *p, *end; 03795 VALUE dirname; 03796 rb_encoding *enc; 03797 03798 FilePathStringValue(fname); 03799 name = StringValueCStr(fname); 03800 end = name + RSTRING_LEN(fname); 03801 enc = rb_enc_get(fname); 03802 root = skiproot(name, end, enc); 03803 #ifdef DOSISH_UNC 03804 if (root > name + 1 && isdirsep(*name)) 03805 root = skipprefix(name = root - 2, end, enc); 03806 #else 03807 if (root > name + 1) 03808 name = root - 1; 03809 #endif 03810 p = strrdirsep(root, end, enc); 03811 if (!p) { 03812 p = root; 03813 } 03814 if (p == name) 03815 return rb_usascii_str_new2("."); 03816 #ifdef DOSISH_DRIVE_LETTER 03817 if (has_drive_letter(name) && isdirsep(*(name + 2))) { 03818 const char *top = skiproot(name + 2, end, enc); 03819 dirname = rb_str_new(name, 3); 03820 rb_str_cat(dirname, top, p - top); 03821 } 03822 else 03823 #endif 03824 dirname = rb_str_new(name, p - name); 03825 #ifdef DOSISH_DRIVE_LETTER 03826 if (has_drive_letter(name) && root == name + 2 && p - name == 2) 03827 rb_str_cat(dirname, ".", 1); 03828 #endif 03829 rb_enc_copy(dirname, fname); 03830 OBJ_INFECT(dirname, fname); 03831 return dirname; 03832 } 03833 03834 /* 03835 * accept a String, and return the pointer of the extension. 03836 * if len is passed, set the length of extension to it. 03837 * returned pointer is in ``name'' or NULL. 03838 * returns *len 03839 * no dot NULL 0 03840 * dotfile top 0 03841 * end with dot dot 1 03842 * .ext dot len of .ext 03843 * .ext:stream dot len of .ext without :stream (NT only) 03844 * 03845 */ 03846 const char * 03847 ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc) 03848 { 03849 const char *p, *e, *end = name + (len ? *len : (long)strlen(name)); 03850 03851 p = strrdirsep(name, end, enc); /* get the last path component */ 03852 if (!p) 03853 p = name; 03854 else 03855 do name = ++p; while (isdirsep(*p)); 03856 03857 e = 0; 03858 while (*p && *p == '.') p++; 03859 while (*p) { 03860 if (*p == '.' || istrailinggarbage(*p)) { 03861 #if USE_NTFS 03862 const char *last = p++, *dot = last; 03863 while (istrailinggarbage(*p)) { 03864 if (*p == '.') dot = p; 03865 p++; 03866 } 03867 if (!*p || *p == ':') { 03868 p = last; 03869 break; 03870 } 03871 if (*last == '.' || dot > last) e = dot; 03872 continue; 03873 #else 03874 e = p; /* get the last dot of the last component */ 03875 #endif 03876 } 03877 #if USE_NTFS 03878 else if (*p == ':') { 03879 break; 03880 } 03881 #endif 03882 else if (isdirsep(*p)) 03883 break; 03884 Inc(p, end, enc); 03885 } 03886 03887 if (len) { 03888 /* no dot, or the only dot is first or end? */ 03889 if (!e || e == name) 03890 *len = 0; 03891 else if (e+1 == p) 03892 *len = 1; 03893 else 03894 *len = p - e; 03895 } 03896 return e; 03897 } 03898 03899 /* 03900 * call-seq: 03901 * File.extname(path) -> string 03902 * 03903 * Returns the extension (the portion of file name in +path+ 03904 * starting from the last period). 03905 * 03906 * If +path+ is a dotfile, or starts with a period, then the starting 03907 * dot is not dealt with the start of the extension. 03908 * 03909 * An empty string will also be returned when the period is the last character 03910 * in +path+. 03911 * 03912 * File.extname("test.rb") #=> ".rb" 03913 * File.extname("a/b/d/test.rb") #=> ".rb" 03914 * File.extname("foo.") #=> "" 03915 * File.extname("test") #=> "" 03916 * File.extname(".profile") #=> "" 03917 * File.extname(".profile.sh") #=> ".sh" 03918 * 03919 */ 03920 03921 static VALUE 03922 rb_file_s_extname(VALUE klass, VALUE fname) 03923 { 03924 const char *name, *e; 03925 long len; 03926 VALUE extname; 03927 03928 FilePathStringValue(fname); 03929 name = StringValueCStr(fname); 03930 len = RSTRING_LEN(fname); 03931 e = ruby_enc_find_extname(name, &len, rb_enc_get(fname)); 03932 if (len <= 1) 03933 return rb_str_new(0, 0); 03934 extname = rb_str_subseq(fname, e - name, len); /* keep the dot, too! */ 03935 OBJ_INFECT(extname, fname); 03936 return extname; 03937 } 03938 03939 /* 03940 * call-seq: 03941 * File.path(path) -> string 03942 * 03943 * Returns the string representation of the path 03944 * 03945 * File.path("/dev/null") #=> "/dev/null" 03946 * File.path(Pathname.new("/tmp")) #=> "/tmp" 03947 * 03948 */ 03949 03950 static VALUE 03951 rb_file_s_path(VALUE klass, VALUE fname) 03952 { 03953 return rb_get_path(fname); 03954 } 03955 03956 /* 03957 * call-seq: 03958 * File.split(file_name) -> array 03959 * 03960 * Splits the given string into a directory and a file component and 03961 * returns them in a two-element array. See also 03962 * <code>File::dirname</code> and <code>File::basename</code>. 03963 * 03964 * File.split("/home/gumby/.profile") #=> ["/home/gumby", ".profile"] 03965 */ 03966 03967 static VALUE 03968 rb_file_s_split(VALUE klass, VALUE path) 03969 { 03970 FilePathStringValue(path); /* get rid of converting twice */ 03971 return rb_assoc_new(rb_file_s_dirname(Qnil, path), rb_file_s_basename(1,&path)); 03972 } 03973 03974 static VALUE separator; 03975 03976 static VALUE rb_file_join(VALUE ary, VALUE sep); 03977 03978 static VALUE 03979 file_inspect_join(VALUE ary, VALUE argp, int recur) 03980 { 03981 VALUE *arg = (VALUE *)argp; 03982 if (recur || ary == arg[0]) rb_raise(rb_eArgError, "recursive array"); 03983 return rb_file_join(arg[0], arg[1]); 03984 } 03985 03986 static VALUE 03987 rb_file_join(VALUE ary, VALUE sep) 03988 { 03989 long len, i; 03990 VALUE result, tmp; 03991 const char *name, *tail; 03992 int checked = TRUE; 03993 rb_encoding *enc; 03994 03995 if (RARRAY_LEN(ary) == 0) return rb_str_new(0, 0); 03996 03997 len = 1; 03998 for (i=0; i<RARRAY_LEN(ary); i++) { 03999 tmp = RARRAY_PTR(ary)[i]; 04000 if (RB_TYPE_P(tmp, T_STRING)) { 04001 check_path_encoding(tmp); 04002 len += RSTRING_LEN(tmp); 04003 } 04004 else { 04005 len += 10; 04006 } 04007 } 04008 if (!NIL_P(sep)) { 04009 StringValue(sep); 04010 len += RSTRING_LEN(sep) * (RARRAY_LEN(ary) - 1); 04011 } 04012 result = rb_str_buf_new(len); 04013 RBASIC(result)->klass = 0; 04014 OBJ_INFECT(result, ary); 04015 for (i=0; i<RARRAY_LEN(ary); i++) { 04016 tmp = RARRAY_PTR(ary)[i]; 04017 switch (TYPE(tmp)) { 04018 case T_STRING: 04019 if (!checked) check_path_encoding(tmp); 04020 StringValueCStr(tmp); 04021 break; 04022 case T_ARRAY: 04023 if (ary == tmp) { 04024 rb_raise(rb_eArgError, "recursive array"); 04025 } 04026 else { 04027 VALUE args[2]; 04028 04029 args[0] = tmp; 04030 args[1] = sep; 04031 tmp = rb_exec_recursive(file_inspect_join, ary, (VALUE)args); 04032 } 04033 break; 04034 default: 04035 FilePathStringValue(tmp); 04036 checked = FALSE; 04037 } 04038 RSTRING_GETMEM(result, name, len); 04039 if (i == 0) { 04040 rb_enc_copy(result, tmp); 04041 } 04042 else if (!NIL_P(sep)) { 04043 tail = chompdirsep(name, name + len, rb_enc_get(result)); 04044 if (RSTRING_PTR(tmp) && isdirsep(RSTRING_PTR(tmp)[0])) { 04045 rb_str_set_len(result, tail - name); 04046 } 04047 else if (!*tail) { 04048 enc = rb_enc_check(result, sep); 04049 rb_str_buf_append(result, sep); 04050 rb_enc_associate(result, enc); 04051 } 04052 } 04053 enc = rb_enc_check(result, tmp); 04054 rb_str_buf_append(result, tmp); 04055 rb_enc_associate(result, enc); 04056 } 04057 RBASIC(result)->klass = rb_cString; 04058 04059 return result; 04060 } 04061 04062 /* 04063 * call-seq: 04064 * File.join(string, ...) -> path 04065 * 04066 * Returns a new string formed by joining the strings using 04067 * <code>File::SEPARATOR</code>. 04068 * 04069 * File.join("usr", "mail", "gumby") #=> "usr/mail/gumby" 04070 * 04071 */ 04072 04073 static VALUE 04074 rb_file_s_join(VALUE klass, VALUE args) 04075 { 04076 return rb_file_join(args, separator); 04077 } 04078 04079 #if defined(HAVE_TRUNCATE) || defined(HAVE_CHSIZE) 04080 /* 04081 * call-seq: 04082 * File.truncate(file_name, integer) -> 0 04083 * 04084 * Truncates the file <i>file_name</i> to be at most <i>integer</i> 04085 * bytes long. Not available on all platforms. 04086 * 04087 * f = File.new("out", "w") 04088 * f.write("1234567890") #=> 10 04089 * f.close #=> nil 04090 * File.truncate("out", 5) #=> 0 04091 * File.size("out") #=> 5 04092 * 04093 */ 04094 04095 static VALUE 04096 rb_file_s_truncate(VALUE klass, VALUE path, VALUE len) 04097 { 04098 off_t pos; 04099 04100 rb_secure(2); 04101 pos = NUM2OFFT(len); 04102 FilePathValue(path); 04103 path = rb_str_encode_ospath(path); 04104 #ifdef HAVE_TRUNCATE 04105 if (truncate(StringValueCStr(path), pos) < 0) 04106 rb_sys_fail_path(path); 04107 #else /* defined(HAVE_CHSIZE) */ 04108 { 04109 int tmpfd; 04110 04111 if ((tmpfd = rb_cloexec_open(StringValueCStr(path), 0, 0)) < 0) { 04112 rb_sys_fail_path(path); 04113 } 04114 rb_update_max_fd(tmpfd); 04115 if (chsize(tmpfd, pos) < 0) { 04116 close(tmpfd); 04117 rb_sys_fail_path(path); 04118 } 04119 close(tmpfd); 04120 } 04121 #endif 04122 return INT2FIX(0); 04123 } 04124 #else 04125 #define rb_file_s_truncate rb_f_notimplement 04126 #endif 04127 04128 #if defined(HAVE_FTRUNCATE) || defined(HAVE_CHSIZE) 04129 /* 04130 * call-seq: 04131 * file.truncate(integer) -> 0 04132 * 04133 * Truncates <i>file</i> to at most <i>integer</i> bytes. The file 04134 * must be opened for writing. Not available on all platforms. 04135 * 04136 * f = File.new("out", "w") 04137 * f.syswrite("1234567890") #=> 10 04138 * f.truncate(5) #=> 0 04139 * f.close() #=> nil 04140 * File.size("out") #=> 5 04141 */ 04142 04143 static VALUE 04144 rb_file_truncate(VALUE obj, VALUE len) 04145 { 04146 rb_io_t *fptr; 04147 off_t pos; 04148 04149 rb_secure(2); 04150 pos = NUM2OFFT(len); 04151 GetOpenFile(obj, fptr); 04152 if (!(fptr->mode & FMODE_WRITABLE)) { 04153 rb_raise(rb_eIOError, "not opened for writing"); 04154 } 04155 rb_io_flush(obj); 04156 #ifdef HAVE_FTRUNCATE 04157 if (ftruncate(fptr->fd, pos) < 0) 04158 rb_sys_fail_path(fptr->pathv); 04159 #else /* defined(HAVE_CHSIZE) */ 04160 if (chsize(fptr->fd, pos) < 0) 04161 rb_sys_fail_path(fptr->pathv); 04162 #endif 04163 return INT2FIX(0); 04164 } 04165 #else 04166 #define rb_file_truncate rb_f_notimplement 04167 #endif 04168 04169 # ifndef LOCK_SH 04170 # define LOCK_SH 1 04171 # endif 04172 # ifndef LOCK_EX 04173 # define LOCK_EX 2 04174 # endif 04175 # ifndef LOCK_NB 04176 # define LOCK_NB 4 04177 # endif 04178 # ifndef LOCK_UN 04179 # define LOCK_UN 8 04180 # endif 04181 04182 #ifdef __CYGWIN__ 04183 #include <winerror.h> 04184 extern unsigned long __attribute__((stdcall)) GetLastError(void); 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