diff options
author | Ted Trask <ttrask01@yahoo.com> | 2009-01-26 21:54:28 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2009-01-26 21:54:28 +0000 |
commit | 4b52fd134e00f67be26b9ad2e668be4eebed9fd5 (patch) | |
tree | 445a2f1b787fac8e5f6b48c5e6b590993ca76684 /lib | |
parent | 80f200377d7ed85ae3f6a93348b386bb63b4aeed (diff) | |
download | acf-core-4b52fd134e00f67be26b9ad2e668be4eebed9fd5.tar.bz2 acf-core-4b52fd134e00f67be26b9ad2e668be4eebed9fd5.tar.xz |
More work to remove unnecessary popen calls.
git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@1696 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'lib')
-rw-r--r-- | lib/fs.lua | 23 |
1 files changed, 18 insertions, 5 deletions
@@ -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 "") |