When you
require 'ftools'
then the File class aquires some utility methods for copying, moving, and deleting files, and more.
See the method descriptions below, and consider using fileutils as it is more comprehensive.
Constants
| BUFSIZE | = | 8 * 1024 |
External Aliases
| copy | -> | cp |
| move | -> | mv |
| compare | -> | cmp |
| safe_unlink | -> | rm_f |
| makedirs | -> | mkpath |
| chmod | -> | o_chmod |
Public Class methods
If to is a valid directory, from will be appended to to, adding and escaping backslashes as necessary. Otherwise, to will be returned. Useful for appending from to to only if the filename was not specified in to.
# File ftools.rb, line 47
47: def catname(from, to)
48: if directory? to
49: join to.sub(%r([/\\]$), ''), basename(from)
50: else
51: to
52: end
53: end
Changes permission bits on files to the bit pattern represented by mode. If the last parameter isn’t a String, verbose mode will be enabled.
File.chmod 0755, 'somecommand' File.chmod 0644, 'my.rb', 'your.rb', true
# File ftools.rb, line 240
240: def chmod(mode, *files)
241: verbose = if files[-1].is_a? String then false else files.pop end
242: $stderr.printf "chmod %04o %s\n", mode, files.join(" ") if verbose
243: o_chmod mode, *files
244: end
Returns true iff the contents of files from and to are identical. If verbose is true, from <=> to is printed.
# File ftools.rb, line 133
133: def compare(from, to, verbose = false)
134: $stderr.print from, " <=> ", to, "\n" if verbose
135:
136: return false if stat(from).size != stat(to).size
137:
138: from = open(from, "rb")
139: to = open(to, "rb")
140:
141: ret = false
142: fr = tr = ''
143:
144: begin
145: while fr == tr
146: fr = from.read(BUFSIZE)
147: if fr
148: tr = to.read(fr.size)
149: else
150: ret = to.read(BUFSIZE)
151: ret = !ret || ret.length == 0
152: break
153: end
154: end
155: rescue
156: ret = false
157: ensure
158: to.close
159: from.close
160: end
161: ret
162: end
If src is not the same as dest, copies it and changes the permission mode to mode. If dest is a directory, destination is dest/src. If mode is not set, default is used. If verbose is set to true, the name of each file copied will be printed.
# File ftools.rb, line 253
253: def install(from, to, mode = nil, verbose = false)
254: to = catname(from, to)
255: unless exist? to and cmp from, to
256: safe_unlink to if exist? to
257: cp from, to, verbose
258: chmod mode, to, verbose if mode
259: end
260: end
Creates a directory and all its parent directories. For example,
File.makedirs '/usr/lib/ruby'
causes the following directories to be made, if they do not exist.
* /usr
* /usr/lib
* /usr/lib/ruby
You can pass several directories, each as a parameter. If the last parameter isn’t a String, verbose mode will be enabled.
# File ftools.rb, line 208
208: def makedirs(*dirs)
209: verbose = if dirs[-1].is_a? String then false else dirs.pop end
210: mode = 0755
211: for dir in dirs
212: parent = dirname(dir)
213: next if parent == dir or directory? dir
214: makedirs parent unless directory? parent
215: $stderr.print "mkdir ", dir, "\n" if verbose
216: if basename(dir) != ""
217: begin
218: Dir.mkdir dir, mode
219: rescue SystemCallError
220: raise unless directory? dir
221: end
222: end
223: end
224: end
Moves a file from to to using syscopy. If to is a directory, copies from from to to/from. If verbose is true, from -> to is printed.
# File ftools.rb, line 102
102: def move(from, to, verbose = false)
103: to = catname(from, to)
104: $stderr.print from, " -> ", to, "\n" if verbose
105:
106: if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and file? to
107: unlink to
108: end
109: fstat = stat(from)
110: begin
111: rename from, to
112: rescue
113: begin
114: symlink readlink(from), to and unlink from
115: rescue
116: from_stat = stat(from)
117: syscopy from, to and unlink from
118: utime(from_stat.atime, from_stat.mtime, to)
119: begin
120: chown(fstat.uid, fstat.gid, to)
121: rescue
122: end
123: end
124: end
125: end
Removes a list of files. Each parameter should be the name of the file to delete. If the last parameter isn’t a String, verbose mode will be enabled. Returns the number of files deleted.
# File ftools.rb, line 171
171: def safe_unlink(*files)
172: verbose = if files[-1].is_a? String then false else files.pop end
173: files.each do |file|
174: begin
175: unlink file
176: $stderr.print "removing ", file, "\n" if verbose
177: rescue Errno::EACCES # for Windows
178: continue if symlink? file
179: begin
180: mode = stat(file).mode
181: o_chmod mode | 0200, file
182: unlink file
183: $stderr.print "removing ", file, "\n" if verbose
184: rescue
185: o_chmod mode, file rescue nil
186: end
187: rescue
188: end
189: end
190: end
Copies a file from to to. If to is a directory, copies from to to/from.
# File ftools.rb, line 59
59: def syscopy(from, to)
60: to = catname(from, to)
61:
62: fmode = stat(from).mode
63: tpath = to
64: not_exist = !exist?(tpath)
65:
66: from = open(from, "rb")
67: to = open(to, "wb")
68:
69: begin
70: while true
71: to.syswrite from.sysread(BUFSIZE)
72: end
73: rescue EOFError
74: ret = true
75: rescue
76: ret = false
77: ensure
78: to.close
79: from.close
80: end
81: chmod(fmode, tpath) if not_exist
82: ret
83: end