aboutsummaryrefslogtreecommitdiff
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2021, 2022 Andrew Tropin <andrew@trop.in>
;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
;;; Copyright © 2023 Bruno Victal <mirai@makinata.eu>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; 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.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; 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
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu home services xdg)
  #:use-module (gnu services configuration)
  #:use-module (gnu home services)
  #:use-module (gnu packages freedesktop)
  #:use-module (gnu home services utils)
  #:use-module (guix deprecation)
  #:use-module (guix gexp)
  #:use-module (guix modules)
  #:use-module (guix records)
  #:use-module (guix i18n)
  #:use-module (guix diagnostics)

  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:use-module (rnrs enums)

  #:export (home-xdg-base-directories-service-type
            home-xdg-base-directories-configuration
            home-xdg-base-directories-configuration?
            home-xdg-base-directories-configuration-cache-home
            home-xdg-base-directories-configuration-config-home
            home-xdg-base-directories-configuration-data-home
            home-xdg-base-directories-configuration-state-home
            home-xdg-base-directories-configuration-log-home  ; deprecated
            home-xdg-base-directories-configuration-runtime-dir

            home-xdg-user-directories-service-type
            home-xdg-user-directories-configuration
            home-xdg-user-directories-configuration?
            home-xdg-user-directories-configuration-desktop
            home-xdg-user-directories-configuration-documents
            home-xdg-user-directories-configuration-download
            home-xdg-user-directories-configuration-music
            home-xdg-user-directories-configuration-pictures
            home-xdg-user-directories-configuration-publicshare
            home-xdg-user-directories-configuration-templates
            home-xdg-user-directories-configuration-videos

            xdg-desktop-action
            xdg-desktop-entry
            home-xdg-mime-applications-service-type
            home-xdg-mime-applications-configuration))

;;; Commentary:
;;
;; This module contains services related to XDG directories and
;; applications.
;;
;; - XDG base directories
;; - XDG user directories
;; - XDG MIME applications
;;
;;; Code:


;;;
;;; XDG base directories.
;;;

(define (serialize-path field-name val) "")
(define path? string?)
(define-maybe path)

(define-configuration home-xdg-base-directories-configuration
  (cache-home
   (path "$HOME/.cache")
   "Base directory for programs to store user-specific non-essential
(cached) data.  Files in this directory can be deleted anytime without
loss of important data.")
  (config-home
   (path "$HOME/.config")
   "Base directory for programs to store configuration files.
Some programs store here log or state files, but it's not desired,
this directory should contain static configurations.")
  (data-home
   (path "$HOME/.local/share")
   "Base directory for programs to store architecture independent
read-only shared data, analogus to @file{/usr/share}, but for user.")
  (runtime-dir
   (path "${XDG_RUNTIME_DIR:-/run/user/$UID}")
   "Base directory for programs to store user-specific runtime files,
like sockets.")
  ;; TODO: deprecated field, use $XDG_STATE_HOME(/log) instead.
  (log-home
   maybe-path
   "Base directory for programs to store log files, analogus to
@file{/var/log}, but for user.  It is not a part of XDG Base Directory
Specification, but helps to make implementation of home services more
consistent."
   (lambda (field-name val)
     (when (maybe-value-set? val)
       (warn-about-deprecation field-name #f #:replacement 'state-home))
     (serialize-path field-name val)))
  (state-home
   (path "$HOME/.local/state")
   "Base directory for programs to store state data that should persist
between (application) restarts, such as logs, but are not important or
portable enough to the user to warrant storing them in
@env{XDG_DATA_HOME}."))

(define (home-xdg-base-directories-environment-variables-service config)
  (map
   (lambda (field)
     (cons (format
            #f "XDG_~a"
            (object->snake-case-string (configuration-field-name field) 'upper))
           ((configuration-field-getter field) config)))
   ;; XXX: deprecated field, remove later
   (if (maybe-value-set?
        (home-xdg-base-directories-configuration-log-home config))
       home-xdg-base-directories-configuration-fields
       (filter-configuration-fields
        home-xdg-base-directories-configuration-fields
        '(log-home) #t))))

(define (ensure-xdg-base-dirs-on-activation config)
  (with-imported-modules '((guix build utils))
    #~(begin
        (use-modules (guix build utils))
        (map (lambda (xdg-base-dir-variable)
               (mkdir-p
                (getenv
                 xdg-base-dir-variable)))
             '#$(filter-map
                 (lambda (field)
                   (let ((variable
                          (string-append
                           "XDG_"
                           (object->snake-case-string
                            (configuration-field-name field) 'upper))))
                     ;; XDG_RUNTIME_DIR shouldn't be created during activation
                     ;; and will be provided by elogind or other service.
                     (and (not (string=? "XDG_RUNTIME_DIR" variable))
                          variable)))
                 ;; XXX: deprecated field, remove later
                 (if (maybe-value-set?
                      (home-xdg-base-directories-configuration-log-home
                       config))
                     home-xdg-base-directories-configuration-fields
                     (filter-configuration-fields
                      home-xdg-base-directories-configuration-fields
                      '(log-home) #t)))))))

(define (last-extension-or-cfg config extensions)
  "Picks configuration value from last provided extension.  If there
are no extensions use configuration instead."
  (or (and (not (null? extensions)) (last extensions)) config))

(define home-xdg-base-directories-service-type
  (service-type (name 'home-xdg-base-directories)
                (extensions
                 (list (service-extension
                        home-environment-variables-service-type
                        home-xdg-base-directories-environment-variables-service)
                       (service-extension
                        home-activation-service-type
                        ensure-xdg-base-dirs-on-activation)))
                (default-value (home-xdg-base-directories-configuration))
                (compose identity)
                (extend last-extension-or-cfg)
                (description "Configure XDG base directories.  The
services of this service-type is instantiated by default, to provide
non-default value, extend the service-type (using @code{simple-service}
for example).")))

(define (generate-home-xdg-base-directories-documentation)
  (generate-documentation
   `((home-xdg-base-directories-configuration
      ,home-xdg-base-directories-configuration-fields))
   'home-xdg-base-directories-configuration))


;;;
;;; XDG user directories.
;;;

(define (serialize-string field-name val)
  ;; The path has to be quoted
  (format #f "XDG_~a_DIR=\"~a\"\n"
          (object->snake-case-string field-name 'upper) val))

(define-configuration home-xdg-user-directories-configuration
  (desktop
   (string "$HOME/Desktop")
   "Default ``desktop'' directory, this is what you see on your
desktop when using a desktop environment,
e.g. GNOME (@pxref{XWindow,,,guix.info}).")
  (documents
   (string "$HOME/Documents")
   "Default directory to put documents like PDFs.")
  (download
   (string "$HOME/Downloads")
   "Default directory downloaded files, this is where your Web-broser
will put downloaded files in.")
  (music
   (string "$HOME/Music")
   "Default directory for audio files.")
  (pictures
   (string "$HOME/Pictures")
   "Default directory for pictures and images.")
  (publicshare
   (string "$HOME/Public")
   "Default directory for shared files, which can be accessed by other
users on local machine or via network.")
  (templates
   (string "$HOME/Templates")
   "Default directory for templates.  They can be used by graphical
file manager or other apps for creating new files with some
pre-populated content.")
  (videos
   (string "$HOME/Videos")
   "Default directory for videos."))

(define (home-xdg-user-directories-files-service config)
  `(("user-dirs.conf"
     ,(mixed-text-file
       "user-dirs.conf"
       "enabled=False\n"))
    ("user-dirs.dirs"
     ,(mixed-text-file
       "user-dirs.dirs"
      (serialize-configuration
       config
       home-xdg-user-directories-configuration-fields)))))

(define (home-xdg-user-directories-activation-service config)
  (let ((dirs (map (lambda (field)
                     ((configuration-field-getter field) config))
                   home-xdg-user-directories-configuration-fields)))
    #~(let ((ensure-dir
             (lambda (path)
               ((@ (guix build utils) mkdir-p)
                ((@ (ice-9 string-fun) string-replace-substring)
                 path "$HOME" (getenv "HOME"))))))
        (display "Creating XDG user directories...")
        (map ensure-dir '#$dirs)
        (display " done\n"))))

(define home-xdg-user-directories-service-type
  (service-type (name 'home-xdg-user-directories)
                (extensions
                 (list (service-extension
                        home-xdg-configuration-files-service-type
                        home-xdg-user-directories-files-service)
                       (service-extension
                        home-activation-service-type
                        home-xdg-user-directories-activation-service)))
                (default-value (home-xdg-user-directories-configuration))
                (compose identity)
                (extend last-extension-or-cfg)
                (description "Configure XDG user directories.  To
disable a directory, point it to the $HOME.")))

(define (generate-home-xdg-user-directories-documentation)
  (generate-documentation
   `((home-xdg-user-directories-configuration
     ,home-xdg-user-directories-configuration-fields))
   'home-xdg-user-directories-configuration))


;;;
;;; XDG MIME applications.
;;;

;; Example config
;;
;;  (home-xdg-mime-applications-configuration
;;   (added '((x-scheme-handler/magnet . torrent.desktop)))
;;   (default '((inode/directory . file.desktop)))
;;   (removed '((inode/directory . thunar.desktop)))
;;   (desktop-entries
;;    (list (xdg-desktop-entry
;;           (file "file")
;;           (name "File manager")
;;           (type 'application)
;;           (config
;;            '((exec . "emacsclient -c -a emacs %u"))))
;;          (xdg-desktop-entry
;;           (file "text")
;;           (name "Text editor")
;;           (type 'application)
;;           (config
;;            '((exec . "emacsclient -c -a emacs %u")))
;;           (actions
;;            (list (xdg-desktop-action
;;                   (action 'create)
;;                   (name "Create an action")
;;                   (config
;;                    '((exec . "echo hi"))))))))))

;; See
;; <https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html>
;; <https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-latest.html>

(define (serialize-alist field-name val)
  (define (serialize-mimelist-entry key val)
    (let ((val (cond
                ((list? val)
                 (string-join (map maybe-object->string val) ";"))
                ((or (string? val) (symbol? val))
                 val)
                (else (raise (formatted-message
                              (G_ "\
The value of an XDG MIME entry must be a list, string or symbol, was given ~a")
                              val))))))
      (format #f "~a=~a\n" key val)))

  (define (merge-duplicates alist acc)
    "Merge values that have the same key.

@example
(merge-duplicates '((key1 . value1)
                    (key2 . value2)
                    (key1 . value3)
                    (key1 . value4)) '())

@result{} ((key1 . (value4 value3 value1)) (key2 . value2))
@end example"
    (cond
     ((null? alist) acc)
     (else (let* ((head (first alist))
                  (tail (cdr alist))
                  (key (first head))
                  (value (cdr head))
                  (duplicate? (assoc key acc))
                  (ensure-list (lambda (x)
                                 (if (list? x) x (list x)))))
             (if duplicate?
                 ;; XXX: This will change the order of things,
                 ;; though, it shouldn't be a problem for XDG MIME.
                 (merge-duplicates
                  tail
                  (alist-cons key
                              (cons value (ensure-list (cdr duplicate?)))
                              (alist-delete key acc)))
                 (merge-duplicates tail (cons head acc)))))))

  (string-append (if (equal? field-name 'default)
                     "\n[Default Applications]\n"
                     (format #f "\n[~a Associations]\n"
                             (string-capitalize (symbol->string field-name))))
                 (generic-serialize-alist string-append
                                          serialize-mimelist-entry
                                          (merge-duplicates val '()))))

(define xdg-desktop-types (make-enumeration
                           '(application
                             link
                             directory)))

(define (xdg-desktop-type? type)
  (unless (enum-set-member? type xdg-desktop-types)
    (raise (formatted-message
            (G_ "XDG desktop type must be of of ~a, was given: ~a")
            (list->human-readable-list (enum-set->list xdg-desktop-types))
            type))))

;; TODO: Add proper docs for this
;; XXX: 'define-configuration' require that fields have a default
;; value.
(define-record-type* <xdg-desktop-action>
  xdg-desktop-action make-xdg-desktop-action
  xdg-desktop-action?
  (action xdg-desktop-action-action)  ; symbol
  (name   xdg-desktop-action-name)    ; string
  (config xdg-desktop-action-config   ; alist
          (default '())))

(define-record-type* <xdg-desktop-entry>
  xdg-desktop-entry make-xdg-desktop-entry
  xdg-desktop-entry?
  ;; ".desktop" will automatically be added
  (file    xdg-desktop-entry-file)    ; string
  (name    xdg-desktop-entry-name)    ; string
  (type    xdg-desktop-entry-type)    ; xdg-desktop-type
  (config  xdg-desktop-entry-config   ; alist
           (default '()))
  (actions xdg-desktop-entry-actions  ; list of <xdg-desktop-action>
           (default '())))

(define desktop-entries? (list-of xdg-desktop-entry?))
(define (serialize-desktop-entries field-name val) "")

(define (serialize-xdg-desktop-entry entry)
  "Return a tuple of the file name for ENTRY and the serialized
configuration."
  (define (format-config key val)
    (let ((val (cond
                ((list? val)
                 (string-join (map maybe-object->string val) ";"))
                ((boolean? val)
                 (if val "true" "false"))
                (else val)))
          (key (string-capitalize (maybe-object->string key))))
      (list (if (string-suffix? key "?")
                (string-drop-right key (- (string-length key) 1))
                key)
            "=" val "\n")))

  (define (serialize-alist config)
    (generic-serialize-alist append format-config config))

  (define (serialize-xdg-desktop-action desktop-action)
    (match-record desktop-action <xdg-desktop-action>
      (action name config)
      `(,(format #f "[Desktop Action ~a]\n"
                 (string-capitalize (maybe-object->string action)))
        ,(format #f "Name=~a\n" name)
        ,@(serialize-alist config))))

  (match-record entry <xdg-desktop-entry>
    (file name type config actions)
    (list (if (string-suffix? file ".desktop")
              file
              (string-append file ".desktop"))
          `("[Desktop Entry]\n"
            ,(format #f "Name=~a\n" name)
            ,(format #f "Type=~a\n"
                     (string-capitalize (symbol->string type)))
            ,@(serialize-alist config)
            ,@(append-map serialize-xdg-desktop-action actions)))))

(define-configuration home-xdg-mime-applications-configuration
  (added
   (alist '())
   "An association list of MIME types and desktop entries which indicate
that the application should used to open the specified MIME type.  The
value has to be string, symbol, or list of strings or symbols, this
applies to the `@code{default}', and `@code{removed}' fields as well.")
  (default
    (alist '())
    "An association list of MIME types and desktop entries which indicate
that the application should be the default for opening the specified
MIME type.")
  (removed
   (alist '())
   "An association list of MIME types and desktop entries which indicate
that the application cannot open the specified MIME type.")
  (desktop-entries
   (desktop-entries '())
   "A list of XDG desktop entries to create.  See
@code{xdg-desktop-entry}."))

(define (home-xdg-mime-applications-files config)
  (define (add-xdg-desktop-entry-file entry)
    (let ((file (first entry))
          (config (second entry)))
      ;; TODO: Use xdg-data-files instead of home-files here
      (list (format #f "applications/~a" file)
          (apply mixed-text-file
                 (format #f "xdg-desktop-~a-entry" file)
                 config))))
  (map (compose add-xdg-desktop-entry-file serialize-xdg-desktop-entry)
       (home-xdg-mime-applications-configuration-desktop-entries config)))

(define (home-xdg-mime-applications-xdg-files config)
  `(("mimeapps.list"
     ,(mixed-text-file
       "xdg-mime-appplications"
       (serialize-configuration
        config
        home-xdg-mime-applications-configuration-fields)))))

(define (home-xdg-mime-applications-extension old-config extension-configs)
  (define (extract-fields config)
    ;; return '(added default removed desktop-entries)
    (list (home-xdg-mime-applications-configuration-added config)
          (home-xdg-mime-applications-configuration-default config)
          (home-xdg-mime-applications-configuration-removed config)
          (home-xdg-mime-applications-configuration-desktop-entries config)))

  (define (append-configs elem acc)
    (list (append (first elem) (first acc))
          (append (second elem) (second acc))
          (append (third elem) (third acc))
          (append (fourth elem) (fourth acc))))

  ;; TODO: Implement procedure to check for duplicates without
  ;; sacrificing performance.
  ;;
  ;; Combine all the alists from 'added', 'default' and 'removed'
  ;; into one big alist.
  (let ((folded-configs (fold append-configs
                              (extract-fields old-config)
                              (map extract-fields extension-configs))))
    (home-xdg-mime-applications-configuration
     (added (first folded-configs))
     (default (second folded-configs))
     (removed (third folded-configs))
     (desktop-entries (fourth folded-configs)))))

(define home-xdg-mime-applications-service-type
  (service-type (name 'home-xdg-mime-applications)
                (extensions
                 (list (service-extension
                        home-xdg-data-files-service-type
                        home-xdg-mime-applications-files)
                       (service-extension
                        home-xdg-configuration-files-service-type
                        home-xdg-mime-applications-xdg-files)))
                (compose identity)
                (extend home-xdg-mime-applications-extension)
                (default-value (home-xdg-mime-applications-configuration))
                (description
                 "Configure XDG MIME applications, and XDG desktop entries.")))
ope file) (random-file-size))) ((file (? integer? mode)) (populate-file (scope file) (random-file-size)) (chmod (scope file) mode)) ((from '-> to) (symlink to (scope from)))))) (define (delete-file-tree dir tree) "Delete file TREE from DIR." (let loop ((dir dir) (tree tree)) (define (scope file) (string-append dir "/" file)) (match tree (('directory name (body ...)) (for-each (cute loop (scope name) <>) body) (rmdir (scope name))) (('directory name (? integer? mode) (body ...)) (chmod (scope name) #o755) ; make sure it can be entered (for-each (cute loop (scope name) <>) body) (rmdir (scope name))) ((from '-> _) (delete-file (scope from))) ((file _ ...) (delete-file (scope file)))))) (define-syntax-rule (with-file-tree dir tree body ...) (dynamic-wind (lambda () (make-file-tree dir 'tree)) (lambda () body ...) (lambda () (delete-file-tree dir 'tree)))) (define (file-tree-equal? input output) "Return #t if the file trees at INPUT and OUTPUT are equal." (define strip (cute string-drop <> (string-length input))) (define sibling (compose (cut string-append output <>) strip)) (file-system-fold (const #t) (lambda (name stat result) ; leaf (and result (file=? name (sibling name)))) (lambda (name stat result) ; down result) (lambda (name stat result) ; up result) (const #f) ; skip (lambda (name stat errno result) (pk 'error name stat errno) #f) #t ; result input lstat)) (define (populate-file file size) (call-with-output-file file (lambda (p) (put-bytevector p (random-bytevector size))))) (define (rm-rf dir) (file-system-fold (const #t) ; enter? (lambda (file stat result) ; leaf (unless (eq? 'symlink (stat:type stat)) (chmod file #o644)) (delete-file file)) (lambda (dir stat result) ; down (chmod dir #o755)) (lambda (dir stat result) ; up (rmdir dir)) (const #t) ; skip (const #t) ; error #t dir lstat)) (define %test-dir ;; An output directory under $top_builddir. (string-append (dirname (search-path %load-path "pre-inst-env")) "/test-nar-" (number->string (getpid)))) (test-begin "nar") (test-assert "write-file-tree + restore-file" (let* ((file1 (search-path %load-path "guix.scm")) (file2 (search-path %load-path "guix/base32.scm")) (file3 "#!/bin/something") (output (string-append %test-dir "/output"))) (dynamic-wind (lambda () #t) (lambda () (define-values (port get-bytevector) (open-bytevector-output-port)) (write-file-tree "root" port #:file-type+size (match-lambda ("root" (values 'directory 0)) ("root/foo" (values 'regular (stat:size (stat file1)))) ("root/lnk" (values 'symlink 0)) ("root/dir" (values 'directory 0)) ("root/dir/bar" (values 'regular (stat:size (stat file2)))) ("root/dir/exe" (values 'executable (string-length file3)))) #:file-port (match-lambda ("root/foo" (open-input-file file1)) ("root/dir/bar" (open-input-file file2)) ("root/dir/exe" (open-input-string file3))) #:symlink-target (match-lambda ("root/lnk" "foo")) #:directory-entries (match-lambda ("root" '("foo" "dir" "lnk")) ("root/dir" '("bar" "exe")))) (close-port port) (rm-rf %test-dir) (mkdir %test-dir) (restore-file (open-bytevector-input-port (get-bytevector)) output) (and (file=? (string-append output "/foo") file1) (string=? (readlink (string-append output "/lnk")) "foo") (file=? (string-append output "/dir/bar") file2) (string=? (call-with-input-file (string-append output "/dir/exe") get-string-all) file3) (> (logand (stat:mode (lstat (string-append output "/dir/exe"))) #o100) 0) (equal? '("." ".." "bar" "exe") (scandir (string-append output "/dir"))) (equal? '("." ".." "dir" "foo" "lnk") (scandir output)))) (lambda () (false-if-exception (rm-rf %test-dir)))))) (test-equal "write-file-tree + fold-archive" '(("R" directory #f) ("R/dir" directory #f) ("R/dir/exe" executable "1234") ("R/dir" directory-complete #f) ("R/foo" regular "abcdefg") ("R/lnk" symlink "foo") ("R" directory-complete #f)) (let () (define-values (port get-bytevector) (open-bytevector-output-port)) (write-file-tree "root" port #:file-type+size (match-lambda ("root" (values 'directory 0)) ("root/foo" (values 'regular 7)) ("root/lnk" (values 'symlink 0)) ("root/dir" (values 'directory 0)) ("root/dir/exe" (values 'executable 4))) #:file-port (match-lambda ("root/foo" (open-input-string "abcdefg")) ("root/dir/exe" (open-input-string "1234"))) #:symlink-target (match-lambda ("root/lnk" "foo")) #:directory-entries (match-lambda ("root" '("foo" "dir" "lnk")) ("root/dir" '("exe")))) (close-port port) (reverse (fold-archive (lambda (file type contents result) (let ((contents (if (memq type '(regular executable)) (utf8->string (get-bytevector-n (car contents) (cdr contents))) contents))) (cons `(,file ,type ,contents) result))) '() (open-bytevector-input-port (get-bytevector)) "R")))) (test-equal "write-file-tree + fold-archive, flat file" '(("R" regular "abcdefg")) (let () (define-values (port get-bytevector) (open-bytevector-output-port)) (write-file-tree "root" port #:file-type+size (match-lambda ("root" (values 'regular 7))) #:file-port (match-lambda ("root" (open-input-string "abcdefg")))) (close-port port) (reverse (fold-archive (lambda (file type contents result) (let ((contents (utf8->string (get-bytevector-n (car contents) (cdr contents))))) (cons `(,file ,type ,contents) result))) '() (open-bytevector-input-port (get-bytevector)) "R")))) (test-assert "write-file supports non-file output ports" (let ((input (string-append (dirname (search-path %load-path "guix.scm")) "/guix")) (output (%make-void-port "w"))) (write-file input output) #t)) (test-equal "write-file puts file in C locale collation order" (base32 "0sfn5r63k88w9ls4hivnvscg82bqg8a0w7955l6xlk4g96jnb2z3") (let ((input (string-append %test-dir ".input"))) (dynamic-wind (lambda () (define (touch file) (call-with-output-file (string-append input "/" file) (const #t))) (mkdir input) (touch "B") (touch "Z") (touch "a") (symlink "B" (string-append input "/z"))) (lambda () (let-values (((port get-hash) (open-sha256-port))) (write-file input port) (close-port port) (get-hash))) (lambda () (rm-rf input))))) (test-equal "restore-file with incomplete input" (string-append %test-dir "/foo") (let ((port (open-bytevector-input-port #vu8(1 2 3)))) (guard (c ((nar-error? c) (and (eq? port (nar-error-port c)) (nar-error-file c)))) (restore-file port (string-append %test-dir "/foo")) #f))) (test-assert "write-file + restore-file" (let* ((input (string-append (dirname (search-path %load-path "guix.scm")) "/guix")) (output %test-dir) (nar (string-append output ".nar"))) (dynamic-wind (lambda () #t) (lambda () (call-with-output-file nar (cut write-file input <>)) (call-with-input-file nar (cut restore-file <> output)) (file-tree-equal? input output)) (lambda () (false-if-exception (delete-file nar)) (false-if-exception (rm-rf output)))))) (test-assert "write-file + restore-file with symlinks" (let ((input (string-append %test-dir ".input"))) (mkdir input) (dynamic-wind (const #t) (lambda () (with-file-tree input (directory "root" (("reg") ("exe" #o777) ("sym" -> "reg"))) (let* ((output %test-dir) (nar (string-append output ".nar"))) (dynamic-wind (lambda () #t) (lambda () (call-with-output-file nar (cut write-file input <>)) (call-with-input-file nar (cut restore-file <> output)) (and (file-tree-equal? input output) (every (lambda (file) (canonical-file? (string-append output "/" file))) '("root" "root/reg" "root/exe")))) (lambda () (false-if-exception (delete-file nar)) (false-if-exception (rm-rf output))))))) (lambda () (rmdir input))))) (test-assert "write-file #:select? + restore-file" (let ((input (string-append %test-dir ".input"))) (mkdir input) (dynamic-wind (const #t) (lambda () (with-file-tree input (directory "root" ((directory "a" (("x") ("y") ("z"))) ("b") ("c") ("d" -> "b"))) (let* ((output %test-dir) (nar (string-append output ".nar"))) (dynamic-wind (lambda () #t) (lambda () (call-with-output-file nar (lambda (port) (write-file input port #:select? (lambda (file stat) (and (not (string=? (basename file) "a")) (not (eq? (stat:type stat) 'symlink))))))) (call-with-input-file nar (cut restore-file <> output)) ;; Make sure "a" and "d" have been filtered out. (and (not (file-exists? (string-append output "/root/a"))) (file=? (string-append output "/root/b") (string-append input "/root/b")) (file=? (string-append output "/root/c") (string-append input "/root/c")) (not (file-exists? (string-append output "/root/d"))))) (lambda () (false-if-exception (delete-file nar)) (false-if-exception (rm-rf output))))))) (lambda () (rmdir input))))) (test-eq "restore-file with non-UTF8 locale" ;<https://bugs.gnu.org/33603> 'encoding-error (let* ((file (search-path %load-path "guix.scm")) (output (string-append %test-dir "/output")) (locale (setlocale LC_ALL "C"))) (dynamic-wind (lambda () #t) (lambda () (define-values (port get-bytevector) (open-bytevector-output-port)) (write-file-tree "root" port #:file-type+size (match-lambda ("root" (values 'directory 0)) ("root/λ" (values 'regular 0))) #:file-port (const (%make-void-port "r")) #:symlink-target (const #f) #:directory-entries (const '("λ"))) (close-port port) (mkdir %test-dir) (catch 'encoding-error (lambda () ;; This show throw to 'encoding-error. (restore-file (open-bytevector-input-port (get-bytevector)) output) (scandir output)) (lambda args 'encoding-error))) (lambda () (false-if-exception (rm-rf %test-dir)) (setlocale LC_ALL locale))))) ;; XXX: Tell the 'deduplicate' procedure what store we're actually using. (setenv "NIX_STORE" (%store-prefix)) (test-assert "restore-file-set (signed, valid)" (with-store store (let* ((texts (unfold (cut >= <> 10) (lambda _ (random-text)) 1+ 0)) (files (map (cut add-text-to-store store "text" <>) texts)) (dump (call-with-bytevector-output-port (cut export-paths store files <>)))) (delete-paths store files) (and (every (negate file-exists?) files) (let* ((source (open-bytevector-input-port dump)) (imported (restore-file-set source))) (and (equal? imported files) (every (lambda (file) (and (file-exists? file) (valid-path? store file))) files) (equal? texts (map (lambda (file) (call-with-input-file file get-string-all)) files)) (every canonical-file? files))))))) (test-assert "restore-file-set with directories (signed, valid)" ;; <https://bugs.gnu.org/33361> describes a bug whereby directories ;; containing files subject to deduplication were not canonicalized--i.e., ;; their mtime and permissions were not reset. Ensure that this bug is ;; gone. (with-store store ;; Note: TEXT1 and TEXT2 must be longer than %DEDUPLICATION-MINIMUM-SIZE. (let* ((text1 (string-concatenate (make-list 200 (random-text)))) (text2 (string-concatenate (make-list 200 (random-text)))) (tree `("tree" directory ("a" regular (data ,text1)) ("b" directory ("c" regular (data ,text2)) ("d" regular (data ,text1))))) ;duplicate (file (add-file-tree-to-store store tree)) (dump (call-with-bytevector-output-port (cute export-paths store (list file) <>)))) (delete-paths store (list file)) (and (not (file-exists? file)) (let* ((source (open-bytevector-input-port dump)) (imported (restore-file-set source))) (and (equal? imported (list file)) (file-exists? file) (valid-path? store file) (string=? text1 (call-with-input-file (string-append file "/a") get-string-all)) (string=? text2 (call-with-input-file (string-append file "/b/c") get-string-all)) (= (stat:ino (stat (string-append file "/a"))) ;deduplication (stat:ino (stat (string-append file "/b/d")))) (every canonical-file? (find-files file #:directories? #t)))))))) (test-assert "restore-file-set (missing signature)" (let/ec return (with-store store (let* ((file (add-text-to-store store "foo" (random-text))) (dump (call-with-bytevector-output-port (cute export-paths store (list file) <> #:sign? #f)))) (delete-paths store (list file)) (and (not (file-exists? file)) (let ((source (open-bytevector-input-port dump))) (guard (c ((nar-signature-error? c) (let ((message (condition-message c)) (port (nar-error-port c))) (return (and (string-match "lacks.*signature" message) (string=? file (nar-error-file c)) (eq? source port)))))) (restore-file-set source)) #f)))))) (test-assert "restore-file-set (corrupt)" (let/ec return (with-store store (let* ((file (add-text-to-store store "foo" (random-text))) (dump (call-with-bytevector-output-port (cute export-paths store (list file) <>)))) (delete-paths store (list file)) ;; Flip a byte in the file contents. (let* ((index 120) (byte (bytevector-u8-ref dump index))) (bytevector-u8-set! dump index (logxor #xff byte))) (and (not (file-exists? file)) (let ((source (open-bytevector-input-port dump))) (guard (c ((nar-invalid-hash-error? c) (let ((message (condition-message c)) (port (nar-error-port c))) (return (and (string-contains message "hash") (string=? file (nar-error-file c)) (eq? source port)))))) (restore-file-set source)) #f)))))) (test-end "nar") ;;; Local Variables: ;;; eval: (put 'with-file-tree 'scheme-indent-function 2) ;;; End: