cache: Catch invalid 'last-expiry-cleanup'.

Fixes <http://issues.guix.gnu.org/55638>.

* guix/cache.scm (maybe-remove-expired-cache-entries)[last-expiry-date]:
Use 'get-string-all' + 'string->number' instead of 'read'; ignore
invalid numbers.
* tests/cache.scm ("maybe-remove-expired-cache-entries, empty cache")
("maybe-remove-expired-cache-entries, corrupted cache"): New tests.

Co-authored-by: Ludovic Courtès <ludo@gnu.org>
This commit is contained in:
zimoun 2022-05-30 15:07:14 +02:00 committed by Ludovic Courtès
parent c332f1f4fb
commit 104b4e25ab
No known key found for this signature in database
GPG key ID: 090B11993D9AEBB5
2 changed files with 22 additions and 2 deletions

View file

@ -1,5 +1,6 @@
;;; GNU Guix --- Functional package management for GNU ;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2020, 2021 Ludovic Courtès <ludo@gnu.org> ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2022 Simon Tournier <zimon.toutoune@gmail.com>
;;; ;;;
;;; This file is part of GNU Guix. ;;; This file is part of GNU Guix.
;;; ;;;
@ -17,9 +18,11 @@
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
(define-module (guix cache) (define-module (guix cache)
#:use-module ((guix utils) #:select (with-atomic-file-output))
#:use-module (srfi srfi-19) #:use-module (srfi srfi-19)
#:use-module (srfi srfi-26) #:use-module (srfi srfi-26)
#:use-module (ice-9 match) #:use-module (ice-9 match)
#:use-module ((ice-9 textual-ports) #:select (get-string-all))
#:export (obsolete? #:export (obsolete?
delete-file* delete-file*
file-expiration-time file-expiration-time
@ -93,7 +96,9 @@ (define expiry-file
(define last-expiry-date (define last-expiry-date
(catch 'system-error (catch 'system-error
(lambda () (lambda ()
(call-with-input-file expiry-file read)) (or (string->number
(call-with-input-file expiry-file get-string-all))
0))
(const 0))) (const 0)))
(when (obsolete? last-expiry-date now cleanup-period) (when (obsolete? last-expiry-date now cleanup-period)
@ -103,7 +108,7 @@ (define last-expiry-date
#:delete-entry delete-entry) #:delete-entry delete-entry)
(catch 'system-error (catch 'system-error
(lambda () (lambda ()
(call-with-output-file expiry-file (with-atomic-file-output expiry-file
(cute write (time-second now) <>))) (cute write (time-second now) <>)))
(lambda args (lambda args
;; ENOENT means CACHE does not exist. ;; ENOENT means CACHE does not exist.

View file

@ -1,5 +1,6 @@
;;; GNU Guix --- Functional package management for GNU ;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2017, 2020 Ludovic Courtès <ludo@gnu.org> ;;; Copyright © 2017, 2020 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2022 Simon Tournier <zimon.toutoune@gmail.com>
;;; ;;;
;;; This file is part of GNU Guix. ;;; This file is part of GNU Guix.
;;; ;;;
@ -74,6 +75,20 @@ (define-syntax-rule (test-cache-cleanup cache exp ...)
(lambda (port) (lambda (port)
(display 0 port))))) (display 0 port)))))
(test-equal "maybe-remove-expired-cache-entries, empty cache"
'("a" "b" "c")
(test-cache-cleanup cache
(call-with-output-file (string-append cache "/last-expiry-cleanup")
(lambda (port)
(display "" port)))))
(test-equal "maybe-remove-expired-cache-entries, corrupted cache"
'("a" "b" "c")
(test-cache-cleanup cache
(call-with-output-file (string-append cache "/last-expiry-cleanup")
(lambda (port)
(display "1\"34657890" port)))))
(test-end "cache") (test-end "cache")
;;; Local Variables: ;;; Local Variables: