Module: Scripto::FileCommands

Included in:
Scripto, Main
Defined in:
lib/scripto/file_commands.rb

Instance Method Summary collapse

Instance Method Details

#chmod(file, mode) ⇒ Object



82
83
84
85
86
# File 'lib/scripto/file_commands.rb', line 82

def chmod(file, mode)
  if File.stat(file).mode != mode
    FileUtils.chmod(mode, file, verbose: verbose?)
  end
end

#chown(file, user) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/scripto/file_commands.rb', line 72

def chown(file, user)
  # who is the current owner?
  @scripto_uids ||= {}
  @scripto_uids[user] ||= Etc.getpwnam(user).uid
  uid = @scripto_uids[user]
  if File.stat(file).uid != uid
    FileUtils.chown(uid, uid, file, verbose: verbose?)
  end
end

#copy_metadata(src, dst) ⇒ Object



94
95
96
97
98
# File 'lib/scripto/file_commands.rb', line 94

def (src, dst)
  stat = File.stat(src)
  File.chmod(stat.mode, dst)
  File.utime(stat.atime, stat.mtime, dst)
end

#cp(src, dst, mkdir: false, owner: nil, mode: nil) ⇒ Object



12
13
14
15
16
17
# File 'lib/scripto/file_commands.rb', line 12

def cp(src, dst, mkdir: false, owner: nil, mode: nil)
  mkdir_if_necessary(File.dirname(dst)) if mkdir
  FileUtils.cp_r(src, dst, preserve: true, verbose: verbose?)
  chown(dst, owner) if owner && !File.symlink?(dst)
  chmod(dst, mode) if mode
end

#cp_if_necessary(src, dst, mkdir: false, owner: nil, mode: nil) ⇒ Object



45
46
47
48
49
50
# File 'lib/scripto/file_commands.rb', line 45

def cp_if_necessary(src, dst, mkdir: false, owner: nil, mode: nil)
  if !(File.exists?(dst) && FileUtils.compare_file(src, dst))
    cp(src, dst, mkdir: mkdir, owner: owner, mode: mode)
    true
  end
end

#ln(src, dst) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/scripto/file_commands.rb', line 24

def ln(src, dst)
  begin
    FileUtils.ln_sf(src, dst, verbose: verbose?)
  rescue Errno::EEXIST => e
    # It's a race - this can occur because ln_sf removes the old
    # dst, then creates the symlink. Raise if they don't match.
    raise e if !(File.symlink?(dst) && src == File.readlink(dst))
  end
end

#ln_if_necessary(src, dst) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/scripto/file_commands.rb', line 52

def ln_if_necessary(src, dst)
  ln = if !File.symlink?(dst)
    true
  elsif File.readlink(dst) != src
    rm(dst)
    true
  end
  if ln
    ln(src, dst)
    true
  end
end

#mkdir(dir, owner: nil, mode: nil) ⇒ Object



6
7
8
9
10
# File 'lib/scripto/file_commands.rb', line 6

def mkdir(dir, owner: nil, mode: nil)
  FileUtils.mkdir_p(dir, verbose: verbose?)
  chown(dir, owner) if owner
  chmod(dir, mode) if mode
end

#mkdir_if_necessary(dir, owner: nil, mode: nil) ⇒ Object



38
39
40
41
42
43
# File 'lib/scripto/file_commands.rb', line 38

def mkdir_if_necessary(dir, owner: nil, mode: nil)
  if !(File.exists?(dir) || File.symlink?(dir))
    mkdir(dir, owner: owner, mode: mode)
    true
  end
end

#mv(src, dst, mkdir: false) ⇒ Object



19
20
21
22
# File 'lib/scripto/file_commands.rb', line 19

def mv(src, dst, mkdir: false)
  mkdir_if_necessary(File.dirname(dst)) if mkdir
  FileUtils.mv(src, dst, verbose: verbose?)
end

#rm(file) ⇒ Object



34
35
36
# File 'lib/scripto/file_commands.rb', line 34

def rm(file)
  FileUtils.rm_f(file, verbose: verbose?)
end

#rm_and_mkdir(dir) ⇒ Object



88
89
90
91
92
# File 'lib/scripto/file_commands.rb', line 88

def rm_and_mkdir(dir)
  raise "don't do this" if dir == ""
  FileUtils.rm_rf(dir, verbose: verbose?)
  mkdir(dir)
end

#rm_if_necessary(file) ⇒ Object



65
66
67
68
69
70
# File 'lib/scripto/file_commands.rb', line 65

def rm_if_necessary(file)
  if File.exists?(file)
    rm(file)
    true
  end
end