1998-01-16 15:19:09 +03:00
|
|
|
class << File
|
|
|
|
|
2002-06-20 12:21:00 +04:00
|
|
|
BUFSIZE = 8 * 1024
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
def catname from, to
|
|
|
|
if FileTest.directory? to
|
2001-10-02 14:36:34 +04:00
|
|
|
File.join to.sub(%r([/\\]$), ''), basename(from)
|
1998-01-16 15:19:09 +03:00
|
|
|
else
|
|
|
|
to
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# copy file
|
|
|
|
|
|
|
|
def syscopy from, to
|
|
|
|
to = catname(from, to)
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
fmode = stat(from).mode
|
|
|
|
tpath = to
|
2001-05-07 10:57:40 +04:00
|
|
|
not_exist = !exist?(tpath)
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2002-06-20 12:21:00 +04:00
|
|
|
from = open(from, "rb")
|
|
|
|
to = open(to, "wb")
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
begin
|
1999-01-20 07:59:39 +03:00
|
|
|
while true
|
2002-06-20 12:21:00 +04:00
|
|
|
to.syswrite from.sysread(BUFSIZE)
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
rescue EOFError
|
1999-01-20 07:59:39 +03:00
|
|
|
ret = true
|
1998-01-16 15:19:09 +03:00
|
|
|
rescue
|
1999-01-20 07:59:39 +03:00
|
|
|
ret = false
|
1998-01-16 15:19:09 +03:00
|
|
|
ensure
|
|
|
|
to.close
|
|
|
|
from.close
|
|
|
|
end
|
2001-05-07 10:57:40 +04:00
|
|
|
chmod(fmode, tpath) if not_exist
|
1998-01-16 15:19:09 +03:00
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
def copy from, to, verbose = false
|
1998-01-16 15:19:09 +03:00
|
|
|
$stderr.print from, " -> ", catname(from, to), "\n" if verbose
|
|
|
|
syscopy from, to
|
|
|
|
end
|
|
|
|
|
|
|
|
alias cp copy
|
|
|
|
|
|
|
|
# move file
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
def move from, to, verbose = false
|
1998-01-16 15:19:09 +03:00
|
|
|
to = catname(from, to)
|
|
|
|
$stderr.print from, " -> ", to, "\n" if verbose
|
|
|
|
|
2002-06-20 12:21:00 +04:00
|
|
|
if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and FileTest.file? to
|
1998-01-16 15:19:09 +03:00
|
|
|
unlink to
|
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
fstat = stat(from)
|
1998-01-16 15:19:09 +03:00
|
|
|
begin
|
|
|
|
rename from, to
|
|
|
|
rescue
|
1999-08-13 09: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
|
2000-08-05 12:40:11 +04:00
|
|
|
chown(fstat.uid, fstat.gid, to)
|
1999-08-13 09:45:20 +04:00
|
|
|
rescue
|
|
|
|
end
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias mv move
|
|
|
|
|
|
|
|
# compare two files
|
1999-01-20 07:59:39 +03:00
|
|
|
# true: identical
|
|
|
|
# false: not identical
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
def compare from, to, verbose = false
|
1998-01-16 15:19:09 +03:00
|
|
|
$stderr.print from, " <=> ", to, "\n" if verbose
|
|
|
|
|
2002-06-20 12:59:59 +04:00
|
|
|
return false if stat(from).size != stat(to).size
|
|
|
|
|
2002-06-20 12:21:00 +04:00
|
|
|
from = open(from, "rb")
|
|
|
|
to = open(to, "rb")
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
ret = false
|
1998-01-16 15:19:09 +03:00
|
|
|
fr = tr = ''
|
|
|
|
|
|
|
|
begin
|
|
|
|
while fr == tr
|
2002-06-20 12:21:00 +04:00
|
|
|
fr = from.read(BUFSIZE)
|
2000-08-05 12:40:11 +04:00
|
|
|
if fr
|
1998-01-16 15:19:09 +03:00
|
|
|
tr = to.read(fr.size)
|
|
|
|
else
|
2002-06-20 12:21:00 +04:00
|
|
|
ret = to.read(BUFSIZE)
|
1999-08-13 09:45:20 +04:00
|
|
|
ret = !ret || ret.length == 0
|
1998-01-16 15:19:09 +03:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue
|
1999-01-20 07:59:39 +03:00
|
|
|
ret = false
|
1998-01-16 15:19:09 +03:00
|
|
|
ensure
|
|
|
|
to.close
|
|
|
|
from.close
|
|
|
|
end
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
|
|
|
alias cmp compare
|
|
|
|
|
|
|
|
# unlink files safely
|
|
|
|
|
|
|
|
def safe_unlink(*files)
|
1999-01-20 07:59:39 +03:00
|
|
|
verbose = if files[-1].is_a? String then false else files.pop end
|
1998-01-16 15:19:09 +03:00
|
|
|
begin
|
|
|
|
$stderr.print files.join(" "), "\n" if verbose
|
|
|
|
chmod 0777, *files
|
2000-08-05 12:40:11 +04:00
|
|
|
unlink(*files)
|
1998-01-16 15:19:09 +03:00
|
|
|
rescue
|
|
|
|
# STDERR.print "warning: Couldn't unlink #{files.join ' '}\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias rm_f safe_unlink
|
|
|
|
|
|
|
|
def makedirs(*dirs)
|
1999-01-20 07:59:39 +03:00
|
|
|
verbose = if dirs[-1].is_a? String then false else dirs.pop end
|
1998-01-16 15:19:09 +03: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 09:45:20 +04:00
|
|
|
if basename(dir) != ""
|
|
|
|
Dir.mkdir dir, mode
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias mkpath makedirs
|
|
|
|
|
|
|
|
alias o_chmod chmod
|
|
|
|
|
2000-08-05 12:40:11 +04:00
|
|
|
vsave, $VERBOSE = $VERBOSE, false
|
1998-01-16 15:19:09 +03:00
|
|
|
def chmod(mode, *files)
|
1999-01-20 07:59:39 +03:00
|
|
|
verbose = if files[-1].is_a? String then false else files.pop end
|
1998-01-16 15:19:09 +03:00
|
|
|
$stderr.printf "chmod %04o %s\n", mode, files.join(" ") if verbose
|
|
|
|
o_chmod mode, *files
|
|
|
|
end
|
2000-08-05 12:40:11 +04:00
|
|
|
$VERBOSE = vsave
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
def install(from, to, mode = nil, verbose = false)
|
1998-01-16 15:19:09 +03:00
|
|
|
to = catname(from, to)
|
|
|
|
unless FileTest.exist? to and cmp from, to
|
1999-08-13 09:45:20 +04:00
|
|
|
safe_unlink to if FileTest.exist? to
|
1998-01-16 15:19:09 +03:00
|
|
|
cp from, to, verbose
|
|
|
|
chmod mode, to, verbose if mode
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
# vi:set sw=2:
|