2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
16 local io = require "io"
17 local fs = require "nixio.fs"
18 local util = require "luci.util"
19 local nixio = require "nixio"
20 local debug = require "debug"
21 local string = require "string"
22 local package = require "package"
24 local type, loadfile = type, loadfile
29 function cache_ondemand(...)
30 if debug.getinfo(1, 'S').source ~= "=?" then
35 function cache_enable(cachepath, mode)
36 cachepath = cachepath or "/tmp/luci-modulecache"
37 mode = mode or "r--r--r--"
39 local loader = package.loaders[2]
40 local uid = nixio.getuid()
42 if not fs.stat(cachepath) then
46 local function _encode_filename(name)
49 encoded = encoded .. ("%2X" % string.byte(name, i))
54 local function _load_sane(file)
55 local stat = fs.stat(file)
56 if stat and stat.uid == uid and stat.modestr == mode then
61 local function _write_sane(file, func)
62 if nixio.getuid() == uid then
63 local fp = io.open(file, "w")
65 fp:write(util.get_bytecode(func))
72 package.loaders[2] = function(mod)
73 local encoded = cachepath .. "/" .. _encode_filename(mod)
74 local modcons = _load_sane(encoded)
82 if type(modcons) == "function" then
83 _write_sane(encoded, modcons)