Remove luci.cutil: does not affect performance
[project/luci.git] / libs / core / luasrc / fs.lua
index 0fc7193..abea5b5 100644 (file)
@@ -24,10 +24,15 @@ limitations under the License.
 
 ]]--
 
---- 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
@@ -89,6 +94,32 @@ function writefile(filename, data)
        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
@@ -192,14 +223,16 @@ stat = posix.stat
 -- @return             Number containing the os specific errno on error
 chmod = posix.chmod
 
---- Create a hardlink from given file to specified target file path.
--- @class              function
--- @name               link
--- @param path1        String containing the source path of a file to hardlink
--- @param path2        String containing the destination path for the link
--- @return             Number with the return code, 0 on sucess or nil on error
--- @return             String containing the error description on error
--- @return             Number containing the os specific errno on error
+--- Create a hard- or symlink from given file (or directory) to specified target
+-- file (or directory) path.
+-- @class                      function
+-- @name                       link
+-- @param path1                String containing the source path to link
+-- @param path2                String containing the destination path for the link
+-- @param symlink      Boolean indicating wheather to create a symlink (optional)
+-- @return                     Number with the return code, 0 on sucess or nil on error
+-- @return                     String containing the error description on error
+-- @return                     Number containing the os specific errno on error
 link = posix.link
 
 --- Remove the given file.
@@ -210,3 +243,12 @@ link = posix.link
 -- @return             String containing the error description on error
 -- @return             Number containing the os specific errno on error
 unlink = posix.unlink
+
+--- Retrieve target of given symlink.
+-- @class              function
+-- @name               readlink
+-- @param path String containing the path of the symlink to read
+-- @return             String containing the link target or nil on error
+-- @return             String containing the error description on error
+-- @return             Number containing the os specific errno on error
+readlink = posix.readlink