diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2011-12-09 23:23:53 -0200 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2011-12-09 23:23:53 -0200 |
commit | e74f312e318868dcb931aa5b6c911af0ab830abc (patch) | |
tree | 125e4a518c0335140e6ce3ede42635e11a5b1bf8 | |
parent | 5320579bfe756704fc1743d0d3e43cc5037a0edd (diff) | |
download | luarocks-e74f312e318868dcb931aa5b6c911af0ab830abc.tar.gz luarocks-e74f312e318868dcb931aa5b6c911af0ab830abc.tar.bz2 luarocks-e74f312e318868dcb931aa5b6c911af0ab830abc.zip |
Add extra check for integrity of manifest file and document the internal workings of luarocks.loader module. Closes #52.
-rw-r--r-- | src/luarocks/loader.lua | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/src/luarocks/loader.lua b/src/luarocks/loader.lua index c3cba55a..cab7762e 100644 --- a/src/luarocks/loader.lua +++ b/src/luarocks/loader.lua | |||
@@ -88,8 +88,20 @@ local function sort_versions(a,b) | |||
88 | return a.version > b.version | 88 | return a.version > b.version |
89 | end | 89 | end |
90 | 90 | ||
91 | --- Request module to be loaded through other loaders, | ||
92 | -- once the proper name of the module has been determined. | ||
93 | -- For example, in case the module "socket.core" has been requested | ||
94 | -- to the LuaRocks loader and it determined based on context that | ||
95 | -- the version 2.0.2 needs to be loaded and it is not the current | ||
96 | -- version, the module requested for the other loaders will be | ||
97 | -- "socket.core_2_0_2". | ||
98 | -- @param module The module name requested by the user, such as "socket.core" | ||
99 | -- @param name The rock name, such as "luasocket" | ||
100 | -- @param version The rock version, such as "2.0.2-1" | ||
101 | -- @param module_name The actual module name, such as "socket.core" or "socket.core_2_0_2". | ||
102 | -- @return table or (nil, string): The module table as returned by some other loader, | ||
103 | -- or nil followed by an error message if no other loader managed to load the module. | ||
91 | local function call_other_loaders(module, name, version, module_name) | 104 | local function call_other_loaders(module, name, version, module_name) |
92 | |||
93 | for i, loader in pairs(package.loaders) do | 105 | for i, loader in pairs(package.loaders) do |
94 | if loader ~= luarocks_loader then | 106 | if loader ~= luarocks_loader then |
95 | local results = { loader(module_name) } | 107 | local results = { loader(module_name) } |
@@ -101,6 +113,17 @@ local function call_other_loaders(module, name, version, module_name) | |||
101 | return nil, "Failed loading module "..module.." in LuaRocks rock "..name.." "..version | 113 | return nil, "Failed loading module "..module.." in LuaRocks rock "..name.." "..version |
102 | end | 114 | end |
103 | 115 | ||
116 | --- Search for a module in the rocks trees | ||
117 | -- @param module string: module name (eg. "socket.core") | ||
118 | -- @param filter_module_name function(string, string, string, string, number): | ||
119 | -- a function that takes the module name (eg "socket.core"), the rock name | ||
120 | -- (eg "luasocket"), the version (eg "2.0.2-1"), the path of the rocks tree | ||
121 | -- (eg "/usr/local"), and the numeric index of the matching entry, so the | ||
122 | -- filter function can know if the matching module was the first entry or not. | ||
123 | -- @return string, string, string: name of the rock containing the module | ||
124 | -- (eg. "luasocket"), version of the rock (eg. "2.0.2-1"), | ||
125 | -- name of the module (eg. "socket.core", or "socket.core_2_0_2" if file is | ||
126 | -- stored versioned). | ||
104 | local function select_module(module, filter_module_name) | 127 | local function select_module(module, filter_module_name) |
105 | --assert(type(module) == "string") | 128 | --assert(type(module) == "string") |
106 | --assert(type(filter_module_name) == "function") | 129 | --assert(type(filter_module_name) == "function") |
@@ -116,6 +139,9 @@ local function select_module(module, filter_module_name) | |||
116 | for i, entry in ipairs(entries) do | 139 | for i, entry in ipairs(entries) do |
117 | local name, version = entry:match("^([^/]*)/(.*)$") | 140 | local name, version = entry:match("^([^/]*)/(.*)$") |
118 | local module_name = tree.manifest.repository[name][version][1].modules[module] | 141 | local module_name = tree.manifest.repository[name][version][1].modules[module] |
142 | if not type(module_name) == "string" then | ||
143 | error("Invalid format in manifest file (invalid data for "..tostring(name).." "..tostring(version)..")") | ||
144 | end | ||
119 | module_name = filter_module_name(module_name, name, version, tree.tree, i) | 145 | module_name = filter_module_name(module_name, name, version, tree.tree, i) |
120 | if context[name] == version then | 146 | if context[name] == version then |
121 | return name, version, module_name | 147 | return name, version, module_name |
@@ -133,6 +159,12 @@ local function select_module(module, filter_module_name) | |||
133 | end | 159 | end |
134 | end | 160 | end |
135 | 161 | ||
162 | --- Search for a module | ||
163 | -- @param module string: module name (eg. "socket.core") | ||
164 | -- @return string, string, string: name of the rock containing the module | ||
165 | -- (eg. "luasocket"), version of the rock (eg. "2.0.2-1"), | ||
166 | -- name of the module (eg. "socket.core", or "socket.core_2_0_2" if file is | ||
167 | -- stored versioned). | ||
136 | local function pick_module(module) | 168 | local function pick_module(module) |
137 | return | 169 | return |
138 | select_module(module, function(module_name, name, version, tree, i) | 170 | select_module(module, function(module_name, name, version, tree, i) |
@@ -144,6 +176,11 @@ local function pick_module(module) | |||
144 | end) | 176 | end) |
145 | end | 177 | end |
146 | 178 | ||
179 | --- Return the pathname of the file that would be loaded for a module. | ||
180 | -- @param module string: module name (eg. "socket.core") | ||
181 | -- @return string, string, string: name of the rock containing the module | ||
182 | -- (eg. "luasocket"), version of the rock (eg. "2.0.2-1"), | ||
183 | -- filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so") | ||
147 | function which(module) | 184 | function which(module) |
148 | local name, version, module_name = | 185 | local name, version, module_name = |
149 | select_module(module, function(module_name, name, version, tree, i) | 186 | select_module(module, function(module_name, name, version, tree, i) |
@@ -172,7 +209,6 @@ end | |||
172 | -- @return table: The module table (typically), like in plain | 209 | -- @return table: The module table (typically), like in plain |
173 | -- require(). See <a href="http://www.lua.org/manual/5.1/manual.html#pdf-require">require()</a> | 210 | -- require(). See <a href="http://www.lua.org/manual/5.1/manual.html#pdf-require">require()</a> |
174 | -- in the Lua reference manual for details. | 211 | -- in the Lua reference manual for details. |
175 | |||
176 | function luarocks_loader(module) | 212 | function luarocks_loader(module) |
177 | local name, version, module_name = pick_module(module) | 213 | local name, version, module_name = pick_module(module) |
178 | if not name then | 214 | if not name then |