summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/data-tar.lua
blob: b2416330ff2cbaee8e3d314bee0731608cb2b887 (plain)
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
if not modules then modules = { } end modules ['data-tar'] = {
    version   = 1.001,
    comment   = "companion to luat-lib.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local format, find, match = string.format, string.find, string.match

local trace_locating = false  trackers.register("resolvers.locating", function(v) trace_locating = v end)

local report_tar = logs.reporter("resolvers","tar")

-- We use a url syntax for accessing the tar file itself and file in it:
--
--   tar:///oeps.tar?name=bla/bla.tex
--   tar:///oeps.tar?tree=tex/texmf-local

local resolvers    = resolvers
local findfile     = resolvers.findfile
local registerfile = resolvers.registerfile
local splitmethod  = resolvers.splitmethod
local starttiming  = resolvers.starttiming
local stoptiming   = resolvers.stoptiming

local urlquery     = url.query

--- hm, zip sits in the global namespace, but tar doesn't

local tar          = utilities.tar or { }
utilities.tar      = tar -- not needed

local archives     = tar.archives or { }
tar.archives       = archives

local registeredfiles = tar.registeredfiles or { }
tar.registeredfiles   = registeredfiles

-- foo.tar.xz : done
-- foo.tar.gz : todo
-- foo.tar    : done

local hashtar, fetchtar, wipetar  do

    local suffix = file.suffix -- hassuffix .. no need to split

    local tarfiles       = utilities.tar.file
    local tarstrings     = utilities.tar.string

    local hashtarfile    = tar.files.hash
    local fetchtarfile   = tar.files.fetch

    local hashtarstring  = tar.strings.hash
    local fetchtarstring = tar.strings.fetch

    local register       = resolvers.decompressors.register

    hashtar = function(archive,strip)
        local a = register(archive)
        if a then
            return hashtarstring(a,archive)
        else
            return hashtarfile(archive,archive)
        end
    end

    fetchtar = function(archive,filename,list)
        local a = register(archive)
        if a then
            return fetchtarstring(a,filename,list)
        else
            return fetchtarfile(archive,filename,list)
        end
    end

    wipetar = resolvers.decompressors.unregister

end

local function validfile(archive,name)
    return archive[name]
end

local function openarchive(name)
    if not name or name == "" then
        return nil
    else
        local arch = archives[name]
        if not arch then
           local full = findfile(name) or ""
           arch = full ~= "" and hashtar(full,name) or false
           archives[name] = arch
        end
        return arch
    end
end

local function closearchive(name)
    if not name or (name == "" and archives[name]) then
        archives[name] = nil
        wipetar(name)
    end
end

tar.openarchive  = openarchive
tar.closearchive = closearchive

function resolvers.locators.tar(specification)
    local archive = specification.filename
    local tarfile = archive and archive ~= "" and openarchive(archive)
    if trace_locating then
        if tarfile then
            report_tar("locator: archive %a found",archive)
        else
            report_tar("locator: archive %a not found",archive)
        end
    end
end

function resolvers.concatinators.tar(tarfile,path,name) -- ok ?
    if not path or path == "" then
        return format('%s?name=%s',tarfile,name)
    else
        return format('%s?name=%s/%s',tarfile,path,name)
    end
end

local finders  = resolvers.finders
local notfound = finders.notfound

function finders.tar(specification)
    local original = specification.original
    local archive  = specification.filename
    if archive then
        local query     = urlquery(specification.query)
        local queryname = query.name
        if queryname then
            local tfile = openarchive(archive)
            if tfile then
                if trace_locating then
                    report_tar("finder: archive %a found",archive)
                end
                if validfile(tfile,queryname) then
                    if trace_locating then
                        report_tar("finder: file %a found",queryname)
                    end
                    return specification.original
                elseif trace_locating then
                    report_tar("finder: file %a not found",queryname)
                end
            elseif trace_locating then
                report_tar("finder: unknown archive %a",archive)
            end
        end
    end
    if trace_locating then
        report_tar("finder: %a not found",original)
    end
    return notfound()
end

local openers    = resolvers.openers
local notfound   = openers.notfound
local textopener = openers.helpers.textopener

function openers.tar(specification)
    local original = specification.original
    local archive  = specification.filename
    if archive then
        local query     = urlquery(specification.query)
        local queryname = query.name
        if queryname then
            local tfile = openarchive(archive)
            if tfile then
                if trace_locating then
                    report_tar("opener; archive %a opened",archive)
                end
                local data = fetchtar(archive,queryname,tfile)
                if data then
                    if trace_locating then
                        report_tar("opener: file %a found",queryname)
                    end
                    return textopener('tar',original,data) -- a string handle
                elseif trace_locating then
                    report_tar("opener: file %a not found",queryname)
                end
            elseif trace_locating then
                report_tar("opener: unknown archive %a",archive)
            end
        end
    end
    if trace_locating then
        report_tar("opener: %a not found",original)
    end
    return notfound()
end

loaders  = resolvers.loaders
local notfound = loaders.notfound

function loaders.tar(specification)
    local original = specification.original
    local archive  = specification.filename
    if archive then
        local query     = urlquery(specification.query)
        local queryname = query.name
        if queryname then
            local tfile = openarchive(archive)
            if tfile then
                if trace_locating then
                    report_tar("loader: archive %a opened",archive)
                end
                local data = fetchtar(archive,queryname,tfile)
                if data then
                    if trace_locating then
                        report_tar("loader; file %a loaded",original)
                    end
                    return true, data, #data
                elseif trace_locating then
                    report_tar("loader: file %a not found",queryname)
                end
            elseif trace_locating then
                report_tar("loader; unknown archive %a",archive)
            end
        end
    end
    if trace_locating then
        report_tar("loader: %a not found",original)
    end
    return notfound()
end