summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2009-12-28 11:09:51 +0000
committerTed Trask <ttrask01@yahoo.com>2009-12-28 11:09:51 +0000
commit37679c1bba4cb10af9d7b8096c4c54aed6c6edbe (patch)
tree0582eacafe792d23497487aa87e8f6a9ec3a3a13 /lib
parent2678d43e1f155b2b692c729bb0470967072a8d58 (diff)
downloadacf-core-37679c1bba4cb10af9d7b8096c4c54aed6c6edbe.tar.bz2
acf-core-37679c1bba4cb10af9d7b8096c4c54aed6c6edbe.tar.xz
Cleaned up fs library use of posix.basename / dirname.
Diffstat (limited to 'lib')
-rw-r--r--lib/fs.lua26
1 files changed, 15 insertions, 11 deletions
diff --git a/lib/fs.lua b/lib/fs.lua
index 2f45e21..84cc6d5 100644
--- a/lib/fs.lua
+++ b/lib/fs.lua
@@ -55,20 +55,22 @@ end
-- Creates a blank file (and the directory if necessary)
function create_file ( path )
path = path or ""
- if posix.dirname(path) and not posix.stat(posix.dirname(path)) then create_directory(posix.dirname(path)) end
+ if not posix.stat(posix.dirname(path)) then create_directory(posix.dirname(path)) end
local f = io.open(path, "w")
if f then f:close() end
return is_file(path)
end
-- Copies a file to a directory or new filename (creating the directory if necessary)
--- fails if new file is already a directory
+-- fails if new file is already a directory (this is different than cp function)
+-- if newpath ends in "/", will treat as a directory
function copy_file(oldpath, newpath)
- if not is_file(oldpath) or not newpath or newpath == "" or (posix.basename(newpath) ~= "." and is_dir(newpath)) or (posix.basename(newpath) == "." and is_dir(newpath .. posix.basename(oldpath))) then
+ local use_dir = string.find(newpath or "", "/%s*$")
+ if not is_file(oldpath) or not newpath or newpath == "" or (not use_dir and is_dir(newpath)) or (use_dir and is_dir(newpath .. posix.basename(oldpath))) then
return false
end
- if posix.dirname(newpath) and not posix.stat(posix.dirname(newpath)) then create_directory(posix.dirname(newpath)) end
- if posix.basename(newpath) == "." then newpath = newpath .. posix.basename(oldpath) end
+ if use_dir then newpath = newpath .. posix.basename(oldpath) end
+ if not posix.stat(posix.dirname(newpath)) then create_directory(posix.dirname(newpath)) end
local old = io.open(oldpath, "r")
local new = io.open(newpath, "w")
new:write(old:read("*a"))
@@ -78,13 +80,15 @@ function copy_file(oldpath, newpath)
end
-- Moves a file to a directory or new filename (creating the directory if necessary)
--- fails if new file is already a directory
+-- fails if new file is already a directory (this is different than mv function)
+-- if newpath ends in "/", will treat as a directory
function move_file(oldpath, newpath)
- if not is_file(oldpath) or not newpath or newpath == "" or (posix.basename(newpath) ~= "." and is_dir(newpath)) or (posix.basename(newpath) == "." and is_dir(newpath .. posix.basename(oldpath))) then
+ local use_dir = string.find(newpath or "", "/%s*$")
+ if not is_file(oldpath) or not newpath or newpath == "" or (not use_dir and is_dir(newpath)) or (use_dir and is_dir(newpath .. posix.basename(oldpath))) then
return false
end
- if posix.dirname(newpath) and not posix.stat(posix.dirname(newpath)) then create_directory(posix.dirname(newpath)) end
- if posix.basename(newpath) == "." then newpath = newpath .. posix.basename(oldpath) end
+ if use_dir then newpath = newpath .. posix.basename(oldpath) end
+ if not posix.stat(posix.dirname(newpath)) then create_directory(posix.dirname(newpath)) end
local status, errstr, errno = os.rename(oldpath, newpath)
-- errno 18 means Invalid cross-device link
if status or errno ~= 18 then
@@ -130,7 +134,7 @@ end
-- write a string to a file, will replace file contents
function write_file ( path, str )
path = path or ""
- if posix.dirname(path) and not posix.stat(posix.dirname(path)) then create_directory(posix.dirname(path)) end
+ if not posix.stat(posix.dirname(path)) then create_directory(posix.dirname(path)) end
local file = io.open(path, "w")
--append a newline char to EOF
str = string.gsub(str or "", "\n*$", "\n")
@@ -144,7 +148,7 @@ end
-- fs.write_line_file ("filename", "Line1 \nLines2 \nLines3")
function write_line_file ( path, str )
path = path or ""
- if posix.dirname(path) and not posix.stat(posix.dirname(path)) then create_directory(posix.dirname(path)) end
+ if not posix.stat(posix.dirname(path)) then create_directory(posix.dirname(path)) end
local file = io.open(path)
if ( file) then
local c = file:read("*a") or ""