Class: File
Overview
A File
is an abstraction of any file object accessible by the program and is closely associated with class IO
File
includes the methods of module FileTest
as class methods, allowing you to write (for example) File.exist?("foo")
.
In the description of File methods, permission bits are a platform-specific set of bits that indicate permissions of a file. On Unix-based systems, permissions are viewed as a set of three octets, for the owner, the group, and the rest of the world. For each of these entities, permissions may be set to read, write, or execute the file:
The permission bits 0644
(in octal) would thus be interpreted as read/write for owner, and read-only for group and other. Higher-order bits may also be used to indicate the type of file (plain, directory, pipe, socket, and so on) and various other special features. If the permissions are for a directory, the meaning of the execute bit changes; when set the directory can be searched.
On non-Posix operating systems, there may be only the ability to make a file read-only or read-write. In this case, the remaining permission bits will be synthesized to resemble typical values. For instance, on Windows NT the default permission bits are 0644
, which means read/write for owner, read-only for all others. The only change that can be made is to make the file read-only, which is reported as 0444
.
Various constants for the methods in File can be found in File::Constants.
Defined Under Namespace
Modules: Constants Classes: Stat
Constant Summary collapse
- Separator =
separates directory parts in path
separator
- SEPARATOR =
separator
- ALT_SEPARATOR =
platform specific alternative separator
Qnil
- PATH_SEPARATOR =
path list separator
rb_obj_freeze(rb_str_new2(PATH_SEP))
Constants inherited from IO
IO::SEEK_CUR, IO::SEEK_DATA, IO::SEEK_END, IO::SEEK_HOLE, IO::SEEK_SET
Constants included from Constants
Constants::APPEND, Constants::BINARY, Constants::CREAT, Constants::DIRECT, Constants::DSYNC, Constants::EXCL, Constants::LOCK_EX, Constants::LOCK_NB, Constants::LOCK_SH, Constants::LOCK_UN, Constants::NOATIME, Constants::NOCTTY, Constants::NOFOLLOW, Constants::NONBLOCK, Constants::NULL, Constants::RDONLY, Constants::RDWR, Constants::RSYNC, Constants::SYNC, Constants::TRUNC, Constants::WRONLY
Class Method Summary collapse
-
.absolute_path(file_name[, dir_string]) ⇒ Object
Converts a pathname to an absolute pathname.
-
.atime(file_name) ⇒ Time
Returns the last access time for the named file as a Time object).
-
.basename(file_name[, suffix]) ⇒ Object
Returns the last component of the filename given in file_name, which can be formed using both
File::SEPARATOR
andFile::ALT_SEPARATOR
as the separator whenFile::ALT_SEPARATOR
is notnil
. -
.blockdev?(file_name) ⇒ Boolean
Returns
true
if the named file is a block device. -
.chardev?(file_name) ⇒ Boolean
Returns
true
if the named file is a character device. -
.chmod(mode_int, file_name, ...) ⇒ Integer
Changes permission bits on the named file(s) to the bit pattern represented by mode_int.
-
.chown(owner_int, group_int, file_name, ...) ⇒ Integer
Changes the owner and group of the named file(s) to the given numeric owner and group id’s.
-
.ctime(file_name) ⇒ Time
Returns the change time for the named file (the time at which directory information about the file was changed, not the file itself).
-
.delete(args) ⇒ Object
Deletes the named files, returning the number of names passed as arguments.
-
.directory?(fname) ⇒ Boolean
call-seq: File.directory?(file_name) -> true or false.
-
.dirname(file_name) ⇒ Object
Returns all components of the filename given in file_name except the last one.
-
.executable?(file_name) ⇒ Boolean
Returns
true
if the named file is executable by the effective user id of this process. -
.executable_real?(file_name) ⇒ Boolean
Returns
true
if the named file is executable by the real user id of this process. -
.exist?(fname) ⇒ Object
Return
true
if the named file exists. - .exists?(fname) ⇒ Boolean
-
.expand_path(file_name[, dir_string]) ⇒ Object
Converts a pathname to an absolute pathname.
-
.extname(path) ⇒ String
Returns the extension (the portion of file name in
path
starting from the last period). -
.file?(file_name) ⇒ Boolean
Returns
true
if the named file exists and is a regular file. -
.fnmatch(*args) ⇒ Object
Returns true if
path
matches againstpattern
. -
.fnmatch?(*args) ⇒ Object
Returns true if
path
matches againstpattern
. -
.ftype(file_name) ⇒ String
Identifies the type of the named file; the return string is one of “
file
”, “directory
”, “characterSpecial
”, “blockSpecial
”, “fifo
”, “link
”, “socket
”, or “unknown
”. -
.grpowned?(file_name) ⇒ Boolean
Returns
true
if the named file exists and the effective group id of the calling process is the owner of the file. -
.identical?(file_1, file_2) ⇒ Boolean
Returns
true
if the named files are identical. -
.join(string, ...) ⇒ Object
Returns a new string formed by joining the strings using
File::SEPARATOR
. -
.lchmod(mode_int, file_name, ...) ⇒ Integer
Equivalent to
File::chmod
, but does not follow symbolic links (so it will change the permissions associated with the link, not the file referenced by the link). -
.lchown(owner_int, group_int, file_name, ..) ⇒ Integer
Equivalent to
File::chown
, but does not follow symbolic links (so it will change the owner associated with the link, not the file referenced by the link). -
.link(old_name, new_name) ⇒ 0
Creates a new name for an existing file using a hard link.
-
.lstat(file_name) ⇒ Object
Same as
File::stat
, but does not follow the last symbolic link. -
.mtime(file_name) ⇒ Time
Returns the modification time for the named file as a Time object.
-
.open(*args) ⇒ Object
call-seq: IO.open(fd, mode=“r” [, opt]) -> io IO.open(fd, mode=“r” [, opt]) { |io| block } -> obj.
-
.owned?(file_name) ⇒ Boolean
Returns
true
if the named file exists and the effective used id of the calling process is the owner of the file. -
.path(path) ⇒ String
Returns the string representation of the path.
-
.pipe?(file_name) ⇒ Boolean
Returns
true
if the named file is a pipe. -
.readable?(file_name) ⇒ Boolean
Returns
true
if the named file is readable by the effective user id of this process. -
.readable_real?(file_name) ⇒ Boolean
Returns
true
if the named file is readable by the real user id of this process. -
.readlink(link_name) ⇒ Object
Returns the name of the file referenced by the given link.
-
.realdirpath(pathname[, dir_string]) ⇒ Object
Returns the real (absolute) pathname of pathname in the actual filesystem.
-
.realpath(pathname[, dir_string]) ⇒ Object
Returns the real (absolute) pathname of pathname in the actual filesystem not containing symlinks or useless dots.
-
.rename(old_name, new_name) ⇒ 0
Renames the given file to the new name.
-
.setgid?(file_name) ⇒ Boolean
Returns
true
if the named file has the setgid bit set. -
.setuid?(file_name) ⇒ Boolean
Returns
true
if the named file has the setuid bit set. -
.size(file_name) ⇒ Integer
Returns the size of
file_name
. -
.size?(file_name) ⇒ Integer?
Returns
nil
iffile_name
doesn’t exist or has zero size, the size of the file otherwise. -
.socket?(file_name) ⇒ Boolean
Returns
true
if the named file is a socket. -
.split(file_name) ⇒ Array
Splits the given string into a directory and a file component and returns them in a two-element array.
-
.stat(file_name) ⇒ Object
Returns a
File::Stat
object for the named file (seeFile::Stat
). -
.sticky?(file_name) ⇒ Boolean
Returns
true
if the named file has the sticky bit set. -
.symlink(old_name, new_name) ⇒ 0
Creates a symbolic link called new_name for the existing file old_name.
-
.symlink?(file_name) ⇒ Boolean
Returns
true
if the named file is a symbolic link. -
.truncate(file_name, integer) ⇒ 0
Truncates the file file_name to be at most integer bytes long.
-
.umask(*args) ⇒ Object
Returns the current umask value for this process.
-
.unlink(args) ⇒ Object
Deletes the named files, returning the number of names passed as arguments.
-
.utime(atime, mtime, file_name, ...) ⇒ Integer
Sets the access and modification times of each named file to the first two arguments.
-
.world_readable?(file_name) ⇒ Fixnum?
If file_name is readable by others, returns an integer representing the file permission bits of file_name.
-
.world_writable?(file_name) ⇒ Fixnum?
If file_name is writable by others, returns an integer representing the file permission bits of file_name.
-
.writable?(file_name) ⇒ Boolean
Returns
true
if the named file is writable by the effective user id of this process. -
.writable_real?(file_name) ⇒ Boolean
Returns
true
if the named file is writable by the real user id of this process. -
.zero?(file_name) ⇒ Boolean
Returns
true
if the named file exists and has a zero size.
Instance Method Summary collapse
-
#atime ⇒ Time
Returns the last access time (a
Time
object) for file, or epoch if file has not been accessed. -
#chmod(mode_int) ⇒ 0
Changes permission bits on file to the bit pattern represented by mode_int.
-
#chown(owner_int, group_int) ⇒ 0
Changes the owner and group of file to the given numeric owner and group id’s.
-
#ctime ⇒ Time
Returns the change time for file (that is, the time directory information about the file was changed, not the file itself).
-
#flock(locking_constant) ⇒ 0, false
Locks or unlocks a file according to locking_constant (a logical or of the values in the table below).
-
#initialize(*args) ⇒ Object
constructor
Opens the file named by
filename
according to the givenmode
and returns a new File object. -
#lstat ⇒ Object
Same as
IO#stat
, but does not follow the last symbolic link. -
#mtime ⇒ Time
Returns the modification time for file.
-
#path ⇒ Object
Returns the pathname used to create file as a string.
-
#size ⇒ Integer
Returns the size of file in bytes.
-
#to_path ⇒ Object
Returns the pathname used to create file as a string.
-
#truncate(integer) ⇒ 0
Truncates file to at most integer bytes.
Methods inherited from IO
#<<, #advise, #autoclose=, #autoclose?, #binmode, #binmode?, binread, binwrite, #bytes, #chars, #close, #close_on_exec=, #close_on_exec?, #close_read, #close_write, #closed?, #codepoints, copy_stream, #each, #each_byte, #each_char, #each_codepoint, #each_line, #eof, #eof?, #external_encoding, #fcntl, #fdatasync, #fileno, #flush, for_fd, foreach, #fsync, #getbyte, #getc, #gets, #initialize_copy, #inspect, #internal_encoding, #ioctl, #isatty, #lineno, #lineno=, #lines, new, #pid, pipe, popen, #pos, #pos=, #print, #printf, #putc, #puts, read, #read, #read_nonblock, #readbyte, #readchar, #readline, #readlines, readlines, #readpartial, #reopen, #rewind, #seek, select, #set_encoding, #stat, #sync, #sync=, sysopen, #sysread, #sysseek, #syswrite, #tell, #to_io, try_convert, #tty?, #ungetbyte, #ungetc, #write, write, #write_nonblock
Methods included from Enumerable
#all?, #any?, #chunk, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #find, #find_all, #find_index, #first, #flat_map, #grep, #group_by, #include?, #inject, #lazy, #map, #max, #max_by, #member?, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reduce, #reject, #reverse_each, #select, #slice_before, #sort, #sort_by, #take, #take_while, #to_a, #to_h, #zip
Constructor Details
#new(filename, mode = "r"[, opt]) ⇒ File #new(filename[, mode [, perm]][, opt]) ⇒ File
Opens the file named by filename
according to the given mode
and returns a new File object.
See IO.new for a description of mode
and opt
.
If a file is being created, permission bits may be given in perm
. These mode and permission bits are platform dependent; on Unix systems, see open(2) and chmod(2) man pages for details.
Examples
f = File.new("testfile", "r")
f = File.new("newfile", "w+")
f = File.new("newfile", File::CREAT|File::TRUNC|File::RDWR, 0644)
7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 |
# File 'io.c', line 7457
static VALUE
rb_file_initialize(int argc, VALUE *argv, VALUE io)
{
if (RFILE(io)->fptr) {
rb_raise(rb_eRuntimeError, "reinitializing File");
}
if (0 < argc && argc < 3) {
VALUE fd = rb_check_convert_type(argv[0], T_FIXNUM, "Fixnum", "to_int");
if (!NIL_P(fd)) {
argv[0] = fd;
return rb_io_initialize(argc, argv, io);
}
}
rb_open_file(argc, argv, io);
return io;
}
|
Class Method Details
.absolute_path(file_name[, dir_string]) ⇒ Object
Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point. If the given pathname starts with a “~
” it is NOT expanded, it is treated as a normal directory name.
File.absolute_path("~oracle/bin") #=> "<relative_path>/~oracle/bin"
3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 |
# File 'file.c', line 3484
VALUE
rb_file_s_absolute_path(int argc, VALUE *argv)
{
VALUE fname, dname;
if (argc == 1) {
return rb_file_absolute_path(argv[0], Qnil);
}
rb_scan_args(argc, argv, "11", &fname, &dname);
return rb_file_absolute_path(fname, dname);
}
|
.atime(file_name) ⇒ Time
Returns the last access time for the named file as a Time object).
file_name can be an IO object.
File.atime("testfile") #=> Wed Apr 09 08:51:48 CDT 2003
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 |
# File 'file.c', line 1948
static VALUE
rb_file_s_atime(VALUE klass, VALUE fname)
{
struct stat st;
if (rb_stat(fname, &st) < 0) {
FilePathValue(fname);
rb_sys_fail_path(fname);
}
return stat_atime(&st);
}
|
.basename(file_name[, suffix]) ⇒ Object
Returns the last component of the filename given in file_name, which can be formed using both File::SEPARATOR
and File::ALT_SEPARATOR
as the separator when File::ALT_SEPARATOR
is not nil
. If suffix is given and present at the end of file_name, it is removed.
File.basename("/home/gumby/work/ruby.rb") #=> "ruby.rb"
File.basename("/home/gumby/work/ruby.rb", ".rb") #=> "ruby"
3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 |
# File 'file.c', line 3839
static VALUE
rb_file_s_basename(int argc, VALUE *argv)
{
VALUE fname, fext, basename;
const char *name, *p;
long f, n;
rb_encoding *enc;
if (rb_scan_args(argc, argv, "11", &fname, &fext) == 2) {
StringValue(fext);
enc = check_path_encoding(fext);
}
FilePathStringValue(fname);
if (NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
enc = rb_enc_get(fname);
fext = Qnil;
}
if ((n = RSTRING_LEN(fname)) == 0 || !*(name = RSTRING_PTR(fname)))
return rb_str_new_shared(fname);
p = ruby_enc_find_basename(name, &f, &n, enc);
if (n >= 0) {
if (NIL_P(fext)) {
f = n;
}
else {
const char *fp;
fp = StringValueCStr(fext);
if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
f = n;
}
RB_GC_GUARD(fext);
}
if (f == RSTRING_LEN(fname)) return rb_str_new_shared(fname);
}
basename = rb_str_new(p, f);
rb_enc_copy(basename, fname);
OBJ_INFECT(basename, fname);
return basename;
}
|
.blockdev?(file_name) ⇒ Boolean
Returns true
if the named file is a block device.
file_name can be an IO object.
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 |
# File 'file.c', line 1336
static VALUE
rb_file_blockdev_p(VALUE obj, VALUE fname)
{
#ifndef S_ISBLK
# ifdef S_IFBLK
# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
# else
# define S_ISBLK(m) (0) /* anytime false */
# endif
#endif
#ifdef S_ISBLK
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (S_ISBLK(st.st_mode)) return Qtrue;
#endif
return Qfalse;
}
|
.chardev?(file_name) ⇒ Boolean
Returns true
if the named file is a character device.
file_name can be an IO object.
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 |
# File 'file.c', line 1365
static VALUE
rb_file_chardev_p(VALUE obj, VALUE fname)
{
#ifndef S_ISCHR
# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
#endif
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (S_ISCHR(st.st_mode)) return Qtrue;
return Qfalse;
}
|
.chmod(mode_int, file_name, ...) ⇒ Integer
Changes permission bits on the named file(s) to the bit pattern represented by mode_int. Actual effects are operating system dependent (see the beginning of this section). On Unix systems, see chmod(2)
for details. Returns the number of files processed.
File.chmod(0644, "testfile", "out") #=> 2
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 |
# File 'file.c', line 2131
static VALUE
rb_file_s_chmod(int argc, VALUE *argv)
{
VALUE vmode;
VALUE rest;
int mode;
long n;
rb_secure(2);
rb_scan_args(argc, argv, "1*", &vmode, &rest);
mode = NUM2INT(vmode);
n = apply2files(chmod_internal, rest, &mode);
return LONG2FIX(n);
}
|
.chown(owner_int, group_int, file_name, ...) ⇒ Integer
Changes the owner and group of the named file(s) to the given numeric owner and group id’s. Only a process with superuser privileges may change the owner of a file. The current owner of a file may change the file’s group to any group to which the owner belongs. A nil
or -1 owner or group id is ignored. Returns the number of files processed.
File.chown(nil, 100, "testfile")
2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 |
# File 'file.c', line 2250
static VALUE
rb_file_s_chown(int argc, VALUE *argv)
{
VALUE o, g, rest;
struct chown_args arg;
long n;
rb_secure(2);
rb_scan_args(argc, argv, "2*", &o, &g, &rest);
if (NIL_P(o)) {
arg.owner = -1;
}
else {
arg.owner = NUM2UIDT(o);
}
if (NIL_P(g)) {
arg.group = -1;
}
else {
arg.group = NUM2GIDT(g);
}
n = apply2files(chown_internal, rest, &arg);
return LONG2FIX(n);
}
|
.ctime(file_name) ⇒ Time
Returns the change time for the named file (the time at which directory information about the file was changed, not the file itself).
file_name can be an IO object.
Note that on Windows (NTFS), returns creation time (birth time).
File.ctime("testfile") #=> Wed Apr 09 08:53:13 CDT 2003
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 |
# File 'file.c', line 2047
static VALUE
rb_file_s_ctime(VALUE klass, VALUE fname)
{
struct stat st;
if (rb_stat(fname, &st) < 0) {
FilePathValue(fname);
rb_sys_fail_path(fname);
}
return stat_ctime(&st);
}
|
.delete(file_name, ...) ⇒ Integer .unlink(file_name, ...) ⇒ Integer
Deletes the named files, returning the number of names passed as arguments. Raises an exception on any error. See also Dir::rmdir
.
2652 2653 2654 2655 2656 2657 2658 2659 2660 |
# File 'file.c', line 2652
static VALUE
rb_file_s_unlink(VALUE klass, VALUE args)
{
long n;
rb_secure(2);
n = apply2files(unlink_internal, args, 0);
return LONG2FIX(n);
}
|
.directory?(fname) ⇒ Boolean
call-seq:
File.directory?(file_name) -> true or false
Returns true
if the named file is a directory, or a symlink that points at a directory, and false
otherwise.
file_name can be an IO object.
File.directory?(".")
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 |
# File 'file.c', line 1214
VALUE
rb_file_directory_p(VALUE obj, VALUE fname)
{
#ifndef S_ISDIR
# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (S_ISDIR(st.st_mode)) return Qtrue;
return Qfalse;
}
|
.dirname(file_name) ⇒ Object
Returns all components of the filename given in file_name except the last one. The filename can be formed using both File::SEPARATOR
and File::ALT_SEPARATOR
as the separator when File::ALT_SEPARATOR
is not nil
.
File.dirname("/home/gumby/work/ruby.rb") #=> "/home/gumby/work"
3893 3894 3895 3896 3897 |
# File 'file.c', line 3893
static VALUE
rb_file_s_dirname(VALUE klass, VALUE fname)
{
return rb_file_dirname(fname);
}
|
.executable?(file_name) ⇒ Boolean
Returns true
if the named file is executable by the effective user id of this process.
1565 1566 1567 1568 1569 1570 1571 1572 1573 |
# File 'file.c', line 1565
static VALUE
rb_file_executable_p(VALUE obj, VALUE fname)
{
rb_secure(2);
FilePathValue(fname);
fname = rb_str_encode_ospath(fname);
if (eaccess(StringValueCStr(fname), X_OK) < 0) return Qfalse;
return Qtrue;
}
|
.executable_real?(file_name) ⇒ Boolean
Returns true
if the named file is executable by the real user id of this process.
1583 1584 1585 1586 1587 1588 1589 1590 1591 |
# File 'file.c', line 1583
static VALUE
rb_file_executable_real_p(VALUE obj, VALUE fname)
{
rb_secure(2);
FilePathValue(fname);
fname = rb_str_encode_ospath(fname);
if (access(StringValueCStr(fname), X_OK) < 0) return Qfalse;
return Qtrue;
}
|
.exist?(file_name) ⇒ Boolean .exists?(file_name) ⇒ Boolean
Return true
if the named file exists.
file_name can be an IO object.
“file exists” means that stat() or fstat() system call is successful.
1392 1393 1394 1395 1396 1397 1398 1399 |
# File 'file.c', line 1392
static VALUE
rb_file_exist_p(VALUE obj, VALUE fname)
{
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
return Qtrue;
}
|
.exists?(fname) ⇒ Boolean
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 |
# File 'file.c', line 1401
static VALUE
rb_file_exists_p(VALUE obj, VALUE fname)
{
const char *s = "FileTest#";
if (obj == rb_mFileTest) {
s = "FileTest.";
}
else if (obj == rb_cFile ||
(RB_TYPE_P(obj, T_CLASS) &&
RTEST(rb_class_inherited_p(obj, rb_cFile)))) {
s = "File.";
}
rb_warning("%sexists? is a deprecated name, use %sexist? instead", s, s);
return rb_file_exist_p(obj, fname);
}
|
.expand_path(file_name[, dir_string]) ⇒ Object
Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string
is given, in which case it will be used as the starting point. The given pathname may start with a “~
”, which expands to the process owner’s home directory (the environment variable HOME
must be set correctly). “~
user” expands to the named user’s home directory.
File.("~oracle/bin") #=> "/home/oracle/bin"
A simple example of using dir_string
is as follows.
File.("ruby", "/usr/bin") #=> "/usr/bin/ruby"
A more complex example which also resolves parent directory is as follows. Suppose we are in bin/mygem and want the absolute path of lib/mygem.rb.
File.("../../lib/mygem.rb", __FILE__)
#=> ".../path/to/project/lib/mygem.rb"
So first it resolves the parent of __FILE__, that is bin/, then go to the parent, the root of the project and appends lib/mygem.rb
.
3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 |
# File 'file.c', line 3451
VALUE
rb_file_s_expand_path(int argc, VALUE *argv)
{
VALUE fname, dname;
if (argc == 1) {
return rb_file_expand_path(argv[0], Qnil);
}
rb_scan_args(argc, argv, "11", &fname, &dname);
return rb_file_expand_path(fname, dname);
}
|
.extname(path) ⇒ String
Returns the extension (the portion of file name in path
starting from the last period).
If path
is a dotfile, or starts with a period, then the starting dot is not dealt with the start of the extension.
An empty string will also be returned when the period is the last character in path
.
File.extname("test.rb") #=> ".rb"
File.extname("a/b/d/test.rb") #=> ".rb"
File.extname("foo.") #=> ""
File.extname("test") #=> ""
File.extname(".profile") #=> ""
File.extname(".profile.sh") #=> ".sh"
4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 |
# File 'file.c', line 4029
static VALUE
rb_file_s_extname(VALUE klass, VALUE fname)
{
const char *name, *e;
long len;
VALUE extname;
FilePathStringValue(fname);
name = StringValueCStr(fname);
len = RSTRING_LEN(fname);
e = ruby_enc_find_extname(name, &len, rb_enc_get(fname));
if (len <= 1)
return rb_str_new(0, 0);
extname = rb_str_subseq(fname, e - name, len); /* keep the dot, too! */
OBJ_INFECT(extname, fname);
return extname;
}
|
.file?(file_name) ⇒ Boolean
Returns true
if the named file exists and is a regular file.
file_name can be an IO object.
1607 1608 1609 1610 1611 1612 1613 1614 1615 |
# File 'file.c', line 1607
static VALUE
rb_file_file_p(VALUE obj, VALUE fname)
{
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (S_ISREG(st.st_mode)) return Qtrue;
return Qfalse;
}
|
.fnmatch(pattern, path, [flags]) ⇒ Boolean .fnmatch?(pattern, path, [flags]) ⇒ Boolean
Returns true if path
matches against pattern
. The pattern is not a regular expression; instead it follows rules similar to shell filename globbing. It may contain the following metacharacters:
*
-
Matches any file. Can be restricted by other values in the glob. Equivalent to
/ .* /x
in regexp.*
-
Matches all files regular files
c*
-
Matches all files beginning with
c
*c
-
Matches all files ending with
c
*c*
-
Matches all files that have
c
in them (including at the beginning or end).
To match hidden files (that start with a
.
set the File::FNM_DOTMATCH flag. **
-
Matches directories recursively or files expansively.
?
-
Matches any one character. Equivalent to
/.{1}/
in regexp. [set]
-
Matches any one character in
set
. Behaves exactly like character sets in Regexp, including set negation ([^a-z]
). \
-
Escapes the next metacharacter.
{a,b}
-
Matches pattern a and pattern b if File::FNM_EXTGLOB flag is enabled. Behaves like a Regexp union (
(?:a|b)
).
flags
is a bitwise OR of the FNM_XXX
constants. The same glob pattern and flags are used by Dir::glob.
Examples:
File.fnmatch('cat', 'cat') #=> true # match entire string
File.fnmatch('cat', 'category') #=> false # only match partial string
File.fnmatch('c{at,ub}s', 'cats') #=> false # { } isn't supported by default
File.fnmatch('c{at,ub}s', 'cats', File::FNM_EXTGLOB) #=> true # { } is supported on FNM_EXTGLOB
File.fnmatch('c?t', 'cat') #=> true # '?' match only 1 character
File.fnmatch('c??t', 'cat') #=> false # ditto
File.fnmatch('c*', 'cats') #=> true # '*' match 0 or more characters
File.fnmatch('c*t', 'c/a/b/t') #=> true # ditto
File.fnmatch('ca[a-z]', 'cat') #=> true # inclusive bracket expression
File.fnmatch('ca[^t]', 'cat') #=> false # exclusive bracket expression ('^' or '!')
File.fnmatch('cat', 'CAT') #=> false # case sensitive
File.fnmatch('cat', 'CAT', File::FNM_CASEFOLD) #=> true # case insensitive
File.fnmatch('?', '/', File::FNM_PATHNAME) #=> false # wildcard doesn't match '/' on FNM_PATHNAME
File.fnmatch('*', '/', File::FNM_PATHNAME) #=> false # ditto
File.fnmatch('[/]', '/', File::FNM_PATHNAME) #=> false # ditto
File.fnmatch('\?', '?') #=> true # escaped wildcard becomes ordinary
File.fnmatch('\a', 'a') #=> true # escaped ordinary remains ordinary
File.fnmatch('\a', '\a', File::FNM_NOESCAPE) #=> true # FNM_NOESCAPE makes '\' ordinary
File.fnmatch('[\?]', '?') #=> true # can escape inside bracket expression
File.fnmatch('*', '.profile') #=> false # wildcard doesn't match leading
File.fnmatch('*', '.profile', File::FNM_DOTMATCH) #=> true # period by default.
File.fnmatch('.*', '.profile') #=> true
rbfiles = '**' '/' '*.rb' # you don't have to do like this. just write in single string.
File.fnmatch(rbfiles, 'main.rb') #=> false
File.fnmatch(rbfiles, './main.rb') #=> false
File.fnmatch(rbfiles, 'lib/song.rb') #=> true
File.fnmatch('**.rb', 'main.rb') #=> true
File.fnmatch('**.rb', './main.rb') #=> false
File.fnmatch('**.rb', 'lib/song.rb') #=> true
File.fnmatch('*', 'dave/.profile') #=> true
pattern = '*' '/' '*'
File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME) #=> false
File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true
pattern = '**' '/' 'foo'
File.fnmatch(pattern, 'a/b/c/foo', File::FNM_PATHNAME) #=> true
File.fnmatch(pattern, '/a/b/c/foo', File::FNM_PATHNAME) #=> true
File.fnmatch(pattern, 'c:/a/b/c/foo', File::FNM_PATHNAME) #=> true
File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME) #=> false
File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 |
# File 'dir.c', line 2092
static VALUE
file_s_fnmatch(int argc, VALUE *argv, VALUE obj)
{
VALUE pattern, path;
VALUE rflags;
int flags;
if (rb_scan_args(argc, argv, "21", &pattern, &path, &rflags) == 3)
flags = NUM2INT(rflags);
else
flags = 0;
StringValue(pattern);
FilePathStringValue(path);
if (flags & FNM_EXTGLOB) {
struct brace_args args;
args.value = path;
args.flags = flags;
if (ruby_brace_expand(RSTRING_PTR(pattern), flags, fnmatch_brace,
(VALUE)&args, rb_enc_get(pattern)) > 0)
return Qtrue;
}
else {
rb_encoding *enc = rb_enc_compatible(pattern, path);
if (!enc) return Qfalse;
if (fnmatch(RSTRING_PTR(pattern), enc, RSTRING_PTR(path), flags) == 0)
return Qtrue;
}
RB_GC_GUARD(pattern);
return Qfalse;
}
|
.fnmatch(pattern, path, [flags]) ⇒ Boolean .fnmatch?(pattern, path, [flags]) ⇒ Boolean
Returns true if path
matches against pattern
. The pattern is not a regular expression; instead it follows rules similar to shell filename globbing. It may contain the following metacharacters:
*
-
Matches any file. Can be restricted by other values in the glob. Equivalent to
/ .* /x
in regexp.*
-
Matches all files regular files
c*
-
Matches all files beginning with
c
*c
-
Matches all files ending with
c
*c*
-
Matches all files that have
c
in them (including at the beginning or end).
To match hidden files (that start with a
.
set the File::FNM_DOTMATCH flag. **
-
Matches directories recursively or files expansively.
?
-
Matches any one character. Equivalent to
/.{1}/
in regexp. [set]
-
Matches any one character in
set
. Behaves exactly like character sets in Regexp, including set negation ([^a-z]
). \
-
Escapes the next metacharacter.
{a,b}
-
Matches pattern a and pattern b if File::FNM_EXTGLOB flag is enabled. Behaves like a Regexp union (
(?:a|b)
).
flags
is a bitwise OR of the FNM_XXX
constants. The same glob pattern and flags are used by Dir::glob.
Examples:
File.fnmatch('cat', 'cat') #=> true # match entire string
File.fnmatch('cat', 'category') #=> false # only match partial string
File.fnmatch('c{at,ub}s', 'cats') #=> false # { } isn't supported by default
File.fnmatch('c{at,ub}s', 'cats', File::FNM_EXTGLOB) #=> true # { } is supported on FNM_EXTGLOB
File.fnmatch('c?t', 'cat') #=> true # '?' match only 1 character
File.fnmatch('c??t', 'cat') #=> false # ditto
File.fnmatch('c*', 'cats') #=> true # '*' match 0 or more characters
File.fnmatch('c*t', 'c/a/b/t') #=> true # ditto
File.fnmatch('ca[a-z]', 'cat') #=> true # inclusive bracket expression
File.fnmatch('ca[^t]', 'cat') #=> false # exclusive bracket expression ('^' or '!')
File.fnmatch('cat', 'CAT') #=> false # case sensitive
File.fnmatch('cat', 'CAT', File::FNM_CASEFOLD) #=> true # case insensitive
File.fnmatch('?', '/', File::FNM_PATHNAME) #=> false # wildcard doesn't match '/' on FNM_PATHNAME
File.fnmatch('*', '/', File::FNM_PATHNAME) #=> false # ditto
File.fnmatch('[/]', '/', File::FNM_PATHNAME) #=> false # ditto
File.fnmatch('\?', '?') #=> true # escaped wildcard becomes ordinary
File.fnmatch('\a', 'a') #=> true # escaped ordinary remains ordinary
File.fnmatch('\a', '\a', File::FNM_NOESCAPE) #=> true # FNM_NOESCAPE makes '\' ordinary
File.fnmatch('[\?]', '?') #=> true # can escape inside bracket expression
File.fnmatch('*', '.profile') #=> false # wildcard doesn't match leading
File.fnmatch('*', '.profile', File::FNM_DOTMATCH) #=> true # period by default.
File.fnmatch('.*', '.profile') #=> true
rbfiles = '**' '/' '*.rb' # you don't have to do like this. just write in single string.
File.fnmatch(rbfiles, 'main.rb') #=> false
File.fnmatch(rbfiles, './main.rb') #=> false
File.fnmatch(rbfiles, 'lib/song.rb') #=> true
File.fnmatch('**.rb', 'main.rb') #=> true
File.fnmatch('**.rb', './main.rb') #=> false
File.fnmatch('**.rb', 'lib/song.rb') #=> true
File.fnmatch('*', 'dave/.profile') #=> true
pattern = '*' '/' '*'
File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME) #=> false
File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true
pattern = '**' '/' 'foo'
File.fnmatch(pattern, 'a/b/c/foo', File::FNM_PATHNAME) #=> true
File.fnmatch(pattern, '/a/b/c/foo', File::FNM_PATHNAME) #=> true
File.fnmatch(pattern, 'c:/a/b/c/foo', File::FNM_PATHNAME) #=> true
File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME) #=> false
File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 |
# File 'dir.c', line 2092
static VALUE
file_s_fnmatch(int argc, VALUE *argv, VALUE obj)
{
VALUE pattern, path;
VALUE rflags;
int flags;
if (rb_scan_args(argc, argv, "21", &pattern, &path, &rflags) == 3)
flags = NUM2INT(rflags);
else
flags = 0;
StringValue(pattern);
FilePathStringValue(path);
if (flags & FNM_EXTGLOB) {
struct brace_args args;
args.value = path;
args.flags = flags;
if (ruby_brace_expand(RSTRING_PTR(pattern), flags, fnmatch_brace,
(VALUE)&args, rb_enc_get(pattern)) > 0)
return Qtrue;
}
else {
rb_encoding *enc = rb_enc_compatible(pattern, path);
if (!enc) return Qfalse;
if (fnmatch(RSTRING_PTR(pattern), enc, RSTRING_PTR(path), flags) == 0)
return Qtrue;
}
RB_GC_GUARD(pattern);
return Qfalse;
}
|
.ftype(file_name) ⇒ String
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 |
# File 'file.c', line 1921
static VALUE
rb_file_s_ftype(VALUE klass, VALUE fname)
{
struct stat st;
rb_secure(2);
FilePathValue(fname);
fname = rb_str_encode_ospath(fname);
if (lstat(StringValueCStr(fname), &st) == -1) {
rb_sys_fail_path(fname);
}
return rb_file_ftype(&st);
}
|
.grpowned?(file_name) ⇒ Boolean
Returns true
if the named file exists and the effective group id of the calling process is the owner of the file. Returns false
on Windows.
file_name can be an IO object.
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 |
# File 'file.c', line 1699
static VALUE
rb_file_grpowned_p(VALUE obj, VALUE fname)
{
#ifndef _WIN32
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (rb_group_member(st.st_gid)) return Qtrue;
#endif
return Qfalse;
}
|
.identical?(file_1, file_2) ⇒ Boolean
Returns true
if the named files are identical.
file_1 and file_2 can be an IO object.
open("a", "w") {}
p File.identical?("a", "a") #=> true
p File.identical?("a", "./a") #=> true
File.link("a", "b")
p File.identical?("a", "b") #=> true
File.symlink("a", "c")
p File.identical?("a", "c") #=> true
open("d", "w") {}
p File.identical?("a", "d") #=> false
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 |
# File 'file.c', line 1796
static VALUE
rb_file_identical_p(VALUE obj, VALUE fname1, VALUE fname2)
{
#ifndef DOSISH
struct stat st1, st2;
if (rb_stat(fname1, &st1) < 0) return Qfalse;
if (rb_stat(fname2, &st2) < 0) return Qfalse;
if (st1.st_dev != st2.st_dev) return Qfalse;
if (st1.st_ino != st2.st_ino) return Qfalse;
#else
# ifdef _WIN32
BY_HANDLE_FILE_INFORMATION st1, st2;
HANDLE f1 = 0, f2 = 0;
# endif
rb_secure(2);
# ifdef _WIN32
f1 = w32_io_info(&fname1, &st1);
if (f1 == INVALID_HANDLE_VALUE) return Qfalse;
f2 = w32_io_info(&fname2, &st2);
if (f1) CloseHandle(f1);
if (f2 == INVALID_HANDLE_VALUE) return Qfalse;
if (f2) CloseHandle(f2);
if (st1.dwVolumeSerialNumber == st2.dwVolumeSerialNumber &&
st1.nFileIndexHigh == st2.nFileIndexHigh &&
st1.nFileIndexLow == st2.nFileIndexLow)
return Qtrue;
if (!f1 || !f2) return Qfalse;
# else
FilePathValue(fname1);
fname1 = rb_str_new4(fname1);
fname1 = rb_str_encode_ospath(fname1);
FilePathValue(fname2);
fname2 = rb_str_encode_ospath(fname2);
if (access(RSTRING_PTR(fname1), 0)) return Qfalse;
if (access(RSTRING_PTR(fname2), 0)) return Qfalse;
# endif
fname1 = rb_file_expand_path(fname1, Qnil);
fname2 = rb_file_expand_path(fname2, Qnil);
if (RSTRING_LEN(fname1) != RSTRING_LEN(fname2)) return Qfalse;
if (rb_memcicmp(RSTRING_PTR(fname1), RSTRING_PTR(fname2), RSTRING_LEN(fname1)))
return Qfalse;
#endif
return Qtrue;
}
|
.join(string, ...) ⇒ Object
Returns a new string formed by joining the strings using File::SEPARATOR
.
File.join("usr", "mail", "gumby") #=> "usr/mail/gumby"
4181 4182 4183 4184 4185 |
# File 'file.c', line 4181
static VALUE
rb_file_s_join(VALUE klass, VALUE args)
{
return rb_file_join(args, separator);
}
|
.lchmod(mode_int, file_name, ...) ⇒ Integer
Equivalent to File::chmod
, but does not follow symbolic links (so it will change the permissions associated with the link, not the file referenced by the link). Often not available.
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 |
# File 'file.c', line 2204
static VALUE
rb_file_s_lchmod(int argc, VALUE *argv)
{
VALUE vmode;
VALUE rest;
long mode, n;
rb_secure(2);
rb_scan_args(argc, argv, "1*", &vmode, &rest);
mode = NUM2INT(vmode);
n = apply2files(lchmod_internal, rest, (void *)(long)mode);
return LONG2FIX(n);
}
|
.lchown(owner_int, group_int, file_name, ..) ⇒ Integer
Equivalent to File::chown
, but does not follow symbolic links (so it will change the owner associated with the link, not the file referenced by the link). Often not available. Returns number of files in the argument list.
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 |
# File 'file.c', line 2337
static VALUE
rb_file_s_lchown(int argc, VALUE *argv)
{
VALUE o, g, rest;
struct chown_args arg;
long n;
rb_secure(2);
rb_scan_args(argc, argv, "2*", &o, &g, &rest);
if (NIL_P(o)) {
arg.owner = -1;
}
else {
arg.owner = NUM2UIDT(o);
}
if (NIL_P(g)) {
arg.group = -1;
}
else {
arg.group = NUM2GIDT(g);
}
n = apply2files(lchown_internal, rest, &arg);
return LONG2FIX(n);
}
|
.link(old_name, new_name) ⇒ 0
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 |
# File 'file.c', line 2534
static VALUE
rb_file_s_link(VALUE klass, VALUE from, VALUE to)
{
rb_secure(2);
FilePathValue(from);
FilePathValue(to);
from = rb_str_encode_ospath(from);
to = rb_str_encode_ospath(to);
if (link(StringValueCStr(from), StringValueCStr(to)) < 0) {
sys_fail2(from, to);
}
return INT2FIX(0);
}
|
.lstat(file_name) ⇒ Object
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 |
# File 'file.c', line 1036
static VALUE
rb_file_s_lstat(VALUE klass, VALUE fname)
{
#ifdef HAVE_LSTAT
struct stat st;
rb_secure(2);
FilePathValue(fname);
fname = rb_str_encode_ospath(fname);
if (lstat(StringValueCStr(fname), &st) == -1) {
rb_sys_fail_path(fname);
}
return rb_stat_new(&st);
#else
return rb_file_s_stat(klass, fname);
#endif
}
|
.mtime(file_name) ⇒ Time
Returns the modification time for the named file as a Time object.
file_name can be an IO object.
File.mtime("testfile") #=> Tue Apr 08 12:58:04 CDT 2003
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 |
# File 'file.c', line 1996
static VALUE
rb_file_s_mtime(VALUE klass, VALUE fname)
{
struct stat st;
if (rb_stat(fname, &st) < 0) {
FilePathValue(fname);
rb_sys_fail_path(fname);
}
return stat_mtime(&st);
}
|
.open(*args) ⇒ Object
call-seq:
IO.open(fd, mode="r" [, opt]) -> io
IO.open(fd, mode="r" [, opt]) { |io| block } -> obj
With no associated block, IO.open
is a synonym for IO.new. If the optional code block is given, it will be passed io
as an argument, and the IO object will automatically be closed when the block terminates. In this instance, IO.open returns the value of the block.
See IO.new for a description of the fd
, mode
and opt
parameters.
6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 |
# File 'io.c', line 6257
static VALUE
rb_io_s_open(int argc, VALUE *argv, VALUE klass)
{
VALUE io = rb_class_new_instance(argc, argv, klass);
if (rb_block_given_p()) {
return rb_ensure(rb_yield, io, io_close, io);
}
return io;
}
|
.owned?(file_name) ⇒ Boolean
Returns true
if the named file exists and the effective used id of the calling process is the owner of the file.
file_name can be an IO object.
1668 1669 1670 1671 1672 1673 1674 1675 1676 |
# File 'file.c', line 1668
static VALUE
rb_file_owned_p(VALUE obj, VALUE fname)
{
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (st.st_uid == geteuid()) return Qtrue;
return Qfalse;
}
|
.path(path) ⇒ String
4058 4059 4060 4061 4062 |
# File 'file.c', line 4058
static VALUE
rb_file_s_path(VALUE klass, VALUE fname)
{
return rb_get_path(fname);
}
|
.pipe?(file_name) ⇒ Boolean
Returns true
if the named file is a pipe.
file_name can be an IO object.
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 |
# File 'file.c', line 1237
static VALUE
rb_file_pipe_p(VALUE obj, VALUE fname)
{
#ifdef S_IFIFO
# ifndef S_ISFIFO
# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
# endif
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (S_ISFIFO(st.st_mode)) return Qtrue;
#endif
return Qfalse;
}
|
.readable?(file_name) ⇒ Boolean
Returns true
if the named file is readable by the effective user id of this process.
1425 1426 1427 1428 1429 1430 1431 1432 1433 |
# File 'file.c', line 1425
static VALUE
rb_file_readable_p(VALUE obj, VALUE fname)
{
rb_secure(2);
FilePathValue(fname);
fname = rb_str_encode_ospath(fname);
if (eaccess(StringValueCStr(fname), R_OK) < 0) return Qfalse;
return Qtrue;
}
|
.readable_real?(file_name) ⇒ Boolean
Returns true
if the named file is readable by the real user id of this process.
1443 1444 1445 1446 1447 1448 1449 1450 1451 |
# File 'file.c', line 1443
static VALUE
rb_file_readable_real_p(VALUE obj, VALUE fname)
{
rb_secure(2);
FilePathValue(fname);
fname = rb_str_encode_ospath(fname);
if (access(StringValueCStr(fname), R_OK) < 0) return Qfalse;
return Qtrue;
}
|
.readlink(link_name) ⇒ Object
2597 2598 2599 2600 2601 |
# File 'file.c', line 2597
static VALUE
rb_file_s_readlink(VALUE klass, VALUE path)
{
return rb_readlink(path);
}
|
.realdirpath(pathname[, dir_string]) ⇒ Object
Returns the real (absolute) pathname of pathname in the actual filesystem.
The real pathname doesn't contain symlinks or useless dots.
If _dir_string_ is given, it is used as a base directory
for interpreting relative pathname instead of the current directory.
The last component of the real pathname can be nonexistent.
3717 3718 3719 3720 3721 3722 3723 |
# File 'file.c', line 3717
static VALUE
rb_file_s_realdirpath(int argc, VALUE *argv, VALUE klass)
{
VALUE path, basedir;
rb_scan_args(argc, argv, "11", &path, &basedir);
return rb_realpath_internal(basedir, path, 0);
}
|
.realpath(pathname[, dir_string]) ⇒ Object
Returns the real (absolute) pathname of pathname in the actual
filesystem not containing symlinks or useless dots.
If _dir_string_ is given, it is used as a base directory
for interpreting relative pathname instead of the current directory.
All components of the pathname must exist when this method is
called.
3697 3698 3699 3700 3701 3702 3703 |
# File 'file.c', line 3697
static VALUE
rb_file_s_realpath(int argc, VALUE *argv, VALUE klass)
{
VALUE path, basedir;
rb_scan_args(argc, argv, "11", &path, &basedir);
return rb_realpath_internal(basedir, path, 1);
}
|
.rename(old_name, new_name) ⇒ 0
Renames the given file to the new name. Raises a SystemCallError
if the file cannot be renamed.
File.rename("afile", "afile.bak") #=> 0
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 |
# File 'file.c', line 2672
static VALUE
rb_file_s_rename(VALUE klass, VALUE from, VALUE to)
{
const char *src, *dst;
VALUE f, t;
rb_secure(2);
FilePathValue(from);
FilePathValue(to);
f = rb_str_encode_ospath(from);
t = rb_str_encode_ospath(to);
src = StringValueCStr(f);
dst = StringValueCStr(t);
#if defined __CYGWIN__
errno = 0;
#endif
if (rename(src, dst) < 0) {
#if defined DOSISH
switch (errno) {
case EEXIST:
#if defined (__EMX__)
case EACCES:
#endif
if (chmod(dst, 0666) == 0 &&
unlink(dst) == 0 &&
rename(src, dst) == 0)
return INT2FIX(0);
}
#endif
sys_fail2(from, to);
}
return INT2FIX(0);
}
|
.setgid?(file_name) ⇒ Boolean
Returns true
if the named file has the setgid bit set.
1750 1751 1752 1753 1754 1755 1756 1757 1758 |
# File 'file.c', line 1750
static VALUE
rb_file_sgid_p(VALUE obj, VALUE fname)
{
#ifdef S_ISGID
return check3rdbyte(fname, S_ISGID);
#else
return Qfalse;
#endif
}
|
.setuid?(file_name) ⇒ Boolean
Returns true
if the named file has the setuid bit set.
1733 1734 1735 1736 1737 1738 1739 1740 1741 |
# File 'file.c', line 1733
static VALUE
rb_file_suid_p(VALUE obj, VALUE fname)
{
#ifdef S_ISUID
return check3rdbyte(fname, S_ISUID);
#else
return Qfalse;
#endif
}
|
.size(file_name) ⇒ Integer
Returns the size of file_name
.
file_name can be an IO object.
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 |
# File 'file.c', line 1853
static VALUE
rb_file_s_size(VALUE klass, VALUE fname)
{
struct stat st;
if (rb_stat(fname, &st) < 0) {
FilePathValue(fname);
rb_sys_fail_path(fname);
}
return OFFT2NUM(st.st_size);
}
|
.size?(file_name) ⇒ Integer?
Returns nil
if file_name
doesn’t exist or has zero size, the size of the file otherwise.
file_name can be an IO object.
1647 1648 1649 1650 1651 1652 1653 1654 1655 |
# File 'file.c', line 1647
static VALUE
rb_file_size_p(VALUE obj, VALUE fname)
{
struct stat st;
if (rb_stat(fname, &st) < 0) return Qnil;
if (st.st_size == 0) return Qnil;
return OFFT2NUM(st.st_size);
}
|
.socket?(file_name) ⇒ Boolean
Returns true
if the named file is a socket.
file_name can be an IO object.
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 |
# File 'file.c', line 1300
static VALUE
rb_file_socket_p(VALUE obj, VALUE fname)
{
#ifndef S_ISSOCK
# ifdef _S_ISSOCK
# define S_ISSOCK(m) _S_ISSOCK(m)
# else
# ifdef _S_IFSOCK
# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
# else
# ifdef S_IFSOCK
# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
# endif
# endif
# endif
#endif
#ifdef S_ISSOCK
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (S_ISSOCK(st.st_mode)) return Qtrue;
#endif
return Qfalse;
}
|
.split(file_name) ⇒ Array
Splits the given string into a directory and a file component and returns them in a two-element array. See also File::dirname
and File::basename
.
File.split("/home/gumby/.profile") #=> ["/home/gumby", ".profile"]
4075 4076 4077 4078 4079 4080 |
# File 'file.c', line 4075
static VALUE
rb_file_s_split(VALUE klass, VALUE path)
{
FilePathStringValue(path); /* get rid of converting twice */
return rb_assoc_new(rb_file_s_dirname(Qnil, path), rb_file_s_basename(1,&path));
}
|
.stat(file_name) ⇒ Object
Returns a File::Stat
object for the named file (see File::Stat
).
File.stat("testfile").mtime #=> Tue Apr 08 12:58:04 CDT 2003
982 983 984 985 986 987 988 989 990 991 992 |
# File 'file.c', line 982
static VALUE
rb_file_s_stat(VALUE klass, VALUE fname)
{
struct stat st;
FilePathValue(fname);
if (rb_stat(fname, &st) < 0) {
rb_sys_fail_path(fname);
}
return rb_stat_new(&st);
}
|
.sticky?(file_name) ⇒ Boolean
Returns true
if the named file has the sticky bit set.
1767 1768 1769 1770 1771 1772 1773 1774 1775 |
# File 'file.c', line 1767
static VALUE
rb_file_sticky_p(VALUE obj, VALUE fname)
{
#ifdef S_ISVTX
return check3rdbyte(fname, S_ISVTX);
#else
return Qnil;
#endif
}
|
.symlink(old_name, new_name) ⇒ 0
Creates a symbolic link called new_name for the existing file old_name. Raises a NotImplemented
exception on platforms that do not support symbolic links.
File.symlink("testfile", "link2test") #=> 0
2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 |
# File 'file.c', line 2565
static VALUE
rb_file_s_symlink(VALUE klass, VALUE from, VALUE to)
{
rb_secure(2);
FilePathValue(from);
FilePathValue(to);
from = rb_str_encode_ospath(from);
to = rb_str_encode_ospath(to);
if (symlink(StringValueCStr(from), StringValueCStr(to)) < 0) {
sys_fail2(from, to);
}
return INT2FIX(0);
}
|
.symlink?(file_name) ⇒ Boolean
Returns true
if the named file is a symbolic link.
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 |
# File 'file.c', line 1261
static VALUE
rb_file_symlink_p(VALUE obj, VALUE fname)
{
#ifndef S_ISLNK
# ifdef _S_ISLNK
# define S_ISLNK(m) _S_ISLNK(m)
# else
# ifdef _S_IFLNK
# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
# else
# ifdef S_IFLNK
# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
# endif
# endif
# endif
#endif
#ifdef S_ISLNK
struct stat st;
rb_secure(2);
FilePathValue(fname);
fname = rb_str_encode_ospath(fname);
if (lstat(StringValueCStr(fname), &st) < 0) return Qfalse;
if (S_ISLNK(st.st_mode)) return Qtrue;
#endif
return Qfalse;
}
|
.truncate(file_name, integer) ⇒ 0
4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 |
# File 'file.c', line 4203
static VALUE
rb_file_s_truncate(VALUE klass, VALUE path, VALUE len)
{
#ifdef HAVE_TRUNCATE
#define NUM2POS(n) NUM2OFFT(n)
off_t pos;
#else
#define NUM2POS(n) NUM2LONG(n)
long pos;
#endif
rb_secure(2);
pos = NUM2POS(len);
FilePathValue(path);
path = rb_str_encode_ospath(path);
#ifdef HAVE_TRUNCATE
if (truncate(StringValueCStr(path), pos) < 0)
rb_sys_fail_path(path);
#else /* defined(HAVE_CHSIZE) */
{
int tmpfd;
if ((tmpfd = rb_cloexec_open(StringValueCStr(path), 0, 0)) < 0) {
rb_sys_fail_path(path);
}
rb_update_max_fd(tmpfd);
if (chsize(tmpfd, pos) < 0) {
close(tmpfd);
rb_sys_fail_path(path);
}
close(tmpfd);
}
#endif
return INT2FIX(0);
#undef NUM2POS
}
|
.umask ⇒ Integer .umask(integer) ⇒ Integer
Returns the current umask value for this process. If the optional argument is given, set the umask to that value and return the previous value. Umask values are subtracted from the default permissions, so a umask of 0222
would make a file read-only for everyone.
File.umask(0006) #=> 18
File.umask #=> 6
2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 |
# File 'file.c', line 2722
static VALUE
rb_file_s_umask(int argc, VALUE *argv)
{
int omask = 0;
rb_secure(2);
if (argc == 0) {
omask = umask(0);
umask(omask);
}
else if (argc == 1) {
omask = umask(NUM2INT(argv[0]));
}
else {
rb_check_arity(argc, 0, 1);
}
return INT2FIX(omask);
}
|
.delete(file_name, ...) ⇒ Integer .unlink(file_name, ...) ⇒ Integer
Deletes the named files, returning the number of names passed as arguments. Raises an exception on any error. See also Dir::rmdir
.
2652 2653 2654 2655 2656 2657 2658 2659 2660 |
# File 'file.c', line 2652
static VALUE
rb_file_s_unlink(VALUE klass, VALUE args)
{
long n;
rb_secure(2);
n = apply2files(unlink_internal, args, 0);
return LONG2FIX(n);
}
|
.utime(atime, mtime, file_name, ...) ⇒ Integer
Sets the access and modification times of each named file to the first two arguments. Returns the number of file names in the argument list.
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 |
# File 'file.c', line 2480
static VALUE
rb_file_s_utime(int argc, VALUE *argv)
{
VALUE rest;
struct utime_args args;
struct timespec tss[2], *tsp = NULL;
long n;
rb_secure(2);
rb_scan_args(argc, argv, "2*", &args.atime, &args.mtime, &rest);
if (!NIL_P(args.atime) || !NIL_P(args.mtime)) {
tsp = tss;
tsp[0] = rb_time_timespec(args.atime);
tsp[1] = rb_time_timespec(args.mtime);
}
args.tsp = tsp;
n = apply2files(utime_internal, rest, &args);
return LONG2FIX(n);
}
|
.world_readable?(file_name) ⇒ Fixnum?
If file_name is readable by others, returns an integer representing the file permission bits of file_name. Returns nil
otherwise. The meaning of the bits is platform dependent; on Unix systems, see stat(2)
.
file_name can be an IO object.
File.world_readable?("/etc/passwd") #=> 420
m = File.world_readable?("/etc/passwd")
sprintf("%o", m) #=> "644"
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 |
# File 'file.c', line 1477
static VALUE
rb_file_world_readable_p(VALUE obj, VALUE fname)
{
#ifdef S_IROTH
struct stat st;
if (rb_stat(fname, &st) < 0) return Qnil;
if ((st.st_mode & (S_IROTH)) == S_IROTH) {
return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
}
#endif
return Qnil;
}
|
.world_writable?(file_name) ⇒ Fixnum?
If file_name is writable by others, returns an integer representing the file permission bits of file_name. Returns nil
otherwise. The meaning of the bits is platform dependent; on Unix systems, see stat(2)
.
file_name can be an IO object.
File.world_writable?("/tmp") #=> 511
m = File.world_writable?("/tmp")
sprintf("%o", m) #=> "777"
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 |
# File 'file.c', line 1543
static VALUE
rb_file_world_writable_p(VALUE obj, VALUE fname)
{
#ifdef S_IWOTH
struct stat st;
if (rb_stat(fname, &st) < 0) return Qnil;
if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
}
#endif
return Qnil;
}
|
.writable?(file_name) ⇒ Boolean
Returns true
if the named file is writable by the effective user id of this process.
1499 1500 1501 1502 1503 1504 1505 1506 1507 |
# File 'file.c', line 1499
static VALUE
rb_file_writable_p(VALUE obj, VALUE fname)
{
rb_secure(2);
FilePathValue(fname);
fname = rb_str_encode_ospath(fname);
if (eaccess(StringValueCStr(fname), W_OK) < 0) return Qfalse;
return Qtrue;
}
|
.writable_real?(file_name) ⇒ Boolean
Returns true
if the named file is writable by the real user id of this process.
1517 1518 1519 1520 1521 1522 1523 1524 1525 |
# File 'file.c', line 1517
static VALUE
rb_file_writable_real_p(VALUE obj, VALUE fname)
{
rb_secure(2);
FilePathValue(fname);
fname = rb_str_encode_ospath(fname);
if (access(StringValueCStr(fname), W_OK) < 0) return Qfalse;
return Qtrue;
}
|
.zero?(file_name) ⇒ Boolean
Returns true
if the named file exists and has a zero size.
file_name can be an IO object.
1627 1628 1629 1630 1631 1632 1633 1634 1635 |
# File 'file.c', line 1627
static VALUE
rb_file_zero_p(VALUE obj, VALUE fname)
{
struct stat st;
if (rb_stat(fname, &st) < 0) return Qfalse;
if (st.st_size == 0) return Qtrue;
return Qfalse;
}
|
Instance Method Details
#atime ⇒ Time
Returns the last access time (a Time
object)
for <i>file</i>, or epoch if <i>file</i> has not been accessed.
File.new("testfile").atime #=> Wed Dec 31 18:00:00 CST 1969
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 |
# File 'file.c', line 1971
static VALUE
rb_file_atime(VALUE obj)
{
rb_io_t *fptr;
struct stat st;
GetOpenFile(obj, fptr);
if (fstat(fptr->fd, &st) == -1) {
rb_sys_fail_path(fptr->pathv);
}
return stat_atime(&st);
}
|
#chmod(mode_int) ⇒ 0
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 |
# File 'file.c', line 2160
static VALUE
rb_file_chmod(VALUE obj, VALUE vmode)
{
rb_io_t *fptr;
int mode;
#ifndef HAVE_FCHMOD
VALUE path;
#endif
rb_secure(2);
mode = NUM2INT(vmode);
GetOpenFile(obj, fptr);
#ifdef HAVE_FCHMOD
if (fchmod(fptr->fd, mode) == -1)
rb_sys_fail_path(fptr->pathv);
#else
if (NIL_P(fptr->pathv)) return Qnil;
path = rb_str_encode_ospath(fptr->pathv);
if (chmod(RSTRING_PTR(path), mode) == -1)
rb_sys_fail_path(fptr->pathv);
#endif
return INT2FIX(0);
}
|
#chown(owner_int, group_int) ⇒ 0
Changes the owner and group of file to the given numeric owner and group id’s. Only a process with superuser privileges may change the owner of a file. The current owner of a file may change the file’s group to any group to which the owner belongs. A nil
or -1 owner or group id is ignored. Follows symbolic links. See also File#lchown
.
File.new("testfile").chown(502, 1000)
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 |
# File 'file.c', line 2291
static VALUE
rb_file_chown(VALUE obj, VALUE owner, VALUE group)
{
rb_io_t *fptr;
int o, g;
#ifndef HAVE_FCHOWN
VALUE path;
#endif
rb_secure(2);
o = NIL_P(owner) ? -1 : NUM2INT(owner);
g = NIL_P(group) ? -1 : NUM2INT(group);
GetOpenFile(obj, fptr);
#ifndef HAVE_FCHOWN
if (NIL_P(fptr->pathv)) return Qnil;
path = rb_str_encode_ospath(fptr->pathv);
if (chown(RSTRING_PTR(path), o, g) == -1)
rb_sys_fail_path(fptr->pathv);
#else
if (fchown(fptr->fd, o, g) == -1)
rb_sys_fail_path(fptr->pathv);
#endif
return INT2FIX(0);
}
|
#ctime ⇒ Time
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 |
# File 'file.c', line 2072
static VALUE
rb_file_ctime(VALUE obj)
{
rb_io_t *fptr;
struct stat st;
GetOpenFile(obj, fptr);
if (fstat(fptr->fd, &st) == -1) {
rb_sys_fail_path(fptr->pathv);
}
return stat_ctime(&st);
}
|
#flock(locking_constant) ⇒ 0, false
Locks or unlocks a file according to locking_constant (a logical or of the values in the table below). Returns false
if File::LOCK_NB
is specified and the operation would otherwise have blocked. Not available on all platforms.
Locking constants (in class File):
LOCK_EX | Exclusive lock. Only one process may hold an
| exclusive lock for a given file at a time.
----------+------------------------------------------------
LOCK_NB | Don't block when locking. May be combined
| with other lock options using logical or.
----------+------------------------------------------------
LOCK_SH | Shared lock. Multiple processes may each hold a
| shared lock for a given file at the same time.
----------+------------------------------------------------
LOCK_UN | Unlock.
Example:
# update a counter using write lock
# don't use "w" because it truncates the file before lock.
File.open("counter", File::RDWR|File::CREAT, 0644) {|f|
f.flock(File::LOCK_EX)
value = f.read.to_i + 1
f.rewind
f.write("#{value}\n")
f.flush
f.truncate(f.pos)
}
# read the counter using read lock
File.open("counter", "r") {|f|
f.flock(File::LOCK_SH)
p f.read
}
4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 |
# File 'file.c', line 4369
static VALUE
rb_file_flock(VALUE obj, VALUE operation)
{
rb_io_t *fptr;
int op[2], op1;
struct timeval time;
rb_secure(2);
op[1] = op1 = NUM2INT(operation);
GetOpenFile(obj, fptr);
op[0] = fptr->fd;
if (fptr->mode & FMODE_WRITABLE) {
rb_io_flush_raw(obj, 0);
}
while ((int)rb_thread_io_blocking_region(rb_thread_flock, op, fptr->fd) < 0) {
switch (errno) {
case EAGAIN:
case EACCES:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
if (op1 & LOCK_NB) return Qfalse;
time.tv_sec = 0;
time.tv_usec = 100 * 1000; /* 0.1 sec */
rb_thread_wait_for(time);
rb_io_check_closed(fptr);
continue;
case EINTR:
#if defined(ERESTART)
case ERESTART:
#endif
break;
default:
rb_sys_fail_path(fptr->pathv);
}
}
return INT2FIX(0);
}
|
#lstat ⇒ Object
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 |
# File 'file.c', line 1068
static VALUE
rb_file_lstat(VALUE obj)
{
#ifdef HAVE_LSTAT
rb_io_t *fptr;
struct stat st;
VALUE path;
rb_secure(2);
GetOpenFile(obj, fptr);
if (NIL_P(fptr->pathv)) return Qnil;
path = rb_str_encode_ospath(fptr->pathv);
if (lstat(RSTRING_PTR(path), &st) == -1) {
rb_sys_fail_path(fptr->pathv);
}
return rb_stat_new(&st);
#else
return rb_io_stat(obj);
#endif
}
|
#mtime ⇒ Time
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 |
# File 'file.c', line 2018
static VALUE
rb_file_mtime(VALUE obj)
{
rb_io_t *fptr;
struct stat st;
GetOpenFile(obj, fptr);
if (fstat(fptr->fd, &st) == -1) {
rb_sys_fail_path(fptr->pathv);
}
return stat_mtime(&st);
}
|
#path ⇒ Object #to_path ⇒ Object
345 346 347 348 349 350 351 352 353 354 |
# File 'file.c', line 345
static VALUE
rb_file_path(VALUE obj)
{
rb_io_t *fptr;
fptr = RFILE(rb_io_taint_check(obj))->fptr;
rb_io_check_initialized(fptr);
if (NIL_P(fptr->pathv)) return Qnil;
return rb_obj_taint(rb_str_dup(fptr->pathv));
}
|
#size ⇒ Integer
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 |
# File 'file.c', line 2095
static VALUE
rb_file_size(VALUE obj)
{
rb_io_t *fptr;
struct stat st;
GetOpenFile(obj, fptr);
if (fptr->mode & FMODE_WRITABLE) {
rb_io_flush_raw(obj, 0);
}
if (fstat(fptr->fd, &st) == -1) {
rb_sys_fail_path(fptr->pathv);
}
return OFFT2NUM(st.st_size);
}
|
#path ⇒ Object #to_path ⇒ Object
345 346 347 348 349 350 351 352 353 354 |
# File 'file.c', line 345
static VALUE
rb_file_path(VALUE obj)
{
rb_io_t *fptr;
fptr = RFILE(rb_io_taint_check(obj))->fptr;
rb_io_check_initialized(fptr);
if (NIL_P(fptr->pathv)) return Qnil;
return rb_obj_taint(rb_str_dup(fptr->pathv));
}
|
#truncate(integer) ⇒ 0
4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 |
# File 'file.c', line 4258
static VALUE
rb_file_truncate(VALUE obj, VALUE len)
{
rb_io_t *fptr;
#if defined(HAVE_FTRUNCATE)
#define NUM2POS(n) NUM2OFFT(n)
off_t pos;
#else
#define NUM2POS(n) NUM2LONG(n)
long pos;
#endif
rb_secure(2);
pos = NUM2POS(len);
GetOpenFile(obj, fptr);
if (!(fptr->mode & FMODE_WRITABLE)) {
rb_raise(rb_eIOError, "not opened for writing");
}
rb_io_flush_raw(obj, 0);
#ifdef HAVE_FTRUNCATE
if (ftruncate(fptr->fd, pos) < 0)
rb_sys_fail_path(fptr->pathv);
#else /* defined(HAVE_CHSIZE) */
if (chsize(fptr->fd, pos) < 0)
rb_sys_fail_path(fptr->pathv);
#endif
return INT2FIX(0);
#undef NUM2POS
}
|