* General code cleanup
[project/luci.git] / core / src / ffluci / fs.lua
1 --[[
2 FFLuCI - Filesystem tools
3
4 Description:
5 A module offering often needed filesystem manipulation functions
6
7 FileId:
8 $Id$
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
16
17 http://www.apache.org/licenses/LICENSE-2.0
18
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24
25 ]]--
26
27 module("ffluci.fs", package.seeall)
28
29 require("posix")
30
31 -- Glob
32 function glob(pattern)
33 return posix.glob(pattern)
34 end
35
36 -- Checks whether a file exists
37 function isfile(filename)
38 local fp = io.open(path, "r")
39 if file then file:close() end
40 return file ~= nil
41 end
42
43 -- Returns the content of file
44 function readfile(filename)
45 local fp, err = io.open(filename)
46
47 if fp == nil then
48 return nil, err
49 end
50
51 local data = fp:read("*a")
52 fp:close()
53 return data
54 end
55
56 -- Returns the content of file as array of lines
57 function readfilel(filename)
58 local fp, err = io.open(filename)
59 local line = ""
60 local data = {}
61
62 if fp == nil then
63 return nil, err
64 end
65
66 while true do
67 line = fp:read()
68 if (line == nil) then break end
69 table.insert(data, line)
70 end
71
72 fp:close()
73 return data
74 end
75
76 -- Writes given data to a file
77 function writefile(filename, data)
78 local fp, err = io.open(filename, "w")
79
80 if fp == nil then
81 return nil, err
82 end
83
84 fp:write(data)
85 fp:close()
86
87 return true
88 end
89
90 -- Returns the file modification date/time of "path"
91 function mtime(path)
92 return posix.stat(path, "mtime")
93 end
94
95 -- basename wrapper
96 basename = posix.basename
97
98 -- dirname wrapper
99 dirname = posix.dirname
100
101 -- dir wrapper
102 function dir(path)
103 local dir = {}
104 for node in posix.files(path) do
105 table.insert(dir, 1, node)
106 end
107 return dir
108 end
109
110 -- Alias for lfs.mkdir
111 mkdir = posix.mkdir