2 # aside from this initial boilerplate, this is actually -*- scheme -*- code
5 datarootdir
="@datarootdir@"
7 GUILE_LOAD_COMPILED_PATH
="@guilemoduledir@:$GUILE_LOAD_COMPILED_PATH"
8 export GUILE_LOAD_COMPILED_PATH
10 main
='(module-ref (resolve-interface '\''(guix-gc)) '\'guix-gc
')'
11 exec ${GUILE-@GUILE@} -L "@guilemoduledir@" -l "$0" \
12 -c "(apply $main (cdr (command-line)))" "$@"
14 ;;; GNU Guix
--- Functional package management
for GNU
15 ;;; Copyright ©
2012, 2013 Ludovic Courtès
<ludo@gnu.org
>
17 ;;; This
file is part of GNU Guix.
19 ;;; GNU Guix is free software
; you can redistribute it and
/or modify it
20 ;;; under the terms of the GNU General Public License as published by
21 ;;; the Free Software Foundation
; either version
3 of the License
, or
(at
22 ;;; your option
) any later version.
24 ;;; GNU Guix is distributed
in the hope that it will be useful
, but
25 ;;; WITHOUT ANY WARRANTY
; without even the implied warranty of
26 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;;; GNU General Public License
for more details.
29 ;;; You should have received a copy of the GNU General Public License
30 ;;; along with GNU Guix. If not
, see
<http
://www.gnu.org
/licenses
/>.
32 (define-module
(guix-gc
)
33 #:use-module (guix ui)
34 #:use-module (guix store)
35 #:use-module (ice-9 match)
36 #:use-module (srfi srfi-1)
37 #:use-module (srfi srfi-26)
38 #:use-module (srfi srfi-37)
43 ;;; Command-line options.
46 (define
%default-options
47 ;; Alist of default option values.
48 `((action . collect-garbage)))
51 (display (_ "Usage: guix-gc [OPTION]... PATHS...
52 Invoke the garbage collector.\n"))
54 -C, --collect-garbage[=MIN]
55 collect at least MIN bytes of garbage"))
57 -d, --delete attempt to delete PATHS"))
59 --list-dead list dead paths"))
61 --list-live list live paths"))
64 -h, --help display this help and exit"))
66 -V, --version display version information and exit"))
68 (show-bug-report-information))
70 (define (size->number str)
71 "Convert STR, a storage measurement representation such as \"1024\" or
72 \"1MiB\", to a number of bytes. Raise an error if STR could not be
75 (string-rindex str char-set:digit))
78 (and unit-pos (substring str (+ 1 unit-pos))))
80 (let* ((numstr (if unit-pos
81 (substring str 0 (+ 1 unit-pos))
83 (num (string->number numstr)))
97 (format (current-error-port) (_ "error: unknown unit: ~a~%")
101 (format (current-error-port)
102 (_ "error: invalid number: ~a") numstr)
106 ;; Specification of the command-line options.
107 (list (option '(#\h "help") #f #f
111 (option '(#\V "version") #f #f
113 (show-version-and-exit "guix-gc")))
115 (option '(#\C "collect-garbage") #f #t
116 (lambda (opt name arg result)
117 (let ((result (alist-cons 'action 'collect-garbage
118 (alist-delete 'action result))))
121 (let ((amount (size->number arg)))
123 (alist-cons 'min-freed amount result)
125 (format (current-error-port)
126 (_ "error: invalid amount of storage: ~a~%")
130 (option '(#\d "delete") #f #f
131 (lambda (opt name arg result)
132 (alist-cons 'action 'delete
133 (alist-delete 'action result))))
134 (option '("list-dead") #f #f
135 (lambda (opt name arg result)
136 (alist-cons 'action 'list-dead
137 (alist-delete 'action result))))
138 (option '("list-live") #f #f
139 (lambda (opt name arg result)
140 (alist-cons 'action 'list-live
141 (alist-delete 'action result))))))
148 (define (guix-gc . args)
149 (define (parse-options)
150 ;; Return the alist of option values.
151 (args-fold args %options
152 (lambda (opt name arg result)
153 (leave (_ "~A: unrecognized option~%") name))
155 (alist-cons 'argument arg result))
158 (setlocale LC_ALL "")
160 (setvbuf (current-output-port) _IOLBF)
161 (setvbuf (current-error-port) _IOLBF)
164 (let ((opts (parse-options))
165 (store (open-connection)))
166 (case (assoc-ref opts 'action)
168 (let ((min-freed (assoc-ref opts 'min-freed)))
170 (collect-garbage store min-freed)
171 (collect-garbage store))))
173 (let ((paths (filter-map (match-lambda
174 (('argument . arg) arg)
177 (delete-paths store paths)))
179 (for-each (cut simple-format #t "~a~%" <>)
182 (for-each (cut simple-format #t "~a~%" <>)
183 (live-paths store)))))))