Class: FSSM::Pathname

Inherits:
String
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Pathname

Returns a new instance of Pathname.

Raises:

  • (ArgumentError)


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

.join(*parts) ⇒ Object



370
371
372
373
374
# File 'lib/fssm/pathname.rb', line 370

def self.join(*parts)
  last_part = FSSM::Pathname.new(parts.last)
  return last_part if last_part.absolute?
  FSSM::Pathname.new(File.join(*parts.reject {|p| p.empty? }))
end

.pwdObject Also known as: getwd



205
206
207
# File 'lib/fssm/pathname.rb', line 205

def self.pwd
  FSSM::Pathname.new(Dir.pwd)
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

Returns:

  • (Boolean)


46
47
48
# File 'lib/fssm/pathname.rb', line 46

def absolute?
  self[0, 1].to_s == ROOT
end

#ascendObject



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

#atimeObject



340
341
342
# File 'lib/fssm/pathname.rb', line 340

def atime
  File.atime(self)
end

#basenameObject



376
377
378
# File 'lib/fssm/pathname.rb', line 376

def basename
  self.class.new(File.basename(self))
end

#blockdev?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/fssm/pathname.rb', line 250

def blockdev?
  FileTest.blockdev?(self)
end

#chardev?Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/fssm/pathname.rb', line 254

def chardev?
  FileTest.chardev?(self)
end

#chdirObject



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

#childrenObject



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

#cleanpathObject



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

#ctimeObject



344
345
346
# File 'lib/fssm/pathname.rb', line 344

def ctime
  File.ctime(self)
end

#descendObject



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

Returns:

  • (Boolean)


258
259
260
# File 'lib/fssm/pathname.rb', line 258

def directory?
  FileTest.directory?(self)
end

#dirnameObject



388
389
390
# File 'lib/fssm/pathname.rb', line 388

def dirname
  self.class.new(File.dirname(self))
end

#dot?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/fssm/pathname.rb', line 97

def dot?
  self == DOT
end

#dot_dot?Boolean

Returns:

  • (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

#entriesObject



209
210
211
# File 'lib/fssm/pathname.rb', line 209

def entries
  Dir.entries(self).map! {|e| FSSM::Pathname.new(e) }
end

#executable?Boolean

Returns:

  • (Boolean)


262
263
264
# File 'lib/fssm/pathname.rb', line 262

def executable?
  FileTest.executable?(self)
end

#executable_real?Boolean

Returns:

  • (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?

Returns:

  • (Boolean)


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 expand_path(from = nil)
  self.class.new(File.expand_path(self, from))
end

#extnameObject



396
397
398
# File 'lib/fssm/pathname.rb', line 396

def extname
  File.extname(self)
end

#file?Boolean

Returns:

  • (Boolean)


274
275
276
# File 'lib/fssm/pathname.rb', line 274

def file?
  FileTest.file?(self)
end

#findObject



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

Returns:

  • (Boolean)


400
401
402
# File 'lib/fssm/pathname.rb', line 400

def fnmatch?(pat, flags = 0)
  File.fnmatch(pat, self, flags)
end

#ftypeObject



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

Returns:

  • (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


416
417
418
# File 'lib/fssm/pathname.rb', line 416

def link(to)
  File.link(self, to)
end

#lstatObject



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

#mkpathObject



455
456
457
# File 'lib/fssm/pathname.rb', line 455

def mkpath
  self.class.new(FileUtils.mkpath(self))
end

#mountpoint?Boolean

Returns:

  • (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

#mtimeObject



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

Returns:

  • (Boolean)


282
283
284
# File 'lib/fssm/pathname.rb', line 282

def owned?
  FileTest.owned?(self)
end

#parentObject



118
119
120
# File 'lib/fssm/pathname.rb', line 118

def parent
  self + '..'
end

#pipe?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


290
291
292
# File 'lib/fssm/pathname.rb', line 290

def readable?
  FileTest.readable?(self)
end

#readable_real?Boolean

Returns:

  • (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


424
425
426
# File 'lib/fssm/pathname.rb', line 424

def readlink
  self.class.new(File.readlink(self))
end

#realpathObject



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.expand_path
end

#relative?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/fssm/pathname.rb', line 136

def relative?
  !absolute?
end

#relative_path_from(base) ⇒ Object

Raises:

  • (ArgumentError)


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

#rmdirObject



221
222
223
# File 'lib/fssm/pathname.rb', line 221

def rmdir
  Dir.rmdir(self)
end

#rmtreeObject



459
460
461
# File 'lib/fssm/pathname.rb', line 459

def rmtree
  self.class.new(FileUtils.rmtree(self).first)
end

#root?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/fssm/pathname.rb', line 168

def root?
  !!(self =~ %r{^#{ROOT}+$})
end

#setgid?Boolean

Returns:

  • (Boolean)


298
299
300
# File 'lib/fssm/pathname.rb', line 298

def setgid?
  FileTest.setgit?(self)
end

#setuid?Boolean

Returns:

  • (Boolean)


302
303
304
# File 'lib/fssm/pathname.rb', line 302

def setuid?
  FileTest.setuid?(self)
end

#sizeObject



433
434
435
# File 'lib/fssm/pathname.rb', line 433

def size
  File.size(self)
end

#size?Boolean

Returns:

  • (Boolean)


437
438
439
# File 'lib/fssm/pathname.rb', line 437

def size?
  File.size?(self)
end

#socket?Boolean

Returns:

  • (Boolean)


306
307
308
# File 'lib/fssm/pathname.rb', line 306

def socket?
  FileTest.socket?(self)
end

#splitObject



441
442
443
# File 'lib/fssm/pathname.rb', line 441

def split
  File.split(self).map {|part| FSSM::Pathname.new(part) }
end

#statObject



360
361
362
# File 'lib/fssm/pathname.rb', line 360

def stat
  File.stat(self)
end

#sticky?Boolean

Returns:

  • (Boolean)


310
311
312
# File 'lib/fssm/pathname.rb', line 310

def sticky?
  FileTest.sticky?(self)
end


445
446
447
# File 'lib/fssm/pathname.rb', line 445

def symlink(to)
  File.symlink(self, to)
end

#symlink?Boolean

Returns:

  • (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_aObject 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_pathObject



181
182
183
# File 'lib/fssm/pathname.rb', line 181

def to_path
  self
end

#to_sObject Also known as: to_str



185
186
187
# File 'lib/fssm/pathname.rb', line 185

def to_s
  "#{self}"
end

#touchObject



463
464
465
# File 'lib/fssm/pathname.rb', line 463

def touch
  self.class.new(FileUtils.touch(self).first)
end

#truncateObject



449
450
451
# File 'lib/fssm/pathname.rb', line 449

def truncate
  File.truncate(self)
end


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

Returns:

  • (Boolean)


318
319
320
# File 'lib/fssm/pathname.rb', line 318

def world_readable?
  FileTest.world_readable?(self)
end

#world_writable?Boolean

Returns:

  • (Boolean)


322
323
324
# File 'lib/fssm/pathname.rb', line 322

def world_writable?
  FileTest.world_writable?(self)
end

#writable?Boolean

Returns:

  • (Boolean)


326
327
328
# File 'lib/fssm/pathname.rb', line 326

def writable?
  FileTest.writable?(self)
end

#writable_real?Boolean

Returns:

  • (Boolean)


330
331
332
# File 'lib/fssm/pathname.rb', line 330

def writable_real?
  FileTest.writable_real?(self)
end

#zero?Boolean

Returns:

  • (Boolean)


334
335
336
# File 'lib/fssm/pathname.rb', line 334

def zero?
  FileTest.zero?(self)
end