From a8b9fa81f2a7c9b812d2c03d2b8f36d9a5e9f30f Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Wed, 20 Jan 2021 12:37:58 +0100 Subject: [PATCH] ambrevar: Move and update old match- functions to file.lisp. --- .../common-lisp/source/ambrevar/file.lisp | 35 +++++++++++++++++++ .../common-lisp/source/ambrevar/shell.lisp | 32 ----------------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/.local/share/common-lisp/source/ambrevar/file.lisp b/.local/share/common-lisp/source/ambrevar/file.lisp index c24936b8..30aa3213 100644 --- a/.local/share/common-lisp/source/ambrevar/file.lisp +++ b/.local/share/common-lisp/source/ambrevar/file.lisp @@ -226,6 +226,29 @@ Basename includes the extension. Useful for `finder'." (str:contains? name (basename file))) (cons name more-names)))) +;; TODO: Better control to filter in/out directories? +;; (export-always 'match-directory) +;; (defun match-directory (&key (empty? t) (non-empty? t) (files? t)) +;; "Return a predicate that matches on directories. +;; If target is a file, return FILES?. +;; Useful for `walk'." +;; (lambda (directory) +;; (if (uiop:directory-exists-p directory) +;; (let ((files-or-dirs? (or (uiop:directory-files directory) +;; (uiop:subdirectories directory)))) +;; (or (and empty? +;; (not files-or-dirs?)) +;; (and non-empty? +;; files-or-dirs?))) +;; files?))) + +(export-always 'match-executable) +(defun match-executable () + (lambda (file) + (intersection + (permissions file) + '(:user-exec :group-exec :other-exec)))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defparameter %magic-cookie-mime nil "Internal storage for `magic-cookie-mime'.") @@ -285,6 +308,18 @@ See `%description'." (let ((*finder-constructor* #'file+mime)) (apply #'finder root predicates))) +(export-always 'match-elf-binary) +(defun match-elf-binary () + (lambda (file) + (and (slot-boundp file 'mime-type) + (string= "application/x-executable" (mime-type file))))) + +(export-always 'match-elf-library) +(defun match-elf-library () + (lambda (file) + (and (slot-boundp file 'mime-type) + (ppcre:scan "application/x-sharedlib" (first (mime-type file)))))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defclass* mediafile (file+mime) ; REVIEW: Include MIME? diff --git a/.local/share/common-lisp/source/ambrevar/shell.lisp b/.local/share/common-lisp/source/ambrevar/shell.lisp index 07909f08..32906c64 100644 --- a/.local/share/common-lisp/source/ambrevar/shell.lisp +++ b/.local/share/common-lisp/source/ambrevar/shell.lisp @@ -30,38 +30,6 @@ (delete-if #'sera:resolve-executable (cons program more-programs)))) (error "Missing programs: ~{~a~,^, ~}" missing-programs))) -(export-always 'match-directory) -(defun match-directory (&key (empty? t) (non-empty? t) (files? t)) - "Return a predicate that matches on directories. -If target is a file, return FILES?. -Useful for `walk'." - (lambda (directory) - (if (uiop:directory-exists-p directory) - (let ((files-or-dirs? (or (uiop:directory-files directory) - (uiop:subdirectories directory)))) - (or (and empty? - (not files-or-dirs?)) - (and non-empty? - files-or-dirs?))) - files?))) - -(export-always 'match-executable) -(defun match-executable () - (lambda (file) - (intersection - (osicat:file-permissions file) - '(:user-exec :group-exec :other-exec)))) - -(export-always 'match-elf-binary) -(defun match-elf-binary () - (lambda (file) - (string= "application/x-executable" (file-mime-type file)))) - -(export-always 'match-elf-library) -(defun match-elf-library () - (lambda (file) - (ppcre:scan "application/x-sharedlib" (first (file-mime-type file))))) - (export-always 'delete-empty-directory-upward) (defun delete-empty-directory-upward (directory) "Delete directory and its parents until non-empty.