File: pathutil.rb

package info (click to toggle)
ruby-pathutil 0.16.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 136 kB
  • sloc: ruby: 614; makefile: 3
file content (895 lines) | stat: -rw-r--r-- 23,156 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
57
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
136
137
138
139
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
# Frozen-string-literal: true
# Copyright: 2015 - 2017 Jordon Bedwell - MIT License
# Encoding: utf-8

require "pathutil/helpers"
require "forwardable/extended"
require "find"

class Pathutil
  attr_writer :encoding
  extend Forwardable::Extended
  extend Helpers

  # --
  # @note A lot of this class can be compatible with Pathname.
  # Initialize a new instance.
  # @return Pathutil
  # --
  def initialize(path)
    return @path = path if path.is_a?(String)
    return @path = path.to_path if path.respond_to?(:to_path)
    return @path = path.to_s
  end

  # --
  # Make a path relative.
  # --
  def relative
    return self if relative?
    self.class.new(strip_windows_drive.gsub(
      %r!\A(\\+|/+)!, ""
    ))
  end

  # --
  # Make a path absolute
  # --
  def absolute
    return self if absolute?
    self.class.new("/").join(
      @path
    )
  end

  # --
  # @see Pathname#cleanpath.
  # @note This is a wholesale rip and cleanup of Pathname#cleanpath
  # @return Pathutil
  # --
  def cleanpath(symlink = false)
    symlink ? conservative_cleanpath : aggressive_cleanpath
  end

  # --
  # @yield Pathutil
  # @note It will return all results that it finds across all ascending paths.
  # @example Pathutil.new("~/").expand_path.search_backwards(".bashrc") => [#<Pathutil:/home/user/.bashrc>]
  # Search backwards for a file (like Rakefile, _config.yml, opts.yml).
  # @return Enum
  # --
  def search_backwards(file, backwards: Float::INFINITY)
    ary = []

    ascend.with_index(1).each do |path, index|
      if index > backwards
        break

      else
        Dir.chdir path do
          if block_given?
            file = self.class.new(file)
            if yield(file)
              ary.push(
                file
              )
            end

          elsif File.exist?(file)
            ary.push(self.class.new(
              path.join(file)
            ))
          end
        end
      end
    end

    ary
  end

  # --
  # Read the file as a YAML file turning it into an object.
  # @see self.class.load_yaml as this a direct alias of that method.
  # @return Hash
  # --
  def read_yaml(throw_missing: false, **kwd)
    self.class.load_yaml(
      read, **kwd
    )

  rescue Errno::ENOENT
    throw_missing ? raise : (
      return {}
    )
  end

  # --
  # Read the file as a JSON file turning it into an object.
  # @see self.class.read_json as this is a direct alias of that method.
  # @return Hash
  # --
  def read_json(throw_missing: false)
    JSON.parse(
      read
    )

  rescue Errno::ENOENT
    throw_missing ? raise : (
      return {}
    )
  end

  # --
  # @note The blank part is intentionally left there so that you can rejoin.
  # Splits the path into all parts so that you can do step by step comparisons
  # @example Pathutil.new("/my/path").split_path # => ["", "my", "path"]
  # @return Array<String>
  # --
  def split_path
    @path.split(
      %r!\\+|/+!
    )
  end

  # --
  # @see `String#==` for more details.
  # A stricter version of `==` that also makes sure the object matches.
  # @return true|false
  # --
  def ===(other)
    other.is_a?(self.class) && @path == other
  end

  # --
  # @example Pathutil.new("/hello") >= Pathutil.new("/") # => true
  # @example Pathutil.new("/hello") >= Pathutil.new("/hello") # => true
  # Checks to see if a path falls within a path and deeper or is the other.
  # @return true|false
  # --
  def >=(other)
    mine, other = expanded_paths(other)
    return true if other == mine
    mine.in_path?(other)
  end

  # --
  # @example Pathutil.new("/hello/world") > Pathutil.new("/hello") # => true
  # Strictly checks to see if a path is deeper but within the path of the other.
  # @return true|false
  # --
  def >(other)
    mine, other = expanded_paths(other)
    return false if other == mine
    mine.in_path?(other)
  end

  # --
  # @example Pathutil.new("/") < Pathutil.new("/hello") # => true
  # Strictly check to see if a path is behind other path but within it.
  # @return true|false
  # --
  def <(other)
    mine, other = expanded_paths(other)
    return false if other == mine
    other.in_path?(mine)
  end

  # --
  # Check to see if a path is behind the other path but within it.
  # @example Pathutil.new("/hello") < Pathutil.new("/hello") # => true
  # @example Pathutil.new("/") < Pathutil.new("/hello") # => true
  # @return true|false
  # --
  def <=(other)
    mine, other = expanded_paths(other)
    return true if other == mine
    other.in_path?(mine)
  end

  # --
  # @note "./" is considered relative.
  # Check to see if the path is absolute, as in: starts with "/"
  # @return true|false
  # --
  def absolute?
    return !!(
      @path =~ %r!\A(?:[A-Za-z]:)?(?:\\+|/+)!
    )
  end

  # --
  # @yield Pathutil
  # Break apart the path and yield each with the previous parts.
  # @example Pathutil.new("/hello/world").ascend.to_a # => ["/", "/hello", "/hello/world"]
  # @example Pathutil.new("/hello/world").ascend { |path| $stdout.puts path }
  # @return Enum
  # --
  def ascend
    unless block_given?
      return to_enum(
        __method__
      )
    end

    yield(
      path = self
    )

    while (new_path = path.dirname)
      if path == new_path || new_path == "."
        break
      else
        path = new_path
        yield  new_path
      end
    end

    nil
  end

  # --
  # @yield Pathutil
  # Break apart the path in reverse order and descend into the path.
  # @example Pathutil.new("/hello/world").descend.to_a # => ["/hello/world", "/hello", "/"]
  # @example Pathutil.new("/hello/world").descend { |path| $stdout.puts path }
  # @return Enum
  # --
  def descend
    unless block_given?
      return to_enum(
        __method__
      )
    end

    ascend.to_a.reverse_each do |val|
      yield val
    end

    nil
  end

  # --
  # @yield Pathutil
  # @example Pathutil.new("/hello/world").each_line { |line| $stdout.puts line }
  # Wraps `readlines` and allows you to yield on the result.
  # @return Enum
  # --
  def each_line
    return to_enum(__method__) unless block_given?
    readlines.each do |line|
      yield line
    end

    nil
  end

  # --
  # @example Pathutil.new("/hello").fnmatch?("/hello") # => true
  # Unlike traditional `fnmatch`, with this one `Regexp` is allowed.
  # @example Pathutil.new("/hello").fnmatch?(/h/) # => true
  # @see `File#fnmatch` for more information.
  # @return true|false
  # --
  def fnmatch?(matcher)
    matcher.is_a?(Regexp) ? !!(self =~ matcher) : \
      File.fnmatch(matcher, self)
  end

  # --
  # Allows you to quickly determine if the file is the root folder.
  # @return true|false
  # --
  def root?
    !!(self =~ %r!\A(?:[A-Za-z]:)?(?:\\+|/+)\z!)
  end

  # --
  # Allows you to check if the current path is in the path you want.
  # @return true|false
  # --
  def in_path?(path)
    path = self.class.new(path).expand_path.split_path
    mine = (symlink?? expand_path.realpath : expand_path).split_path
    path.each_with_index { |part, index| return false if mine[index] != part }
    true
  end

  # --
  def inspect
    "#<#{self.class}:#{@path}>"
  end

  # --
  # @return Array<Pathutil>
  # Grab all of the children from the current directory, including hidden.
  # @yield Pathutil
  # --
  def children
    ary = []

    Dir.foreach(@path) do |path|
      if path == "." || path == ".."
        next
      else
        path = self.class.new(File.join(@path, path))
        yield path if block_given?
        ary.push(
          path
        )
      end
    end

    ary
  end

  # --
  # @yield Pathutil
  # Allows you to glob however you wish to glob in the current `Pathutil`
  # @see `File::Constants` for a list of flags.
  # @return Enum
  # --
  def glob(pattern, flags = 0)
    unless block_given?
      return to_enum(
        __method__, pattern, flags
      )
    end

    chdir do
      Dir.glob(pattern, flags).each do |file|
        yield self.class.new(
          File.join(@path, file)
        )
      end
    end

    nil
  end

  # --
  # @yield &block
  # Move to the current directory temporarily (or for good) and do work son.
  # @note you do not need to ship a block at all.
  # @return nil
  # --
  def chdir
    if !block_given?
      Dir.chdir(
        @path
      )

    else
      Dir.chdir @path do
        yield
      end
    end
  end

  # --
  # @yield Pathutil
  # Find all files without care and yield the given block.
  # @return Enum
  # --
  def find
    return to_enum(__method__) unless block_given?
    Find.find @path do |val|
      yield self.class.new(val)
    end
  end

  # --
  # @yield Pathutil
  # Splits the path returning each part (filename) back to you.
  # @return Enum
  # --
  def each_filename
    return to_enum(__method__) unless block_given?
    @path.split(File::SEPARATOR).delete_if(&:empty?).each do |file|
      yield file
    end
  end

  # --
  # Get the parent of the current path.
  # @note This will simply return self if "/".
  # @return Pathutil
  # --
  def parent
    return self if @path == "/"
    self.class.new(absolute?? File.dirname(@path) : File.join(
      @path, ".."
    ))
  end

  # --
  # @yield Pathutil
  # Split the file into its dirname and basename, so you can do stuff.
  # @return nil
  # --
  def split
    File.split(@path).collect! do |path|
      self.class.new(path)
    end
  end

  # --
  # @note Your extension should start with "."
  # Replace a files extension with your given extension.
  # @return Pathutil
  # --
  def sub_ext(ext)
    self.class.new(@path.chomp(File.extname(@path)) + ext)
  end

  # --
  # A less complex version of `relative_path_from` that simply uses a
  # `Regexp` and returns the full path if it cannot be determined.
  # @return Pathutil
  # --
  def relative_path_from(from)
    from = self.class.new(from).expand_path.gsub(%r!/$!, "")
    self.class.new(expand_path.gsub(%r!^#{
      from.regexp_escape
    }/!, ""))
  end

  # --
  # Expands the path and left joins the root to the path.
  # @return Pathutil
  # --
  def enforce_root(root)
    return self if !relative? && in_path?(root)
    self.class.new(root).join(
      self
    )
  end

  # --
  # Copy a directory, allowing symlinks if the link falls inside of the root.
  # This is indented for people who wish some safety to their copies.
  # @note Ignore is ignored on safe_copy file because it's explicit.
  # @return nil
  # --
  def safe_copy(to, root: nil, ignore: [])
    raise ArgumentError, "must give a root" unless root
    root = self.class.new(root)
    to   = self.class.new(to)

    if directory?
      safe_copy_directory(to, root: root, ignore: ignore)

    else
      safe_copy_file(to, root: root)
    end
  end

  # --
  # @see `self.class.normalize` as this is an alias.
  # --
  def normalize
    return @normalize ||= begin
      self.class.normalize
    end
  end

  # --
  # @see `self.class.encoding` as this is an alias.
  # --
  def encoding
    return @encoding ||= begin
      self.class.encoding
    end
  end

  # --
  # @note You can set the default encodings via the class.
  # Read took two steroid shots: it can normalize your string, and encode.
  # @return String
  # --
  def read(*args, **kwd)
    kwd[:encoding] ||= encoding

    if normalize[:read]
      File.read(self, *args, **kwd).encode(universal_newline: true)

    else
      File.read(self, *args, **kwd)
    end
  end

  # --
  # @note You can set the default encodings via the class.
  # Binread took two steroid shots: it can normalize your string, and encode.
  # @return String
  # --
  def binread(*args, **kwd)
    kwd[:encoding] ||= encoding

    if normalize[:read]
      File.binread(self, *args, kwd).encode({
        :universal_newline => true
      })

    else
      File.read(
        self, *args, kwd
      )
    end
  end

  # --
  # @note You can set the default encodings via the class.
  # Readlines took two steroid shots: it can normalize your string, and encode.
  # @return Array<String>
  # --
  def readlines(*args, **kwd)
    kwd[:encoding] ||= encoding

    if normalize[:read]
      File.readlines(self, *args, **kwd).encode({
        :universal_newline => true
      })

    else
      File.readlines(
        self, *args, **kwd
      )
    end
  end

  # --
  # @note You can set the default encodings via the class.
  # Write took two steroid shots: it can normalize your string, and encode.
  # @return Fixnum<Bytes>
  # --
  def write(data, *args, **kwd)
    kwd[:encoding] ||= encoding

    if normalize[:write]
      File.write(self, data.encode(
        :crlf_newline => true
      ), *args, **kwd)

    else
      File.write(
        self, data, *args, **kwd
      )
    end
  end

  # --
  # @note You can set the default encodings via the class.
  # Binwrite took two steroid shots: it can normalize your string, and encode.
  # @return Fixnum<Bytes>
  # --
  def binwrite(data, *args, **kwd)
    kwd[:encoding] ||= encoding

    if normalize[:write]
      File.binwrite(self, data.encode(
        :crlf_newline => true
      ), *args, kwd)

    else
      File.binwrite(
        self, data, *args, kwd
      )
    end
  end

  # --
  def to_regexp(guard: true)
    Regexp.new((guard ? "\\A" : "") + Regexp.escape(
      self
    ))
  end

  # --
  # Strips the windows drive from the path.
  # --
  def strip_windows_drive(path = @path)
    self.class.new(path.gsub(
      %r!\A[A-Za-z]:(?:\\+|/+)!, ""
    ))
  end

  # --
  # rubocop:disable Metrics/AbcSize
  # rubocop:disable Metrics/CyclomaticComplexity
  # rubocop:disable Metrics/PerceivedComplexity
  # --

  def aggressive_cleanpath
    return self.class.new("/") if root?

    _out = split_path.each_with_object([]) do |part, out|
      next if part == "." || (part == ".." && out.last == "")
      if part == ".." && out.last && out.last != ".."
        out.pop

      else
        out.push(
          part
        )
      end
    end

    # --

    return self.class.new("/") if _out == [""].freeze
    return self.class.new(".") if _out.empty? && (end_with?(".") || relative?)
    self.class.new(_out.join("/"))
  end

  # --
  def conservative_cleanpath
    _out = split_path.each_with_object([]) do |part, out|
      next if part == "." || (part == ".." && out.last == "")
      out.push(
        part
      )
    end

    # --

    if !_out.empty? && basename == "." && _out.last != "" && _out.last != ".."
      _out << "."
    end

    # --

    return self.class.new("/") if _out == [""].freeze
    return self.class.new(".") if _out.empty? && (end_with?(".") || relative?)
    return self.class.new(_out.join("/")).join("") if @path =~ %r!/\z! \
      && _out.last != "." && _out.last != ".."
    self.class.new(_out.join("/"))
  end

  # --
  # rubocop:enable Metrics/AbcSize
  # rubocop:enable Metrics/CyclomaticComplexity
  # rubocop:enable Metrics/PerceivedComplexity
  # Expand the paths and return.
  # --
  private
  def expanded_paths(path)
    return expand_path, self.class.new(path).expand_path
  end

  # --
  # Safely copy a file.
  # --
  private
  def safe_copy_file(to, root: nil)
    raise Errno::EPERM, "#{self} not in #{root}" unless in_path?(root)
    FileUtils.cp(self, to, preserve: true)
  end

  # --
  # Safely copy a directory and it's sub-files.
  # --
  private
  def safe_copy_directory(to, root: nil, ignore: [])
    ignore = [ignore].flatten.uniq

    if !in_path?(root)
      raise Errno::EPERM, "#{self} not in #{
        root
      }"

    else
      to.mkdir_p unless to.exist?
      children do |file|
        unless ignore.any? { |path| file.in_path?(path) }
          if !file.in_path?(root)
            raise Errno::EPERM, "#{file} not in #{
              root
            }"

          elsif file.file?
            FileUtils.cp(file, to, preserve: true)

          else
            path = file.realpath
            path.safe_copy(to.join(file.basename), root: root, ignore: ignore)
          end
        end
      end
    end
  end

  class << self
    attr_writer :encoding

    # --
    # @note We do nothing special here.
    # Get the current directory that Ruby knows about.
    # @return Pathutil
    # --
    def pwd
      new(
        Dir.pwd
      )
    end

    alias gcwd pwd
    alias cwd  pwd

    # --
    # @note you are encouraged to override this if you need to.
    # Aliases the default system encoding to us so that we can do most read
    # and write operations with that encoding, instead of being crazy.
    # --
    def encoding
      return @encoding ||= begin
        Encoding.default_external
      end
    end

    # --
    # Normalize CRLF -> LF   on Windows reads, to ease  your troubles.
    # Normalize LF   -> CLRF on Windows write, to ease  your troubles.
    # --
    def normalize
      return @normalize ||= {
        :read  => Gem.win_platform?,
        :write => Gem.win_platform?
      }
    end

    # --
    # Make a temporary directory.
    # @note if you adruptly exit it will not remove the dir.
    # @note this directory is removed on exit.
    # @return Pathutil
    # --
    def tmpdir(*args)
      rtn = new(make_tmpname(*args)).tap(&:mkdir)
      ObjectSpace.define_finalizer(rtn, proc do
        rtn.rm_rf
      end)

      rtn
    end

    # --
    # Make a temporary file.
    # @note if you adruptly exit it will not remove the dir.
    # @note this file is removed on exit.
    # @return Pathutil
    # --
    def tmpfile(*args)
      rtn = new(make_tmpname(*args)).tap(&:touch)
      ObjectSpace.define_finalizer(rtn, proc do
        rtn.rm_rf
      end)

      rtn
    end
  end

  # --

  rb_delegate :gcwd, :to => :"self.class"
  rb_delegate :pwd,  :to => :"self.class"

  # --

  rb_delegate :sub,         :to => :@path, :wrap => true
  rb_delegate :chomp,       :to => :@path, :wrap => true
  rb_delegate :gsub,        :to => :@path, :wrap => true
  rb_delegate :[],          :to => :@path
  rb_delegate :=~,          :to => :@path
  rb_delegate :==,          :to => :@path
  rb_delegate :to_s,        :to => :@path
  rb_delegate :freeze,      :to => :@path
  rb_delegate :end_with?,   :to => :@path
  rb_delegate :start_with?, :to => :@path
  rb_delegate :frozen?,     :to => :@path
  rb_delegate :to_str,      :to => :@path
  rb_delegate :"!~",        :to => :@path
  rb_delegate :<=>,         :to => :@path

  # --

  rb_delegate :chmod,        :to => :File, :args => { :after => :@path }
  rb_delegate :lchown,       :to => :File, :args => { :after => :@path }
  rb_delegate :lchmod,       :to => :File, :args => { :after => :@path }
  rb_delegate :chown,        :to => :File, :args => { :after => :@path }
  rb_delegate :basename,     :to => :File, :args => :@path, :wrap => true
  rb_delegate :dirname,      :to => :File, :args => :@path, :wrap => true
  rb_delegate :readlink,     :to => :File, :args => :@path, :wrap => true
  rb_delegate :expand_path,  :to => :File, :args => :@path, :wrap => true
  rb_delegate :realdirpath,  :to => :File, :args => :@path, :wrap => true
  rb_delegate :realpath,     :to => :File, :args => :@path, :wrap => true
  rb_delegate :rename,       :to => :File, :args => :@path, :wrap => true
  rb_delegate :join,         :to => :File, :args => :@path, :wrap => true
  rb_delegate :empty?,       :to => :file, :args => :@path
  rb_delegate :size,         :to => :File, :args => :@path
  rb_delegate :link,         :to => :File, :args => :@path
  rb_delegate :atime,        :to => :File, :args => :@path
  rb_delegate :ctime,        :to => :File, :args => :@path
  rb_delegate :lstat,        :to => :File, :args => :@path
  rb_delegate :utime,        :to => :File, :args => :@path
  rb_delegate :sysopen,      :to => :File, :args => :@path
  rb_delegate :birthtime,    :to => :File, :args => :@path
  rb_delegate :mountpoint?,  :to => :File, :args => :@path
  rb_delegate :truncate,     :to => :File, :args => :@path
  rb_delegate :symlink,      :to => :File, :args => :@path
  rb_delegate :extname,      :to => :File, :args => :@path
  rb_delegate :zero?,        :to => :File, :args => :@path
  rb_delegate :ftype,        :to => :File, :args => :@path
  rb_delegate :mtime,        :to => :File, :args => :@path
  rb_delegate :open,         :to => :File, :args => :@path
  rb_delegate :stat,         :to => :File, :args => :@path

  # --

  rb_delegate :pipe?,            :to => :FileTest, :args => :@path
  rb_delegate :file?,            :to => :FileTest, :args => :@path
  rb_delegate :owned?,           :to => :FileTest, :args => :@path
  rb_delegate :setgid?,          :to => :FileTest, :args => :@path
  rb_delegate :socket?,          :to => :FileTest, :args => :@path
  rb_delegate :readable?,        :to => :FileTest, :args => :@path
  rb_delegate :blockdev?,        :to => :FileTest, :args => :@path
  rb_delegate :directory?,       :to => :FileTest, :args => :@path
  rb_delegate :readable_real?,   :to => :FileTest, :args => :@path
  rb_delegate :world_readable?,  :to => :FileTest, :args => :@path
  rb_delegate :executable_real?, :to => :FileTest, :args => :@path
  rb_delegate :world_writable?,  :to => :FileTest, :args => :@path
  rb_delegate :writable_real?,   :to => :FileTest, :args => :@path
  rb_delegate :executable?,      :to => :FileTest, :args => :@path
  rb_delegate :writable?,        :to => :FileTest, :args => :@path
  rb_delegate :grpowned?,        :to => :FileTest, :args => :@path
  rb_delegate :chardev?,         :to => :FileTest, :args => :@path
  rb_delegate :symlink?,         :to => :FileTest, :args => :@path
  rb_delegate :sticky?,          :to => :FileTest, :args => :@path
  rb_delegate :setuid?,          :to => :FileTest, :args => :@path
  rb_delegate :exist?,           :to => :FileTest, :args => :@path
  rb_delegate :size?,            :to => :FileTest, :args => :@path

  # --

  rb_delegate :rm_rf,   :to => :FileUtils, :args => :@path
  rb_delegate :rm_r,    :to => :FileUtils, :args => :@path
  rb_delegate :rm_f,    :to => :FileUtils, :args => :@path
  rb_delegate :rm,      :to => :FileUtils, :args => :@path
  rb_delegate :cp_r,    :to => :FileUtils, :args => :@path
  rb_delegate :touch,   :to => :FileUtils, :args => :@path
  rb_delegate :mkdir_p, :to => :FileUtils, :args => :@path
  rb_delegate :mkpath,  :to => :FileUtils, :args => :@path
  rb_delegate :cp,      :to => :FileUtils, :args => :@path

  # --

  rb_delegate :each_child, :to => :children
  rb_delegate :each_entry, :to => :children
  rb_delegate :to_a,       :to => :children

  # --

  rb_delegate :opendir, :to => :Dir, :alias_of => :open
  rb_delegate :relative?, :to => :self, :alias_of => :absolute?, :bool => :reverse
  rb_delegate :regexp_escape, :to => :Regexp, :args => :@path, :alias_of => :escape
  rb_delegate :shellescape, :to => :Shellwords, :args => :@path
  rb_delegate :mkdir, :to => :Dir, :args => :@path

  # --

  alias + join
  alias delete rm
  alias rmtree rm_r
  alias to_path to_s
  alias last basename
  alias entries children
  alias make_symlink symlink
  alias cleanpath_conservative conservative_cleanpath
  alias cleanpath_aggressive aggressive_cleanpath
  alias prepend enforce_root
  alias fnmatch fnmatch?
  alias make_link link
  alias first dirname
  alias rmdir rm_r
  alias unlink rm
  alias / join
end