Subject: [bug#29409] [PATCH 4/4] bootloader: Factorize write-file-on-device.

From: [email-masked]
Date: Wed, 6 Dec 2017 13:59:53 +0100

From: Mathieu Othacehe <[email-masked]>

* gnu/bootloader/extlinux.scm (install-extlinux): Factorize bootloader
  writing in a new procedure ...
* gnu/bootloader.scm (write-file-on-device): ... defined and exported here.
---
 gnu/bootloader.scm          | 21 ++++++++++++++++++++-
 gnu/bootloader/extlinux.scm |  9 ++-------
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/gnu/bootloader.scm b/gnu/bootloader.scm
index 736f119..a715ba1 100644
--- a/gnu/bootloader.scm
+++ b/gnu/bootloader.scm
@@ -22,6 +22,7 @@
   #:use-module (guix discovery)
   #:use-module (guix records)
   #:use-module (guix ui)
+  #:use-module (guix gexp)
   #:use-module (srfi srfi-1)
   #:export (menu-entry
             menu-entry?
@@ -55,7 +56,8 @@
             bootloader-configuration-additional-configuration
 
             %bootloaders
-            lookup-bootloader-by-name))
+            lookup-bootloader-by-name
+            write-file-on-device))
 
 
 ;;;
@@ -163,3 +165,20 @@
               (eq? name (bootloader-name bootloader)))
             (force %bootloaders))
       (leave (G_ "~a: no such bootloader~%") name)))
+
+
+;;;
+;;; Writing utils.
+;;;
+
+(define write-file-on-device
+  #~(lambda (file size device offset)
+      ;; Write SIZE bytes from FILE to DEVICE starting at OFFSET.
+      (call-with-input-file file
+        (lambda (input)
+          (let ((bv (get-bytevector-n input size)))
+            (call-with-output-file device
+              (lambda (output)
+                (seek output offset SEEK_SET)
+                (put-bytevector output bv))
+              #:binary #t))))))
diff --git a/gnu/bootloader/extlinux.scm b/gnu/bootloader/extlinux.scm
index 9b6e2c7..304fb98 100644
--- a/gnu/bootloader/extlinux.scm
+++ b/gnu/bootloader/extlinux.scm
@@ -95,13 +95,8 @@ TIMEOUT ~a~%"
                   (find-files syslinux-dir "\\.c32$"))
         (unless
             (and (zero? (system* extlinux "--install" install-dir))
-                 (call-with-input-file (string-append syslinux-dir "/" #$mbr)
-                   (lambda (input)
-                     (let ((bv (get-bytevector-n input 440)))
-                       (call-with-output-file device
-                         (lambda (output)
-                           (put-bytevector output bv))
-                         #:binary #t)))))
+                 (#$write-file-on-device
+                  (string-append syslinux-dir "/" #$mbr) 440 device 0))
           (error "failed to install SYSLINUX")))))
 
 (define install-extlinux-mbr
-- 
2.7.4