| 1 |
class File < IO |
| 2 |
attr_accessor :path |
| 3 |
|
| 4 |
def initialize(fd_or_path, mode = "r", perm = 0666) |
| 5 |
if fd_or_path.kind_of? Integer |
| 6 |
super(fd_or_path, mode) |
| 7 |
else |
| 8 |
@path = fd_or_path |
| 9 |
fd = IO.sysopen(@path, mode, perm) |
| 10 |
super(fd, mode) |
| 11 |
end |
| 12 |
end |
| 13 |
|
| 14 |
def atime |
| 15 |
t = self._atime |
| 16 |
t && Time.at(t) |
| 17 |
end |
| 18 |
|
| 19 |
def ctime |
| 20 |
t = self._ctime |
| 21 |
t && Time.at(t) |
| 22 |
end |
| 23 |
|
| 24 |
def mtime |
| 25 |
t = self._mtime |
| 26 |
t && Time.at(t) |
| 27 |
end |
| 28 |
|
| 29 |
def inspect |
| 30 |
"<#{self.class}:#{@path}>" |
| 31 |
end |
| 32 |
|
| 33 |
def self.join(*names) |
| 34 |
return "" if names.empty? |
| 35 |
|
| 36 |
names.map! do |name| |
| 37 |
case name |
| 38 |
when String |
| 39 |
name |
| 40 |
when Array |
| 41 |
if names == name |
| 42 |
raise ArgumentError, "recursive array" |
| 43 |
end |
| 44 |
join(*name) |
| 45 |
else |
| 46 |
raise TypeError, "no implicit conversion of #{name.class} into String" |
| 47 |
end |
| 48 |
end |
| 49 |
|
| 50 |
return names[0] if names.size == 1 |
| 51 |
|
| 52 |
if names[0][-1] == File::SEPARATOR |
| 53 |
s = names[0][0..-2] |
| 54 |
else |
| 55 |
s = names[0].dup |
| 56 |
end |
| 57 |
|
| 58 |
(1..names.size-2).each { |i| |
| 59 |
t = names[i] |
| 60 |
if t[0] == File::SEPARATOR and t[-1] == File::SEPARATOR |
| 61 |
t = t[1..-2] |
| 62 |
elsif t[0] == File::SEPARATOR |
| 63 |
t = t[1..-1] |
| 64 |
elsif t[-1] == File::SEPARATOR |
| 65 |
t = t[0..-2] |
| 66 |
end |
| 67 |
s += File::SEPARATOR + t if t != "" |
| 68 |
} |
| 69 |
if names[-1][0] == File::SEPARATOR |
| 70 |
s += File::SEPARATOR + names[-1][1..-1] |
| 71 |
else |
| 72 |
s += File::SEPARATOR + names[-1] |
| 73 |
end |
| 74 |
s |
| 75 |
end |
| 76 |
|
| 77 |
def self._concat_path(path, base_path) |
| 78 |
if path[0] == "/" || path[1] == ':' # Windows root! |
| 79 |
expanded_path = path |
| 80 |
elsif path[0] == "~" |
| 81 |
if (path[1] == "/" || path[1] == nil) |
| 82 |
dir = path[1, path.size] |
| 83 |
home_dir = _gethome |
| 84 |
|
| 85 |
unless home_dir |
| 86 |
raise ArgumentError, "couldn't find HOME environment -- expanding '~'" |
| 87 |
end |
| 88 |
|
| 89 |
expanded_path = home_dir |
| 90 |
expanded_path += dir if dir |
| 91 |
expanded_path += "/" |
| 92 |
else |
| 93 |
splitted_path = path.split("/") |
| 94 |
user = splitted_path[0][1, splitted_path[0].size] |
| 95 |
dir = "/" + splitted_path[1, splitted_path.size].join("/") |
| 96 |
|
| 97 |
home_dir = _gethome(user) |
| 98 |
|
| 99 |
unless home_dir |
| 100 |
raise ArgumentError, "user #{user} doesn't exist" |
| 101 |
end |
| 102 |
|
| 103 |
expanded_path = home_dir |
| 104 |
expanded_path += dir if dir |
| 105 |
expanded_path += "/" |
| 106 |
end |
| 107 |
else |
| 108 |
expanded_path = _concat_path(base_path, _getwd) |
| 109 |
expanded_path += "/" + path |
| 110 |
end |
| 111 |
|
| 112 |
expanded_path |
| 113 |
end |
| 114 |
|
| 115 |
def self.expand_path(path, default_dir = '.') |
| 116 |
expanded_path = _concat_path(path, default_dir) |
| 117 |
drive_prefix = "" |
| 118 |
if File::ALT_SEPARATOR && expanded_path.size > 2 && |
| 119 |
("A".."Z").include?(expanded_path[0].upcase) && expanded_path[1] == ":" |
| 120 |
drive_prefix = expanded_path[0, 2] |
| 121 |
expanded_path = expanded_path[2, expanded_path.size] |
| 122 |
end |
| 123 |
expand_path_array = [] |
| 124 |
if File::ALT_SEPARATOR && expanded_path.include?(File::ALT_SEPARATOR) |
| 125 |
expanded_path.gsub!(File::ALT_SEPARATOR, '/') |
| 126 |
end |
| 127 |
while expanded_path.include?('//') |
| 128 |
expanded_path = expanded_path.gsub('//', '/') |
| 129 |
end |
| 130 |
|
| 131 |
if expanded_path != "/" |
| 132 |
expanded_path.split('/').each do |path_token| |
| 133 |
if path_token == '..' |
| 134 |
if expand_path_array.size > 1 |
| 135 |
expand_path_array.pop |
| 136 |
end |
| 137 |
elsif path_token == '.' |
| 138 |
# nothing to do. |
| 139 |
else |
| 140 |
expand_path_array << path_token |
| 141 |
end |
| 142 |
end |
| 143 |
|
| 144 |
expanded_path = expand_path_array.join("/") |
| 145 |
if expanded_path.empty? |
| 146 |
expanded_path = '/' |
| 147 |
end |
| 148 |
end |
| 149 |
if drive_prefix.empty? |
| 150 |
expanded_path |
| 151 |
else |
| 152 |
drive_prefix + expanded_path.gsub("/", File::ALT_SEPARATOR) |
| 153 |
end |
| 154 |
end |
| 155 |
|
| 156 |
def self.foreach(file) |
| 157 |
if block_given? |
| 158 |
self.open(file) do |f| |
| 159 |
f.each {|l| yield l} |
| 160 |
end |
| 161 |
else |
| 162 |
return self.new(file) |
| 163 |
end |
| 164 |
end |
| 165 |
|
| 166 |
def self.directory?(file) |
| 167 |
FileTest.directory?(file) |
| 168 |
end |
| 169 |
|
| 170 |
def self.exist?(file) |
| 171 |
FileTest.exist?(file) |
| 172 |
end |
| 173 |
|
| 174 |
def self.exists?(file) |
| 175 |
FileTest.exists?(file) |
| 176 |
end |
| 177 |
|
| 178 |
def self.file?(file) |
| 179 |
FileTest.file?(file) |
| 180 |
end |
| 181 |
|
| 182 |
def self.pipe?(file) |
| 183 |
FileTest.pipe?(file) |
| 184 |
end |
| 185 |
|
| 186 |
def self.size(file) |
| 187 |
FileTest.size(file) |
| 188 |
end |
| 189 |
|
| 190 |
def self.size?(file) |
| 191 |
FileTest.size?(file) |
| 192 |
end |
| 193 |
|
| 194 |
def self.socket?(file) |
| 195 |
FileTest.socket?(file) |
| 196 |
end |
| 197 |
|
| 198 |
def self.symlink?(file) |
| 199 |
FileTest.symlink?(file) |
| 200 |
end |
| 201 |
|
| 202 |
def self.zero?(file) |
| 203 |
FileTest.zero?(file) |
| 204 |
end |
| 205 |
|
| 206 |
def self.extname(filename) |
| 207 |
fname = self.basename(filename) |
| 208 |
epos = fname.rindex('.') |
| 209 |
return '' if epos == 0 || epos.nil? |
| 210 |
return fname[epos..-1] |
| 211 |
end |
| 212 |
|
| 213 |
def self.path(filename) |
| 214 |
if filename.kind_of?(String) |
| 215 |
filename |
| 216 |
else |
| 217 |
raise TypeError, "no implicit conversion of #{filename.class} into String" |
| 218 |
end |
| 219 |
end |
| 220 |
end |