summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@gmail.com>2013-05-23 21:33:56 +0100
committerRichard Maw <richard.maw@gmail.com>2013-05-27 14:38:09 +0100
commit630306f6163fed02035b3ef7d65df07e69191910 (patch)
tree1edc4234a7e6e0980a140b452774e312df96591e
parentae904f76a8ffbb89dce7c9d68de7c3ba1e5fb256 (diff)
downloadgitano-630306f6163fed02035b3ef7d65df07e69191910.tar.gz
util: add copy_file
-rw-r--r--lib/gitano/util.lua44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/gitano/util.lua b/lib/gitano/util.lua
index c5ff673..2e25f91 100644
--- a/lib/gitano/util.lua
+++ b/lib/gitano/util.lua
@@ -186,6 +186,49 @@ local function rm_rf(path)
return (ret == 0), luxio.strerror(err)
end
+local function _write_all(file, data)
+ local towrite = #data
+ local written = 0
+ while written < towrite do
+ local write_count, emsg = file:write(data, written)
+ if not write_count then
+ return false, written, emsg
+ end
+ written = written + write_count
+ end
+ return true, written
+end
+
+local function copy_file(from, to, buffer_size)
+ -- Default buffer size is 4M, but can be changed
+ buffer_size = buffer_size or 4 * 1024 * 1024
+ local fromfile, emsg = sio.open(from, "r")
+ if not fromfile then
+ return false, emsg
+ end
+ local tofile, emsg = sio.open(to, "wce")
+ if not tofile then
+ return false, emsg
+ end
+ local write_count
+ repeat
+ local ok
+ local bytes, emsg = fromfile:read(buffer_size)
+ if not bytes then
+ fromfile:close()
+ tofile:close()
+ return false, emsg
+ end
+ ok, write_count, emsg = _write_all(tofile, bytes)
+ if not ok then
+ fromfile:close()
+ tofile:close()
+ return false, emsg
+ end
+ until write_count == 0
+ return true
+end
+
local function html_escape(s)
return (s:gsub("&", "&amp;"):
gsub("<", "&lt;"):
@@ -329,6 +372,7 @@ return {
dirname = dirname,
basename = basename,
+ copy_file = copy_file,
mkdir_p = mkdir_p,
rm_rf = rm_rf,