|
|
|
|
|
|
|
|
local fetch = {}
|
|
|
|
|
|
local fs = require("luarocks.fs")
|
|
|
local dir = require("luarocks.dir")
|
|
|
local rockspecs = require("luarocks.rockspecs")
|
|
|
local signing = require("luarocks.signing")
|
|
|
local persist = require("luarocks.persist")
|
|
|
local util = require("luarocks.util")
|
|
|
local cfg = require("luarocks.core.cfg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function fetch.fetch_url(url, filename, cache)
|
|
|
assert(type(url) == "string")
|
|
|
assert(type(filename) == "string" or not filename)
|
|
|
|
|
|
local protocol, pathname = dir.split_url(url)
|
|
|
if protocol == "file" then
|
|
|
return fs.absolute_name(pathname)
|
|
|
elseif dir.is_basic_protocol(protocol) then
|
|
|
local ok, name, from_cache = fs.download(url, filename, cache)
|
|
|
if not ok then
|
|
|
return nil, "Failed downloading "..url..(filename and " - "..filename or ""), "network"
|
|
|
end
|
|
|
return name, nil, nil, from_cache
|
|
|
else
|
|
|
return nil, "Unsupported protocol "..protocol
|
|
|
end
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function fetch.fetch_url_at_temp_dir(url, tmpname, filename)
|
|
|
assert(type(url) == "string")
|
|
|
assert(type(tmpname) == "string")
|
|
|
assert(type(filename) == "string" or not filename)
|
|
|
filename = filename or dir.base_name(url)
|
|
|
|
|
|
local protocol, pathname = dir.split_url(url)
|
|
|
if protocol == "file" then
|
|
|
if fs.exists(pathname) then
|
|
|
return pathname, dir.dir_name(fs.absolute_name(pathname))
|
|
|
else
|
|
|
return nil, "File not found: "..pathname
|
|
|
end
|
|
|
else
|
|
|
local temp_dir, err = fs.make_temp_dir(tmpname)
|
|
|
if not temp_dir then
|
|
|
return nil, "Failed creating temporary directory "..tmpname..": "..err
|
|
|
end
|
|
|
util.schedule_function(fs.delete, temp_dir)
|
|
|
local ok, err = fs.change_dir(temp_dir)
|
|
|
if not ok then return nil, err end
|
|
|
local file, err, errcode = fetch.fetch_url(url, filename)
|
|
|
fs.pop_dir()
|
|
|
if not file then
|
|
|
return nil, "Error fetching file: "..err, errcode
|
|
|
end
|
|
|
return file, temp_dir
|
|
|
end
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function fetch.find_base_dir(file, temp_dir, src_url, src_dir)
|
|
|
local ok, err = fs.change_dir(temp_dir)
|
|
|
if not ok then return nil, err end
|
|
|
fs.unpack_archive(file)
|
|
|
local inferred_dir = src_dir or dir.deduce_base_dir(src_url)
|
|
|
local found_dir = nil
|
|
|
if fs.exists(inferred_dir) then
|
|
|
found_dir = inferred_dir
|
|
|
else
|
|
|
util.printerr("Directory "..inferred_dir.." not found")
|
|
|
local files = fs.list_dir()
|
|
|
if files then
|
|
|
table.sort(files)
|
|
|
for i,filename in ipairs(files) do
|
|
|
if fs.is_dir(filename) then
|
|
|
util.printerr("Found "..filename)
|
|
|
found_dir = filename
|
|
|
break
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
fs.pop_dir()
|
|
|
return inferred_dir, found_dir
|
|
|
end
|
|
|
|
|
|
local function fetch_and_verify_signature_for(url, filename, tmpdir)
|
|
|
local sig_url = signing.signature_url(url)
|
|
|
local sig_file, err, errcode = fetch.fetch_url_at_temp_dir(sig_url, tmpdir)
|
|
|
if not sig_file then
|
|
|
return nil, "Could not fetch signature file for verification: " .. err, errcode
|
|
|
end
|
|
|
|
|
|
local ok, err = signing.verify_signature(filename, sig_file)
|
|
|
if not ok then
|
|
|
return nil, "Failed signature verification: " .. err
|
|
|
end
|
|
|
|
|
|
return fs.absolute_name(sig_file)
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function fetch.fetch_and_unpack_rock(url, dest, verify)
|
|
|
assert(type(url) == "string")
|
|
|
assert(type(dest) == "string" or not dest)
|
|
|
|
|
|
local name = dir.base_name(url):match("(.*)%.[^.]*%.rock")
|
|
|
local tmpname = "luarocks-rock-" .. name
|
|
|
|
|
|
local rock_file, err, errcode = fetch.fetch_url_at_temp_dir(url, tmpname)
|
|
|
if not rock_file then
|
|
|
return nil, "Could not fetch rock file: " .. err, errcode
|
|
|
end
|
|
|
|
|
|
local sig_file
|
|
|
if verify then
|
|
|
sig_file, err = fetch_and_verify_signature_for(url, rock_file, tmpname)
|
|
|
if err then
|
|
|
return nil, err
|
|
|
end
|
|
|
end
|
|
|
|
|
|
rock_file = fs.absolute_name(rock_file)
|
|
|
|
|
|
local unpack_dir
|
|
|
if dest then
|
|
|
unpack_dir = dest
|
|
|
local ok, err = fs.make_dir(unpack_dir)
|
|
|
if not ok then
|
|
|
return nil, "Failed unpacking rock file: " .. err
|
|
|
end
|
|
|
else
|
|
|
unpack_dir, err = fs.make_temp_dir(name)
|
|
|
if not unpack_dir then
|
|
|
return nil, "Failed creating temporary dir: " .. err
|
|
|
end
|
|
|
end
|
|
|
if not dest then
|
|
|
util.schedule_function(fs.delete, unpack_dir)
|
|
|
end
|
|
|
local ok, err = fs.change_dir(unpack_dir)
|
|
|
if not ok then return nil, err end
|
|
|
ok, err = fs.unzip(rock_file)
|
|
|
if not ok then
|
|
|
return nil, "Failed unpacking rock file: " .. rock_file .. ": " .. err
|
|
|
end
|
|
|
if sig_file then
|
|
|
ok, err = fs.copy(sig_file, ".")
|
|
|
if not ok then
|
|
|
return nil, "Failed copying signature file"
|
|
|
end
|
|
|
end
|
|
|
fs.pop_dir()
|
|
|
return unpack_dir
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function fetch.load_local_rockspec(rel_filename, quick)
|
|
|
assert(type(rel_filename) == "string")
|
|
|
local abs_filename = fs.absolute_name(rel_filename)
|
|
|
|
|
|
local basename = dir.base_name(abs_filename)
|
|
|
if basename ~= "rockspec" then
|
|
|
if not basename:match("(.*)%-[^-]*%-[0-9]*") then
|
|
|
return nil, "Expected filename in format 'name-version-revision.rockspec'."
|
|
|
end
|
|
|
end
|
|
|
|
|
|
local tbl, err = persist.load_into_table(abs_filename)
|
|
|
if not tbl then
|
|
|
return nil, "Could not load rockspec file "..abs_filename.." ("..err..")"
|
|
|
end
|
|
|
|
|
|
local rockspec, err = rockspecs.from_persisted_table(abs_filename, tbl, err, quick)
|
|
|
if not rockspec then
|
|
|
return nil, abs_filename .. ": " .. err
|
|
|
end
|
|
|
|
|
|
local name_version = rockspec.package:lower() .. "-" .. rockspec.version
|
|
|
if basename ~= "rockspec" and basename ~= name_version .. ".rockspec" then
|
|
|
return nil, "Inconsistency between rockspec filename ("..basename..") and its contents ("..name_version..".rockspec)."
|
|
|
end
|
|
|
|
|
|
return rockspec
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function fetch.load_rockspec(url, location, verify)
|
|
|
assert(type(url) == "string")
|
|
|
|
|
|
local name
|
|
|
local basename = dir.base_name(url)
|
|
|
if basename == "rockspec" then
|
|
|
name = "rockspec"
|
|
|
else
|
|
|
name = basename:match("(.*)%.rockspec")
|
|
|
if not name then
|
|
|
return nil, "Filename '"..url.."' does not look like a rockspec."
|
|
|
end
|
|
|
end
|
|
|
|
|
|
local tmpname = "luarocks-rockspec-"..name
|
|
|
local filename, err, errcode
|
|
|
if location then
|
|
|
local ok, err = fs.change_dir(location)
|
|
|
if not ok then return nil, err end
|
|
|
filename, err = fetch.fetch_url(url)
|
|
|
fs.pop_dir()
|
|
|
else
|
|
|
filename, err, errcode = fetch.fetch_url_at_temp_dir(url, tmpname)
|
|
|
end
|
|
|
if not filename then
|
|
|
return nil, err, errcode
|
|
|
end
|
|
|
|
|
|
if verify then
|
|
|
local _, err = fetch_and_verify_signature_for(url, filename, tmpname)
|
|
|
if err then
|
|
|
return nil, err
|
|
|
end
|
|
|
end
|
|
|
|
|
|
return fetch.load_local_rockspec(filename)
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function fetch.get_sources(rockspec, extract, dest_dir)
|
|
|
assert(rockspec:type() == "rockspec")
|
|
|
assert(type(extract) == "boolean")
|
|
|
assert(type(dest_dir) == "string" or not dest_dir)
|
|
|
|
|
|
local url = rockspec.source.url
|
|
|
local name = rockspec.name.."-"..rockspec.version
|
|
|
local filename = rockspec.source.file
|
|
|
local source_file, store_dir
|
|
|
local ok, err, errcode
|
|
|
if dest_dir then
|
|
|
ok, err = fs.change_dir(dest_dir)
|
|
|
if not ok then return nil, err, "dest_dir" end
|
|
|
source_file, err, errcode = fetch.fetch_url(url, filename)
|
|
|
fs.pop_dir()
|
|
|
store_dir = dest_dir
|
|
|
else
|
|
|
source_file, store_dir, errcode = fetch.fetch_url_at_temp_dir(url, "luarocks-source-"..name, filename)
|
|
|
end
|
|
|
if not source_file then
|
|
|
return nil, err or store_dir, errcode
|
|
|
end
|
|
|
if rockspec.source.md5 then
|
|
|
if not fs.check_md5(source_file, rockspec.source.md5) then
|
|
|
return nil, "MD5 check for "..filename.." has failed.", "md5"
|
|
|
end
|
|
|
end
|
|
|
if extract then
|
|
|
local ok, err = fs.change_dir(store_dir)
|
|
|
if not ok then return nil, err end
|
|
|
ok, err = fs.unpack_archive(rockspec.source.file)
|
|
|
if not ok then return nil, err end
|
|
|
if not fs.exists(rockspec.source.dir) then
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local file_count, found_dir = 0
|
|
|
|
|
|
if not rockspec.source.dir_set and rockspec:format_is_at_least("3.0") then
|
|
|
for file in fs.dir() do
|
|
|
file_count = file_count + 1
|
|
|
if fs.is_dir(file) then
|
|
|
found_dir = file
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
|
|
|
if file_count == 1 and found_dir then
|
|
|
rockspec.source.dir = found_dir
|
|
|
else
|
|
|
return nil, "Directory "..rockspec.source.dir.." not found inside archive "..rockspec.source.file, "source.dir", source_file, store_dir
|
|
|
end
|
|
|
end
|
|
|
fs.pop_dir()
|
|
|
end
|
|
|
return source_file, store_dir
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function fetch.fetch_sources(rockspec, extract, dest_dir)
|
|
|
assert(rockspec:type() == "rockspec")
|
|
|
assert(type(extract) == "boolean")
|
|
|
assert(type(dest_dir) == "string" or not dest_dir)
|
|
|
|
|
|
local protocol = rockspec.source.protocol
|
|
|
local ok, proto
|
|
|
if dir.is_basic_protocol(protocol) then
|
|
|
proto = fetch
|
|
|
else
|
|
|
ok, proto = pcall(require, "luarocks.fetch."..protocol:gsub("[+-]", "_"))
|
|
|
if not ok then
|
|
|
return nil, "Unknown protocol "..protocol
|
|
|
end
|
|
|
end
|
|
|
|
|
|
if cfg.only_sources_from
|
|
|
and rockspec.source.pathname
|
|
|
and #rockspec.source.pathname > 0 then
|
|
|
if #cfg.only_sources_from == 0 then
|
|
|
return nil, "Can't download "..rockspec.source.url.." -- download from remote servers disabled"
|
|
|
elseif rockspec.source.pathname:find(cfg.only_sources_from, 1, true) ~= 1 then
|
|
|
return nil, "Can't download "..rockspec.source.url.." -- only downloading from "..cfg.only_sources_from
|
|
|
end
|
|
|
end
|
|
|
return proto.get_sources(rockspec, extract, dest_dir)
|
|
|
end
|
|
|
|
|
|
return fetch
|
|
|
|