~aleteoryx/muditaos

ref: 6cc2bc9df4ed70d5548f7f4296be4062553b5c7a muditaos/scripts/lua/share/ltar.lua -rw-r--r-- 6.2 KiB
6cc2bc9d — Dawid Wojtas [BH-1830] Fix eink crash while refreshing 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
--- LUA support for TAR packages
-- @module ltar
local microtar = require("lmicrotar")
local lfs = require("lfs")

local tar = {}

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

local function get_filename(file)
    return file:match("^.+/(.+)$")
end

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

local function 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 = mkdirp(b)
        if not r then
            return r, m
        end
    end
    return lfs.mkdir(p)
end

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 write_tarfile_chunks(handle, fd)
    local size = 1024 * 512
    collectgarbage()
    while true do
        local block = fd:read(size)
        if not block then
            break
        end
        handle:write_data(block, block:len())
    end
end

local function read_tarfile_chunks(handle, fd, total_size)
    local block_size = 1024 * 512
    local to_read = {};

    collectgarbage()
    while total_size > 0 do
        if total_size > block_size then
            to_read = block_size
        else
            to_read = total_size
        end
        fd:write(handle:read_data(to_read))
        total_size = total_size - to_read
    end
end

--- Create empty tar file
-- @function create
-- @param path where to put newly created tar file
-- @return tar handle or nil
function tar.create(path)
    Handle = {
        handle = microtar.open(path, "w"),
        add_file = function(self, filename)
            local size = lfs.attributes(filename, "size")
            local fd = io.open(filename, "r")
            self.handle:write_file_header(filename, size)
            write_tarfile_chunks(self.handle, fd)
            fd:close()
        end,
        add_directory = function(self, path)
            self.handle:write_dir_header(path)
        end,
        close = function(self)
            self.handle:close()
        end
    }
    return Handle
end

--- Iterate over contents of tar file
-- @function iter_by_handle
-- @param handle tar handle create by @{create}
function tar.iter_by_handle(handle)
    return function()
        local f = handle:read_header()
        if f then
            handle:next()
            return f
        end
    end
end

--- Iterate over contents of tar file
-- @function iter_by_path
-- @param path path to tar file
function tar.iter_by_path(path)
    local handle = microtar.open(path)
    return function()
        local f = handle:read_header()
        if f then
            handle:next()
            return f
        end
    end
end

--- Pack contents of specified dir to tar file
-- @function create_from_path
-- @param path directory 
-- @param where where to save tar file 
function tar.create_from_path(path, where)
    tar.create_from_path_match(path, where, ".*")
end

--- Pack contents of specified dir to tar file using regex
-- @function create_from_path_regex
-- @param path directory 
-- @param where where to save tar file 
-- @param matcher regex expression
function tar.create_from_path_regex(path, where, matcher)
    local handle = microtar.open(where, "w")
    for filename, attr in dirtree(path) do
        local name = strip_from_prefix(path, filename)
        if name:match(matcher) then
            if attr.mode == "directory" then
                handle:write_dir_header(name)
            else
                handle:write_file_header(name, attr.size)
                local fd = io.open(filename, "r")
                write_tarfile_chunks(handle, fd)
                fd:close()
            end
        end
    end
    handle:close()
end

--- Unpack tar file to specified directory
-- @function unpack
-- @param path directory 
-- @param where where to store unpacked files 
function tar.unpack(path, where)
    local handle = microtar.open(path)
    local header = handle:read_header()
    while header do
        if header.type == microtar.TDIR then
            mkdirp(where .. "/" .. header.name)
        elseif header.type == microtar.TREG then
            local fd = io.open(where .. "/" .. header.name, "w")
            read_tarfile_chunks(handle, fd, header.size)
            fd:close()
        end
        handle:next()
        header = handle:read_header()
    end
    handle:close()
end

--- Append directory to already existing tar file
-- @function append
-- @param path directory 
-- @param where location of already existing tar file 
function tar.append(path, where)
    local handle = microtar.open(where, "a")
    for filename, attr in dirtree(path) do
        if attr.mode == "directory" then
            handle:write_dir_header(filename)
        else
            handle:write_file_header(filename, attr.size)
            local fd = io.open(filename, "r")
            write_tarfile_chunks(handle, fd)
            fd:close()
        end
    end
    handle:close()
end

--- Append file to already existing tar file
-- @function append_file
-- @param path file path 
-- @param where location of already existing tar file 
function tar.append_file(path, where)
    local filename = get_filename(path)
    local size = lfs.attributes(path, 'size')
    local handle = microtar.open(where, "a")
    handle:write_file_header(filename, size)
    local fd = io.open(path, "r")
    write_tarfile_chunks(handle, fd)
    fd:close()
    handle:close()
end

function tar.find(tar_path, what)
    local handle = microtar.open(tar_path)
    local stats = handle:find(what)
    if stats ~= nil then
        return handle:read_data()
    end
    return nil
end

return tar