~aleteoryx/muditaos

ref: 7dc758bbc3b0fc095fbac01b33336435c2489332 muditaos/scripts/lua/share/helpers.lua -rw-r--r-- 7.5 KiB
7dc758bb — Lukasz Mastalerz [CP-1968] Disconnecting the device during file upload causes problems with the USB 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
-- Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
-- For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
--- helper scripts
-- @module helpers
local lfs = require('lfs')
local json = require('lunajson')

local helpers = {}

local function dirtree(dir)
    assert(dir and dir ~= "", "directory parameter is missing or empty")
    if string.sub(dir, -1) == "/" then
        dir = string.sub(dir, 1, -2)
    end

    local function yieldtree(dir)
        for entry in lfs.dir(dir) do
            if entry ~= "." and entry ~= ".." then
                entry = dir .. "/" .. entry
                local attr = lfs.attributes(entry)
                coroutine.yield(entry, attr)
                if attr.mode == "directory" then
                    yieldtree(entry)
                end
            end
        end
    end

    return coroutine.wrap(function()
        yieldtree(dir)
    end)
end

local function ends_with(str, ending)
    return ending == "" or str:sub(-#ending) == ending
end

local function starts_with(str, start)
    return str:sub(1, #start) == start
end

local function build_path(prefix, name)
    if not ends_with(prefix, '/') then
        prefix = prefix .. '/'
    end
    if starts_with(name, '/') then
        name = name:sub(2, -1)
    end
    return prefix .. name
end

local function strip_from_prefix(prefix, path)
    local name = path:gsub(prefix, "")
    name = name:sub(2, -1)
    return name
end

local function basedir(p)
    return p:gsub('[^\\/]+[\\/]?$', '')
end

--- Copy file
-- @function copy_file
-- @param filename_in input file
-- @param filename_out output file
function helpers.copy_file(filename_in, filename_out)
    local size = 1024 * 512
    local size_in = lfs.attributes(filename_in, "size")
    local fd_in = assert(io.open(filename_in, "r"))
    local fd_out = assert(io.open(filename_out, "w"))
    collectgarbage()
    while true do
        local block = fd_in:read(size)
        if not block then
            break
        end
        assert(fd_out:write(block))
    end
    fd_in:close()
    fd_out:close()
end

--- Remove directory and its contents
-- @function rmdir
-- @param dir directory to remove
function helpers.rmdir(dir)
    for file in lfs.dir(dir) do
        local file_path = dir .. "/" .. file
        if file ~= "." and file ~= ".." then
            if lfs.attributes(file_path, "mode") == "file" then
                assert(os.remove(file_path))
            elseif lfs.attributes(file_path, "mode") == "directory" then
                helpers.rmdir(file_path)
            end
        end
    end
    lfs.rmdir(dir)
end

--- Remove directory content
-- @function rmdir_content
-- @param dir directory path
function helpers.rmdir_content(dir)
    for file in lfs.dir(dir) do
        local file_path = dir .. "/" .. file
        if file ~= "." and file ~= ".." then
            if lfs.attributes(file_path, "mode") == "file" then
                assert(os.remove(file_path))
            elseif lfs.attributes(file_path, "mode") == "directory" then
                helpers.rmdir(file_path)
            end
        end
    end
end

--- Remove all files from directory without touching internal directories
-- @function rm_files_from_dir
-- @param dir directory to remove files from
function helpers.rm_files_from_dir(dir)
    for file in lfs.dir(dir) do
        local file_path = dir .. "/" .. file
        if file ~= "." and file ~= ".." then
            if lfs.attributes(file_path, "mode") == "file" then
                assert(os.remove(file_path))
            end
        end
    end
end

--- Copy directory recursively
-- @function copy_dir
-- @param from source directory
-- @param where target directory
function helpers.copy_dir(from, where)
    for filename, attr in dirtree(from) do
        local name = filename:gsub(from, "")
        if attr.mode == "directory" then
            assert(lfs.mkdir(build_path(where, name)))
        else
            helpers.copy_file(filename, build_path(where, name))
        end
    end
end

--- Move directory recursively
-- @function move_dir
-- @param from source directory
-- @param where target directory
function helpers.move_dir(from, where)
    for filename, attr in dirtree(from) do
        local name = filename:gsub(from, "")
        if attr.mode == "directory" then
            assert(lfs.mkdir(build_path(where, name)))
        else
            assert(os.rename(build_path(from, name), build_path(where, name)))
        end
    end
end

--- Copy directory recursively using regex filter
-- @function copy_dir_filtered
-- @param from source directory
-- @param where target directory
-- @param matcher regex expression
function helpers.copy_dir_filtered(from, where, filter)
    for filename, attr in dirtree(from) do
        local name = strip_from_prefix(from, filename)
        if name:match(filter) then
            if attr.mode == "directory" then
                assert(lfs.mkdir(build_path(where, name)))
            else
                helpers.copy_file(filename, build_path(where, name))
            end
        end
    end
end

--- Get the size of specified directory using regex filter
-- @function dir_size_filtered
-- @param path directory path
-- @param matcher regex expression
-- @return total size of directory in bytes
function helpers.dir_size_filtered(path, filter)
    local total_size = 0
    for filename, attr in dirtree(path) do
        local name = strip_from_prefix(path, filename)
        if name:match(filter) then
            total_size = total_size + attr.size
        end
    end
    return total_size
end

--- Get the size of specified directory
-- @function dir_size
-- @param path directory path
-- @return total size of directory in bytes
function helpers.dir_size(path)
    return helpers.dir_size_regex('.*')
end

--- Check if specified dir/file exists
-- @function exists
-- @param path directory or file path
-- @return true or false
function helpers.exists(path)
    local ret = lfs.attributes(path)
    return ret ~= nil
end

--- Read the whole file at once 
-- @function read_whole_file
-- @param file file path
-- @return file contents
function helpers.read_whole_file(file)
    local f = assert(io.open(file, "rb"))
    local content = assert(f:read("*a"))
    f:close()
    return content
end

--- Extract filename from filename path
-- @function get_filename
-- @param file file path
-- @return filename
function helpers.get_filename(file)
    return file:match("^.+/(.+)$")
end

--- Extract file extension from filename
-- @function get_file_extension
-- @param file file path
-- @return file extension
function helpers.get_file_extension(file)
    return file:match("^.+(%..+)$")
end

--- Strips file name from its extension
-- @function strip_from_extension
-- @param file file path
-- @return file name without extension
function helpers.strip_from_extension(file)
    return file:match("^(.*)%..*$")
end

--- Create directory and all required subdirectories
-- @function mkdirp
-- @param file file path
function helpers.mkdirp(p)
    if lfs.attributes(p, 'mode') == 'directory' then
        return nil, 'already exists'
    end

    local b = basedir(p)
    if #b > 0 and lfs.attributes(b, 'mode') ~= 'directory' then
        local r, m = helpers.mkdirp(b)
        if not r then
            return r, m
        end
    end
    return lfs.mkdir(p)
end

--- Get OS version from the 'version.json'
-- @function get_os_version
-- @param file file path
function helpers.get_os_version(file)
    local contents = helpers.read_whole_file(file)
    local root = json.decode(contents)
    return root.os.version
end

return helpers