diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2021-05-03 17:23:21 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2021-05-06 12:23:51 -0700 |
commit | 9e5770606c30cb8b763a572aba59fac42484898c (patch) | |
tree | ea0ebc36d84deadd2e80a08e99107f9819b32e67 /src/property/file.lisp | |
parent | 0dba628638acd2aa5d990cd1edd41ac689f808c8 (diff) | |
download | consfigurator-9e5770606c30cb8b763a572aba59fac42484898c.tar.gz |
factor out FILE:UPDATE-UNIX-TABLE
Signed-off-by: Sean Whitton <spwhitton@spwhitton.name>
Diffstat (limited to 'src/property/file.lisp')
-rw-r--r-- | src/property/file.lisp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/property/file.lisp b/src/property/file.lisp index 71372d8..dd4ae89 100644 --- a/src/property/file.lisp +++ b/src/property/file.lisp @@ -409,3 +409,35 @@ removed, and semicolon comment chars will be replaced with '#'." finally (return (nconc new-lines (sort accum #'string< :key #'cadr))))))))))) + +(defun update-unix-table (file source target entries &key (no-source "none")) + "Ensure that the UNIX table at FILE (e.g. /etc/fstab) contains each of +ENTRIES, using a simple merge procedure: existing lines of the file with the +same value for the TARGETth field are updated to match the corresponding +members of ENTRIES, except that if the SOURCEth field of the existing entry is +not NO-SOURCE and the corresponding member of ENTRIES is STRING= to either +NO-SOURCE or \"PLACEHOLDER\", use the existing field value." + (let ((unknown (list no-source "PLACEHOLDER")) + (pending (make-hash-table :test #'equal))) + (flet ((fields (entry) + (remove "" (split-string entry) :test #'string=))) + (dolist (entry entries) + (setf (gethash (nth target (fields entry)) pending) entry)) + (map-file-lines + file + (lambda (lines) + (loop for line in lines + for line-fields = (fields line) + for line-source = (nth source line-fields) + and line-target = (nth target line-fields) + for entry = (when-let* ((entry (gethash line-target pending)) + (fields (fields entry))) + (and (member (nth source fields) + unknown :test #'string=) + (not (string= line-source no-source)) + (setf (nth source fields) line-source) + (format nil "~{~A~^ ~}" fields))) + if entry + collect it into accum and do (remhash line-target pending) + else collect line into accum + finally (return (nconc accum (hash-table-values pending))))))))) |