Class: File
- Inherits:
-
IO
show all
- Includes:
- Constants
- Defined in:
- mrbgems/mruby-io/src/file.c,
mrbgems/mruby-io/mrblib/file.rb,
mrbgems/mruby-io/mrblib/file_constants.rb,
mrbgems/mruby-io/mrblib/file_constants.rb
Defined Under Namespace
Modules: Constants
Constant Summary
Constants included
from Constants
Constants::APPEND, Constants::BINARY, Constants::CREAT, Constants::DSYNC, Constants::EXCL, Constants::FNM_CASEFOLD, Constants::FNM_DOTMATCH, Constants::FNM_NOESCAPE, Constants::FNM_PATHNAME, Constants::FNM_SYSCASE, Constants::NOCTTY, Constants::NOFOLLOW, Constants::NONBLOCK, Constants::RDONLY, Constants::RDWR, Constants::SYNC, Constants::TRUNC, Constants::WRONLY
Constants inherited
from IO
IO::BUF_SIZE, IO::SEEK_CUR, IO::SEEK_END, IO::SEEK_SET
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from IO
#<<, #_check_readable, #_read_buf, #close, #close_on_exec=, #close_on_exec?, #close_write, #closed?, #each, #each_byte, #eof?, #fileno, #flush, #getc, #gets, #hash, #initialize_copy, #isatty, open, #pid, pipe, popen, #pos, #pos=, #print, #printf, #puts, read, #read, #readchar, #readline, #readlines, #rewind, #seek, #sync, #sync=, #sysread, #sysseek, #syswrite, #ungetc, #write
Constructor Details
#initialize(fd_or_path, mode = "r", perm = 0666) ⇒ File
Returns a new instance of File
4
5
6
7
8
9
10
11
12
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 4
def initialize(fd_or_path, mode = "r", perm = 0666)
if fd_or_path.kind_of? Fixnum
super(fd_or_path, mode)
else
@path = fd_or_path
fd = IO.sysopen(@path, mode, perm)
super(fd, mode)
end
end
|
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path
2
3
4
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 2
def path
@path
end
|
Class Method Details
.directory?(file) ⇒ Boolean
147
148
149
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 147
def self.directory?(file)
FileTest.directory?(file)
end
|
.exist?(file) ⇒ Boolean
151
152
153
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 151
def self.exist?(file)
FileTest.exist?(file)
end
|
.exists?(file) ⇒ Boolean
155
156
157
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 155
def self.exists?(file)
FileTest.exists?(file)
end
|
.expand_path(path, default_dir = '.') ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 58
def self.expand_path(path, default_dir = '.')
def concat_path(path, base_path)
if path[0] == "/" || path[1] == ':' # Windows root!
expanded_path = path
elsif path[0] == "~"
if (path[1] == "/" || path[1] == nil)
dir = path[1, path.size]
home_dir = _gethome
unless home_dir
raise ArgumentError, "couldn't find HOME environment -- expanding '~'"
end
expanded_path = home_dir
expanded_path += dir if dir
expanded_path += "/"
else
splitted_path = path.split("/")
user = splitted_path[0][1, splitted_path[0].size]
dir = "/" + splitted_path[1, splitted_path.size].join("/")
home_dir = _gethome(user)
unless home_dir
raise ArgumentError, "user #{user} doesn't exist"
end
expanded_path = home_dir
expanded_path += dir if dir
expanded_path += "/"
end
else
expanded_path = concat_path(base_path, _getwd)
expanded_path += "/" + path
end
expanded_path
end
expanded_path = concat_path(path, default_dir)
drive_prefix = ""
if File::ALT_SEPARATOR && expanded_path.size > 2 &&
("A".."Z").include?(expanded_path[0].upcase) && expanded_path[1] == ":"
drive_prefix = expanded_path[0, 2]
expanded_path = expanded_path[2, expanded_path.size]
end
expand_path_array = []
if File::ALT_SEPARATOR && expanded_path.include?(File::ALT_SEPARATOR)
expanded_path.gsub!(File::ALT_SEPARATOR, '/')
end
while expanded_path.include?('//')
expanded_path = expanded_path.gsub('//', '/')
end
if expanded_path != "/"
expanded_path.split('/').each do |path_token|
if path_token == '..'
if expand_path_array.size > 1
expand_path_array.pop
end
elsif path_token == '.'
# nothing to do.
else
expand_path_array << path_token
end
end
expanded_path = expand_path_array.join("/")
if expanded_path.empty?
expanded_path = '/'
end
end
if drive_prefix.empty?
expanded_path
else
drive_prefix + expanded_path.gsub("/", File::ALT_SEPARATOR)
end
end
|
.extname(filename) ⇒ Object
187
188
189
190
191
192
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 187
def self.extname(filename)
fname = self.basename(filename)
return '' if fname[0] == '.' || fname.index('.').nil?
ext = fname.split('.').last
ext.empty? ? '' : ".#{ext}"
end
|
.file?(file) ⇒ Boolean
159
160
161
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 159
def self.file?(file)
FileTest.file?(file)
end
|
.foreach(file) ⇒ Object
137
138
139
140
141
142
143
144
145
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 137
def self.foreach(file)
if block_given?
self.open(file) do |f|
f.each {|l| yield l}
end
else
return self.new(file)
end
end
|
.join(*names) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 14
def self.join(*names)
return "" if names.empty?
names.map! do |name|
case name
when String
name
when Array
if names == name
raise ArgumentError, "recursive array"
end
join(*name)
else
raise TypeError, "no implicit conversion of #{name.class} into String"
end
end
return names[0] if names.size == 1
if names[0][-1] == File::SEPARATOR
s = names[0][0..-2]
else
s = names[0].dup
end
(1..names.size-2).each { |i|
t = names[i]
if t[0] == File::SEPARATOR and t[-1] == File::SEPARATOR
t = t[1..-2]
elsif t[0] == File::SEPARATOR
t = t[1..-1]
elsif t[-1] == File::SEPARATOR
t = t[0..-2]
end
s += File::SEPARATOR + t if t != ""
}
if names[-1][0] == File::SEPARATOR
s += File::SEPARATOR + names[-1][1..-1]
else
s += File::SEPARATOR + names[-1]
end
s
end
|
.path(filename) ⇒ Object
194
195
196
197
198
199
200
201
202
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 194
def self.path(filename)
if filename.kind_of?(String)
filename
elsif filename.respond_to?(:to_path)
filename.to_path
else
raise TypeError, "no implicit conversion of #{filename.class} into String"
end
end
|
.pipe?(file) ⇒ Boolean
163
164
165
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 163
def self.pipe?(file)
FileTest.pipe?(file)
end
|
.size(file) ⇒ Object
167
168
169
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 167
def self.size(file)
FileTest.size(file)
end
|
.size?(file) ⇒ Boolean
171
172
173
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 171
def self.size?(file)
FileTest.size?(file)
end
|
.socket?(file) ⇒ Boolean
175
176
177
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 175
def self.socket?(file)
FileTest.socket?(file)
end
|
.symlink?(file) ⇒ Boolean
179
180
181
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 179
def self.symlink?(file)
FileTest.symlink?(file)
end
|
.zero?(file) ⇒ Boolean
183
184
185
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 183
def self.zero?(file)
FileTest.zero?(file)
end
|
Instance Method Details
#concat_path(path, base_path) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'mrbgems/mruby-io/mrblib/file.rb', line 59
def concat_path(path, base_path)
if path[0] == "/" || path[1] == ':' # Windows root!
expanded_path = path
elsif path[0] == "~"
if (path[1] == "/" || path[1] == nil)
dir = path[1, path.size]
home_dir = _gethome
unless home_dir
raise ArgumentError, "couldn't find HOME environment -- expanding '~'"
end
expanded_path = home_dir
expanded_path += dir if dir
expanded_path += "/"
else
splitted_path = path.split("/")
user = splitted_path[0][1, splitted_path[0].size]
dir = "/" + splitted_path[1, splitted_path.size].join("/")
home_dir = _gethome(user)
unless home_dir
raise ArgumentError, "user #{user} doesn't exist"
end
expanded_path = home_dir
expanded_path += dir if dir
expanded_path += "/"
end
else
expanded_path = concat_path(base_path, _getwd)
expanded_path += "/" + path
end
expanded_path
end
|
#flock ⇒ Object
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
# File 'mrbgems/mruby-io/src/file.c', line 353
mrb_value
mrb_file_flock(mrb_state *mrb, mrb_value self)
{
#if defined(sun)
mrb_raise(mrb, E_NOTIMP_ERROR, "flock is not supported on Illumos/Solaris/Windows");
#else
mrb_int operation;
int fd;
mrb_get_args(mrb, "i", &operation);
fd = (int)mrb_fixnum(mrb_io_fileno(mrb, self));
while (flock(fd, (int)operation) == -1) {
switch (errno) {
case EINTR:
/* retry */
break;
case EAGAIN: /* NetBSD */
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK: /* FreeBSD OpenBSD Linux */
#endif
if (operation & LOCK_NB) {
return mrb_false_value();
}
/* FALLTHRU - should not happen */
default:
mrb_sys_fail(mrb, "flock failed");
break;
}
}
#endif
return mrb_fixnum_value(0);
}
|
#mtime ⇒ Object
339
340
341
342
343
344
345
346
347
348
349
350
351
|
# File 'mrbgems/mruby-io/src/file.c', line 339
static mrb_value
mrb_file_mtime(mrb_state *mrb, mrb_value self)
{
mrb_value obj;
struct stat st;
int fd;
obj = mrb_obj_value(mrb_class_get(mrb, "Time"));
fd = (int)mrb_fixnum(mrb_io_fileno(mrb, self));
if (fstat(fd, &st) == -1)
return mrb_false_value();
return mrb_funcall(mrb, obj, "at", 1, mrb_fixnum_value(st.st_mtime));
}
|