summaryrefslogtreecommitdiff
path: root/lib/ftools.rb
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-01-16 12:19:09 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-01-16 12:19:09 +0000
commit62e41d3f2e48422bbdf1bb2db83ae60b255b1a1a (patch)
tree4d0edb1c1986e1578b181ebe2441acfee27f1fab /lib/ftools.rb
parent3db12e8b236ac8f88db8eb4690d10e4a3b8dbcd4 (diff)
Initial revision
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/ftools.rb')
-rw-r--r--lib/ftools.rb163
1 files changed, 163 insertions, 0 deletions
diff --git a/lib/ftools.rb b/lib/ftools.rb
new file mode 100644
index 0000000000..59bc81b365
--- /dev/null
+++ b/lib/ftools.rb
@@ -0,0 +1,163 @@
+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
+
+ from = open(from, "r")
+ from.binmode
+ to = open(to, "w")
+ to.binmode
+
+ begin
+ while TRUE
+ 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
+ ret = TRUE
+ rescue
+ ret = FALSE
+ ensure
+ to.close
+ from.close
+ end
+ ret
+ end
+
+ def copy from, to, verbose = FALSE
+ $stderr.print from, " -> ", catname(from, to), "\n" if verbose
+ syscopy from, to
+ end
+
+ alias cp copy
+
+# move file
+
+ def move from, to, verbose = FALSE
+ to = catname(from, to)
+ $stderr.print from, " -> ", to, "\n" if verbose
+
+ if PLATFORM =~ /djgpp|cygwin32|mswin32/ and FileTest.file? to
+ unlink to
+ end
+ begin
+ rename from, to
+ rescue
+ syscopy from, to and unlink from
+ end
+ end
+
+ alias mv move
+
+# compare two files
+# TRUE: identical
+# FALSE: not identical
+
+ def compare from, to, verbose = FALSE
+ $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
+
+ ret = FALSE
+ fr = tr = ''
+
+ begin
+ while fr == tr
+ if fr = from.read(fsize)
+ tr = to.read(fr.size)
+ else
+ ret = !to.read(fsize)
+ break
+ end
+ end
+ rescue
+ ret = FALSE
+ ensure
+ to.close
+ from.close
+ end
+ ret
+ end
+
+ alias cmp compare
+
+# unlink files safely
+
+ def safe_unlink(*files)
+ verbose = if files[-1].is_a? String then FALSE else files.pop end
+ 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)
+ verbose = if dirs[-1].is_a? String then FALSE else dirs.pop end
+# 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
+ Dir.mkdir dir, mode
+ end
+ end
+
+ alias mkpath makedirs
+
+ alias o_chmod chmod
+
+ def chmod(mode, *files)
+ verbose = if files[-1].is_a? String then FALSE else files.pop end
+ $stderr.printf "chmod %04o %s\n", mode, files.join(" ") if verbose
+ o_chmod mode, *files
+ end
+
+ def install(from, to, mode, verbose)
+ to = catname(from, to)
+ unless FileTest.exist? to and cmp from, to
+ cp from, to, verbose
+ chmod mode, to, verbose if mode
+ end
+ end
+
+end
+# vi:set sw=2: