Class: FSSM::Pathname
- Inherits:
-
String
- Object
- String
- FSSM::Pathname
- Defined in:
- lib/fssm/pathname.rb,
lib/fssm/pathname.rb,
lib/fssm/pathname.rb,
lib/fssm/pathname.rb,
lib/fssm/pathname.rb,
lib/fssm/pathname.rb,
lib/fssm/pathname.rb,
lib/fssm/pathname.rb,
lib/fssm/pathname.rb
Constant Summary collapse
- SYMLOOP_MAX =
8
- ROOT =
'/'.freeze
- DOT =
'.'.freeze
- DOT_DOT =
'..'.freeze
Class Method Summary collapse
- .[](pattern) ⇒ Object
- .for(path) ⇒ Object
- .glob(pattern, flags = 0) ⇒ Object
- .join(*parts) ⇒ Object
- .pwd ⇒ Object (also: getwd)
Instance Method Summary collapse
- #+(path) ⇒ Object
- #<<(path) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #absolute? ⇒ Boolean
- #ascend ⇒ Object
- #atime ⇒ Object
- #basename ⇒ Object
- #blockdev? ⇒ Boolean
- #chardev? ⇒ Boolean
- #chdir ⇒ Object
- #children ⇒ Object
- #chmod(mode) ⇒ Object
- #chown(owner, group) ⇒ Object
- #cleanpath ⇒ Object
- #cleanpath! ⇒ Object
- #ctime ⇒ Object
- #descend ⇒ Object
- #directory? ⇒ Boolean
- #dirname ⇒ Object
- #dot? ⇒ Boolean
- #dot_dot? ⇒ Boolean
- #each_filename(&blk) ⇒ Object
- #each_line(sep = $/, &blk) ⇒ Object
- #entries ⇒ Object
- #executable? ⇒ Boolean
- #executable_real? ⇒ Boolean
- #exists? ⇒ Boolean (also: #exist?)
- #expand_path(from = nil) ⇒ Object (also: #absolute)
- #extname ⇒ Object
- #file? ⇒ Boolean
- #find ⇒ Object
- #fnmatch?(pat, flags = 0) ⇒ Boolean (also: #fnmatch)
- #ftype ⇒ Object
- #glob(pattern, flags = 0, &block) ⇒ Object
- #grpowned? ⇒ Boolean
-
#initialize(path) ⇒ Pathname
constructor
A new instance of Pathname.
- #join(*parts) ⇒ Object
- #lchmod(mode) ⇒ Object
- #lchown(owner, group) ⇒ Object
- #link(to) ⇒ Object
- #lstat ⇒ Object
- #mkdir(mode = 0777) ⇒ Object
- #mkpath ⇒ Object
- #mountpoint? ⇒ Boolean
- #mtime ⇒ Object
- #open(mode = 'r', perm = nil, &blk) ⇒ Object
- #opendir(&blk) ⇒ Object
- #owned? ⇒ Boolean
- #parent ⇒ Object
- #pipe? ⇒ Boolean
- #read(len = nil, off = 0) ⇒ Object
- #readable? ⇒ Boolean
- #readable_real? ⇒ Boolean
- #readlines(sep = $/) ⇒ Object
- #readlink ⇒ Object
- #realpath ⇒ Object
- #relative? ⇒ Boolean
- #relative_path_from(base) ⇒ Object
- #rename(to) ⇒ Object
- #rmdir ⇒ Object
- #rmtree ⇒ Object
- #root? ⇒ Boolean
- #setgid? ⇒ Boolean
- #setuid? ⇒ Boolean
- #size ⇒ Object
- #size? ⇒ Boolean
- #socket? ⇒ Boolean
- #split ⇒ Object
- #stat ⇒ Object
- #sticky? ⇒ Boolean
- #symlink(to) ⇒ Object
- #symlink? ⇒ Boolean
- #sysopen(mode = 'r', perm = nil) ⇒ Object
- #to_a ⇒ Object (also: #segments)
- #to_path ⇒ Object
- #to_s ⇒ Object (also: #to_str)
- #touch ⇒ Object
- #truncate ⇒ Object
- #unlink ⇒ Object (also: #delete)
- #utime(atime, mtime) ⇒ Object
- #world_readable? ⇒ Boolean
- #world_writable? ⇒ Boolean
- #writable? ⇒ Boolean
- #writable_real? ⇒ Boolean
- #zero? ⇒ Boolean
Constructor Details
#initialize(path) ⇒ Pathname
Returns a new instance of Pathname.
18 19 20 21 |
# File 'lib/fssm/pathname.rb', line 18 def initialize(path) raise ArgumentError, "path cannot contain ASCII NULLs" if path =~ %r{\0} super(path) end |
Class Method Details
.[](pattern) ⇒ Object
201 202 203 |
# File 'lib/fssm/pathname.rb', line 201 def self.[](pattern) Dir[pattern].map! {|d| FSSM::Pathname.new(d) } end |
.for(path) ⇒ Object
13 14 15 |
# File 'lib/fssm/pathname.rb', line 13 def for(path) path.is_a?(::FSSM::Pathname) ? path : new("#{path}") end |
.glob(pattern, flags = 0) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/fssm/pathname.rb', line 225 def self.glob(pattern, flags = 0) dirs = Dir.glob(pattern, flags) dirs.map! {|path| FSSM::Pathname.new(path) } if block_given? dirs.each {|dir| yield dir } nil else dirs end end |
Instance Method Details
#+(path) ⇒ Object
38 39 40 |
# File 'lib/fssm/pathname.rb', line 38 def +(path) dup << path end |
#<<(path) ⇒ Object
42 43 44 |
# File 'lib/fssm/pathname.rb', line 42 def <<(path) replace( join(path).cleanpath! ) end |
#<=>(other) ⇒ Object
23 24 25 26 27 |
# File 'lib/fssm/pathname.rb', line 23 def <=>(other) self.tr('/', "\0").to_s <=> other.to_str.tr('/', "\0") rescue NoMethodError nil end |
#==(other) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/fssm/pathname.rb', line 29 def ==(other) left = self.cleanpath.tr('/', "\0").to_s right = self.class.for(other).cleanpath.tr('/', "\0").to_s left == right rescue NoMethodError false end |
#absolute? ⇒ Boolean
46 47 48 |
# File 'lib/fssm/pathname.rb', line 46 def absolute? self[0, 1].to_s == ROOT end |
#ascend ⇒ Object
50 51 52 53 54 55 |
# File 'lib/fssm/pathname.rb', line 50 def ascend parts = to_a parts.length.downto(1) do |i| yield self.class.join(parts[0, i]) end end |
#atime ⇒ Object
340 341 342 |
# File 'lib/fssm/pathname.rb', line 340 def atime File.atime(self) end |
#basename ⇒ Object
376 377 378 |
# File 'lib/fssm/pathname.rb', line 376 def basename self.class.new(File.basename(self)) end |
#blockdev? ⇒ Boolean
250 251 252 |
# File 'lib/fssm/pathname.rb', line 250 def blockdev? FileTest.blockdev?(self) end |
#chardev? ⇒ Boolean
254 255 256 |
# File 'lib/fssm/pathname.rb', line 254 def chardev? FileTest.chardev?(self) end |
#chdir ⇒ Object
243 244 245 246 |
# File 'lib/fssm/pathname.rb', line 243 def chdir blk = lambda { yield self } if block_given? Dir.chdir(self, &blk) end |
#children ⇒ Object
57 58 59 |
# File 'lib/fssm/pathname.rb', line 57 def children entries[2..-1] end |
#chmod(mode) ⇒ Object
380 381 382 |
# File 'lib/fssm/pathname.rb', line 380 def chmod(mode) File.chmod(mode, self) end |
#chown(owner, group) ⇒ Object
384 385 386 |
# File 'lib/fssm/pathname.rb', line 384 def chown(owner, group) File.chown(owner, group, self) end |
#cleanpath ⇒ Object
88 89 90 |
# File 'lib/fssm/pathname.rb', line 88 def cleanpath dup.cleanpath! end |
#cleanpath! ⇒ Object
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 |
# File 'lib/fssm/pathname.rb', line 61 def cleanpath! parts = to_a final = [] parts.each do |part| case part when DOT then next when DOT_DOT then case final.last when ROOT then next when DOT_DOT then final.push(DOT_DOT) when nil then final.push(DOT_DOT) else final.pop end else final.push(part) end end replace(final.empty? ? DOT : self.class.join(*final)) end |
#ctime ⇒ Object
344 345 346 |
# File 'lib/fssm/pathname.rb', line 344 def ctime File.ctime(self) end |
#descend ⇒ Object
92 93 94 95 |
# File 'lib/fssm/pathname.rb', line 92 def descend parts = to_a 1.upto(parts.length) { |i| yield self.class.join(parts[0, i]) } end |
#directory? ⇒ Boolean
258 259 260 |
# File 'lib/fssm/pathname.rb', line 258 def directory? FileTest.directory?(self) end |
#dirname ⇒ Object
388 389 390 |
# File 'lib/fssm/pathname.rb', line 388 def dirname self.class.new(File.dirname(self)) end |
#dot_dot? ⇒ Boolean
101 102 103 |
# File 'lib/fssm/pathname.rb', line 101 def dot_dot? self == DOT_DOT end |
#each_filename(&blk) ⇒ Object
105 106 107 |
# File 'lib/fssm/pathname.rb', line 105 def each_filename(&blk) to_a.each(&blk) end |
#each_line(sep = $/, &blk) ⇒ Object
469 470 471 |
# File 'lib/fssm/pathname.rb', line 469 def each_line(sep = $/, &blk) IO.foreach(self, sep, &blk) end |
#entries ⇒ Object
209 210 211 |
# File 'lib/fssm/pathname.rb', line 209 def entries Dir.entries(self).map! {|e| FSSM::Pathname.new(e) } end |
#executable? ⇒ Boolean
262 263 264 |
# File 'lib/fssm/pathname.rb', line 262 def executable? FileTest.executable?(self) end |
#executable_real? ⇒ Boolean
266 267 268 |
# File 'lib/fssm/pathname.rb', line 266 def executable_real? FileTest.executable_real?(self) end |
#exists? ⇒ Boolean Also known as: exist?
270 271 272 |
# File 'lib/fssm/pathname.rb', line 270 def exists? FileTest.exists?(self) end |
#expand_path(from = nil) ⇒ Object Also known as: absolute
392 393 394 |
# File 'lib/fssm/pathname.rb', line 392 def (from = nil) self.class.new(File.(self, from)) end |
#extname ⇒ Object
396 397 398 |
# File 'lib/fssm/pathname.rb', line 396 def extname File.extname(self) end |
#file? ⇒ Boolean
274 275 276 |
# File 'lib/fssm/pathname.rb', line 274 def file? FileTest.file?(self) end |
#find ⇒ Object
487 488 489 |
# File 'lib/fssm/pathname.rb', line 487 def find Find.find(self) {|path| yield FSSM::Pathname.new(path) } end |
#fnmatch?(pat, flags = 0) ⇒ Boolean Also known as: fnmatch
400 401 402 |
# File 'lib/fssm/pathname.rb', line 400 def fnmatch?(pat, flags = 0) File.fnmatch(pat, self, flags) end |
#ftype ⇒ Object
348 349 350 |
# File 'lib/fssm/pathname.rb', line 348 def ftype File.ftype(self) end |
#glob(pattern, flags = 0, &block) ⇒ Object
237 238 239 240 241 |
# File 'lib/fssm/pathname.rb', line 237 def glob(pattern, flags = 0, &block) patterns = [pattern].flatten patterns.map! {|p| self.class.glob(self.to_s + p, flags, &block) } patterns.flatten end |
#grpowned? ⇒ Boolean
278 279 280 |
# File 'lib/fssm/pathname.rb', line 278 def grpowned? FileTest.grpowned?(self) end |
#join(*parts) ⇒ Object
404 405 406 |
# File 'lib/fssm/pathname.rb', line 404 def join(*parts) self.class.join(self, *parts) end |
#lchmod(mode) ⇒ Object
408 409 410 |
# File 'lib/fssm/pathname.rb', line 408 def lchmod(mode) File.lchmod(mode, self) end |
#lchown(owner, group) ⇒ Object
412 413 414 |
# File 'lib/fssm/pathname.rb', line 412 def lchown(owner, group) File.lchown(owner, group, self) end |
#link(to) ⇒ Object
416 417 418 |
# File 'lib/fssm/pathname.rb', line 416 def link(to) File.link(self, to) end |
#lstat ⇒ Object
352 353 354 |
# File 'lib/fssm/pathname.rb', line 352 def lstat File.lstat(self) end |
#mkdir(mode = 0777) ⇒ Object
213 214 215 |
# File 'lib/fssm/pathname.rb', line 213 def mkdir(mode = 0777) Dir.mkdir(self, mode) end |
#mkpath ⇒ Object
455 456 457 |
# File 'lib/fssm/pathname.rb', line 455 def mkpath self.class.new(FileUtils.mkpath(self)) end |
#mountpoint? ⇒ Boolean
109 110 111 112 113 114 115 116 |
# File 'lib/fssm/pathname.rb', line 109 def mountpoint? stat1 = self.lstat stat2 = self.parent.lstat stat1.dev != stat2.dev || stat1.ino == stat2.ino rescue Errno::ENOENT false end |
#mtime ⇒ Object
356 357 358 |
# File 'lib/fssm/pathname.rb', line 356 def mtime File.mtime(self) end |
#open(mode = 'r', perm = nil, &blk) ⇒ Object
420 421 422 |
# File 'lib/fssm/pathname.rb', line 420 def open(mode = 'r', perm = nil, &blk) File.open(self, mode, perm, &blk) end |
#opendir(&blk) ⇒ Object
217 218 219 |
# File 'lib/fssm/pathname.rb', line 217 def opendir(&blk) Dir.open(self, &blk) end |
#owned? ⇒ Boolean
282 283 284 |
# File 'lib/fssm/pathname.rb', line 282 def owned? FileTest.owned?(self) end |
#parent ⇒ Object
118 119 120 |
# File 'lib/fssm/pathname.rb', line 118 def parent self + '..' end |
#pipe? ⇒ Boolean
286 287 288 |
# File 'lib/fssm/pathname.rb', line 286 def pipe? FileTest.pipe?(self) end |
#read(len = nil, off = 0) ⇒ Object
473 474 475 |
# File 'lib/fssm/pathname.rb', line 473 def read(len = nil, off = 0) IO.read(self, len, off) end |
#readable? ⇒ Boolean
290 291 292 |
# File 'lib/fssm/pathname.rb', line 290 def readable? FileTest.readable?(self) end |
#readable_real? ⇒ Boolean
294 295 296 |
# File 'lib/fssm/pathname.rb', line 294 def readable_real? FileTest.readable_real?(self) end |
#readlines(sep = $/) ⇒ Object
477 478 479 |
# File 'lib/fssm/pathname.rb', line 477 def readlines(sep = $/) IO.readlines(self, sep) end |
#readlink ⇒ Object
424 425 426 |
# File 'lib/fssm/pathname.rb', line 424 def readlink self.class.new(File.readlink(self)) end |
#realpath ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/fssm/pathname.rb', line 122 def realpath path = self SYMLOOP_MAX.times do link = path.readlink link = path.dirname + link if link.relative? path = link end raise Errno::ELOOP, self rescue Errno::EINVAL path. end |
#relative? ⇒ Boolean
136 137 138 |
# File 'lib/fssm/pathname.rb', line 136 def relative? !absolute? end |
#relative_path_from(base) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/fssm/pathname.rb', line 140 def relative_path_from(base) base = self.class.for(base) raise ArgumentError, 'no relative path between a relative and absolute' if self.absolute? != base.absolute? return self if base.dot? return self.class.new(DOT) if self == base base = base.cleanpath.to_a dest = self.cleanpath.to_a while !dest.empty? && !base.empty? && dest[0] == base[0] base.shift dest.shift end base.shift if base[0] == DOT dest.shift if dest[0] == DOT raise ArgumentError, "base directory may not contain '#{DOT_DOT}'" if base.include?(DOT_DOT) path = base.fill(DOT_DOT) + dest path = self.class.join(*path) path = self.class.new(DOT) if path.empty? path end |
#rename(to) ⇒ Object
428 429 430 431 |
# File 'lib/fssm/pathname.rb', line 428 def rename(to) File.rename(self, to) replace(to) end |
#rmdir ⇒ Object
221 222 223 |
# File 'lib/fssm/pathname.rb', line 221 def rmdir Dir.rmdir(self) end |
#rmtree ⇒ Object
459 460 461 |
# File 'lib/fssm/pathname.rb', line 459 def rmtree self.class.new(FileUtils.rmtree(self).first) end |
#root? ⇒ Boolean
168 169 170 |
# File 'lib/fssm/pathname.rb', line 168 def root? !!(self =~ %r{^#{ROOT}+$}) end |
#setgid? ⇒ Boolean
298 299 300 |
# File 'lib/fssm/pathname.rb', line 298 def setgid? FileTest.setgit?(self) end |
#setuid? ⇒ Boolean
302 303 304 |
# File 'lib/fssm/pathname.rb', line 302 def setuid? FileTest.setuid?(self) end |
#size ⇒ Object
433 434 435 |
# File 'lib/fssm/pathname.rb', line 433 def size File.size(self) end |
#size? ⇒ Boolean
437 438 439 |
# File 'lib/fssm/pathname.rb', line 437 def size? File.size?(self) end |
#socket? ⇒ Boolean
306 307 308 |
# File 'lib/fssm/pathname.rb', line 306 def socket? FileTest.socket?(self) end |
#split ⇒ Object
441 442 443 |
# File 'lib/fssm/pathname.rb', line 441 def split File.split(self).map {|part| FSSM::Pathname.new(part) } end |
#stat ⇒ Object
360 361 362 |
# File 'lib/fssm/pathname.rb', line 360 def stat File.stat(self) end |
#sticky? ⇒ Boolean
310 311 312 |
# File 'lib/fssm/pathname.rb', line 310 def sticky? FileTest.sticky?(self) end |
#symlink(to) ⇒ Object
445 446 447 |
# File 'lib/fssm/pathname.rb', line 445 def symlink(to) File.symlink(self, to) end |
#symlink? ⇒ Boolean
314 315 316 |
# File 'lib/fssm/pathname.rb', line 314 def symlink? FileTest.symlink?(self) end |
#sysopen(mode = 'r', perm = nil) ⇒ Object
481 482 483 |
# File 'lib/fssm/pathname.rb', line 481 def sysopen(mode = 'r', perm = nil) IO.sysopen(self, mode, perm) end |
#to_a ⇒ Object Also known as: segments
172 173 174 175 176 177 |
# File 'lib/fssm/pathname.rb', line 172 def to_a array = to_s.split(File::SEPARATOR) array.delete('') array.insert(0, ROOT) if absolute? array end |
#to_path ⇒ Object
181 182 183 |
# File 'lib/fssm/pathname.rb', line 181 def to_path self end |
#to_s ⇒ Object Also known as: to_str
185 186 187 |
# File 'lib/fssm/pathname.rb', line 185 def to_s "#{self}" end |
#touch ⇒ Object
463 464 465 |
# File 'lib/fssm/pathname.rb', line 463 def touch self.class.new(FileUtils.touch(self).first) end |
#truncate ⇒ Object
449 450 451 |
# File 'lib/fssm/pathname.rb', line 449 def truncate File.truncate(self) end |
#unlink ⇒ Object Also known as: delete
191 192 193 194 195 196 197 |
# File 'lib/fssm/pathname.rb', line 191 def unlink Dir.unlink(self) true rescue Errno::ENOTDIR File.unlink(self) true end |
#utime(atime, mtime) ⇒ Object
364 365 366 |
# File 'lib/fssm/pathname.rb', line 364 def utime(atime, mtime) File.utime(self, atime, mtime) end |
#world_readable? ⇒ Boolean
318 319 320 |
# File 'lib/fssm/pathname.rb', line 318 def world_readable? FileTest.world_readable?(self) end |
#world_writable? ⇒ Boolean
322 323 324 |
# File 'lib/fssm/pathname.rb', line 322 def world_writable? FileTest.world_writable?(self) end |
#writable? ⇒ Boolean
326 327 328 |
# File 'lib/fssm/pathname.rb', line 326 def writable? FileTest.writable?(self) end |
#writable_real? ⇒ Boolean
330 331 332 |
# File 'lib/fssm/pathname.rb', line 330 def writable_real? FileTest.writable_real?(self) end |
#zero? ⇒ Boolean
334 335 336 |
# File 'lib/fssm/pathname.rb', line 334 def zero? FileTest.zero?(self) end |