summaryrefslogtreecommitdiffstats
path: root/lib/fs.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fs.lua')
-rw-r--r--lib/fs.lua23
1 files 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 "")