Class: Cfruby::FileOps::LocalFileCommand
- Inherits:
-
Object
- Object
- Cfruby::FileOps::LocalFileCommand
- Defined in:
- lib/libcfruby/fileops.rb
Overview
FileCommand interface for local to local operations
Instance Method Summary collapse
-
#copy(filename, newfilename, options = {}) ⇒ Object
Executes FileUtils.cp followed by FileOps.chmod and FileOps.chown (using :user, :group, and :mode).
-
#copy_single(filename, newfilename, options = {}) ⇒ Object
Executes FileUtils.cp followed by FileOps.chmod and FileOps.chown (using :user, :group, and :mode).
-
#move(filename, newfilename, options = {}) ⇒ Object
- Options:
:force
- (defaults to true) force the move
:mode
- set the mode of
newfilename
:preserve
- attempts to preserve the mode and ownership of newfilename if it exists
:onlyonchange
-
only copy if the file has changed (implies force).
- attempts to preserve the mode and ownership of newfilename if it exists
- set the mode of
- (defaults to true) force the move
- Options:
Instance Method Details
#copy(filename, newfilename, options = {}) ⇒ Object
Executes FileUtils.cp followed by FileOps.chmod and FileOps.chown (using :user, :group, and :mode). If filename is a glob it will be expanded and all resultant filenames will be copied with the assumption that newfilename is a directory. Options:
:backup
-
true to make a backup of
newfilename
before copying :force
-
(defaults to true) force the copy even if newfilename exists
:onlyonchange
-
only copy if the file has changed (implies force)
:recursive
-
recursively copy
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 |
# File 'lib/libcfruby/fileops.rb', line 129 def copy(filename, newfilename, = {}) # set default options if([:force] == nil) [:force] = true end if([:onlyonchange]) [:force] = true end # first, a basic check that filename exists somehow if(Dir.glob(filename).length == 0) raise(FileOpsFileExistError, "\"#{filename}\" does not exist") end # get the base directory of the copy basedir = File.dirname(Pathname.new(Dir.glob(filename)[0]).realpath.to_s) basedirregex = Regexp.new(Regexp.escape(basedir) + "/?(.*)$") # use file find to get a list of files to copy FileFind.find(filename, ) { |filename| # copy each file after adjusting for the base directories basename = basedirregex.match(filename)[1] if(File.directory?(newfilename)) copy_single(filename, newfilename + "/#{basename}", ) else copy_single(filename, newfilename, ) end } end |
#copy_single(filename, newfilename, options = {}) ⇒ Object
Executes FileUtils.cp followed by FileOps.chmod and FileOps.chown (using :user, :group, and :mode). filename and newfilename must be single files Options:
:backup
-
true to make a backup of
newfilename
before copying :force
-
(defaults to true) force the copy even if newfilename exists
:onlyonchange
-
only copy if the file has changed (implies force)
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 |
# File 'lib/libcfruby/fileops.rb', line 166 def copy_single(filename, newfilename, = {}) mode = [:mode] owner = [:user] group = [:group] .delete :mode .delete :user .delete :group force = [:force] if(force == nil) force = true end Cfruby.controller.attempt("copy #{filename} to #{newfilename}", 'destructive') { if(!File.exists?(filename)) raise(FileOpsFileExistError, "\"#{filename}\" does not exist") end if(!force and File.exists?(newfilename)) raise(FileOpsOverwriteError, "\"#{newfilename}\" already exists") end if([:onlyonchange] and File.exist?(newfilename)) [:force] = true originalsum = Cfruby::Checksum::Checksum.get_checksums(filename) newsum = Cfruby::Checksum::Checksum.get_checksums(newfilename) if(originalsum.sha1 == newsum.sha1) Cfruby.controller.attempt_abort("files have the same sha1 hash") end end if [:backup] FileOps.backup(newfilename) if File.exist? newfilename .delete :backup .delete :onlyonchange end if(File.exists?(newfilename) and force) FileOps.delete(newfilename) end if(File.directory?(filename)) FileUtils.mkdir(newfilename) else FileUtils.cp(filename, newfilename, :preserve => true) end } # change ownership and mode if we need to FileOps.chown(newfilename,owner,group,) if owner or group FileOps.chmod(newfilename,mode) if mode end |
#move(filename, newfilename, options = {}) ⇒ Object
Options:
:force
-
(defaults to true) force the move
:mode
-
set the mode of
newfilename
:preserve
-
attempts to preserve the mode and ownership of newfilename if it exists
:onlyonchange
-
only copy if the file has changed (implies force)
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 |
# File 'lib/libcfruby/fileops.rb', line 80 def move(filename, newfilename, = {}) if([:force] == nil) [:force] = true end currentstat = nil Cfruby.controller.attempt("move #{filename} to #{newfilename}", 'destructive') { if([:onlyonchange] and File.exist?(newfilename)) [:force] = true originalsum = Cfruby::Checksum::Checksum.get_checksums(filename) newsum = Cfruby::Checksum::Checksum.get_checksums(newfilename) if(originalsum.sha1 == newsum.sha1) Cfruby.controller.attempt_abort("files have the same sha1 hash") end end if(File.exists?(newfilename)) if([:preserve]) currentstat = File.stat(newfilename) end if([:force]) FileOps.delete(newfilename) else raise(FileOpsOverwriteError, "\"#{newfilename}\" already exists") end end FileUtils.mv(filename, newfilename) if(currentstat and [:preserve]) FileOps.chmod(newfilename, currentstat.mode) FileOps.chown(newfilename, currentstat.uid, currentstat.gid) end if([:mode] != nil) FileOps.chmod(newfilename, [:mode]) end } end |