diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2024-02-28 20:36:29 -0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2024-02-29 02:34:52 +0000 |
| commit | d84129429a7553211fffd956fc2c05675aa03a06 (patch) | |
| tree | d8526e7fd38e94948d6c99490316b091a4d42bf2 /src | |
| parent | 63ac87a36cd1a11659243813c6dae35f08be8152 (diff) | |
| download | luarocks-d84129429a7553211fffd956fc2c05675aa03a06.tar.gz luarocks-d84129429a7553211fffd956fc2c05675aa03a06.tar.bz2 luarocks-d84129429a7553211fffd956fc2c05675aa03a06.zip | |
feat: always reuse cached files younger than 10 seconds
This feature depends on lfs being available.
Diffstat (limited to 'src')
| -rw-r--r-- | src/luarocks/fetch.lua | 27 | ||||
| -rw-r--r-- | src/luarocks/fs/lua.lua | 12 |
2 files changed, 32 insertions, 7 deletions
diff --git a/src/luarocks/fetch.lua b/src/luarocks/fetch.lua index 373a998f..435108ae 100644 --- a/src/luarocks/fetch.lua +++ b/src/luarocks/fetch.lua | |||
| @@ -33,7 +33,21 @@ function fetch.fetch_caching(url, mirroring) | |||
| 33 | local name = repo_url:gsub("[/:]","_") | 33 | local name = repo_url:gsub("[/:]","_") |
| 34 | local cache_dir = dir.path(cfg.local_cache, name) | 34 | local cache_dir = dir.path(cfg.local_cache, name) |
| 35 | local ok = fs.make_dir(cache_dir) | 35 | local ok = fs.make_dir(cache_dir) |
| 36 | local lock = ok and fs.lock_access(cache_dir) | 36 | |
| 37 | local cachefile = dir.path(cache_dir, filename) | ||
| 38 | local checkfile = cachefile .. ".check" | ||
| 39 | |||
| 40 | if (fs.file_age(checkfile) < 10 or | ||
| 41 | cfg.aggressive_cache and (not name:match("^manifest"))) and fs.exists(cachefile) | ||
| 42 | then | ||
| 43 | return cachefile, nil, nil, true | ||
| 44 | end | ||
| 45 | |||
| 46 | local lock, errlock | ||
| 47 | if ok then | ||
| 48 | lock, errlock = fs.lock_access(cache_dir) | ||
| 49 | end | ||
| 50 | |||
| 37 | if not (ok and lock) then | 51 | if not (ok and lock) then |
| 38 | cfg.local_cache = fs.make_temp_dir("local_cache") | 52 | cfg.local_cache = fs.make_temp_dir("local_cache") |
| 39 | if not cfg.local_cache then | 53 | if not cfg.local_cache then |
| @@ -47,13 +61,12 @@ function fetch.fetch_caching(url, mirroring) | |||
| 47 | lock = fs.lock_access(cache_dir) | 61 | lock = fs.lock_access(cache_dir) |
| 48 | end | 62 | end |
| 49 | 63 | ||
| 50 | local cachefile = dir.path(cache_dir, filename) | ||
| 51 | if cfg.aggressive_cache and (not name:match("^manifest")) and fs.exists(cachefile) then | ||
| 52 | fs.unlock_access(lock) | ||
| 53 | return cachefile, nil, nil, true | ||
| 54 | end | ||
| 55 | |||
| 56 | local file, err, errcode, from_cache = fetch.fetch_url(url, cachefile, true, mirroring) | 64 | local file, err, errcode, from_cache = fetch.fetch_url(url, cachefile, true, mirroring) |
| 65 | |||
| 66 | local fd = io.open(checkfile, "wb") | ||
| 67 | fd:write("!") | ||
| 68 | fd:close() | ||
| 69 | |||
| 57 | fs.unlock_access(lock) | 70 | fs.unlock_access(lock) |
| 58 | if not file then | 71 | if not file then |
| 59 | return nil, err or "Failed downloading "..url, errcode | 72 | return nil, err or "Failed downloading "..url, errcode |
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 55c64b72..f64b0c18 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua | |||
| @@ -291,6 +291,14 @@ end | |||
| 291 | 291 | ||
| 292 | if lfs_ok then | 292 | if lfs_ok then |
| 293 | 293 | ||
| 294 | function fs_lua.file_age(filename) | ||
| 295 | local attr = lfs.attributes(filename) | ||
| 296 | if attr and attr.change then | ||
| 297 | return os.difftime(os.time(), attr.change) | ||
| 298 | end | ||
| 299 | return math.huge | ||
| 300 | end | ||
| 301 | |||
| 294 | function fs_lua.lock_access(dirname, force) | 302 | function fs_lua.lock_access(dirname, force) |
| 295 | fs.make_dir(dirname) | 303 | fs.make_dir(dirname) |
| 296 | if force then | 304 | if force then |
| @@ -669,6 +677,10 @@ function fs_lua.exists(file) | |||
| 669 | return util.exists(fs.absolute_name(file)) | 677 | return util.exists(fs.absolute_name(file)) |
| 670 | end | 678 | end |
| 671 | 679 | ||
| 680 | function fs_lua.file_age(_) | ||
| 681 | return math.huge | ||
| 682 | end | ||
| 683 | |||
| 672 | end | 684 | end |
| 673 | 685 | ||
| 674 | --------------------------------------------------------------------- | 686 | --------------------------------------------------------------------- |
