emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Proposal: cpio-mode


From: Stefan Monnier
Subject: Re: Proposal: cpio-mode
Date: Thu, 16 Apr 2015 16:42:45 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

> A cpio mode has been in the emacs TODO file for as long as I've looked there.
> I'd like to write that. Is there really still interest?

It'd fit very nicely in GNU ELPA, I think, yes.  Of course, the fact
that noone wrote one up might indicate that the demand is not very high,
but I for one would enjoy easier access to the initramfs files, for example.


        Stefan


PS: For what it's worth, here's my earlier attempt at such a thing.
Obviously I stopped before getting to the meat of it.
PPS: Also, there are various possible options in terms of UI: you could
integrate cpio support into arc-mode.el, or you could "do it by hand"
along the lines of tar-mode.el, or you make a magic file-name handler
(and rely on dired for the UI).


;;; cpio-mode.el --- View/edit cpio archives

;; Copyright (C) 2012  Stefan Monnier

;; Author: Stefan Monnier <address@hidden>
;; Keywords: 

;; This program 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.

;; This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; 

;;; Code:

;; The format supported is the "New ASCII Format" as documented in
;; http://people.freebsd.org/~kientzle/libarchive/man/cpio.5.txt.

(eval-when-compile (require 'cl))

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.cpio\\'" . cpio-mode))

;;;###autoload
(when (boundp 'inhibit-local-variables-regexps)
  (add-to-list 'inhibit-local-variables-regexps "\\.cpio\\'"))

;;;###autoload
(define-derived-mode cpio-mode special-mode "CPIO"
  "View/edit cpio archives."
  (add-hook 'change-major-mode-hook
            (lambda () (with-silent-modifications
                    (save-restriction
                      (widen)
                      (remove-text-properties (point-min) (point-max)
                                              '(display nil invisible nil))))))
  (cpio--parse-buffer))

(defun cpio--parse-mode (mode)
  (string
   (case (logand #o170000 mode)
     (#o140000 ?s)
     (#o120000 ?l)
     (#o100000 ?-)
     (#o060000 ?b)
     (#o040000 ?d)
     (#o020000 ?c)
     (#o010000 ?p)
     (t (error "Unknown mode %S" mode)))
   (if (zerop (logand #o400 mode)) ?- ?r)
   (if (zerop (logand #o200 mode)) ?- ?w)
   (if (zerop (logand #o100 mode))
       (if (zerop (logand #o4000 mode)) ?- ?S)
     (if (zerop (logand #o4000 mode)) ?x ?s))
   (if (zerop (logand #o040 mode)) ?- ?r)
   (if (zerop (logand #o020 mode)) ?- ?w)
   (if (zerop (logand #o010 mode))
       (if (zerop (logand #o2000 mode)) ?- ?S)
     (if (zerop (logand #o2000 mode)) ?x ?s))
   (if (zerop (logand #o004 mode)) ?- ?r)
   (if (zerop (logand #o002 mode)) ?- ?w)
   (if (zerop (logand #o001 mode)) ?- ?x)))
     

(defun cpio--parse-buffer ()
  (save-excursion
    (save-restriction
      (widen)
      (goto-char (point-min))
      (with-silent-modifications
        (while (not (eobp))
          (assert (zerop (mod (- (point) (point-min)) 4)))
          (cond
           ((not (looking-at 
"07070[12]\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)\\([[:xdigit:]]\\{8\\}\\)[[:xdigit:]]\\{8\\}\\|\0+\\'"))
            (error "Unrecognized cpio header format"))
           ((not (match-beginning 1))
            ;; Reached the trailing padding, just skip it.
            (put-text-property (point) (point-max) 'invisible t)
            (goto-char (match-end 0)))
           (t
            (let* ((ino (string-to-number (match-string 1) 16))
                   (mode (string-to-number (match-string 2) 16))
                   (uid (string-to-number (match-string 3) 16))
                   (gid (string-to-number (match-string 4) 16))
                   (nlink (string-to-number (match-string 5) 16))
                   (mtime (string-to-number (match-string 6) 16))
                   (filesize (string-to-number (match-string 7) 16))
                   (devmajor (string-to-number (match-string 8) 16))
                   (devminor (string-to-number (match-string 9) 16))
                   (rdevmajor (string-to-number (match-string 10) 16))
                   (rdevminor (string-to-number (match-string 11) 16))
                   (namesize (string-to-number (match-string 12) 16))
                   (namebeg (match-end 0))
                   (name (buffer-substring namebeg (+ namebeg namesize -1)))
                   (filebeg (+ (match-end 0) 2 (* (/ (+ namesize 1) 4) 4)))
                   (next (+ filebeg (* (/ (+ filesize 3) 4) 4))))
              (if (and (zerop ino) (zerop mode) (zerop filesize)
                       (equal name "TRAILER!!!"))
                  (put-text-property (match-beginning 0) next
                                     'display "\n<trailer>")
                (if (bobp)
                    (put-text-property (point) (match-end 1) 'invisible t)
                  (put-text-property (point) (match-end 1) 'display "\n"))
                (put-text-property (match-beginning 2) (match-end 2)
                                   'display (cpio--parse-mode mode))
                (put-text-property (match-beginning 3) (match-end 3)
                                   'display (format "%10d" uid))
                (put-text-property (match-beginning 4) (match-end 4)
                                   'display (format "/%-10d" gid))
                (put-text-property (match-beginning 5) ;; (match-end 6)
                ;;                    'invisible t)
                ;; (put-text-property (match-beginning 7)
                                   (match-end 7)
                                   'display (format "%10d" filesize))
                (put-text-property (match-beginning 8) namebeg
                                   'display " ")
                (if (= (logand #o170000 mode) #o120000) ;Symlink
                    (progn
                      (put-text-property (+ namebeg namesize -1) filebeg
                                         'display " -> ")
                      (put-text-property (+ filebeg filesize) next
                                         'invisible t))
                  (put-text-property (+ namebeg namesize -1) next
                                     'invisible t))
                (message "Parsed %S of size %d" name filesize))
              (goto-char next)))))))))

(provide 'cpio-mode)
;;; cpio-mode.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]