@@ -216,6 +216,26 @@ function location_to_sha256() {
216
216
)
217
217
}
218
218
219
+ # e.g.
220
+ # ```console
221
+ # $ cache_download_dir
222
+ # .download # on GitHub Actions
223
+ # /home/user/.cache/lima/download # on Linux
224
+ # /Users/user/Library/Caches/lima/download # on macOS
225
+ # /home/user/.cache/lima/download # on others
226
+ # ```
227
+ function cache_download_dir() {
228
+ if [[ ${GITHUB_ACTIONS:- false} == true ]]; then
229
+ echo " .download"
230
+ else
231
+ case " $( uname -s) " in
232
+ Linux) echo " ${XDG_CACHE_HOME:- ${HOME} / .cache} /lima/download" ;;
233
+ Darwin) echo " ${HOME} /Library/Caches/lima/download" ;;
234
+ * ) echo " ${HOME} /.cache/lima/download" ;;
235
+ esac
236
+ fi
237
+ }
238
+
219
239
# e.g.
220
240
# ```console
221
241
# $ location_to_cache_path "https://cloud-images.ubuntu.com/releases/24.04/release-20240809/ubuntu-24.04-server-cloudimg-arm64.img"
@@ -224,7 +244,7 @@ function location_to_sha256() {
224
244
function location_to_cache_path() {
225
245
local location=$1
226
246
[[ ${location} != " null" ]] || return
227
- sha256=$( location_to_sha256 " ${location} " ) && echo " .download /by-url-sha256/${sha256} "
247
+ sha256=$( location_to_sha256 " ${location} " ) && download_dir= $( cache_download_dir ) && echo " ${download_dir} /by-url-sha256/${sha256} "
228
248
}
229
249
230
250
# e.g.
@@ -321,3 +341,48 @@ function hash_file() {
321
341
echo " ${hash} " | xxd -r -p | sha256sum | cut -d' ' -f1
322
342
)
323
343
}
344
+
345
+ # Download the file to the cache directory and print the path.
346
+ # e.g.
347
+ # ```console
348
+ # $ download_to_cache "https://cloud-images.ubuntu.com/releases/24.04/release-20240821/ubuntu-24.04-server-cloudimg-arm64.img"
349
+ # .download/by-url-sha256/346ee1ff9e381b78ba08e2a29445960b5cd31c51f896fc346b82e26e345a5b9a/data # on GitHub Actions
350
+ # /home/user/.cache/lima/download/by-url-sha256/346ee1ff9e381b78ba08e2a29445960b5cd31c51f896fc346b82e26e345a5b9a/data # on Linux
351
+ # /Users/user/Library/Caches/lima/download/by-url-sha256/346ee1ff9e381b78ba08e2a29445960b5cd31c51f896fc346b82e26e345a5b9a/data # on macOS
352
+ # /home/user/.cache/lima/download/by-url-sha256/346ee1ff9e381b78ba08e2a29445960b5cd31c51f896fc346b82e26e345a5b9a/data # on others
353
+ function download_to_cache() {
354
+ local code_time_type_url
355
+ code_time_type_url=$(
356
+ curl -sSLI -w " %{http_code}\t%header{Last-Modified}\t%header{Content-Type}\t%{url_effective}" " $1 " -o /dev/null
357
+ )
358
+
359
+ local code time type url
360
+ IFS=$' \t ' read -r code time type url filename <<< " ${code_time_type_url}"
361
+ [[ ${code} == 200 ]] || exit 1
362
+
363
+ local cache_path
364
+ cache_path=$( location_to_cache_path " ${url} " )
365
+ [[ -d ${cache_path} ]] || mkdir -p " ${cache_path} "
366
+
367
+ local needs_download=0
368
+ [[ -f ${cache_path} /data ]] || needs_download=1
369
+ [[ -f ${cache_path} /time && " $( < " ${cache_path} /time" ) " == " ${time} " ]] || needs_download=1
370
+ [[ -f ${cache_path} /type && " $( < " ${cache_path} /type" ) " == " ${type} " ]] || needs_download=1
371
+ if [[ ${needs_download} -eq 1 ]]; then
372
+ local code_time_type_url_filename
373
+ code_time_type_url_filename=$(
374
+ echo " downloading ${url} " >&2
375
+ curl -SL -w " %{http_code}\t%header{Last-Modified}\t%header{Content-Type}\t%{url_effective}\t%{filename_effective}" --no-clobber -o " ${cache_path} /data" " ${url} "
376
+ )
377
+ local filename
378
+ IFS=$' \t ' read -r code time type url filename <<< " ${code_time_type_url_filename}"
379
+ [[ ${code} == 200 ]] || exit 1
380
+ [[ " ${cache_path} /data" == " ${filename} " ]] || mv " ${filename} " " ${cache_path} /data"
381
+ # sha256.digest seems existing if expected digest is available. so, not creating it here.
382
+ # sha256sum "${cache_path}/data" | awk '{print "sha256:"$1}' >"${cache_path}/sha256.digest"
383
+ echo -n " ${time} " > " ${cache_path} /time"
384
+ fi
385
+ [[ -f ${cache_path} /type ]] || echo -n " ${type} " > " ${cache_path} /type"
386
+ [[ -f ${cache_path} /url ]] || echo -n " ${url} " > " ${cache_path} /url"
387
+ echo " ${cache_path} /data"
388
+ }
0 commit comments