Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: ftools.rb
#
[0] Fix | Delete
# = ftools.rb: Extra tools for the File class
[1] Fix | Delete
#
[2] Fix | Delete
# Author:: WATANABE, Hirofumi
[3] Fix | Delete
# Documentation:: Zachary Landau
[4] Fix | Delete
#
[5] Fix | Delete
# This library can be distributed under the terms of the Ruby license.
[6] Fix | Delete
# You can freely distribute/modify this library.
[7] Fix | Delete
#
[8] Fix | Delete
# It is included in the Ruby standard library.
[9] Fix | Delete
#
[10] Fix | Delete
# == Description
[11] Fix | Delete
#
[12] Fix | Delete
# ftools adds several (class, not instance) methods to the File class, for
[13] Fix | Delete
# copying, moving, deleting, installing, and comparing files, as well as
[14] Fix | Delete
# creating a directory path. See the File class for details.
[15] Fix | Delete
#
[16] Fix | Delete
# FileUtils contains all or nearly all the same functionality and more, and
[17] Fix | Delete
# is a recommended option over ftools
[18] Fix | Delete
#
[19] Fix | Delete
# When you
[20] Fix | Delete
#
[21] Fix | Delete
# require 'ftools'
[22] Fix | Delete
#
[23] Fix | Delete
# then the File class aquires some utility methods for copying, moving, and
[24] Fix | Delete
# deleting files, and more.
[25] Fix | Delete
#
[26] Fix | Delete
# See the method descriptions below, and consider using FileUtils as it is
[27] Fix | Delete
# more comprehensive.
[28] Fix | Delete
#
[29] Fix | Delete
class File
[30] Fix | Delete
end
[31] Fix | Delete
[32] Fix | Delete
class << File
[33] Fix | Delete
[34] Fix | Delete
BUFSIZE = 8 * 1024
[35] Fix | Delete
[36] Fix | Delete
#
[37] Fix | Delete
# If +to+ is a valid directory, +from+ will be appended to +to+, adding
[38] Fix | Delete
# and escaping backslashes as necessary. Otherwise, +to+ will be returned.
[39] Fix | Delete
# Useful for appending +from+ to +to+ only if the filename was not specified
[40] Fix | Delete
# in +to+.
[41] Fix | Delete
#
[42] Fix | Delete
def catname(from, to)
[43] Fix | Delete
if directory? to
[44] Fix | Delete
join to.sub(%r([/\\]$), ''), basename(from)
[45] Fix | Delete
else
[46] Fix | Delete
to
[47] Fix | Delete
end
[48] Fix | Delete
end
[49] Fix | Delete
[50] Fix | Delete
#
[51] Fix | Delete
# Copies a file +from+ to +to+. If +to+ is a directory, copies +from+
[52] Fix | Delete
# to <tt>to/from</tt>.
[53] Fix | Delete
#
[54] Fix | Delete
def syscopy(from, to)
[55] Fix | Delete
to = catname(from, to)
[56] Fix | Delete
[57] Fix | Delete
fmode = stat(from).mode
[58] Fix | Delete
tpath = to
[59] Fix | Delete
not_exist = !exist?(tpath)
[60] Fix | Delete
[61] Fix | Delete
from = open(from, "rb")
[62] Fix | Delete
to = open(to, "wb")
[63] Fix | Delete
[64] Fix | Delete
begin
[65] Fix | Delete
while true
[66] Fix | Delete
to.syswrite from.sysread(BUFSIZE)
[67] Fix | Delete
end
[68] Fix | Delete
rescue EOFError
[69] Fix | Delete
ret = true
[70] Fix | Delete
rescue
[71] Fix | Delete
ret = false
[72] Fix | Delete
ensure
[73] Fix | Delete
to.close
[74] Fix | Delete
from.close
[75] Fix | Delete
end
[76] Fix | Delete
chmod(fmode, tpath) if not_exist
[77] Fix | Delete
ret
[78] Fix | Delete
end
[79] Fix | Delete
[80] Fix | Delete
#
[81] Fix | Delete
# Copies a file +from+ to +to+ using #syscopy. If +to+ is a directory,
[82] Fix | Delete
# copies +from+ to <tt>to/from</tt>. If +verbose+ is true, <tt>from -> to</tt>
[83] Fix | Delete
# is printed.
[84] Fix | Delete
#
[85] Fix | Delete
def copy(from, to, verbose = false)
[86] Fix | Delete
$stderr.print from, " -> ", catname(from, to), "\n" if verbose
[87] Fix | Delete
syscopy from, to
[88] Fix | Delete
end
[89] Fix | Delete
[90] Fix | Delete
alias cp copy
[91] Fix | Delete
[92] Fix | Delete
#
[93] Fix | Delete
# Moves a file +from+ to +to+ using #syscopy. If +to+ is a directory,
[94] Fix | Delete
# copies from +from+ to <tt>to/from</tt>. If +verbose+ is true, <tt>from ->
[95] Fix | Delete
# to</tt> is printed.
[96] Fix | Delete
#
[97] Fix | Delete
def move(from, to, verbose = false)
[98] Fix | Delete
to = catname(from, to)
[99] Fix | Delete
$stderr.print from, " -> ", to, "\n" if verbose
[100] Fix | Delete
[101] Fix | Delete
if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and file? to
[102] Fix | Delete
unlink to
[103] Fix | Delete
end
[104] Fix | Delete
fstat = stat(from)
[105] Fix | Delete
begin
[106] Fix | Delete
rename from, to
[107] Fix | Delete
rescue
[108] Fix | Delete
begin
[109] Fix | Delete
symlink readlink(from), to and unlink from
[110] Fix | Delete
rescue
[111] Fix | Delete
from_stat = stat(from)
[112] Fix | Delete
syscopy from, to and unlink from
[113] Fix | Delete
utime(from_stat.atime, from_stat.mtime, to)
[114] Fix | Delete
begin
[115] Fix | Delete
chown(fstat.uid, fstat.gid, to)
[116] Fix | Delete
rescue
[117] Fix | Delete
end
[118] Fix | Delete
end
[119] Fix | Delete
end
[120] Fix | Delete
end
[121] Fix | Delete
[122] Fix | Delete
alias mv move
[123] Fix | Delete
[124] Fix | Delete
#
[125] Fix | Delete
# Returns +true+ if and only if the contents of files +from+ and +to+ are
[126] Fix | Delete
# identical. If +verbose+ is +true+, <tt>from <=> to</tt> is printed.
[127] Fix | Delete
#
[128] Fix | Delete
def compare(from, to, verbose = false)
[129] Fix | Delete
$stderr.print from, " <=> ", to, "\n" if verbose
[130] Fix | Delete
[131] Fix | Delete
return false if stat(from).size != stat(to).size
[132] Fix | Delete
[133] Fix | Delete
from = open(from, "rb")
[134] Fix | Delete
to = open(to, "rb")
[135] Fix | Delete
[136] Fix | Delete
ret = false
[137] Fix | Delete
fr = tr = ''
[138] Fix | Delete
[139] Fix | Delete
begin
[140] Fix | Delete
while fr == tr
[141] Fix | Delete
fr = from.read(BUFSIZE)
[142] Fix | Delete
if fr
[143] Fix | Delete
tr = to.read(fr.size)
[144] Fix | Delete
else
[145] Fix | Delete
ret = to.read(BUFSIZE)
[146] Fix | Delete
ret = !ret || ret.length == 0
[147] Fix | Delete
break
[148] Fix | Delete
end
[149] Fix | Delete
end
[150] Fix | Delete
rescue
[151] Fix | Delete
ret = false
[152] Fix | Delete
ensure
[153] Fix | Delete
to.close
[154] Fix | Delete
from.close
[155] Fix | Delete
end
[156] Fix | Delete
ret
[157] Fix | Delete
end
[158] Fix | Delete
[159] Fix | Delete
alias cmp compare
[160] Fix | Delete
[161] Fix | Delete
#
[162] Fix | Delete
# Removes a list of files. Each parameter should be the name of the file to
[163] Fix | Delete
# delete. If the last parameter isn't a String, verbose mode will be enabled.
[164] Fix | Delete
# Returns the number of files deleted.
[165] Fix | Delete
#
[166] Fix | Delete
def safe_unlink(*files)
[167] Fix | Delete
verbose = if files[-1].is_a? String then false else files.pop end
[168] Fix | Delete
files.each do |file|
[169] Fix | Delete
begin
[170] Fix | Delete
unlink file
[171] Fix | Delete
$stderr.print "removing ", file, "\n" if verbose
[172] Fix | Delete
rescue Errno::EACCES # for Windows
[173] Fix | Delete
continue if symlink? file
[174] Fix | Delete
begin
[175] Fix | Delete
mode = stat(file).mode
[176] Fix | Delete
o_chmod mode | 0200, file
[177] Fix | Delete
unlink file
[178] Fix | Delete
$stderr.print "removing ", file, "\n" if verbose
[179] Fix | Delete
rescue
[180] Fix | Delete
o_chmod mode, file rescue nil
[181] Fix | Delete
end
[182] Fix | Delete
rescue
[183] Fix | Delete
end
[184] Fix | Delete
end
[185] Fix | Delete
end
[186] Fix | Delete
[187] Fix | Delete
alias rm_f safe_unlink
[188] Fix | Delete
[189] Fix | Delete
#
[190] Fix | Delete
# Creates a directory and all its parent directories.
[191] Fix | Delete
# For example,
[192] Fix | Delete
#
[193] Fix | Delete
# File.makedirs '/usr/lib/ruby'
[194] Fix | Delete
#
[195] Fix | Delete
# causes the following directories to be made, if they do not exist.
[196] Fix | Delete
# * /usr
[197] Fix | Delete
# * /usr/lib
[198] Fix | Delete
# * /usr/lib/ruby
[199] Fix | Delete
#
[200] Fix | Delete
# You can pass several directories, each as a parameter. If the last
[201] Fix | Delete
# parameter isn't a String, verbose mode will be enabled.
[202] Fix | Delete
#
[203] Fix | Delete
def makedirs(*dirs)
[204] Fix | Delete
verbose = if dirs[-1].is_a? String then false else dirs.pop end
[205] Fix | Delete
mode = 0755
[206] Fix | Delete
for dir in dirs
[207] Fix | Delete
parent = dirname(dir)
[208] Fix | Delete
next if parent == dir or directory? dir
[209] Fix | Delete
makedirs parent unless directory? parent
[210] Fix | Delete
$stderr.print "mkdir ", dir, "\n" if verbose
[211] Fix | Delete
if basename(dir) != ""
[212] Fix | Delete
begin
[213] Fix | Delete
Dir.mkdir dir, mode
[214] Fix | Delete
rescue SystemCallError
[215] Fix | Delete
raise unless directory? dir
[216] Fix | Delete
end
[217] Fix | Delete
end
[218] Fix | Delete
end
[219] Fix | Delete
end
[220] Fix | Delete
[221] Fix | Delete
alias mkpath makedirs
[222] Fix | Delete
[223] Fix | Delete
alias o_chmod chmod
[224] Fix | Delete
[225] Fix | Delete
vsave, $VERBOSE = $VERBOSE, false
[226] Fix | Delete
[227] Fix | Delete
#
[228] Fix | Delete
# Changes permission bits on +files+ to the bit pattern represented
[229] Fix | Delete
# by +mode+. If the last parameter isn't a String, verbose mode will
[230] Fix | Delete
# be enabled.
[231] Fix | Delete
#
[232] Fix | Delete
# File.chmod 0755, 'somecommand'
[233] Fix | Delete
# File.chmod 0644, 'my.rb', 'your.rb', true
[234] Fix | Delete
#
[235] Fix | Delete
def chmod(mode, *files)
[236] Fix | Delete
verbose = if files[-1].is_a? String then false else files.pop end
[237] Fix | Delete
$stderr.printf "chmod %04o %s\n", mode, files.join(" ") if verbose
[238] Fix | Delete
o_chmod mode, *files
[239] Fix | Delete
end
[240] Fix | Delete
$VERBOSE = vsave
[241] Fix | Delete
[242] Fix | Delete
#
[243] Fix | Delete
# If +src+ is not the same as +dest+, copies it and changes the permission
[244] Fix | Delete
# mode to +mode+. If +dest+ is a directory, destination is <tt>dest/src</tt>.
[245] Fix | Delete
# If +mode+ is not set, default is used. If +verbose+ is set to true, the
[246] Fix | Delete
# name of each file copied will be printed.
[247] Fix | Delete
#
[248] Fix | Delete
def install(from, to, mode = nil, verbose = false)
[249] Fix | Delete
to = catname(from, to)
[250] Fix | Delete
unless exist? to and cmp from, to
[251] Fix | Delete
safe_unlink to if exist? to
[252] Fix | Delete
cp from, to, verbose
[253] Fix | Delete
chmod mode, to, verbose if mode
[254] Fix | Delete
end
[255] Fix | Delete
end
[256] Fix | Delete
[257] Fix | Delete
end
[258] Fix | Delete
[259] Fix | Delete
# vi:set sw=2:
[260] Fix | Delete
[261] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function