Function: tramp-do-file-attributes-with-ls

tramp-do-file-attributes-with-ls is a byte-compiled function defined in tramp-sh.el.gz.

Signature

(tramp-do-file-attributes-with-ls VEC LOCALNAME &optional ID-FORMAT)

Documentation

Implement file-attributes for Tramp files using the ls(1) command.

Source Code

;; Defined in /usr/src/emacs/lisp/net/tramp-sh.el.gz
(defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
  "Implement `file-attributes' for Tramp files using the ls(1) command."
  (let (symlinkp dirp
	res-inode res-filemodes res-numlinks
	res-uid res-gid res-size res-symlink-target)
    (tramp-message vec 5 "file attributes with ls: %s" localname)
    ;; We cannot send all three commands combined, it could exceed
    ;; NAME_MAX or PATH_MAX.  Happened on macOS, for example.
    (when (or (tramp-send-command-and-check
               vec
               (format "%s %s"
                       (tramp-get-file-exists-command vec)
                       (tramp-shell-quote-argument localname)))
              (tramp-send-command-and-check
               vec
               (format "%s -h %s"
                       (tramp-get-test-command vec)
                       (tramp-shell-quote-argument localname))))
      (tramp-send-command
       vec
       (format "%s %s %s %s"
               (tramp-get-ls-command vec)
               (if (eq id-format 'integer) "-ildn" "-ild")
               ;; On systems which have no quoting style, file names
               ;; with special characters could fail.
               (tramp-sh--quoting-style-options vec)
               (tramp-shell-quote-argument localname)))
      ;; Parse `ls -l' output ...
      (with-current-buffer (tramp-get-buffer vec)
        (when (> (buffer-size) 0)
          (goto-char (point-min))
          ;; ... inode
          (setq res-inode (read (current-buffer)))
          ;; ... file mode flags
          (setq res-filemodes (symbol-name (read (current-buffer))))
          ;; ... number links
          (setq res-numlinks (read (current-buffer)))
          ;; ... uid and gid
          (setq res-uid (read (current-buffer)))
          (setq res-gid (read (current-buffer)))
          (if (eq id-format 'integer)
              (progn
                (unless (numberp res-uid)
		  (setq res-uid tramp-unknown-id-integer))
                (unless (numberp res-gid)
		  (setq res-gid tramp-unknown-id-integer)))
            (progn
              (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
              (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
          ;; ... size
          (setq res-size (read (current-buffer)))
          ;; From the file modes, figure out other stuff.
          (setq symlinkp (eq ?l (aref res-filemodes 0)))
          (setq dirp (eq ?d (aref res-filemodes 0)))
          ;; If symlink, find out file name pointed to.
          (when symlinkp
            (search-forward "-> ")
            (setq res-symlink-target
                  (if (looking-at-p "\"")
                      (read (current-buffer))
                    (buffer-substring (point) (point-at-eol)))))
          ;; Return data gathered.
          (list
           ;; 0. t for directory, string (name linked to) for symbolic
           ;; link, or nil.
           (or dirp res-symlink-target)
           ;; 1. Number of links to file.
           res-numlinks
           ;; 2. File uid.
           res-uid
           ;; 3. File gid.
           res-gid
           ;; 4. Last access time.
           ;; 5. Last modification time.
           ;; 6. Last status change time.
           tramp-time-dont-know tramp-time-dont-know tramp-time-dont-know
           ;; 7. Size in bytes (-1, if number is out of range).
           res-size
           ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
           res-filemodes
           ;; 9. t if file's gid would change if file were deleted and
           ;; recreated.  Will be set in `tramp-convert-file-attributes'.
           t
           ;; 10. Inode number.
           res-inode
           ;; 11. Device number.  Will be replaced by a virtual device number.
           -1))))))