diff options
author | Ted Trask <ttrask01@yahoo.com> | 2009-05-15 09:51:55 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2009-05-15 09:51:55 +0000 |
commit | f21a44e55314948416f4bd59f756bec68f0ae2d5 (patch) | |
tree | 4106f7f878d820499368718b3e7fded6a55d947d | |
parent | 1f42724eca5fea69afce084da898d0d1466599fc (diff) | |
download | acf-core-f21a44e55314948416f4bd59f756bec68f0ae2d5.tar.bz2 acf-core-f21a44e55314948416f4bd59f756bec68f0ae2d5.tar.xz |
Added fs.move_file function because os.rename only works on same device.
-rw-r--r-- | lib/fs.lua | 22 |
1 files changed, 22 insertions, 0 deletions
@@ -93,6 +93,28 @@ function copy_file(oldpath, newpath) return is_file(newpath) end +-- Moves a file to a directory or new filename (creating the directory if necessary) +-- fails if new file already exists +function move_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 status, errstr, errno = os.rename(oldpath, newpath) + -- errno 18 means Invalid cross-device link + if status or errno ~= 18 then + -- successful move or failure due to something else + return (status ~= nil), errstr, errno + else + status = copy_file(oldpath, newpath) + if status then + os.remove(oldpath) + end + return status + end +end + -- Returns the contents of a file as a string function read_file ( path ) local file = io.open(path or "") |