2013-01-05 18:47:50 -05:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2014-01-08 13:23:33 -05:00
|
|
|
|
;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
|
2012-04-18 17:34:12 -04:00
|
|
|
|
;;;
|
2013-01-05 18:47:50 -05:00
|
|
|
|
;;; This file is part of GNU Guix.
|
2012-04-18 17:34:12 -04:00
|
|
|
|
;;;
|
2013-01-05 18:47:50 -05:00
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
2012-04-18 17:34:12 -04:00
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
;;; your option) any later version.
|
|
|
|
|
;;;
|
2013-01-05 18:47:50 -05:00
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
2012-04-18 17:34:12 -04:00
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
|
;;;
|
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
2013-01-05 18:47:50 -05:00
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
2012-04-18 17:34:12 -04:00
|
|
|
|
|
|
|
|
|
(define-module (guix store)
|
2012-10-24 11:03:45 -04:00
|
|
|
|
#:use-module (guix utils)
|
2012-12-05 17:02:47 -05:00
|
|
|
|
#:use-module (guix config)
|
Add (guix nar) and (guix serialization).
* guix/store.scm (write-int, read-int, write-long-long, read-long-long,
write-padding, write-string, read-string, read-latin1-string,
write-string-list, read-string-list, write-store-path,
read-store-path, write-store-path-list, read-store-path-list): Move to
serialization.scm.
(write-contents, write-file): Move to nar.scm.
* guix/nar.scm, guix/serialization.scm: New files.
* Makefile.am (MODULES): Add them.
2013-04-04 16:29:08 -04:00
|
|
|
|
#:use-module (guix serialization)
|
2014-04-12 16:32:10 -04:00
|
|
|
|
#:autoload (guix base32) (bytevector->base32-string)
|
2012-04-18 17:34:12 -04:00
|
|
|
|
#:use-module (rnrs bytevectors)
|
|
|
|
|
#:use-module (rnrs io ports)
|
|
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
|
#:use-module (srfi srfi-9)
|
2013-09-01 13:24:21 -04:00
|
|
|
|
#:use-module (srfi srfi-9 gnu)
|
2012-04-18 17:34:12 -04:00
|
|
|
|
#:use-module (srfi srfi-26)
|
2012-06-01 17:29:55 -04:00
|
|
|
|
#:use-module (srfi srfi-34)
|
|
|
|
|
#:use-module (srfi srfi-35)
|
2012-06-01 17:29:55 -04:00
|
|
|
|
#:use-module (srfi srfi-39)
|
2012-04-18 17:34:12 -04:00
|
|
|
|
#:use-module (ice-9 match)
|
2012-10-31 20:39:23 -04:00
|
|
|
|
#:use-module (ice-9 regex)
|
2013-06-13 16:03:42 -04:00
|
|
|
|
#:use-module (ice-9 vlist)
|
2014-01-11 11:11:14 -05:00
|
|
|
|
#:use-module (ice-9 popen)
|
2013-02-18 15:53:59 -05:00
|
|
|
|
#:export (%daemon-socket-file
|
2014-04-12 16:32:10 -04:00
|
|
|
|
%gc-roots-directory
|
2013-02-18 15:53:59 -05:00
|
|
|
|
|
|
|
|
|
nix-server?
|
2012-04-18 17:34:12 -04:00
|
|
|
|
nix-server-major-version
|
|
|
|
|
nix-server-minor-version
|
|
|
|
|
nix-server-socket
|
|
|
|
|
|
2012-06-01 17:29:55 -04:00
|
|
|
|
&nix-error nix-error?
|
2013-03-07 13:29:12 -05:00
|
|
|
|
&nix-connection-error nix-connection-error?
|
|
|
|
|
nix-connection-error-file
|
|
|
|
|
nix-connection-error-code
|
2012-06-01 17:29:55 -04:00
|
|
|
|
&nix-protocol-error nix-protocol-error?
|
|
|
|
|
nix-protocol-error-message
|
|
|
|
|
nix-protocol-error-status
|
|
|
|
|
|
2012-06-01 17:29:55 -04:00
|
|
|
|
hash-algo
|
|
|
|
|
|
2012-04-18 17:34:12 -04:00
|
|
|
|
open-connection
|
2012-08-22 11:22:46 -04:00
|
|
|
|
close-connection
|
2014-01-19 17:03:43 -05:00
|
|
|
|
with-store
|
2012-04-18 17:34:12 -04:00
|
|
|
|
set-build-options
|
2012-06-30 18:21:16 -04:00
|
|
|
|
valid-path?
|
2012-10-24 11:03:45 -04:00
|
|
|
|
query-path-hash
|
2014-03-14 12:16:10 -04:00
|
|
|
|
hash-part->path
|
2012-04-18 17:34:12 -04:00
|
|
|
|
add-text-to-store
|
|
|
|
|
add-to-store
|
2012-06-01 17:29:55 -04:00
|
|
|
|
build-derivations
|
2012-12-09 17:52:59 -05:00
|
|
|
|
add-temp-root
|
2012-10-29 19:20:53 -04:00
|
|
|
|
add-indirect-root
|
2014-04-12 16:32:10 -04:00
|
|
|
|
add-permanent-root
|
|
|
|
|
remove-permanent-root
|
2012-06-01 17:29:55 -04:00
|
|
|
|
|
2013-02-03 17:24:25 -05:00
|
|
|
|
substitutable?
|
|
|
|
|
substitutable-path
|
|
|
|
|
substitutable-deriver
|
|
|
|
|
substitutable-references
|
|
|
|
|
substitutable-download-size
|
|
|
|
|
substitutable-nar-size
|
|
|
|
|
has-substitutes?
|
|
|
|
|
substitutable-paths
|
|
|
|
|
substitutable-path-info
|
|
|
|
|
|
2013-02-27 16:40:35 -05:00
|
|
|
|
references
|
2013-06-13 16:03:42 -04:00
|
|
|
|
requisites
|
2013-02-27 16:40:35 -05:00
|
|
|
|
referrers
|
2014-01-23 16:13:27 -05:00
|
|
|
|
topologically-sorted
|
2013-02-27 16:40:35 -05:00
|
|
|
|
valid-derivers
|
|
|
|
|
query-derivation-outputs
|
2012-12-09 17:52:59 -05:00
|
|
|
|
live-paths
|
|
|
|
|
dead-paths
|
|
|
|
|
collect-garbage
|
|
|
|
|
delete-paths
|
2013-12-20 11:17:42 -05:00
|
|
|
|
import-paths
|
|
|
|
|
export-paths
|
2012-12-09 17:52:59 -05:00
|
|
|
|
|
2012-06-16 10:13:12 -04:00
|
|
|
|
current-build-output-port
|
|
|
|
|
|
2014-01-11 11:11:14 -05:00
|
|
|
|
register-path
|
|
|
|
|
|
2012-06-01 17:29:55 -04:00
|
|
|
|
%store-prefix
|
|
|
|
|
store-path?
|
2013-11-12 17:36:29 -05:00
|
|
|
|
direct-store-path?
|
2012-10-31 20:39:23 -04:00
|
|
|
|
derivation-path?
|
2013-04-01 10:08:31 -04:00
|
|
|
|
store-path-package-name
|
2013-11-11 18:10:10 -05:00
|
|
|
|
store-path-hash-part
|
|
|
|
|
log-file))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
|
2013-02-03 17:02:14 -05:00
|
|
|
|
(define %protocol-version #x10c)
|
2012-04-18 17:34:12 -04:00
|
|
|
|
|
2014-02-11 17:16:28 -05:00
|
|
|
|
(define %worker-magic-1 #x6e697863) ; "nixc"
|
|
|
|
|
(define %worker-magic-2 #x6478696f) ; "dxio"
|
2012-04-18 17:34:12 -04:00
|
|
|
|
|
|
|
|
|
(define (protocol-major magic)
|
|
|
|
|
(logand magic #xff00))
|
|
|
|
|
(define (protocol-minor magic)
|
|
|
|
|
(logand magic #x00ff))
|
|
|
|
|
|
|
|
|
|
(define-syntax define-enumerate-type
|
|
|
|
|
(syntax-rules ()
|
|
|
|
|
((_ name->int (name id) ...)
|
|
|
|
|
(define-syntax name->int
|
|
|
|
|
(syntax-rules (name ...)
|
|
|
|
|
((_ name) id) ...)))))
|
|
|
|
|
|
|
|
|
|
(define-enumerate-type operation-id
|
|
|
|
|
;; operation numbers from worker-protocol.hh
|
|
|
|
|
(quit 0)
|
|
|
|
|
(valid-path? 1)
|
|
|
|
|
(has-substitutes? 3)
|
|
|
|
|
(query-path-hash 4)
|
|
|
|
|
(query-references 5)
|
|
|
|
|
(query-referrers 6)
|
|
|
|
|
(add-to-store 7)
|
|
|
|
|
(add-text-to-store 8)
|
|
|
|
|
(build-derivations 9)
|
|
|
|
|
(ensure-path 10)
|
|
|
|
|
(add-temp-root 11)
|
|
|
|
|
(add-indirect-root 12)
|
|
|
|
|
(sync-with-gc 13)
|
|
|
|
|
(find-roots 14)
|
|
|
|
|
(export-path 16)
|
|
|
|
|
(query-deriver 18)
|
|
|
|
|
(set-options 19)
|
|
|
|
|
(collect-garbage 20)
|
2013-02-03 17:02:14 -05:00
|
|
|
|
;;(query-substitutable-path-info 21) ; obsolete as of #x10c
|
2012-04-18 17:34:12 -04:00
|
|
|
|
(query-derivation-outputs 22)
|
2013-02-03 17:02:14 -05:00
|
|
|
|
(query-all-valid-paths 23)
|
2012-04-18 17:34:12 -04:00
|
|
|
|
(query-failed-paths 24)
|
|
|
|
|
(clear-failed-paths 25)
|
|
|
|
|
(query-path-info 26)
|
|
|
|
|
(import-paths 27)
|
2013-02-03 17:02:14 -05:00
|
|
|
|
(query-derivation-output-names 28)
|
|
|
|
|
(query-path-from-hash-part 29)
|
|
|
|
|
(query-substitutable-path-infos 30)
|
|
|
|
|
(query-valid-paths 31)
|
2013-02-27 16:40:35 -05:00
|
|
|
|
(query-substitutable-paths 32)
|
|
|
|
|
(query-valid-derivers 33))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
|
|
|
|
|
(define-enumerate-type hash-algo
|
|
|
|
|
;; hash.hh
|
|
|
|
|
(md5 1)
|
|
|
|
|
(sha1 2)
|
|
|
|
|
(sha256 3))
|
|
|
|
|
|
2012-12-09 17:52:59 -05:00
|
|
|
|
(define-enumerate-type gc-action
|
|
|
|
|
;; store-api.hh
|
|
|
|
|
(return-live 0)
|
|
|
|
|
(return-dead 1)
|
|
|
|
|
(delete-dead 2)
|
|
|
|
|
(delete-specific 3))
|
|
|
|
|
|
2012-04-18 17:34:12 -04:00
|
|
|
|
(define %default-socket-path
|
2014-01-08 15:12:55 -05:00
|
|
|
|
(string-append %state-directory "/daemon-socket/socket"))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
|
2013-02-18 15:53:59 -05:00
|
|
|
|
(define %daemon-socket-file
|
|
|
|
|
;; File name of the socket the daemon listens too.
|
|
|
|
|
(make-parameter (or (getenv "GUIX_DAEMON_SOCKET")
|
|
|
|
|
%default-socket-path)))
|
|
|
|
|
|
|
|
|
|
|
2012-04-18 17:34:12 -04:00
|
|
|
|
|
2013-02-03 17:24:25 -05:00
|
|
|
|
;; Information about a substitutable store path.
|
|
|
|
|
(define-record-type <substitutable>
|
|
|
|
|
(substitutable path deriver refs dl-size nar-size)
|
|
|
|
|
substitutable?
|
|
|
|
|
(path substitutable-path)
|
|
|
|
|
(deriver substitutable-deriver)
|
|
|
|
|
(refs substitutable-references)
|
|
|
|
|
(dl-size substitutable-download-size)
|
|
|
|
|
(nar-size substitutable-nar-size))
|
|
|
|
|
|
|
|
|
|
(define (read-substitutable-path-list p)
|
|
|
|
|
(let loop ((len (read-int p))
|
|
|
|
|
(result '()))
|
|
|
|
|
(if (zero? len)
|
|
|
|
|
(reverse result)
|
|
|
|
|
(let ((path (read-store-path p))
|
|
|
|
|
(deriver (read-store-path p))
|
|
|
|
|
(refs (read-store-path-list p))
|
|
|
|
|
(dl-size (read-long-long p))
|
|
|
|
|
(nar-size (read-long-long p)))
|
|
|
|
|
(loop (- len 1)
|
|
|
|
|
(cons (substitutable path deriver refs dl-size nar-size)
|
|
|
|
|
result))))))
|
|
|
|
|
|
2012-04-18 17:34:12 -04:00
|
|
|
|
(define-syntax write-arg
|
2014-03-09 18:01:18 -04:00
|
|
|
|
(syntax-rules (integer boolean file string string-list string-pairs
|
2012-12-09 17:52:59 -05:00
|
|
|
|
store-path store-path-list base16)
|
2012-04-18 17:34:12 -04:00
|
|
|
|
((_ integer arg p)
|
|
|
|
|
(write-int arg p))
|
|
|
|
|
((_ boolean arg p)
|
|
|
|
|
(write-int (if arg 1 0) p))
|
|
|
|
|
((_ file arg p)
|
|
|
|
|
(write-file arg p))
|
|
|
|
|
((_ string arg p)
|
|
|
|
|
(write-string arg p))
|
|
|
|
|
((_ string-list arg p)
|
2012-10-24 11:03:45 -04:00
|
|
|
|
(write-string-list arg p))
|
2014-03-09 18:01:18 -04:00
|
|
|
|
((_ string-pairs arg p)
|
|
|
|
|
(write-string-pairs arg p))
|
2012-12-09 17:52:59 -05:00
|
|
|
|
((_ store-path arg p)
|
|
|
|
|
(write-store-path arg p))
|
|
|
|
|
((_ store-path-list arg p)
|
|
|
|
|
(write-store-path-list arg p))
|
2012-10-24 11:03:45 -04:00
|
|
|
|
((_ base16 arg p)
|
|
|
|
|
(write-string (bytevector->base16-string arg) p))))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
|
|
|
|
|
(define-syntax read-arg
|
2013-02-03 17:24:25 -05:00
|
|
|
|
(syntax-rules (integer boolean string store-path store-path-list
|
|
|
|
|
substitutable-path-list base16)
|
2012-04-18 17:34:12 -04:00
|
|
|
|
((_ integer p)
|
|
|
|
|
(read-int p))
|
|
|
|
|
((_ boolean p)
|
|
|
|
|
(not (zero? (read-int p))))
|
|
|
|
|
((_ string p)
|
|
|
|
|
(read-string p))
|
|
|
|
|
((_ store-path p)
|
2012-10-24 11:03:45 -04:00
|
|
|
|
(read-store-path p))
|
2012-12-09 17:52:59 -05:00
|
|
|
|
((_ store-path-list p)
|
|
|
|
|
(read-store-path-list p))
|
2013-02-03 17:24:25 -05:00
|
|
|
|
((_ substitutable-path-list p)
|
|
|
|
|
(read-substitutable-path-list p))
|
|
|
|
|
((_ base16 p)
|
2012-10-24 11:03:45 -04:00
|
|
|
|
(base16-string->bytevector (read-string p)))))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; remote-store.cc
|
|
|
|
|
|
|
|
|
|
(define-record-type <nix-server>
|
2013-01-29 17:34:50 -05:00
|
|
|
|
(%make-nix-server socket major minor
|
2013-01-29 17:53:09 -05:00
|
|
|
|
ats-cache atts-cache)
|
2012-04-18 17:34:12 -04:00
|
|
|
|
nix-server?
|
|
|
|
|
(socket nix-server-socket)
|
|
|
|
|
(major nix-server-major-version)
|
2013-01-29 17:34:50 -05:00
|
|
|
|
(minor nix-server-minor-version)
|
|
|
|
|
|
|
|
|
|
;; Caches. We keep them per-connection, because store paths build
|
|
|
|
|
;; during the session are temporary GC roots kept for the duration of
|
|
|
|
|
;; the session.
|
2013-01-29 17:53:09 -05:00
|
|
|
|
(ats-cache nix-server-add-to-store-cache)
|
|
|
|
|
(atts-cache nix-server-add-text-to-store-cache))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
|
2013-09-01 13:24:21 -04:00
|
|
|
|
(set-record-type-printer! <nix-server>
|
|
|
|
|
(lambda (obj port)
|
|
|
|
|
(format port "#<build-daemon ~a.~a ~a>"
|
|
|
|
|
(nix-server-major-version obj)
|
|
|
|
|
(nix-server-minor-version obj)
|
|
|
|
|
(number->string (object-address obj)
|
|
|
|
|
16))))
|
|
|
|
|
|
2012-06-01 17:29:55 -04:00
|
|
|
|
(define-condition-type &nix-error &error
|
|
|
|
|
nix-error?)
|
|
|
|
|
|
2013-03-07 13:29:12 -05:00
|
|
|
|
(define-condition-type &nix-connection-error &nix-error
|
|
|
|
|
nix-connection-error?
|
|
|
|
|
(file nix-connection-error-file)
|
|
|
|
|
(errno nix-connection-error-code))
|
|
|
|
|
|
2012-06-01 17:29:55 -04:00
|
|
|
|
(define-condition-type &nix-protocol-error &nix-error
|
|
|
|
|
nix-protocol-error?
|
|
|
|
|
(message nix-protocol-error-message)
|
|
|
|
|
(status nix-protocol-error-status))
|
|
|
|
|
|
2013-02-18 15:53:59 -05:00
|
|
|
|
(define* (open-connection #:optional (file (%daemon-socket-file))
|
2012-07-01 18:45:23 -04:00
|
|
|
|
#:key (reserve-space? #t))
|
2013-01-15 17:32:13 -05:00
|
|
|
|
"Connect to the daemon over the Unix-domain socket at FILE. When
|
|
|
|
|
RESERVE-SPACE? is true, instruct it to reserve a little bit of extra
|
|
|
|
|
space on the file system so that the garbage collector can still
|
|
|
|
|
operate, should the disk become full. Return a server object."
|
2012-04-18 17:34:12 -04:00
|
|
|
|
(let ((s (with-fluids ((%default-port-encoding #f))
|
|
|
|
|
;; This trick allows use of the `scm_c_read' optimization.
|
|
|
|
|
(socket PF_UNIX SOCK_STREAM 0)))
|
|
|
|
|
(a (make-socket-address PF_UNIX file)))
|
2012-10-13 16:36:39 -04:00
|
|
|
|
|
2013-03-07 13:29:12 -05:00
|
|
|
|
(catch 'system-error
|
|
|
|
|
(cut connect s a)
|
|
|
|
|
(lambda args
|
|
|
|
|
;; Translate the error to something user-friendly.
|
|
|
|
|
(let ((errno (system-error-errno args)))
|
|
|
|
|
(raise (condition (&nix-connection-error
|
|
|
|
|
(file file)
|
|
|
|
|
(errno errno)))))))
|
|
|
|
|
|
2012-04-18 17:34:12 -04:00
|
|
|
|
(write-int %worker-magic-1 s)
|
|
|
|
|
(let ((r (read-int s)))
|
|
|
|
|
(and (eqv? r %worker-magic-2)
|
|
|
|
|
(let ((v (read-int s)))
|
|
|
|
|
(and (eqv? (protocol-major %protocol-version)
|
|
|
|
|
(protocol-major v))
|
|
|
|
|
(begin
|
|
|
|
|
(write-int %protocol-version s)
|
2012-07-01 18:45:23 -04:00
|
|
|
|
(if (>= (protocol-minor v) 11)
|
|
|
|
|
(write-int (if reserve-space? 1 0) s))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
(let ((s (%make-nix-server s
|
|
|
|
|
(protocol-major v)
|
2013-01-29 17:34:50 -05:00
|
|
|
|
(protocol-minor v)
|
2013-01-30 11:05:54 -05:00
|
|
|
|
(make-hash-table 100)
|
|
|
|
|
(make-hash-table 100))))
|
2013-01-13 16:27:00 -05:00
|
|
|
|
(let loop ((done? (process-stderr s)))
|
|
|
|
|
(or done? (process-stderr s)))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
s))))))))
|
|
|
|
|
|
2012-08-22 11:22:46 -04:00
|
|
|
|
(define (close-connection server)
|
|
|
|
|
"Close the connection to SERVER."
|
|
|
|
|
(close (nix-server-socket server)))
|
|
|
|
|
|
2014-01-19 17:03:43 -05:00
|
|
|
|
(define-syntax-rule (with-store store exp ...)
|
|
|
|
|
"Bind STORE to an open connection to the store and evaluate EXPs;
|
|
|
|
|
automatically close the store when the dynamic extent of EXP is left."
|
|
|
|
|
(let ((store (open-connection)))
|
|
|
|
|
(dynamic-wind
|
|
|
|
|
(const #f)
|
|
|
|
|
(lambda ()
|
|
|
|
|
exp ...)
|
|
|
|
|
(lambda ()
|
|
|
|
|
(false-if-exception (close-connection store))))))
|
|
|
|
|
|
2012-06-16 10:13:12 -04:00
|
|
|
|
(define current-build-output-port
|
|
|
|
|
;; The port where build output is sent.
|
|
|
|
|
(make-parameter (current-error-port)))
|
|
|
|
|
|
2013-12-20 11:17:42 -05:00
|
|
|
|
(define* (dump-port in out
|
|
|
|
|
#:optional len
|
|
|
|
|
#:key (buffer-size 16384))
|
|
|
|
|
"Read LEN bytes from IN (or as much as possible if LEN is #f) and write it
|
|
|
|
|
to OUT, using chunks of BUFFER-SIZE bytes."
|
|
|
|
|
(define buffer
|
|
|
|
|
(make-bytevector buffer-size))
|
|
|
|
|
|
|
|
|
|
(let loop ((total 0)
|
|
|
|
|
(bytes (get-bytevector-n! in buffer 0
|
|
|
|
|
(if len
|
|
|
|
|
(min len buffer-size)
|
|
|
|
|
buffer-size))))
|
|
|
|
|
(or (eof-object? bytes)
|
|
|
|
|
(and len (= total len))
|
|
|
|
|
(let ((total (+ total bytes)))
|
|
|
|
|
(put-bytevector out buffer 0 bytes)
|
|
|
|
|
(loop total
|
|
|
|
|
(get-bytevector-n! in buffer 0
|
|
|
|
|
(if len
|
|
|
|
|
(min (- len total) buffer-size)
|
|
|
|
|
buffer-size)))))))
|
|
|
|
|
|
2014-05-21 09:29:23 -04:00
|
|
|
|
(define %newlines
|
|
|
|
|
;; Newline characters triggering a flush of 'current-build-output-port'.
|
|
|
|
|
;; Unlike Guile's _IOLBF, we flush upon #\return so that progress reports
|
|
|
|
|
;; that use that trick are correctly displayed.
|
|
|
|
|
(char-set #\newline #\return))
|
|
|
|
|
|
2013-12-20 11:17:42 -05:00
|
|
|
|
(define* (process-stderr server #:optional user-port)
|
2012-06-16 10:13:12 -04:00
|
|
|
|
"Read standard output and standard error from SERVER, writing it to
|
|
|
|
|
CURRENT-BUILD-OUTPUT-PORT. Return #t when SERVER is done sending data, and
|
|
|
|
|
#f otherwise; in the latter case, the caller should call `process-stderr'
|
2013-01-31 08:05:10 -05:00
|
|
|
|
again until #t is returned or an error is raised.
|
|
|
|
|
|
|
|
|
|
Since the build process's output cannot be assumed to be UTF-8, we
|
|
|
|
|
conservatively consider it to be Latin-1, thereby avoiding possible
|
|
|
|
|
encoding conversion errors."
|
2012-04-18 17:34:12 -04:00
|
|
|
|
(define p
|
|
|
|
|
(nix-server-socket server))
|
|
|
|
|
|
|
|
|
|
;; magic cookies from worker-protocol.hh
|
2014-01-09 17:59:07 -05:00
|
|
|
|
(define %stderr-next #x6f6c6d67) ; "olmg", build log
|
|
|
|
|
(define %stderr-read #x64617461) ; "data", data needed from source
|
|
|
|
|
(define %stderr-write #x64617416) ; "dat\x16", data for sink
|
|
|
|
|
(define %stderr-last #x616c7473) ; "alts", we're done
|
|
|
|
|
(define %stderr-error #x63787470) ; "cxtp", error reporting
|
2012-04-18 17:34:12 -04:00
|
|
|
|
|
|
|
|
|
(let ((k (read-int p)))
|
|
|
|
|
(cond ((= k %stderr-write)
|
2013-12-20 11:17:42 -05:00
|
|
|
|
;; Write a byte stream to USER-PORT.
|
|
|
|
|
(let* ((len (read-int p))
|
|
|
|
|
(m (modulo len 8)))
|
|
|
|
|
(dump-port p user-port len)
|
|
|
|
|
(unless (zero? m)
|
|
|
|
|
;; Consume padding, as for strings.
|
|
|
|
|
(get-bytevector-n p (- 8 m))))
|
2012-06-16 10:13:12 -04:00
|
|
|
|
#f)
|
2012-04-18 17:34:12 -04:00
|
|
|
|
((= k %stderr-read)
|
2013-12-20 11:17:42 -05:00
|
|
|
|
;; Read a byte stream from USER-PORT.
|
2014-05-27 16:01:51 -04:00
|
|
|
|
;; Note: Avoid 'get-bytevector-n' to work around
|
|
|
|
|
;; <http://bugs.gnu.org/17591> in Guile up to 2.0.11.
|
2013-12-20 11:17:42 -05:00
|
|
|
|
(let* ((max-len (read-int p))
|
2014-05-27 16:01:51 -04:00
|
|
|
|
(data (make-bytevector max-len))
|
|
|
|
|
(len (get-bytevector-n! user-port data 0 max-len)))
|
2013-12-20 11:17:42 -05:00
|
|
|
|
(write-int len p)
|
2014-05-27 16:01:51 -04:00
|
|
|
|
(put-bytevector p data 0 len)
|
2013-12-20 11:17:42 -05:00
|
|
|
|
(write-padding len p)
|
2012-06-16 10:13:12 -04:00
|
|
|
|
#f))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
((= k %stderr-next)
|
2013-12-20 11:17:42 -05:00
|
|
|
|
;; Log a string.
|
2013-01-31 08:05:10 -05:00
|
|
|
|
(let ((s (read-latin1-string p)))
|
2012-06-16 10:13:12 -04:00
|
|
|
|
(display s (current-build-output-port))
|
2014-05-21 09:29:23 -04:00
|
|
|
|
(when (string-any %newlines s)
|
|
|
|
|
(flush-output-port (current-build-output-port)))
|
2012-06-16 10:13:12 -04:00
|
|
|
|
#f))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
((= k %stderr-error)
|
2013-12-20 11:17:42 -05:00
|
|
|
|
;; Report an error.
|
2013-01-31 08:05:10 -05:00
|
|
|
|
(let ((error (read-latin1-string p))
|
2013-04-20 04:53:31 -04:00
|
|
|
|
;; Currently the daemon fails to send a status code for early
|
|
|
|
|
;; errors like DB schema version mismatches, so check for EOF.
|
|
|
|
|
(status (if (and (>= (nix-server-minor-version server) 8)
|
|
|
|
|
(not (eof-object? (lookahead-u8 p))))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
(read-int p)
|
|
|
|
|
1)))
|
2012-06-01 17:29:55 -04:00
|
|
|
|
(raise (condition (&nix-protocol-error
|
|
|
|
|
(message error)
|
|
|
|
|
(status status))))))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
((= k %stderr-last)
|
2012-06-16 10:13:12 -04:00
|
|
|
|
;; The daemon is done (see `stopWork' in `nix-worker.cc'.)
|
2012-04-18 17:34:12 -04:00
|
|
|
|
#t)
|
|
|
|
|
(else
|
2012-06-01 17:29:55 -04:00
|
|
|
|
(raise (condition (&nix-protocol-error
|
|
|
|
|
(message "invalid error code")
|
|
|
|
|
(status k))))))))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
|
|
|
|
|
(define* (set-build-options server
|
2013-05-29 17:04:15 -04:00
|
|
|
|
#:key keep-failed? keep-going? fallback?
|
2012-04-18 17:34:12 -04:00
|
|
|
|
(verbosity 0)
|
|
|
|
|
(max-build-jobs (current-processor-count))
|
2014-03-09 18:01:18 -04:00
|
|
|
|
timeout
|
2012-04-18 17:34:12 -04:00
|
|
|
|
(max-silent-time 3600)
|
|
|
|
|
(use-build-hook? #t)
|
|
|
|
|
(build-verbosity 0)
|
|
|
|
|
(log-type 0)
|
2012-06-30 18:09:47 -04:00
|
|
|
|
(print-build-trace #t)
|
|
|
|
|
(build-cores 1)
|
2013-02-03 17:02:14 -05:00
|
|
|
|
(use-substitutes? #t)
|
|
|
|
|
(binary-caches '())) ; client "untrusted" cache URLs
|
2012-04-18 17:34:12 -04:00
|
|
|
|
;; Must be called after `open-connection'.
|
|
|
|
|
|
|
|
|
|
(define socket
|
|
|
|
|
(nix-server-socket server))
|
|
|
|
|
|
|
|
|
|
(let-syntax ((send (syntax-rules ()
|
2012-06-30 18:09:47 -04:00
|
|
|
|
((_ (type option) ...)
|
|
|
|
|
(begin
|
|
|
|
|
(write-arg type option socket)
|
|
|
|
|
...)))))
|
|
|
|
|
(write-int (operation-id set-options) socket)
|
|
|
|
|
(send (boolean keep-failed?) (boolean keep-going?)
|
2013-05-29 17:04:15 -04:00
|
|
|
|
(boolean fallback?) (integer verbosity)
|
2012-06-30 18:09:47 -04:00
|
|
|
|
(integer max-build-jobs) (integer max-silent-time))
|
2014-02-27 13:17:10 -05:00
|
|
|
|
(when (>= (nix-server-minor-version server) 2)
|
|
|
|
|
(send (boolean use-build-hook?)))
|
|
|
|
|
(when (>= (nix-server-minor-version server) 4)
|
|
|
|
|
(send (integer build-verbosity) (integer log-type)
|
|
|
|
|
(boolean print-build-trace)))
|
|
|
|
|
(when (>= (nix-server-minor-version server) 6)
|
|
|
|
|
(send (integer build-cores)))
|
|
|
|
|
(when (>= (nix-server-minor-version server) 10)
|
|
|
|
|
(send (boolean use-substitutes?)))
|
|
|
|
|
(when (>= (nix-server-minor-version server) 12)
|
2014-03-09 18:01:18 -04:00
|
|
|
|
(let ((pairs (if timeout
|
|
|
|
|
`(("build-timeout" . ,(number->string timeout))
|
|
|
|
|
,@binary-caches)
|
|
|
|
|
binary-caches)))
|
|
|
|
|
(send (string-pairs pairs))))
|
2012-06-16 10:13:12 -04:00
|
|
|
|
(let loop ((done? (process-stderr server)))
|
|
|
|
|
(or done? (process-stderr server)))))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
|
2013-01-30 10:22:33 -05:00
|
|
|
|
(define-syntax operation
|
2012-04-18 17:34:12 -04:00
|
|
|
|
(syntax-rules ()
|
2013-01-30 10:22:33 -05:00
|
|
|
|
"Define a client-side RPC stub for the given operation."
|
2012-12-09 17:52:59 -05:00
|
|
|
|
((_ (name (type arg) ...) docstring return ...)
|
2013-01-30 10:22:33 -05:00
|
|
|
|
(lambda (server arg ...)
|
2012-04-18 17:34:12 -04:00
|
|
|
|
docstring
|
|
|
|
|
(let ((s (nix-server-socket server)))
|
|
|
|
|
(write-int (operation-id name) s)
|
|
|
|
|
(write-arg type arg s)
|
|
|
|
|
...
|
2012-06-16 10:13:12 -04:00
|
|
|
|
;; Loop until the server is done sending error output.
|
|
|
|
|
(let loop ((done? (process-stderr server)))
|
|
|
|
|
(or done? (loop (process-stderr server))))
|
2012-12-09 17:52:59 -05:00
|
|
|
|
(values (read-arg return s) ...))))))
|
2012-04-18 17:34:12 -04:00
|
|
|
|
|
2013-01-30 10:22:33 -05:00
|
|
|
|
(define-syntax-rule (define-operation (name args ...)
|
|
|
|
|
docstring return ...)
|
|
|
|
|
(define name
|
|
|
|
|
(operation (name args ...) docstring return ...)))
|
|
|
|
|
|
2012-06-30 18:21:16 -04:00
|
|
|
|
(define-operation (valid-path? (string path))
|
|
|
|
|
"Return #t when PATH is a valid store path."
|
|
|
|
|
boolean)
|
|
|
|
|
|
2013-02-03 17:02:14 -05:00
|
|
|
|
(define-operation (query-path-hash (store-path path))
|
2012-10-24 11:03:45 -04:00
|
|
|
|
"Return the SHA256 hash of PATH as a bytevector."
|
|
|
|
|
base16)
|
|
|
|
|
|
2014-03-14 12:16:10 -04:00
|
|
|
|
(define hash-part->path
|
|
|
|
|
(let ((query-path-from-hash-part
|
|
|
|
|
(operation (query-path-from-hash-part (string hash))
|
|
|
|
|
#f
|
|
|
|
|
store-path)))
|
|
|
|
|
(lambda (server hash-part)
|
|
|
|
|
"Return the store path whose hash part is HASH-PART (a nix-base32
|
|
|
|
|
string). Raise an error if no such path exists."
|
|
|
|
|
;; This RPC is primarily used by Hydra to reply to HTTP GETs of
|
|
|
|
|
;; /HASH.narinfo.
|
|
|
|
|
(query-path-from-hash-part server hash-part))))
|
|
|
|
|
|
2013-01-30 10:22:33 -05:00
|
|
|
|
(define add-text-to-store
|
|
|
|
|
;; A memoizing version of `add-to-store', to avoid repeated RPCs with
|
|
|
|
|
;; the very same arguments during a given session.
|
|
|
|
|
(let ((add-text-to-store
|
|
|
|
|
(operation (add-text-to-store (string name) (string text)
|
|
|
|
|
(string-list references))
|
|
|
|
|
#f
|
|
|
|
|
store-path)))
|
2013-09-10 16:36:41 -04:00
|
|
|
|
(lambda* (server name text #:optional (references '()))
|
2013-01-29 17:53:09 -05:00
|
|
|
|
"Add TEXT under file NAME in the store, and return its store path.
|
|
|
|
|
REFERENCES is the list of store paths referred to by the resulting store
|
|
|
|
|
path."
|
2013-01-30 11:05:54 -05:00
|
|
|
|
(let ((args `(,text ,name ,references))
|
2013-01-29 17:53:09 -05:00
|
|
|
|
(cache (nix-server-add-text-to-store-cache server)))
|
|
|
|
|
(or (hash-ref cache args)
|
2013-01-30 10:22:33 -05:00
|
|
|
|
(let ((path (add-text-to-store server name text references)))
|
2013-01-29 17:53:09 -05:00
|
|
|
|
(hash-set! cache args path)
|
|
|
|
|
path))))))
|
|
|
|
|
|
2013-01-30 10:22:33 -05:00
|
|
|
|
(define add-to-store
|
2013-01-28 18:16:18 -05:00
|
|
|
|
;; A memoizing version of `add-to-store'. This is important because
|
|
|
|
|
;; `add-to-store' leads to huge data transfers to the server, and
|
|
|
|
|
;; because it's often called many times with the very same argument.
|
2013-01-30 10:22:33 -05:00
|
|
|
|
(let ((add-to-store (operation (add-to-store (string basename)
|
|
|
|
|
(boolean fixed?) ; obsolete, must be #t
|
|
|
|
|
(boolean recursive?)
|
|
|
|
|
(string hash-algo)
|
|
|
|
|
(file file-name))
|
|
|
|
|
#f
|
|
|
|
|
store-path)))
|
2013-01-30 10:30:49 -05:00
|
|
|
|
(lambda (server basename recursive? hash-algo file-name)
|
|
|
|
|
"Add the contents of FILE-NAME under BASENAME to the store. When
|
|
|
|
|
RECURSIVE? is true and FILE-NAME designates a directory, the contents of
|
|
|
|
|
FILE-NAME are added recursively; if FILE-NAME designates a flat file and
|
|
|
|
|
RECURSIVE? is true, its contents are added, and its permission bits are
|
|
|
|
|
kept. HASH-ALGO must be a string such as \"sha256\"."
|
2013-01-29 17:34:50 -05:00
|
|
|
|
(let* ((st (stat file-name #f))
|
2013-01-30 11:05:54 -05:00
|
|
|
|
(args `(,st ,basename ,recursive? ,hash-algo))
|
2013-01-29 17:34:50 -05:00
|
|
|
|
(cache (nix-server-add-to-store-cache server)))
|
2013-01-28 18:16:18 -05:00
|
|
|
|
(or (and st (hash-ref cache args))
|
2013-01-30 10:30:49 -05:00
|
|
|
|
(let ((path (add-to-store server basename #t recursive?
|
2013-01-28 18:16:18 -05:00
|
|
|
|
hash-algo file-name)))
|
|
|
|
|
(hash-set! cache args path)
|
|
|
|
|
path))))))
|
|
|
|
|
|
2012-04-18 17:34:12 -04:00
|
|
|
|
(define-operation (build-derivations (string-list derivations))
|
2012-06-16 10:13:12 -04:00
|
|
|
|
"Build DERIVATIONS, and return when the worker is done building them.
|
|
|
|
|
Return #t on success."
|
2012-04-18 17:34:12 -04:00
|
|
|
|
boolean)
|
2012-06-01 17:29:55 -04:00
|
|
|
|
|
2012-12-09 17:50:17 -05:00
|
|
|
|
(define-operation (add-temp-root (store-path path))
|
|
|
|
|
"Make PATH a temporary root for the duration of the current session.
|
|
|
|
|
Return #t."
|
|
|
|
|
boolean)
|
|
|
|
|
|
2012-10-29 19:20:53 -04:00
|
|
|
|
(define-operation (add-indirect-root (string file-name))
|
2014-04-12 16:32:10 -04:00
|
|
|
|
"Make the symlink FILE-NAME an indirect root for the garbage collector:
|
|
|
|
|
whatever store item FILE-NAME points to will not be collected. Return #t on
|
|
|
|
|
success.
|
|
|
|
|
|
|
|
|
|
FILE-NAME can be anywhere on the file system, but it must be an absolute file
|
|
|
|
|
name--it is the caller's responsibility to ensure that it is an absolute file
|
|
|
|
|
name."
|
2012-10-29 19:20:53 -04:00
|
|
|
|
boolean)
|
|
|
|
|
|
2014-04-12 16:32:10 -04:00
|
|
|
|
(define %gc-roots-directory
|
|
|
|
|
;; The place where garbage collector roots (symlinks) are kept.
|
|
|
|
|
(string-append %state-directory "/gcroots"))
|
|
|
|
|
|
|
|
|
|
(define (add-permanent-root target)
|
|
|
|
|
"Add a garbage collector root pointing to TARGET, an element of the store,
|
|
|
|
|
preventing TARGET from even being collected. This can also be used if TARGET
|
|
|
|
|
does not exist yet.
|
|
|
|
|
|
|
|
|
|
Raise an error if the caller does not have write access to the GC root
|
|
|
|
|
directory."
|
|
|
|
|
(let* ((root (string-append %gc-roots-directory "/" (basename target))))
|
|
|
|
|
(catch 'system-error
|
|
|
|
|
(lambda ()
|
|
|
|
|
(symlink target root))
|
|
|
|
|
(lambda args
|
|
|
|
|
;; If ROOT already exists, this is fine; otherwise, re-throw.
|
|
|
|
|
(unless (= EEXIST (system-error-errno args))
|
|
|
|
|
(apply throw args))))))
|
|
|
|
|
|
|
|
|
|
(define (remove-permanent-root target)
|
|
|
|
|
"Remove the permanent garbage collector root pointing to TARGET. Raise an
|
|
|
|
|
error if there is no such root."
|
|
|
|
|
(delete-file (string-append %gc-roots-directory "/" (basename target))))
|
|
|
|
|
|
2013-02-27 16:40:35 -05:00
|
|
|
|
(define references
|
|
|
|
|
(operation (query-references (store-path path))
|
|
|
|
|
"Return the list of references of PATH."
|
|
|
|
|
store-path-list))
|
|
|
|
|
|
2013-06-13 16:03:42 -04:00
|
|
|
|
(define* (fold-path store proc seed path
|
|
|
|
|
#:optional (relatives (cut references store <>)))
|
|
|
|
|
"Call PROC for each of the RELATIVES of PATH, exactly once, and return the
|
|
|
|
|
result formed from the successive calls to PROC, the first of which is passed
|
|
|
|
|
SEED."
|
|
|
|
|
(let loop ((paths (list path))
|
|
|
|
|
(result seed)
|
|
|
|
|
(seen vlist-null))
|
|
|
|
|
(match paths
|
|
|
|
|
((path rest ...)
|
|
|
|
|
(if (vhash-assoc path seen)
|
|
|
|
|
(loop rest result seen)
|
|
|
|
|
(let ((seen (vhash-cons path #t seen))
|
|
|
|
|
(rest (append rest (relatives path)))
|
|
|
|
|
(result (proc path result)))
|
|
|
|
|
(loop rest result seen))))
|
|
|
|
|
(()
|
|
|
|
|
result))))
|
|
|
|
|
|
|
|
|
|
(define (requisites store path)
|
|
|
|
|
"Return the requisites of PATH, including PATH---i.e., its closure (all its
|
|
|
|
|
references, recursively)."
|
|
|
|
|
(fold-path store cons '() path))
|
|
|
|
|
|
2014-01-23 16:13:27 -05:00
|
|
|
|
(define (topologically-sorted store paths)
|
|
|
|
|
"Return a list containing PATHS and all their references sorted in
|
|
|
|
|
topological order."
|
|
|
|
|
(define (traverse)
|
|
|
|
|
;; Do a simple depth-first traversal of all of PATHS.
|
|
|
|
|
(let loop ((paths paths)
|
|
|
|
|
(visited vlist-null)
|
|
|
|
|
(result '()))
|
|
|
|
|
(define (visit n)
|
|
|
|
|
(vhash-cons n #t visited))
|
|
|
|
|
|
|
|
|
|
(define (visited? n)
|
|
|
|
|
(vhash-assoc n visited))
|
|
|
|
|
|
|
|
|
|
(match paths
|
|
|
|
|
((head tail ...)
|
|
|
|
|
(if (visited? head)
|
|
|
|
|
(loop tail visited result)
|
|
|
|
|
(call-with-values
|
|
|
|
|
(lambda ()
|
|
|
|
|
(loop (references store head)
|
|
|
|
|
(visit head)
|
|
|
|
|
result))
|
|
|
|
|
(lambda (visited result)
|
|
|
|
|
(loop tail
|
|
|
|
|
visited
|
|
|
|
|
(cons head result))))))
|
|
|
|
|
(()
|
|
|
|
|
(values visited result)))))
|
|
|
|
|
|
|
|
|
|
(call-with-values traverse
|
|
|
|
|
(lambda (_ result)
|
|
|
|
|
(reverse result))))
|
|
|
|
|
|
2013-02-27 16:40:35 -05:00
|
|
|
|
(define referrers
|
|
|
|
|
(operation (query-referrers (store-path path))
|
|
|
|
|
"Return the list of path that refer to PATH."
|
|
|
|
|
store-path-list))
|
|
|
|
|
|
|
|
|
|
(define valid-derivers
|
|
|
|
|
(operation (query-valid-derivers (store-path path))
|
|
|
|
|
"Return the list of valid \"derivers\" of PATH---i.e., all the
|
|
|
|
|
.drv present in the store that have PATH among their outputs."
|
|
|
|
|
store-path-list))
|
|
|
|
|
|
|
|
|
|
(define query-derivation-outputs ; avoid name clash with `derivation-outputs'
|
|
|
|
|
(operation (query-derivation-outputs (store-path path))
|
|
|
|
|
"Return the list of outputs of PATH, a .drv file."
|
|
|
|
|
store-path-list))
|
|
|
|
|
|
2013-02-03 17:24:25 -05:00
|
|
|
|
(define-operation (has-substitutes? (store-path path))
|
|
|
|
|
"Return #t if binary substitutes are available for PATH, and #f otherwise."
|
|
|
|
|
boolean)
|
|
|
|
|
|
|
|
|
|
(define substitutable-paths
|
|
|
|
|
(operation (query-substitutable-paths (store-path-list paths))
|
|
|
|
|
"Return the subset of PATHS that is substitutable."
|
|
|
|
|
store-path-list))
|
|
|
|
|
|
|
|
|
|
(define substitutable-path-info
|
2013-04-02 04:44:20 -04:00
|
|
|
|
(operation (query-substitutable-path-infos (store-path-list paths))
|
2013-02-03 17:24:25 -05:00
|
|
|
|
"Return information about the subset of PATHS that is
|
|
|
|
|
substitutable. For each substitutable path, a `substitutable?' object is
|
|
|
|
|
returned."
|
|
|
|
|
substitutable-path-list))
|
|
|
|
|
|
2012-12-09 17:52:59 -05:00
|
|
|
|
(define (run-gc server action to-delete min-freed)
|
|
|
|
|
"Perform the garbage-collector operation ACTION, one of the
|
|
|
|
|
`gc-action' values. When ACTION is `delete-specific', the TO-DELETE is
|
|
|
|
|
the list of store paths to delete. IGNORE-LIVENESS? should always be
|
|
|
|
|
#f. MIN-FREED is the minimum amount of disk space to be freed, in
|
|
|
|
|
bytes, before the GC can stop. Return the list of store paths delete,
|
|
|
|
|
and the number of bytes freed."
|
|
|
|
|
(let ((s (nix-server-socket server)))
|
|
|
|
|
(write-int (operation-id collect-garbage) s)
|
|
|
|
|
(write-int action s)
|
|
|
|
|
(write-store-path-list to-delete s)
|
|
|
|
|
(write-arg boolean #f s) ; ignore-liveness?
|
|
|
|
|
(write-long-long min-freed s)
|
|
|
|
|
(write-int 0 s) ; obsolete
|
|
|
|
|
(when (>= (nix-server-minor-version server) 5)
|
|
|
|
|
;; Obsolete `use-atime' and `max-atime' parameters.
|
|
|
|
|
(write-int 0 s)
|
|
|
|
|
(write-int 0 s))
|
|
|
|
|
|
|
|
|
|
;; Loop until the server is done sending error output.
|
|
|
|
|
(let loop ((done? (process-stderr server)))
|
|
|
|
|
(or done? (loop (process-stderr server))))
|
|
|
|
|
|
|
|
|
|
(let ((paths (read-store-path-list s))
|
|
|
|
|
(freed (read-long-long s))
|
|
|
|
|
(obsolete (read-long-long s)))
|
|
|
|
|
(values paths freed))))
|
|
|
|
|
|
|
|
|
|
(define-syntax-rule (%long-long-max)
|
|
|
|
|
;; Maximum unsigned 64-bit integer.
|
|
|
|
|
(- (expt 2 64) 1))
|
|
|
|
|
|
|
|
|
|
(define (live-paths server)
|
|
|
|
|
"Return the list of live store paths---i.e., store paths still
|
|
|
|
|
referenced, and thus not subject to being garbage-collected."
|
|
|
|
|
(run-gc server (gc-action return-live) '() (%long-long-max)))
|
|
|
|
|
|
|
|
|
|
(define (dead-paths server)
|
|
|
|
|
"Return the list of dead store paths---i.e., store paths no longer
|
|
|
|
|
referenced, and thus subject to being garbage-collected."
|
|
|
|
|
(run-gc server (gc-action return-dead) '() (%long-long-max)))
|
|
|
|
|
|
|
|
|
|
(define* (collect-garbage server #:optional (min-freed (%long-long-max)))
|
|
|
|
|
"Collect garbage from the store at SERVER. If MIN-FREED is non-zero,
|
|
|
|
|
then collect at least MIN-FREED bytes. Return the paths that were
|
|
|
|
|
collected, and the number of bytes freed."
|
|
|
|
|
(run-gc server (gc-action delete-dead) '() min-freed))
|
|
|
|
|
|
|
|
|
|
(define* (delete-paths server paths #:optional (min-freed (%long-long-max)))
|
|
|
|
|
"Delete PATHS from the store at SERVER, if they are no longer
|
|
|
|
|
referenced. If MIN-FREED is non-zero, then stop after at least
|
|
|
|
|
MIN-FREED bytes have been collected. Return the paths that were
|
|
|
|
|
collected, and the number of bytes freed."
|
|
|
|
|
(run-gc server (gc-action delete-specific) paths min-freed))
|
|
|
|
|
|
2013-12-20 11:17:42 -05:00
|
|
|
|
(define (import-paths server port)
|
|
|
|
|
"Import the set of store paths read from PORT into SERVER's store. An error
|
|
|
|
|
is raised if the set of paths read from PORT is not signed (as per
|
|
|
|
|
'export-path #:sign? #t'.) Return the list of store paths imported."
|
|
|
|
|
(let ((s (nix-server-socket server)))
|
|
|
|
|
(write-int (operation-id import-paths) s)
|
|
|
|
|
(let loop ((done? (process-stderr server port)))
|
|
|
|
|
(or done? (loop (process-stderr server port))))
|
|
|
|
|
(read-store-path-list s)))
|
|
|
|
|
|
|
|
|
|
(define* (export-path server path port #:key (sign? #t))
|
|
|
|
|
"Export PATH to PORT. When SIGN? is true, sign it."
|
|
|
|
|
(let ((s (nix-server-socket server)))
|
|
|
|
|
(write-int (operation-id export-path) s)
|
|
|
|
|
(write-store-path path s)
|
|
|
|
|
(write-arg boolean sign? s)
|
|
|
|
|
(let loop ((done? (process-stderr server port)))
|
|
|
|
|
(or done? (loop (process-stderr server port))))
|
|
|
|
|
(= 1 (read-int s))))
|
|
|
|
|
|
|
|
|
|
(define* (export-paths server paths port #:key (sign? #t))
|
2014-02-10 17:30:09 -05:00
|
|
|
|
"Export the store paths listed in PATHS to PORT, in topological order,
|
|
|
|
|
signing them if SIGN? is true."
|
2014-03-06 18:18:28 -05:00
|
|
|
|
(define ordered
|
|
|
|
|
;; Sort PATHS, but don't include their references.
|
|
|
|
|
(filter (cut member <> paths)
|
|
|
|
|
(topologically-sorted server paths)))
|
|
|
|
|
|
2013-12-20 11:17:42 -05:00
|
|
|
|
(let ((s (nix-server-socket server)))
|
2014-03-06 18:18:28 -05:00
|
|
|
|
(let loop ((paths ordered))
|
2013-12-20 11:17:42 -05:00
|
|
|
|
(match paths
|
|
|
|
|
(()
|
|
|
|
|
(write-int 0 port))
|
|
|
|
|
((head tail ...)
|
|
|
|
|
(write-int 1 port)
|
|
|
|
|
(and (export-path server head port #:sign? sign?)
|
|
|
|
|
(loop tail)))))))
|
|
|
|
|
|
2014-01-11 11:11:14 -05:00
|
|
|
|
(define* (register-path path
|
2014-07-18 05:03:50 -04:00
|
|
|
|
#:key (references '()) deriver prefix
|
|
|
|
|
state-directory)
|
2014-01-11 11:11:14 -05:00
|
|
|
|
"Register PATH as a valid store file, with REFERENCES as its list of
|
2014-05-18 13:12:43 -04:00
|
|
|
|
references, and DERIVER as its deriver (.drv that led to it.) If PREFIX is
|
|
|
|
|
not #f, it must be the name of the directory containing the new store to
|
2014-07-18 05:03:50 -04:00
|
|
|
|
initialize; if STATE-DIRECTORY is not #f, it must be a string containing the
|
|
|
|
|
absolute file name to the state directory of the store being initialized.
|
|
|
|
|
Return #t on success.
|
2014-01-11 11:11:14 -05:00
|
|
|
|
|
|
|
|
|
Use with care as it directly modifies the store! This is primarily meant to
|
|
|
|
|
be used internally by the daemon's build hook."
|
|
|
|
|
;; Currently this is implemented by calling out to the fine C++ blob.
|
|
|
|
|
(catch 'system-error
|
|
|
|
|
(lambda ()
|
2014-05-16 18:08:39 -04:00
|
|
|
|
(let ((pipe (apply open-pipe* OPEN_WRITE %guix-register-program
|
2014-07-18 05:03:50 -04:00
|
|
|
|
`(,@(if prefix
|
|
|
|
|
`("--prefix" ,prefix)
|
|
|
|
|
'())
|
|
|
|
|
,@(if state-directory
|
|
|
|
|
`("--state-directory" ,state-directory)
|
|
|
|
|
'())))))
|
2014-01-11 11:11:14 -05:00
|
|
|
|
(and pipe
|
|
|
|
|
(begin
|
|
|
|
|
(format pipe "~a~%~a~%~a~%"
|
|
|
|
|
path (or deriver "") (length references))
|
|
|
|
|
(for-each (cut format pipe "~a~%" <>) references)
|
|
|
|
|
(zero? (close-pipe pipe))))))
|
|
|
|
|
(lambda args
|
|
|
|
|
;; Failed to run %GUIX-REGISTER-PROGRAM.
|
|
|
|
|
#f)))
|
|
|
|
|
|
2012-06-01 17:29:55 -04:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Store paths.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define %store-prefix
|
|
|
|
|
;; Absolute path to the Nix store.
|
2014-01-08 15:37:06 -05:00
|
|
|
|
(make-parameter %store-directory))
|
2012-06-01 17:29:55 -04:00
|
|
|
|
|
2012-09-01 13:21:06 -04:00
|
|
|
|
(define (store-path? path)
|
|
|
|
|
"Return #t if PATH is a store path."
|
|
|
|
|
;; This is a lightweight check, compared to using a regexp, but this has to
|
|
|
|
|
;; be fast as it's called often in `derivation', for instance.
|
|
|
|
|
;; `isStorePath' in Nix does something similar.
|
|
|
|
|
(string-prefix? (%store-prefix) path))
|
2012-06-01 17:29:55 -04:00
|
|
|
|
|
2013-11-12 17:36:29 -05:00
|
|
|
|
(define (direct-store-path? path)
|
|
|
|
|
"Return #t if PATH is a store path, and not a sub-directory of a store path.
|
|
|
|
|
This predicate is sometimes needed because files *under* a store path are not
|
|
|
|
|
valid inputs."
|
|
|
|
|
(and (store-path? path)
|
2014-04-26 10:38:38 -04:00
|
|
|
|
(not (string=? path (%store-prefix)))
|
2013-11-12 17:36:29 -05:00
|
|
|
|
(let ((len (+ 1 (string-length (%store-prefix)))))
|
|
|
|
|
(not (string-index (substring path len) #\/)))))
|
|
|
|
|
|
2012-06-01 17:29:55 -04:00
|
|
|
|
(define (derivation-path? path)
|
|
|
|
|
"Return #t if PATH is a derivation path."
|
|
|
|
|
(and (store-path? path) (string-suffix? ".drv" path)))
|
2012-10-31 20:39:23 -04:00
|
|
|
|
|
2013-12-10 18:19:27 -05:00
|
|
|
|
(define store-regexp*
|
|
|
|
|
;; The substituter makes repeated calls to 'store-path-hash-part', hence
|
|
|
|
|
;; this optimization.
|
|
|
|
|
(memoize
|
|
|
|
|
(lambda (store)
|
|
|
|
|
"Return a regexp matching a file in STORE."
|
|
|
|
|
(make-regexp (string-append "^" (regexp-quote store)
|
|
|
|
|
"/([0-9a-df-np-sv-z]{32})-([^/]+)$")))))
|
|
|
|
|
|
2012-10-31 20:39:23 -04:00
|
|
|
|
(define (store-path-package-name path)
|
|
|
|
|
"Return the package name part of PATH, a file name in the store."
|
2013-12-10 18:19:27 -05:00
|
|
|
|
(let ((path-rx (store-regexp* (%store-prefix))))
|
|
|
|
|
(and=> (regexp-exec path-rx path)
|
|
|
|
|
(cut match:substring <> 2))))
|
2013-04-01 10:08:31 -04:00
|
|
|
|
|
|
|
|
|
(define (store-path-hash-part path)
|
|
|
|
|
"Return the hash part of PATH as a base32 string, or #f if PATH is not a
|
|
|
|
|
syntactically valid store path."
|
2013-12-10 18:19:27 -05:00
|
|
|
|
(let ((path-rx (store-regexp* (%store-prefix))))
|
2013-04-01 10:08:31 -04:00
|
|
|
|
(and=> (regexp-exec path-rx path)
|
|
|
|
|
(cut match:substring <> 1))))
|
2013-11-11 18:10:10 -05:00
|
|
|
|
|
|
|
|
|
(define (log-file store file)
|
|
|
|
|
"Return the build log file for FILE, or #f if none could be found. FILE
|
|
|
|
|
must be an absolute store file name, or a derivation file name."
|
|
|
|
|
(cond ((derivation-path? file)
|
2014-01-08 13:23:33 -05:00
|
|
|
|
(let* ((base (basename file))
|
2014-01-08 15:12:55 -05:00
|
|
|
|
(log (string-append (dirname %state-directory) ; XXX
|
2014-03-09 17:05:29 -04:00
|
|
|
|
"/log/guix/drvs/"
|
2014-01-08 13:23:33 -05:00
|
|
|
|
(string-take base 2) "/"
|
|
|
|
|
(string-drop base 2)))
|
|
|
|
|
(log.bz2 (string-append log ".bz2")))
|
|
|
|
|
(cond ((file-exists? log.bz2) log.bz2)
|
|
|
|
|
((file-exists? log) log)
|
|
|
|
|
(else #f))))
|
2013-11-11 18:10:10 -05:00
|
|
|
|
(else
|
|
|
|
|
(match (valid-derivers store file)
|
|
|
|
|
((derivers ...)
|
|
|
|
|
;; Return the first that works.
|
|
|
|
|
(any (cut log-file store <>) derivers))
|
|
|
|
|
(_ #f)))))
|