download-nar.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix build download-nar)
  19. #:use-module (guix build download)
  20. #:use-module (guix build utils)
  21. #:use-module ((guix serialization) #:hide (dump-port*))
  22. #:autoload (zlib) (call-with-gzip-input-port)
  23. #:use-module (guix progress)
  24. #:use-module (web uri)
  25. #:use-module (srfi srfi-11)
  26. #:use-module (srfi srfi-26)
  27. #:use-module (ice-9 format)
  28. #:use-module (ice-9 match)
  29. #:export (download-nar))
  30. ;;; Commentary:
  31. ;;;
  32. ;;; Download a normalized archive or "nar", similar to what 'guix substitute'
  33. ;;; does. The intent here is to use substitute servers as content-addressed
  34. ;;; mirrors of VCS checkouts. This is mostly useful for users who have
  35. ;;; disabled substitutes.
  36. ;;;
  37. ;;; Code:
  38. (define (urls-for-item item)
  39. "Return the fallback nar URL for ITEM--e.g.,
  40. \"/gnu/store/cabbag3…-foo-1.2-checkout\"."
  41. ;; Here we hard-code nar URLs without checking narinfos. That's probably OK
  42. ;; though. Use berlin.guix.gnu.org instead of its ci.guix.gnu.org front end to
  43. ;; avoid sending these requests to CDN providers without user consent.
  44. ;; TODO: Use HTTPS? The downside is the extra dependency.
  45. (let ((bases '("http://berlin.guix.gnu.org"))
  46. (item (basename item)))
  47. (append (map (cut string-append <> "/nar/gzip/" item) bases)
  48. (map (cut string-append <> "/nar/" item) bases))))
  49. (define (restore-gzipped-nar port item size)
  50. "Restore the gzipped nar read from PORT, of SIZE bytes (compressed), to
  51. ITEM."
  52. ;; Since PORT is typically a non-file port (for instance because 'http-get'
  53. ;; returns a delimited port), create a child process so we're back to a file
  54. ;; port that can be passed to 'call-with-gzip-input-port'.
  55. (match (pipe)
  56. ((input . output)
  57. (match (primitive-fork)
  58. (0
  59. (dynamic-wind
  60. (const #t)
  61. (lambda ()
  62. (close-port output)
  63. (close-port port)
  64. (catch #t
  65. (lambda ()
  66. (call-with-gzip-input-port input
  67. (cut restore-file <> item)))
  68. (lambda (key . args)
  69. (print-exception (current-error-port)
  70. (stack-ref (make-stack #t) 1)
  71. key args)
  72. (primitive-exit 1))))
  73. (lambda ()
  74. (primitive-exit 0))))
  75. (child
  76. (close-port input)
  77. (dump-port* port output
  78. #:reporter (progress-reporter/file item size
  79. #:abbreviation
  80. store-path-abbreviation))
  81. (close-port output)
  82. (newline)
  83. (match (waitpid child)
  84. ((_ . status)
  85. (unless (zero? status)
  86. (error "nar decompression failed" status)))))))))
  87. (define (download-nar item)
  88. "Download and extract the normalized archive for ITEM. Return #t on
  89. success, #f otherwise."
  90. ;; Let progress reports go through.
  91. (setvbuf (current-error-port) 'none)
  92. (setvbuf (current-output-port) 'none)
  93. (let loop ((urls (urls-for-item item)))
  94. (match urls
  95. ((url rest ...)
  96. (format #t "Trying content-addressed mirror at ~a...~%"
  97. (uri-host (string->uri url)))
  98. (let-values (((port size)
  99. (catch #t
  100. (lambda ()
  101. (http-fetch (string->uri url)))
  102. (lambda args
  103. (values #f #f)))))
  104. (if (not port)
  105. (loop rest)
  106. (begin
  107. (if size
  108. (format #t "Downloading from ~a (~,2h MiB)...~%" url
  109. (/ size (expt 2 20.)))
  110. (format #t "Downloading from ~a...~%" url))
  111. (if (string-contains url "/gzip")
  112. (restore-gzipped-nar port item size)
  113. (begin
  114. ;; FIXME: Add progress report.
  115. (restore-file port item)
  116. (close-port port)))
  117. #t))))
  118. (()
  119. #f))))