store-copy.scm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2017, 2018 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 store-copy)
  19. #:use-module (guix build utils)
  20. #:use-module (guix sets)
  21. #:use-module (guix progress)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-9)
  24. #:use-module (srfi srfi-26)
  25. #:use-module (ice-9 match)
  26. #:use-module (ice-9 rdelim)
  27. #:use-module (ice-9 ftw)
  28. #:use-module (ice-9 vlist)
  29. #:export (store-info?
  30. store-info
  31. store-info-item
  32. store-info-deriver
  33. store-info-references
  34. read-reference-graph
  35. closure-size
  36. populate-store))
  37. ;;; Commentary:
  38. ;;;
  39. ;;; This module provides the tools to copy store items and their dependencies
  40. ;;; to another store. It relies on the availability of "reference graph"
  41. ;;; files as produced by 'gexp->derivation' et al. with the
  42. ;;; #:references-graphs parameter.
  43. ;;;
  44. ;;; Code:
  45. ;; Information about a store item as produced by #:references-graphs.
  46. (define-record-type <store-info>
  47. (store-info item deriver references)
  48. store-info?
  49. (item store-info-item) ;string
  50. (deriver store-info-deriver) ;#f | string
  51. (references store-info-references)) ;?
  52. ;; TODO: Factorize with that in (guix store).
  53. (define (topological-sort nodes edges)
  54. "Return NODES in topological order according to EDGES. EDGES must be a
  55. one-argument procedure that takes a node and returns the nodes it is connected
  56. to."
  57. (define (traverse)
  58. ;; Do a simple depth-first traversal of all of PATHS.
  59. (let loop ((nodes nodes)
  60. (visited (setq))
  61. (result '()))
  62. (match nodes
  63. ((head tail ...)
  64. (if (set-contains? visited head)
  65. (loop tail visited result)
  66. (call-with-values
  67. (lambda ()
  68. (loop (edges head)
  69. (set-insert head visited)
  70. result))
  71. (lambda (visited result)
  72. (loop tail visited (cons head result))))))
  73. (()
  74. (values visited result)))))
  75. (call-with-values traverse
  76. (lambda (_ result)
  77. (reverse result))))
  78. (define (read-reference-graph port)
  79. "Read the reference graph as produced by #:references-graphs from PORT and
  80. return it as a list of <store-info> records in topological order--i.e., leaves
  81. come first. IOW, store items in the resulting list can be registered in the
  82. order in which they appear.
  83. The reference graph format consists of sequences of lines like this:
  84. FILE
  85. DERIVER
  86. NUMBER-OF-REFERENCES
  87. REF1
  88. ...
  89. REFN
  90. It is meant as an internal format."
  91. (let loop ((result '())
  92. (table vlist-null)
  93. (referrers vlist-null))
  94. (match (read-line port)
  95. ((? eof-object?)
  96. ;; 'guix-daemon' gives us something that's in "reverse topological
  97. ;; order"--i.e., leaves (items with zero references) come last. Here
  98. ;; we compute the topological order that we want: leaves come first.
  99. (let ((unreferenced? (lambda (item)
  100. (let ((referrers (vhash-fold* cons '()
  101. (store-info-item item)
  102. referrers)))
  103. (or (null? referrers)
  104. (equal? (list item) referrers))))))
  105. (topological-sort (filter unreferenced? result)
  106. (lambda (item)
  107. (map (lambda (item)
  108. (match (vhash-assoc item table)
  109. ((_ . node) node)))
  110. (store-info-references item))))))
  111. (item
  112. (let* ((deriver (match (read-line port)
  113. ("" #f)
  114. (line line)))
  115. (count (string->number (read-line port)))
  116. (refs (unfold-right (cut >= <> count)
  117. (lambda (n)
  118. (read-line port))
  119. 1+
  120. 0))
  121. (item (store-info item deriver refs)))
  122. (loop (cons item result)
  123. (vhash-cons (store-info-item item) item table)
  124. (fold (cut vhash-cons <> item <>)
  125. referrers
  126. refs)))))))
  127. (define (file-size file)
  128. "Return the size of bytes of FILE, entering it if FILE is a directory."
  129. (file-system-fold (const #t)
  130. (lambda (file stat result) ;leaf
  131. (+ (stat:size stat) result))
  132. (lambda (directory stat result) ;down
  133. (+ (stat:size stat) result))
  134. (lambda (directory stat result) ;up
  135. result)
  136. (lambda (file stat result) ;skip
  137. result)
  138. (lambda (file stat errno result)
  139. (format (current-error-port)
  140. "file-size: ~a: ~a~%" file
  141. (strerror errno))
  142. result)
  143. 0
  144. file
  145. lstat))
  146. (define (closure-size reference-graphs)
  147. "Return an estimate of the size of the closure described by
  148. REFERENCE-GRAPHS, a list of reference-graph files."
  149. (define (graph-from-file file)
  150. (map store-info-item
  151. (call-with-input-file file read-reference-graph)))
  152. (define items
  153. (delete-duplicates (append-map graph-from-file reference-graphs)))
  154. (reduce + 0 (map file-size items)))
  155. (define (reset-permissions file)
  156. "Reset the permissions on FILE and its sub-directories so that they are all
  157. read-only."
  158. ;; XXX: This procedure exists just to work around the inability of
  159. ;; 'copy-recursively' to preserve permissions.
  160. (file-system-fold (const #t) ;enter?
  161. (lambda (file stat _) ;leaf
  162. (unless (eq? 'symlink (stat:type stat))
  163. (chmod file
  164. (if (zero? (logand (stat:mode stat)
  165. #o100))
  166. #o444
  167. #o555))))
  168. (const #t) ;down
  169. (lambda (directory stat _) ;up
  170. (chmod directory #o555))
  171. (const #f) ;skip
  172. (const #f) ;error
  173. #t
  174. file
  175. lstat))
  176. (define* (populate-store reference-graphs target
  177. #:key (log-port (current-error-port)))
  178. "Populate the store under directory TARGET with the items specified in
  179. REFERENCE-GRAPHS, a list of reference-graph files."
  180. (define store
  181. (string-append target (%store-directory)))
  182. (define (things-to-copy)
  183. ;; Return the list of store files to copy to the image.
  184. (define (graph-from-file file)
  185. (map store-info-item
  186. (call-with-input-file file read-reference-graph)))
  187. (delete-duplicates (append-map graph-from-file reference-graphs)))
  188. (mkdir-p store)
  189. (chmod store #o1775)
  190. (let* ((things (things-to-copy))
  191. (len (length things))
  192. (progress (progress-reporter/bar len
  193. (format #f "copying ~a store items"
  194. len)
  195. log-port)))
  196. (call-with-progress-reporter progress
  197. (lambda (report)
  198. (for-each (lambda (thing)
  199. (copy-recursively thing
  200. (string-append target thing)
  201. #:keep-mtime? #t
  202. #:log (%make-void-port "w"))
  203. ;; XXX: Since 'copy-recursively' doesn't allow us to
  204. ;; preserve permissions, we have to traverse TARGET to
  205. ;; make sure everything is read-only.
  206. (reset-permissions (string-append target thing))
  207. (report))
  208. things)))))
  209. ;;; store-copy.scm ends here