diff options
-rw-r--r-- | lib/gitano/util.lua | 44 |
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("&", "&"): gsub("<", "<"): @@ -329,6 +372,7 @@ return { dirname = dirname, basename = basename, + copy_file = copy_file, mkdir_p = mkdir_p, rm_rf = rm_rf, |