Class: Ftools

Inherits:
Object
  • Object
show all
Defined in:
lib/ftools.rb

Class Method Summary collapse

Class Method Details

.clone(args) ⇒ Object

Clones the from-path to to to-path. Also symlinks!



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ftools.rb', line 65

def self.clone(args)
  if File.symlink?(args[:path_f])
    File.symlink(File.readlink(args[:path_f]), args[:path_t])
  elsif File.directory?(args[:path_f])
    Dir.mkdir(args[:path_t], Ftools.perms(args[:path_f]).to_i(8)) if !File.exists?(args[:path_t])
  else
    File.open(args[:path_t], "wb") do |fp_w|
      File.open(args[:path_f], "rb") do |fp_r|
        begin
          while read = fp_r.sysread(4096)
            fp_w.write(read)
          end
        rescue EOFError
          #ignore.
        end
      end
    end
    
    Ftools.clone_perms(:path_f => args[:path_f], :path_t => args[:path_t])
  end
end

.clone_perms(args) ⇒ Object

Clones ownership and permissions from one path to another.



52
53
54
55
# File 'lib/ftools.rb', line 52

def self.clone_perms(args)
  perms = Ftools.perms(args[:path_f])
  File.chmod(perms.to_i(8), args[:path_t])
end

.foreach_r(args) ⇒ Object

Returns enumerator for every single file og directory in a given path.

Raises:

  • (Errno::ENOENT)


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
# File 'lib/ftools.rb', line 3

def self.foreach_r(args)
  if !args.key?(:files) or args[:files]
    files = true
  else
    files = false
  end
  
  if !args.key?(:dirs) or args[:dirs]
    dirs = true
  else
    dirs = false
  end
  
  raise Errno::ENOENT if !File.exists?(args[:path])
  
  Enumerator.new do |y|
    if !File.directory?(args[:path]) or File.symlink?(args[:path])
      if args[:fpath]
        y << File.realpath(args[:path])
      else
        y << File.basename(args[:path])
      end
    else
      Dir.foreach(args[:path]) do |file|
        next if file == "." or file == ".."
        fpath = "#{args[:path]}/#{file}"
        next if args.key?(:ignore) and args[:ignore].index(fpath) != nil
        
        if args[:fpath]
          obj = fpath
        else
          obj = file
        end
        
        if !File.directory?(fpath)
          y << obj if files
        else
          y << obj if dirs
          
          Ftools.foreach_r(args.merge(:path => fpath)).each do |file|
            y << file
          end
        end
      end
    end
  end
end

.perms(path) ⇒ Object

Returns the octal file-permissions for a given path.



58
59
60
61
62
# File 'lib/ftools.rb', line 58

def self.perms(path)
  mode = File.stat(path).mode
  perms = sprintf("%o", mode)[-4, 4]
  return perms
end