1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
3 -- Licensed to the public under the Apache License 2.0.
5 local io = require "io"
6 local fs = require "nixio.fs"
7 local util = require "luci.util"
8 local nixio = require "nixio"
9 local debug = require "debug"
10 local string = require "string"
11 local package = require "package"
13 local type, loadfile = type, loadfile
18 function cache_ondemand(...)
19 if debug.getinfo(1, 'S').source ~= "=?" then
24 function cache_enable(cachepath, mode)
25 cachepath = cachepath or "/tmp/luci-modulecache"
26 mode = mode or "r--r--r--"
28 local loader = package.loaders[2]
29 local uid = nixio.getuid()
31 if not fs.stat(cachepath) then
35 local function _encode_filename(name)
38 encoded = encoded .. ("%2X" % string.byte(name, i))
43 local function _load_sane(file)
44 local stat = fs.stat(file)
45 if stat and stat.uid == uid and stat.modestr == mode then
50 local function _write_sane(file, func)
51 if nixio.getuid() == uid then
52 local fp = io.open(file, "w")
54 fp:write(util.get_bytecode(func))
61 package.loaders[2] = function(mod)
62 local encoded = cachepath .. "/" .. _encode_filename(mod)
63 local modcons = _load_sane(encoded)
71 if type(modcons) == "function" then
72 _write_sane(encoded, modcons)