]]--
---- LuCI filesystem library.
-module("luci.fs", package.seeall)
+local io = require "io"
+local os = require "os"
+local ltn12 = require "luci.ltn12"
+local posix = require "posix"
+
+local type = type
-require("posix")
+--- LuCI filesystem library.
+module "luci.fs"
--- Test for file access permission on given path.
-- @class function
return true
end
+--- Copies a file.
+-- @param source Source file
+-- @param dest Destination
+-- @return Boolean containing true on success or nil on error
+function copy(source, dest)
+ return ltn12.pump.all(
+ ltn12.source.file(io.open(source)),
+ ltn12.sink.file(io.open(dest, "w"))
+ )
+end
+
+--- Renames a file.
+-- @param source Source file
+-- @param dest Destination
+-- @return Boolean containing true on success or nil on error
+function rename(source, dest)
+ local stat, err, code = os.rename(source, dest)
+ if code == 18 then
+ stat, err, code = copy(source, dest)
+ if stat then
+ stat, err, code = unlink(source)
+ end
+ end
+ return stat, err, code
+end
+
--- Get the last modification time of given file path in Unix epoch format.
-- @param path String containing the path of the file or directory to read
-- @return Number containing the epoch time or nil on error