chmod: Move permission code to shell-utils.

* gash/shell-utils.scm (<chmodifier>): New record, move from commands/chmod.
(parse-modifier, parse-modifiers, make-numeric-chmodifier,
apply-chmodifiers): New function, move from commands/chmod.
* gash/commands/chmod.scm (chmod): Remove them.
* gash/util.scm (char->string, string->string-list): New function.
This commit is contained in:
Jan Nieuwenhuizen 2018-11-04 07:41:16 +01:00
parent 6e396759a3
commit 678e192b4f
4 changed files with 122 additions and 71 deletions

View File

@ -105,7 +105,7 @@
(("+u") (set-shell-opt! "nounset" #f))
(("-x") (set-shell-opt! "xtrace" #t))
(("+x") (set-shell-opt! "xtrace" #f))
(((and (? string?) arg)) (let* ((lst (map (cut make-string 1 <>) (string->list arg)))
(((and (? string?) arg)) (let* ((lst (string->string-list arg))
(set (car lst)))
(when (not (member set '("-" "+")))
(error (format #f "set: no such option:~s\n" args)))

View File

@ -38,74 +38,6 @@
chmod
))
(define-immutable-record-type <chmodifier>
(make-chmodifier users operation permissions)
chmodifier?
(users chmodifier-users)
(operation chmodifier-operation)
(permissions chmodifier-permissions))
(define (parse-modifier o)
(let* ((c (string->symbol (substring o 0 1)))
(o (if (memq c '(- + =)) (string-append "a" o) o))
(users (string->symbol (substring o 0 1))))
(when (not (memq users '(u g o a)))
(error (format #f "chmod: no such user: ~a" users)))
(let ((operation (string->symbol (substring o 1 2))))
(when (not (memq operation '(- + =)))
(error (format #f "chmod: no such operation: ~a" operation)))
(let* ((perm-string (substring o 2))
(perm (string->number perm-string 8)))
(if perm (make-numeric-chmodifier perm)
(let ((perms (map char->symbol (string->list perm-string))))
(make-chmodifier users operation perms)))))))
(define (char->symbol c)
(string->symbol (make-string 1 c)))
(define (parse-modifiers o)
(or (and=> (string->number o 8) (compose list (cut make-numeric-chmodifier <>)))
(map parse-modifier (string-split o #\,))))
(define (make-numeric-chmodifier o)
(make-chmodifier 'o '= (list o)))
(define (apply-chmodifiers file modifiers)
(let* ((mode (stat:mode (lstat file)))
(executable? (if (zero? (logand mode #o111)) 0 1)))
(let loop ((modifiers modifiers) (mode mode))
(if (null? modifiers) ((@ (guile) chmod) file mode)
(loop (cdr modifiers)
(let* ((m (car modifiers))
(n (chmodifier-numeric-mode m executable?))
(o (chmodifier-operation m)))
(case o
((=) n)
((+) (logior mode n))
((-) (logand mode n))
(else (error (format #f
"chmod: operation not supported: ~s\n" o))))))))))
(define (chmodifier-numeric-mode o executable?)
(let* ((permissions (chmodifier-permissions o))
(users (chmodifier-users o)))
(let loop ((permissions permissions))
(if (null? permissions) 0
(+ (let* ((p (car permissions))
(base (cond ((number? p) p)
((symbol? p)
(case p
((r) 4)
((w) 2)
((x) 1)
((X) executable?))))))
(case users
((a) (+ base (ash base 3) (ash base 6)))
((o) base)
((g) (ash base 3))
((u) (ash base 6))))
(loop (cdr permissions)))))))
(define (chmod . args)
(let* ((option-spec
'((reference (value #t))
@ -160,7 +92,7 @@ Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.
(m (if executable? (cons (make-chmodifier 'o '- '(x)) m) m))
(m (if xecutable? (cons (make-chmodifier 'o '- '(X)) m) m)))
(values m files)))
(else (values (parse-modifiers (car files)) (cdr files))))
(else (values (parse-chmodifiers (car files)) (cdr files))))
(let ((files (if (option-ref options 'recursive #f) (append-map find-files files)
files)))
(for-each (cut apply-chmodifiers <> modifiers) files)))))))

View File

@ -39,6 +39,7 @@
#:use-module (rnrs bytevectors)
#:use-module (rnrs io ports)
#:use-module (gash util)
#:export (
delete-file-recursively
display-tabulated
@ -48,9 +49,21 @@
file-name-predicate
find-files
file-exists?*
<chmodifier>
make-chmodifier
chmodifier-users
chmodifier-operation
chmodifier-permissions
make-numeric-chmodifier
chmodifier->mode
chmodifiers->mode
apply-chmodifiers
parse-chmodifiers
<grep-match>
grep*
grep+
<grep-match>
grep-match-file-name
grep-match-string
grep-match-line
@ -73,6 +86,7 @@
;;; This code is taken from (guix build utils)
;;;
;;; Directories.
;;;
@ -438,3 +452,81 @@ end of a line; by itself it won't match the terminating newline of a line."
rx+proc)))
(display line out)
(loop (read-line in 'concat)))))))
;;;
;;; Permissions.
;;;
(define-immutable-record-type <chmodifier>
(make-chmodifier users operation permissions)
chmodifier?
(users chmodifier-users)
(operation chmodifier-operation)
(permissions chmodifier-permissions))
(define (parse-chmodifier o)
(let* ((c (string->symbol (substring o 0 1)))
(o (if (memq c '(- + =)) (string-append "a" o) o))
(users (string->symbol (substring o 0 1)))
(program (car (command-line))))
(when (not (memq users '(u g o a)))
(error (format #f "~a: no such user: ~a" program users)))
(let ((operation (string->symbol (substring o 1 2))))
(when (not (memq operation '(- + =)))
(error (format #f "~a: no such operation: ~a" program operation)))
(let* ((perm-string (substring o 2))
(perm (string->number perm-string 8)))
(if perm (make-numeric-chmodifier perm)
(let ((perms (map string->symbol (string->string-list perm-string))))
(make-chmodifier users operation perms)))))))
(define (parse-chmodifiers o)
(or (and=> (string->number o 8) (compose list (cut make-numeric-chmodifier <>)))
(map parse-chmodifier (string-split o #\,))))
(define (make-numeric-chmodifier o)
(make-chmodifier 'o '= (list o)))
(define* (chmodifiers->mode modifiers #:optional (mode 0))
(let loop ((modifiers modifiers) (mode mode))
(if (null? modifiers) mode
(loop (cdr modifiers)
(chmodifier->mode (car modifiers) mode)))))
(define* (chmodifier->mode modifier #:optional (mode 0))
(let* ((executable? (if (zero? (logand mode #o111)) 0 1))
(n (chmodifier-numeric-mode modifier executable?))
(o (chmodifier-operation modifier))
(program (car (command-line))))
(case o
((=) n)
((+) (logior mode n))
((-) (logand mode (logxor n -1)))
(else (error
(format #f
"program: operation not supported: ~s\n"
program o))))))
(define (apply-chmodifiers file modifiers)
(let ((mode (chmodifiers->mode modifiers (warn 'file-mode(stat:mode (lstat file))))))
((@ (guile) chmod) file mode)))
(define (chmodifier-numeric-mode o executable?)
(let* ((permissions (chmodifier-permissions o))
(users (chmodifier-users o)))
(let loop ((permissions permissions))
(if (null? permissions) 0
(+ (let* ((p (car permissions))
(base (cond ((number? p) p)
((symbol? p)
(case p
((r) 4)
((w) 2)
((x) 1)
((X) executable?))))))
(case users
((a) (+ base (ash base 3) (ash base 6)))
((o) base)
((g) (ash base 3))
((u) (ash base 6))))
(loop (cdr permissions)))))))

View File

@ -1,3 +1,22 @@
;;; Gash --- Guile As SHell
;;; Copyright © 2016,2017,2018 R.E.W. van Beusekom <rutger.van.beusekom@gmail.com>
;;; Copyright © 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
;;;
;;; This file is part of Gash.
;;;
;;; Gash 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.
;;;
;;; Gash 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 Gash. If not, see <http://www.gnu.org/licenses/>.
(define-module (gash util)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-26)
@ -6,6 +25,8 @@
conjoin
disjoin
wrap-command
char->string
string->string-list
))
(define (disjoin . predicates)
@ -15,3 +36,9 @@
(define (conjoin . predicates)
(lambda (. arguments)
(every (cut apply <> arguments) predicates)))
(define (string->string-list string)
(map char->string (string->list string)))
(define (char->string c)
(make-string 1 c))