1998-01-16 07:19:09 -05:00
|
|
|
class << File
|
|
|
|
|
|
|
|
TOO_BIG = 1024 * 1024 * 2 # 2MB
|
|
|
|
|
|
|
|
def catname from, to
|
|
|
|
if FileTest.directory? to
|
|
|
|
to +
|
|
|
|
if to =~ /\\/
|
|
|
|
if to[-1,1] != '\\' then '\\' end + basename(from)
|
|
|
|
else
|
|
|
|
if to[-1,1] != '/' then '/' end + basename(from)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
to
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# copy file
|
|
|
|
|
|
|
|
def syscopy from, to
|
|
|
|
to = catname(from, to)
|
|
|
|
|
|
|
|
fsize = size(from)
|
|
|
|
fsize = 1024 if fsize < 512
|
|
|
|
fsize = TOO_BIG if fsize > TOO_BIG
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
fmode = stat(from).mode
|
|
|
|
tpath = to
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
from = open(from, "r")
|
|
|
|
from.binmode
|
|
|
|
to = open(to, "w")
|
|
|
|
to.binmode
|
|
|
|
|
|
|
|
begin
|
1999-01-19 23:59:39 -05:00
|
|
|
while true
|
1998-01-16 07:19:09 -05:00
|
|
|
r = from.sysread(fsize)
|
|
|
|
rsize = r.size
|
|
|
|
w = 0
|
|
|
|
while w < rsize
|
|
|
|
t = to.syswrite(r[w, rsize - w])
|
|
|
|
w += t
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue EOFError
|
1999-01-19 23:59:39 -05:00
|
|
|
ret = true
|
1998-01-16 07:19:09 -05:00
|
|
|
rescue
|
1999-01-19 23:59:39 -05:00
|
|
|
ret = false
|
1998-01-16 07:19:09 -05:00
|
|
|
ensure
|
|
|
|
to.close
|
|
|
|
from.close
|
|
|
|
end
|
1999-08-13 01:45:20 -04:00
|
|
|
chmod(fmode, tpath)
|
1998-01-16 07:19:09 -05:00
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
def copy from, to, verbose = false
|
1998-01-16 07:19:09 -05:00
|
|
|
$stderr.print from, " -> ", catname(from, to), "\n" if verbose
|
|
|
|
syscopy from, to
|
|
|
|
end
|
|
|
|
|
|
|
|
alias cp copy
|
|
|
|
|
|
|
|
# move file
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
def move from, to, verbose = false
|
1998-01-16 07:19:09 -05:00
|
|
|
to = catname(from, to)
|
|
|
|
$stderr.print from, " -> ", to, "\n" if verbose
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if RUBY_PLATFORM =~ /djgpp|cygwin|mswin32/ and FileTest.file? to
|
1998-01-16 07:19:09 -05:00
|
|
|
unlink to
|
|
|
|
end
|
1999-08-13 01:45:20 -04:00
|
|
|
fstat = stat(from)
|
1998-01-16 07:19:09 -05:00
|
|
|
begin
|
|
|
|
rename from, to
|
|
|
|
rescue
|
1999-08-13 01:45:20 -04:00
|
|
|
begin
|
|
|
|
symlink File.readlink(from), to and unlink from
|
|
|
|
rescue
|
|
|
|
from_stat = stat(from)
|
|
|
|
syscopy from, to and unlink from
|
|
|
|
utime(from_stat.atime, from_stat.mtime, to)
|
|
|
|
begin
|
|
|
|
chown(fstat.uid, fstat.gid, tpath)
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias mv move
|
|
|
|
|
|
|
|
# compare two files
|
1999-01-19 23:59:39 -05:00
|
|
|
# true: identical
|
|
|
|
# false: not identical
|
1998-01-16 07:19:09 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
def compare from, to, verbose = false
|
1998-01-16 07:19:09 -05:00
|
|
|
$stderr.print from, " <=> ", to, "\n" if verbose
|
|
|
|
fsize = size(from)
|
|
|
|
fsize = 1024 if fsize < 512
|
|
|
|
fsize = TOO_BIG if fsize > TOO_BIG
|
|
|
|
|
|
|
|
from = open(from, "r")
|
|
|
|
from.binmode
|
|
|
|
to = open(to, "r")
|
|
|
|
to.binmode
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
ret = false
|
1998-01-16 07:19:09 -05:00
|
|
|
fr = tr = ''
|
|
|
|
|
|
|
|
begin
|
|
|
|
while fr == tr
|
|
|
|
if fr = from.read(fsize)
|
|
|
|
tr = to.read(fr.size)
|
|
|
|
else
|
1999-08-13 01:45:20 -04:00
|
|
|
ret = to.read(fsize)
|
|
|
|
ret = !ret || ret.length == 0
|
1998-01-16 07:19:09 -05:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue
|
1999-01-19 23:59:39 -05:00
|
|
|
ret = false
|
1998-01-16 07:19:09 -05:00
|
|
|
ensure
|
|
|
|
to.close
|
|
|
|
from.close
|
|
|
|
end
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
|
|
|
alias cmp compare
|
|
|
|
|
|
|
|
# unlink files safely
|
|
|
|
|
|
|
|
def safe_unlink(*files)
|
1999-01-19 23:59:39 -05:00
|
|
|
verbose = if files[-1].is_a? String then false else files.pop end
|
1998-01-16 07:19:09 -05:00
|
|
|
begin
|
|
|
|
$stderr.print files.join(" "), "\n" if verbose
|
|
|
|
chmod 0777, *files
|
|
|
|
unlink *files
|
|
|
|
rescue
|
|
|
|
# STDERR.print "warning: Couldn't unlink #{files.join ' '}\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias rm_f safe_unlink
|
|
|
|
|
|
|
|
def makedirs(*dirs)
|
1999-01-19 23:59:39 -05:00
|
|
|
verbose = if dirs[-1].is_a? String then false else dirs.pop end
|
1998-01-16 07:19:09 -05:00
|
|
|
# mode = if dirs[-1].is_a? Fixnum then dirs.pop else 0755 end
|
|
|
|
mode = 0755
|
|
|
|
for dir in dirs
|
|
|
|
next if FileTest.directory? dir
|
|
|
|
parent = dirname(dir)
|
|
|
|
makedirs parent unless FileTest.directory? parent
|
|
|
|
$stderr.print "mkdir ", dir, "\n" if verbose
|
1999-08-13 01:45:20 -04:00
|
|
|
if basename(dir) != ""
|
|
|
|
Dir.mkdir dir, mode
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias mkpath makedirs
|
|
|
|
|
|
|
|
alias o_chmod chmod
|
|
|
|
|
|
|
|
def chmod(mode, *files)
|
1999-01-19 23:59:39 -05:00
|
|
|
verbose = if files[-1].is_a? String then false else files.pop end
|
1998-01-16 07:19:09 -05:00
|
|
|
$stderr.printf "chmod %04o %s\n", mode, files.join(" ") if verbose
|
|
|
|
o_chmod mode, *files
|
|
|
|
end
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
def install(from, to, mode = nil, verbose = false)
|
1998-01-16 07:19:09 -05:00
|
|
|
to = catname(from, to)
|
|
|
|
unless FileTest.exist? to and cmp from, to
|
1999-08-13 01:45:20 -04:00
|
|
|
safe_unlink to if FileTest.exist? to
|
1998-01-16 07:19:09 -05:00
|
|
|
cp from, to, verbose
|
|
|
|
chmod mode, to, verbose if mode
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
# vi:set sw=2:
|