From 7d6a269d39c59afb520792f5d7b87ddb8076d370 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sun, 3 Mar 2024 12:22:11 -0500 Subject: Fixed things up a bit. --- src/lpm.c | 9 +++++++++ src/lpm.lua | 21 +++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/lpm.c b/src/lpm.c index c969f1e..10b81e1 100644 --- a/src/lpm.c +++ b/src/lpm.c @@ -1366,6 +1366,15 @@ int main(int argc, char* argv[]) { lua_pushliteral(L, "/"); #endif lua_setglobal(L, "PATHSEP"); + #if _WIN32 + wchar_t tmpdir[MAX_PATH]; + DWORD length = GetTempPathW(MAX_PATH, tmpdir); + lua_toutf8(L, tmpdir); + #else + lua_pushstring(L, getenv("TMPDIR") ? getenv("TMPDIR") : P_tmpdir); + #endif + lua_setglobal(L, "SYSTMPDIR"); + lua_pushliteral(L, LITE_ARCH_TUPLE); lua_setglobal(L, "ARCH"); lua_pushliteral(L, LPM_DEFAULT_REPOSITORY); diff --git a/src/lpm.lua b/src/lpm.lua index 146598a..5a83a36 100644 --- a/src/lpm.lua +++ b/src/lpm.lua @@ -573,8 +573,9 @@ function common.get(source, options) if headers.location then return common.get(headers.location, common.merge(options, { depth = (depth or 0) + 1 })) end return res end - if not system.stat(CACHEDIR .. PATHSEP .. "files") then common.mkdirp(CACHEDIR .. PATHSEP .. "files") end - local cache_path = CACHEDIR .. PATHSEP .. "files" .. PATHSEP .. (checksum ~= "SKIP" and checksum or system.hash(source)) + local cache_dir = options.cache or CACHEDIR + if not system.stat(cache_dir .. PATHSEP .. "files") then common.mkdirp(cache_dir .. PATHSEP .. "files") end + local cache_path = cache_dir .. PATHSEP .. "files" .. PATHSEP .. (checksum ~= "SKIP" and checksum or system.hash(source)) local res if not system.stat(cache_path) then res, headers = system.get(protocol, hostname, port, rest, cache_path, callback) @@ -1985,22 +1986,26 @@ local function lpm_addon_upgrade() end local function lpm_self_upgrade(release) - local path = system.stat(ARGV[1]) and ARGV[1] or common.path(ARGV[1]) + local path = ARGV[1]:find(PATHSEP) and system.stat(ARGV[1]) and ARGV[1] or common.path(ARGV[1]) if not path then error("can't find path to lpm") end - local release_url = release and release:find("^https://") and release or (DEFAULT_RELEASE_URL:gsub("%%r", release or "latest")) + release = release or "latest" + local release_url = release and release:find("^https://") and release or (DEFAULT_RELEASE_URL:gsub("%%r", release)) local stat = system.stat(path) if not stat then error("can't find lpm at " .. path) end - local status, err = pcall(common.get, release_url, { target = path .. ".upgrade", callback = write_progress_bar }) + local target = SYSTMPDIR .. PATHSEP .. "lpm.upgrade" + common.rmrf(target) + local status, err = pcall(common.get, release_url, { cache = SYSTMPDIR, target = target, callback = write_progress_bar }) if not status then error("can't find release for lpm at " .. release_url .. (VERBOSE and (": " .. err) or "")) end - if common.is_path_different(path .. ".new", path) then + if common.is_path_different(target, path) then status, err = pcall(common.rename, path, path .. ".bak") if not status then error("can't move lpm executable; do you need to " .. (PLATFORM == "windows" and "run as administrator" or "be root") .. "?" .. (VERBOSE and ": " .. err or "")) end - common.rename(TMPDIR .. PATHSEP .. "lpm.upgrade", path) + common.rename(target, path) system.chmod(path, stat.mode) common.rmrf(path .. ".bak") + log_action("Upgraded lpm to " .. release .. ".") else log_warning("aborting upgrade; remote executable is identical to current") - common.rmrf(path .. ".new") + common.rmrf(target) end end -- cgit v1.2.3