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

Like chmod mode file. Like all file commands, the operation will be printed out if verbose?.



113
114
115
116
117
# File 'lib/scripto/file_commands.rb', line 113

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

#chown(file, user) ⇒ Object

Like chown user:user file. Like all file commands, the operation will be printed out if verbose?.



101
102
103
104
105
106
107
108
109
# File 'lib/scripto/file_commands.rb', line 101

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

Copy mode, atime and mtime from src to dst. This one is rarely used and doesn’t echo.



129
130
131
132
133
# File 'lib/scripto/file_commands.rb', line 129

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

Like cp -pr src +dst. If mkdir is true, the dst directoy will be created if necessary before the copy. If owner is specified, the directory will be chowned to owner. If mode is specified, the directory will be chmodded to mode. Like all file commands, the operation will be printed out if verbose?.



21
22
23
24
25
26
# File 'lib/scripto/file_commands.rb', line 21

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

Runs #cp, but ONLY if dst doesn’t exist or differs from src. Returns true if the file had to be copied. This is useful with verbose?, to get an exact changelog.



67
68
69
70
71
72
# File 'lib/scripto/file_commands.rb', line 67

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

#ln(src, dst) ⇒ Object

Like ln -sf src +dst. The command will be printed out if verbose?.



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

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

Runs #ln, but ONLY if dst isn’t a symlink or differs from src. Returns true if the file had to be symlinked. This is useful with verbose?, to get an exact changelog.



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/scripto/file_commands.rb', line 77

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

Like mkdir -p dir. If owner is specified, the directory will be chowned to owner. If mode is specified, the directory will be chmodded to mode. Like all file commands, the operation will be printed out if verbose?.



10
11
12
13
14
# File 'lib/scripto/file_commands.rb', line 10

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

Runs #mkdir, but ONLY if dir doesn’t already exist. Returns true if directory had to be created. This is useful with verbose?, to get an exact changelog.



57
58
59
60
61
62
# File 'lib/scripto/file_commands.rb', line 57

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

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

Like mv src +dst. If mkdir is true, the dst directoy will be created if necessary before the copy. Like all file commands, the operation will be printed out if verbose?.



31
32
33
34
# File 'lib/scripto/file_commands.rb', line 31

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

#rm(file) ⇒ Object

Like rm -f file. Like all file commands, the operation will be printed out if verbose?.



50
51
52
# File 'lib/scripto/file_commands.rb', line 50

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

#rm_and_mkdir(dir) ⇒ Object

Like rm -rf && mkdir -p. Like all file commands, the operation will be printed out if verbose?.



121
122
123
124
125
# File 'lib/scripto/file_commands.rb', line 121

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

Runs #rm, but ONLY if file exists. Return true if the file had to be removed. This is useful with verbose?, to get an exact changelog.



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

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