From 4b52fd134e00f67be26b9ad2e668be4eebed9fd5 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Mon, 26 Jan 2009 21:54:28 +0000 Subject: More work to remove unnecessary popen calls. git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@1696 ab2d0c66-481e-0410-8bed-d214d4d58bed --- lib/fs.lua | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/fs.lua b/lib/fs.lua index 0b4c196..15ce442 100644 --- a/lib/fs.lua +++ b/lib/fs.lua @@ -21,11 +21,9 @@ end dirname = function ( string) string = string or "" - -- strip trailing / first - string = string.gsub (string, "/$", "") - local basename = basename ( string) + local basename = basename(string) string = string.sub(string, 1, #string - #basename - 1) - return(string) + return string end -- generic wrapper funcs @@ -41,7 +39,6 @@ function is_link ( pathstr ) return posix.stat ( pathstr or "", "type" ) == "link" end - -- Creates a directory if it doesn't exist, including the parent dirs function create_directory ( path ) local pos = string.find(path, "/") @@ -80,6 +77,22 @@ function create_file ( path ) return is_file(path) end +-- Copies a file to a directory or new filename (creating the directory if necessary) +-- fails if new file already exists +function copy_file(oldpath, newpath) + if not is_file(oldpath) or not newpath or newpath == "" or (basename(newpath) ~= "" and posix.stat(newpath)) or (basename(newpath) == "" and posix.stat(newpath .. basename(oldpath))) then + return false + end + if dirname(newpath) and not posix.stat(dirname(newpath)) then create_directory(dirname(newpath)) end + if basename(newpath) == "" then newpath = newpath .. basename(oldpath) end + local old = io.open(oldpath, "r") + local new = io.open(newpath, "w") + new:write(old:read("*a")) + new:close() + old:close() + return is_file(newpath) +end + -- Returns the contents of a file as a string function read_file ( path ) local file = io.open(path or "") -- cgit v1.2.3