Objects of classFile::Stat encapsulate commonstatus information forFile objects. Theinformation is recorded at the moment theFile::Stat object is created; changes made to the fileafter that point will not be reflected.File::Statobjects are returned byIO#stat,File.stat,File#lstat, andFile.lstat. Many of these methodsreturn platform-specific values, and not all values are meaningful on allsystems. See alsoKernel#test.
Create aFile::Stat object for the given file name(raising an exception if the file doesn't exist).
static VALUErb_stat_init(VALUE obj, VALUE fname){ struct stat st, *nst; FilePathValue(fname); fname = rb_str_encode_ospath(fname); if (STAT(StringValueCStr(fname), &st) == -1) { rb_sys_fail_path(fname); } if (DATA_PTR(obj)) { xfree(DATA_PTR(obj)); DATA_PTR(obj) = NULL; } nst = ALLOC(struct stat); *nst = st; DATA_PTR(obj) = nst; return Qnil;}
ComparesFile::Stat objects by comparing theirrespective modification times.
nil
is returned ifother_stat
is not aFile::Stat object
f1 =File.new("f1","w")sleep1f2 =File.new("f2","w")f1.stat<=>f2.stat#=> -1
static VALUErb_stat_cmp(VALUE self, VALUE other){ if (rb_obj_is_kind_of(other, rb_obj_class(self))) { struct timespec ts1 = stat_mtimespec(get_stat(self)); struct timespec ts2 = stat_mtimespec(get_stat(other)); if (ts1.tv_sec == ts2.tv_sec) { if (ts1.tv_nsec == ts2.tv_nsec) return INT2FIX(0); if (ts1.tv_nsec < ts2.tv_nsec) return INT2FIX(-1); return INT2FIX(1); } if (ts1.tv_sec < ts2.tv_sec) return INT2FIX(-1); return INT2FIX(1); } return Qnil;}
Returns the last access time for this file as an object of classTime.
File.stat("testfile").atime#=> Wed Dec 31 18:00:00 CST 1969
static VALUErb_stat_atime(VALUE self){ return stat_atime(get_stat(self));}
Returns the birth time forstat.
If the platform doesn't have birthtime, raisesNotImplementedError.
File.write("testfile","foo")sleep10File.write("testfile","bar")sleep10File.chmod(0644,"testfile")sleep10File.read("testfile")File.stat("testfile").birthtime#=> 2014-02-24 11:19:17 +0900File.stat("testfile").mtime#=> 2014-02-24 11:19:27 +0900File.stat("testfile").ctime#=> 2014-02-24 11:19:37 +0900File.stat("testfile").atime#=> 2014-02-24 11:19:47 +0900
static VALUErb_stat_birthtime(VALUE self){ return stat_birthtime(get_stat(self));}
Returns the native file system's block size. Will returnnil
on platforms that don't support this information.
File.stat("testfile").blksize#=> 4096
static VALUErb_stat_blksize(VALUE self){#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE return ULONG2NUM(get_stat(self)->st_blksize);#else return Qnil;#endif}
Returnstrue
if the file is a block device,false
if it isn't or if the operating system doesn't support thisfeature.
File.stat("testfile").blockdev?#=> falseFile.stat("/dev/hda1").blockdev?#=> true
static VALUErb_stat_b(VALUE obj){#ifdef S_ISBLK if (S_ISBLK(get_stat(obj)->st_mode)) return Qtrue;#endif return Qfalse;}
Returns the number of native file system blocks allocated for this file, ornil
if the operating system doesn't support this feature.
File.stat("testfile").blocks#=> 2
static VALUErb_stat_blocks(VALUE self){#ifdef HAVE_STRUCT_STAT_ST_BLOCKS# if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG return ULL2NUM(get_stat(self)->st_blocks);# else return ULONG2NUM(get_stat(self)->st_blocks);# endif#else return Qnil;#endif}
Returnstrue
if the file is a character device,false
if it isn't or if the operating system doesn'tsupport this feature.
File.stat("/dev/tty").chardev?#=> true
static VALUErb_stat_c(VALUE obj){ if (S_ISCHR(get_stat(obj)->st_mode)) return Qtrue; return Qfalse;}
Returns the change time forstat (that is, the time directoryinformation about the file was changed, not the file itself).
Note that on Windows (NTFS), returns creation time (birth time).
File.stat("testfile").ctime#=> Wed Apr 09 08:53:14 CDT 2003
static VALUErb_stat_ctime(VALUE self){ return stat_ctime(get_stat(self));}
Returns an integer representing the device on whichstat resides.
File.stat("testfile").dev#=> 774
static VALUErb_stat_dev(VALUE self){ return DEVT2NUM(get_stat(self)->st_dev);}
Returns the major part ofFile_Stat#dev
ornil
.
File.stat("/dev/fd1").dev_major#=> 2File.stat("/dev/tty").dev_major#=> 5
static VALUErb_stat_dev_major(VALUE self){#if defined(major) return UINT2NUM(major(get_stat(self)->st_dev));#else return Qnil;#endif}
Returns the minor part ofFile_Stat#dev
ornil
.
File.stat("/dev/fd1").dev_minor#=> 1File.stat("/dev/tty").dev_minor#=> 0
static VALUErb_stat_dev_minor(VALUE self){#if defined(minor) return UINT2NUM(minor(get_stat(self)->st_dev));#else return Qnil;#endif}
Returnstrue
if the named file is a directory, or a symlinkthat points at a directory, andfalse
otherwise.
file_name can be anIO object.
File.directory?(".")
static VALUErb_stat_d(VALUE obj){ if (S_ISDIR(get_stat(obj)->st_mode)) return Qtrue; return Qfalse;}
Returnstrue
ifstat is executable or if theoperating system doesn't distinguish executable files fromnonexecutable files. The tests are made using the effective owner of theprocess.
File.stat("testfile").executable?#=> false
static VALUErb_stat_x(VALUE obj){ struct stat *st = get_stat(obj);#ifdef USE_GETEUID if (geteuid() == 0) { return st->st_mode & S_IXUGO ? Qtrue : Qfalse; }#endif#ifdef S_IXUSR if (rb_stat_owned(obj)) return st->st_mode & S_IXUSR ? Qtrue : Qfalse;#endif#ifdef S_IXGRP if (rb_stat_grpowned(obj)) return st->st_mode & S_IXGRP ? Qtrue : Qfalse;#endif#ifdef S_IXOTH if (!(st->st_mode & S_IXOTH)) return Qfalse;#endif return Qtrue;}
Same asexecutable?
, but tests using the real owner of theprocess.
static VALUErb_stat_X(VALUE obj){ struct stat *st = get_stat(obj);#ifdef USE_GETEUID if (getuid() == 0) { return st->st_mode & S_IXUGO ? Qtrue : Qfalse; }#endif#ifdef S_IXUSR if (rb_stat_rowned(obj)) return st->st_mode & S_IXUSR ? Qtrue : Qfalse;#endif#ifdef S_IXGRP if (rb_group_member(get_stat(obj)->st_gid)) return st->st_mode & S_IXGRP ? Qtrue : Qfalse;#endif#ifdef S_IXOTH if (!(st->st_mode & S_IXOTH)) return Qfalse;#endif return Qtrue;}
Returnstrue
ifstat is a regular file (not a devicefile, pipe, socket, etc.).
File.stat("testfile").file?#=> true
static VALUErb_stat_f(VALUE obj){ if (S_ISREG(get_stat(obj)->st_mode)) return Qtrue; return Qfalse;}
Identifies the type ofstat. The return string is one of:“file
'', “directory
'',“characterSpecial
'',“blockSpecial
'', “fifo
'',“link
'', “socket
'', or“unknown
''.
File.stat("/dev/tty").ftype#=> "characterSpecial"
static VALUErb_stat_ftype(VALUE obj){ return rb_file_ftype(get_stat(obj));}
Returns the numeric group id of the owner ofstat.
File.stat("testfile").gid#=> 500
static VALUErb_stat_gid(VALUE self){ return GIDT2NUM(get_stat(self)->st_gid);}
Returns true if the effective group id of the process is the same as thegroup id ofstat. On Windows NT, returnsfalse
.
File.stat("testfile").grpowned?#=> trueFile.stat("/etc/passwd").grpowned?#=> false
static VALUErb_stat_grpowned(VALUE obj){#ifndef _WIN32 if (rb_group_member(get_stat(obj)->st_gid)) return Qtrue;#endif return Qfalse;}
Returns the inode number forstat.
File.stat("testfile").ino#=> 1083669
static VALUErb_stat_ino(VALUE self){#ifdef HAVE_STRUCT_STAT_ST_INOHIGH /* assume INTEGER_PACK_LSWORD_FIRST and st_inohigh is just next of st_ino */ return rb_integer_unpack(&get_stat(self)->st_ino, 2, SIZEOF_STRUCT_STAT_ST_INO, 0, INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER| INTEGER_PACK_2COMP);#elif SIZEOF_STRUCT_STAT_ST_INO > SIZEOF_LONG return ULL2NUM(get_stat(self)->st_ino);#else return ULONG2NUM(get_stat(self)->st_ino);#endif}
Produce a nicely formatted description ofstat.
File.stat("/etc/passwd").inspect#=> "#<File::Stat dev=0xe000005, ino=1078078, mode=0100644,# nlink=1, uid=0, gid=0, rdev=0x0, size=1374, blksize=4096,# blocks=8, atime=Wed Dec 10 10:16:12 CST 2003,# mtime=Fri Sep 12 15:41:41 CDT 2003,# ctime=Mon Oct 27 11:20:27 CST 2003,# birthtime=Mon Aug 04 08:13:49 CDT 2003>"
static VALUErb_stat_inspect(VALUE self){ VALUE str; size_t i; static const struct { const char *name; VALUE (*func)(VALUE); } member[] = { {"dev", rb_stat_dev}, {"ino", rb_stat_ino}, {"mode", rb_stat_mode}, {"nlink", rb_stat_nlink}, {"uid", rb_stat_uid}, {"gid", rb_stat_gid}, {"rdev", rb_stat_rdev}, {"size", rb_stat_size}, {"blksize", rb_stat_blksize}, {"blocks", rb_stat_blocks}, {"atime", rb_stat_atime}, {"mtime", rb_stat_mtime}, {"ctime", rb_stat_ctime},#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) {"birthtime", rb_stat_birthtime},#endif }; struct stat* st; TypedData_Get_Struct(self, struct stat, &stat_data_type, st); if (!st) { return rb_sprintf("#<%s: uninitialized>", rb_obj_classname(self)); } str = rb_str_buf_new2("#<"); rb_str_buf_cat2(str, rb_obj_classname(self)); rb_str_buf_cat2(str, " "); for (i = 0; i < sizeof(member)/sizeof(member[0]); i++) { VALUE v; if (i > 0) { rb_str_buf_cat2(str, ", "); } rb_str_buf_cat2(str, member[i].name); rb_str_buf_cat2(str, "="); v = (*member[i].func)(self); if (i == 2) { /* mode */ rb_str_catf(str, "0%lo", (unsigned long)NUM2ULONG(v)); } else if (i == 0 || i == 6) { /* dev/rdev */ rb_str_catf(str, "0x%"PRI_DEVT_PREFIX"x", NUM2DEVT(v)); } else { rb_str_append(str, rb_inspect(v)); } } rb_str_buf_cat2(str, ">"); return str;}
Returns an integer representing the permission bits ofstat. Themeaning of the bits is platform dependent; on Unix systems, seestat(2)
.
File.chmod(0644,"testfile")#=> 1s =File.stat("testfile")sprintf("%o",s.mode)#=> "100644"
static VALUErb_stat_mode(VALUE self){ return UINT2NUM(ST2UINT(get_stat(self)->st_mode));}
Returns the modification time ofstat.
File.stat("testfile").mtime#=> Wed Apr 09 08:53:14 CDT 2003
static VALUErb_stat_mtime(VALUE self){ return stat_mtime(get_stat(self));}
Returns the number of hard links tostat.
File.stat("testfile").nlink#=> 1File.link("testfile","testfile.bak")#=> 0File.stat("testfile").nlink#=> 2
static VALUErb_stat_nlink(VALUE self){ /* struct stat::st_nlink is nlink_t in POSIX. Not the case for Windows. */ const struct stat *ptr = get_stat(self); if (sizeof(ptr->st_nlink) <= sizeof(int)) { return UINT2NUM((unsigned)ptr->st_nlink); } else if (sizeof(ptr->st_nlink) == sizeof(long)) { return ULONG2NUM((unsigned long)ptr->st_nlink); } else if (sizeof(ptr->st_nlink) == sizeof(LONG_LONG)) { return ULL2NUM((unsigned LONG_LONG)ptr->st_nlink); } else { rb_bug(":FIXME: don't know what to do"); }}
Returnstrue
if the effective user id of the process is thesame as the owner ofstat.
File.stat("testfile").owned?#=> trueFile.stat("/etc/passwd").owned?#=> false
static VALUErb_stat_owned(VALUE obj){ if (get_stat(obj)->st_uid == geteuid()) return Qtrue; return Qfalse;}
Returnstrue
if the operating system supports pipes andstat is a pipe;false
otherwise.
static VALUErb_stat_p(VALUE obj){#ifdef S_IFIFO if (S_ISFIFO(get_stat(obj)->st_mode)) return Qtrue;#endif return Qfalse;}
Returns an integer representing the device type on whichstatresides. Returnsnil
if the operating system doesn'tsupport this feature.
File.stat("/dev/fd1").rdev#=> 513File.stat("/dev/tty").rdev#=> 1280
static VALUErb_stat_rdev(VALUE self){#ifdef HAVE_STRUCT_STAT_ST_RDEV return DEVT2NUM(get_stat(self)->st_rdev);#else return Qnil;#endif}
Returns the major part ofFile_Stat#rdev
ornil
.
File.stat("/dev/fd1").rdev_major#=> 2File.stat("/dev/tty").rdev_major#=> 5
static VALUErb_stat_rdev_major(VALUE self){#if defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(major) return UINT2NUM(major(get_stat(self)->st_rdev));#else return Qnil;#endif}
Returns the minor part ofFile_Stat#rdev
ornil
.
File.stat("/dev/fd1").rdev_minor#=> 1File.stat("/dev/tty").rdev_minor#=> 0
static VALUErb_stat_rdev_minor(VALUE self){#if defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(minor) return UINT2NUM(minor(get_stat(self)->st_rdev));#else return Qnil;#endif}
Returnstrue
ifstat is readable by the effectiveuser id of this process.
File.stat("testfile").readable?#=> true
static VALUErb_stat_r(VALUE obj){ struct stat *st = get_stat(obj);#ifdef USE_GETEUID if (geteuid() == 0) return Qtrue;#endif#ifdef S_IRUSR if (rb_stat_owned(obj)) return st->st_mode & S_IRUSR ? Qtrue : Qfalse;#endif#ifdef S_IRGRP if (rb_stat_grpowned(obj)) return st->st_mode & S_IRGRP ? Qtrue : Qfalse;#endif#ifdef S_IROTH if (!(st->st_mode & S_IROTH)) return Qfalse;#endif return Qtrue;}
Returnstrue
ifstat is readable by the real user idof this process.
File.stat("testfile").readable_real?#=> true
static VALUErb_stat_R(VALUE obj){ struct stat *st = get_stat(obj);#ifdef USE_GETEUID if (getuid() == 0) return Qtrue;#endif#ifdef S_IRUSR if (rb_stat_rowned(obj)) return st->st_mode & S_IRUSR ? Qtrue : Qfalse;#endif#ifdef S_IRGRP if (rb_group_member(get_stat(obj)->st_gid)) return st->st_mode & S_IRGRP ? Qtrue : Qfalse;#endif#ifdef S_IROTH if (!(st->st_mode & S_IROTH)) return Qfalse;#endif return Qtrue;}
Returnstrue
ifstat has the set-group-id permissionbit set,false
if it doesn't or if the operating systemdoesn't support this feature.
File.stat("/usr/sbin/lpc").setgid?#=> true
static VALUErb_stat_sgid(VALUE obj){#ifdef S_ISGID if (get_stat(obj)->st_mode & S_ISGID) return Qtrue;#endif return Qfalse;}
Returnstrue
ifstat has the set-user-id permissionbit set,false
if it doesn't or if the operating systemdoesn't support this feature.
File.stat("/bin/su").setuid?#=> true
static VALUErb_stat_suid(VALUE obj){#ifdef S_ISUID if (get_stat(obj)->st_mode & S_ISUID) return Qtrue;#endif return Qfalse;}
Returns the size ofstat in bytes.
File.stat("testfile").size#=> 66
static VALUErb_stat_size(VALUE self){ return OFFT2NUM(get_stat(self)->st_size);}
Returns the size ofstat in bytes.
File.stat("testfile").size#=> 66
static VALUErb_stat_s(VALUE obj){ off_t size = get_stat(obj)->st_size; if (size == 0) return Qnil; return OFFT2NUM(size);}
Returnstrue
ifstat is a socket,false
if it isn't or if the operating system doesn't support thisfeature.
File.stat("testfile").socket?#=> false
static VALUErb_stat_S(VALUE obj){#ifdef S_ISSOCK if (S_ISSOCK(get_stat(obj)->st_mode)) return Qtrue;#endif return Qfalse;}
Returnstrue
ifstat has its sticky bit set,false
if it doesn't or if the operating system doesn'tsupport this feature.
File.stat("testfile").sticky?#=> false
static VALUErb_stat_sticky(VALUE obj){#ifdef S_ISVTX if (get_stat(obj)->st_mode & S_ISVTX) return Qtrue;#endif return Qfalse;}
Returnstrue
ifstat is a symbolic link,false
if it isn't or if the operating system doesn'tsupport this feature. AsFile.statautomatically follows symbolic links,symlink? will always befalse
for an object returned byFile.stat.
File.symlink("testfile","alink")#=> 0File.stat("alink").symlink?#=> falseFile.lstat("alink").symlink?#=> true
static VALUErb_stat_l(VALUE obj){#ifdef S_ISLNK if (S_ISLNK(get_stat(obj)->st_mode)) return Qtrue;#endif return Qfalse;}
Returns the numeric user id of the owner ofstat.
File.stat("testfile").uid#=> 501
static VALUErb_stat_uid(VALUE self){ return UIDT2NUM(get_stat(self)->st_uid);}
Ifstat is readable by others, returns an integer representing thefile permission bits ofstat. Returnsnil
otherwise.The meaning of the bits is platform dependent; on Unix systems, seestat(2)
.
m =File.stat("/etc/passwd").world_readable?#=> 420sprintf("%o",m)#=> "644"
static VALUErb_stat_wr(VALUE obj){#ifdef S_IROTH struct stat *st = get_stat(obj); if ((st->st_mode & (S_IROTH)) == S_IROTH) { return UINT2NUM(st->st_mode & (S_IRUGO|S_IWUGO|S_IXUGO)); } else { return Qnil; }#endif}
Ifstat is writable by others, returns an integer representing thefile permission bits ofstat. Returnsnil
otherwise.The meaning of the bits is platform dependent; on Unix systems, seestat(2)
.
m =File.stat("/tmp").world_writable?#=> 511sprintf("%o",m)#=> "777"
static VALUErb_stat_ww(VALUE obj){#ifdef S_IROTH struct stat *st = get_stat(obj); if ((st->st_mode & (S_IWOTH)) == S_IWOTH) { return UINT2NUM(st->st_mode & (S_IRUGO|S_IWUGO|S_IXUGO)); } else { return Qnil; }#endif}
Returnstrue
ifstat is writable by the effectiveuser id of this process.
File.stat("testfile").writable?#=> true
static VALUErb_stat_w(VALUE obj){ struct stat *st = get_stat(obj);#ifdef USE_GETEUID if (geteuid() == 0) return Qtrue;#endif#ifdef S_IWUSR if (rb_stat_owned(obj)) return st->st_mode & S_IWUSR ? Qtrue : Qfalse;#endif#ifdef S_IWGRP if (rb_stat_grpowned(obj)) return st->st_mode & S_IWGRP ? Qtrue : Qfalse;#endif#ifdef S_IWOTH if (!(st->st_mode & S_IWOTH)) return Qfalse;#endif return Qtrue;}
Returnstrue
ifstat is writable by the real user idof this process.
File.stat("testfile").writable_real?#=> true
static VALUErb_stat_W(VALUE obj){ struct stat *st = get_stat(obj);#ifdef USE_GETEUID if (getuid() == 0) return Qtrue;#endif#ifdef S_IWUSR if (rb_stat_rowned(obj)) return st->st_mode & S_IWUSR ? Qtrue : Qfalse;#endif#ifdef S_IWGRP if (rb_group_member(get_stat(obj)->st_gid)) return st->st_mode & S_IWGRP ? Qtrue : Qfalse;#endif#ifdef S_IWOTH if (!(st->st_mode & S_IWOTH)) return Qfalse;#endif return Qtrue;}
This page was generated for Ruby 3.0.0
Generated with Ruby-doc Rdoc Generator 0.42.0.