Skip to content

Instantly share code, notes, and snippets.

@voins voins/tramp-fix.el
Created Oct 22, 2010

Embed
What would you like to do?
(defun tramp-do-file-attributes-with-stat
(vec localname &optional id-format)
"Implement `file-attributes' for Tramp files using stat(1) command."
(tramp-message vec 5 "file attributes with stat: %s" localname)
(tramp-send-command-and-read
vec
(format
"((%s %s || %s -h %s) && %s -c '((\"%%N\") %%h %s %s \"%%X\" \"%%Y\" \"%%Z\" %%s.0 \"%%A\" t \"%%i\" -1)' %s || echo nil)"
(tramp-get-file-exists-command vec)
(tramp-shell-quote-argument localname)
(tramp-get-test-command vec)
(tramp-shell-quote-argument localname)
(tramp-get-remote-stat vec)
(if (eq id-format 'integer) "%u" "\"%U\"")
(if (eq id-format 'integer) "%g" "\"%G\"")
(tramp-shell-quote-argument localname))))
(defun tramp-convert-stat-number (value &optional f)
(let ((v (cond ((numberp value) value)
((search "." value) (string-to-number value))
(t (string-to-number (concat value ".0"))))))
(funcall (or f 'list)
(floor v 65536)
(floor (mod v 65536)))))
(defun tramp-convert-file-attributes (vec attr)
"Convert file-attributes ATTR generated by perl script, stat or ls.
Convert file mode bits to string and set virtual device number.
Return ATTR."
(when attr
;; Convert last access time.
(unless (listp (nth 4 attr))
(setcar (nthcdr 4 attr)
(tramp-convert-stat-number (nth 4 attr))))
;; Convert last modification time.
(unless (listp (nth 5 attr))
(setcar (nthcdr 5 attr)
(tramp-convert-stat-number (nth 5 attr))))
;; Convert last status change time.
(unless (listp (nth 6 attr))
(setcar (nthcdr 6 attr)
(tramp-convert-stat-number (nth 5 attr))))
;; Convert file size.
(when (< (nth 7 attr) 0)
(setcar (nthcdr 7 attr) -1))
(when (and (floatp (nth 7 attr))
(<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
(setcar (nthcdr 7 attr) (round (nth 7 attr))))
;; Convert file mode bits to string.
(unless (stringp (nth 8 attr))
(setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
(when (stringp (car attr))
(aset (nth 8 attr) 0 ?l)))
;; Convert directory indication bit.
(when (string-match "^d" (nth 8 attr))
(setcar attr t))
;; Convert symlink from `tramp-do-file-attributes-with-stat'.
(when (consp (car attr))
(if (and (stringp (caar attr))
(string-match ".+ -> .\\(.+\\)." (caar attr)))
(setcar attr (match-string 1 (caar attr)))
(setcar attr nil)))
;; Set file's gid change bit.
(setcar (nthcdr 9 attr)
(if (numberp (nth 3 attr))
(not (= (nth 3 attr)
(tramp-get-remote-gid vec 'integer)))
(not (string-equal
(nth 3 attr)
(tramp-get-remote-gid vec 'string)))))
;; Convert inode.
(unless (listp (nth 10 attr))
(setcar (nthcdr 10 attr)
(condition-case nil
(tramp-convert-stat-number (nth 10 attr) 'cons)
;; Inodes can be incredible huge. We must hide this.
(error (tramp-get-inode vec)))))
;; Set virtual device number.
(setcar (nthcdr 11 attr)
(tramp-get-device vec))
attr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.