tramp-devel
[Top][All Lists]
Advanced

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

[PATCH 1/1] Try using file-attributes cache in file-executable-p, code r


From: Julian Scheid
Subject: [PATCH 1/1] Try using file-attributes cache in file-executable-p, code refactoring
Date: Mon, 17 Aug 2009 14:09:43 +1200

---
 lisp/ChangeLog |    7 +++
 lisp/tramp.el  |  116 +++++++++++++++++++++++--------------------------------
 2 files changed, 56 insertions(+), 67 deletions(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 4e75aed..71bb5cb 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,10 @@
+2009-08-17  Julian Scheid  <address@hidden>
+
+       * tramp.el (tramp-check-cached-permissions) New defun.
+       (tramp-handle-file-readable-p): Use it.
+       (tramp-handle-file-writable-p): Likewise.
+       (tramp-handle-file-executable-p): Likewise.
+
 2009-08-16  Michael Albinus  <address@hidden>
 
        * tramp-cache.el (top): Autoload `tramp-time-less-p'.
diff --git a/lisp/tramp.el b/lisp/tramp.el
index c4019cb..e37209a 100644
--- a/lisp/tramp.el
+++ b/lisp/tramp.el
@@ -2853,7 +2853,10 @@ and gid of the corresponding user is taken.  Both 
parameters must be integers."
   "Like `file-executable-p' for Tramp files."
   (with-parsed-tramp-file-name filename nil
     (with-file-property v localname "file-executable-p"
-      (zerop (tramp-run-test "-x" filename)))))
+      ;; Examine file-attributes cache to see if request can be
+      ;; satisfied without remote operation.
+      (or (tramp-check-cached-permissions localname ?x)
+          (zerop (tramp-run-test "-x" filename))))))
 
 (defun tramp-handle-file-readable-p (filename)
   "Like `file-readable-p' for Tramp files."
@@ -2861,39 +2864,7 @@ and gid of the corresponding user is taken.  Both 
parameters must be integers."
     (with-file-property v localname "file-readable-p"
       ;; Examine file-attributes cache to see if request can be
       ;; satisfied without remote operation.
-      (or (let ((result nil))
-            (dolist (suffix '("string" "integer") result)
-              (setq
-               result
-               (or
-                result
-                (let ((file-attr
-                       (tramp-get-file-property
-                        v localname
-                        (concat "file-attributes-" suffix) nil))
-                      (remote-uid
-                       (tramp-get-connection-property
-                        v (concat "uid-" suffix) nil))
-                      (remote-gid
-                       (tramp-get-connection-property
-                        v (concat "gid-" suffix) nil)))
-                  (and
-                   file-attr
-                   (or
-                    (eq t (car file-attr))
-                    (null (car file-attr)))
-                   (or
-                    ;; World readable.
-                    (eq ?r (aref (nth 8 file-attr) 7))
-                    ;; User readable and owned by user.
-                    (and
-                     (eq ?r (aref (nth 8 file-attr) 1))
-                     (equal remote-uid (nth 2 file-attr)))
-                    ;; Group readable and owned by user's principal
-                    ;; group.
-                    (and
-                     (eq ?r (aref (nth 8 file-attr) 4))
-                     (equal remote-gid (nth 3 file-attr))))))))))
+      (or (tramp-check-cached-permissions localname ?r)
           (zerop (tramp-run-test "-r" filename))))))
 
 ;; When the remote shell is started, it looks for a shell which groks
@@ -2986,39 +2957,7 @@ value of `default-file-modes', without execute 
permissions."
       (if (file-exists-p filename)
          ;; Examine file-attributes cache to see if request can be
          ;; satisfied without remote operation.
-          (or (let ((result nil))
-                (dolist (suffix '("string" "integer") result)
-                  (setq
-                   result
-                   (or
-                    result
-                    (let ((file-attr
-                           (tramp-get-file-property
-                            v localname
-                            (concat "file-attributes-" suffix) nil))
-                          (remote-uid
-                           (tramp-get-connection-property
-                            v (concat "uid-" suffix) nil))
-                          (remote-gid
-                           (tramp-get-connection-property
-                            v (concat "gid-" suffix) nil)))
-                      (and
-                       file-attr
-                       (or
-                        (eq t (car file-attr))
-                        (null (car file-attr)))
-                       (or
-                       ;; World writable.
-                        (eq ?w (aref (nth 8 file-attr) 8))
-                        ;; User writable and owned by user.
-                        (and
-                         (eq ?w (aref (nth 8 file-attr) 2))
-                         (equal remote-uid (nth 2 file-attr)))
-                        ;; Group writable and owned by user's
-                        ;; principal group.
-                        (and
-                         (eq ?w (aref (nth 8 file-attr) 5))
-                         (equal remote-gid (nth 3 file-attr))))))))))
+          (or (tramp-check-cached-permissions localname ?w)
               (zerop (tramp-run-test "-w" filename)))
        ;; If file doesn't exist, check if directory is writable.
        (and (zerop (tramp-run-test
@@ -7233,6 +7172,49 @@ Return ATTR."
             (tramp-get-device vec))
     attr))
 
+(defun tramp-check-cached-permissions (localname access)
+  "Check file-attributes caches for LOCALNAME and return t if
+  according to the cache access type ACCESS is known to be
+  granted."
+  (let ((result nil)
+        (offset (cond
+                 ((eq ?r access) 1)
+                 ((eq ?w access) 2)
+                 ((eq ?x access) 3))))
+    (dolist (suffix '("string" "integer") result)
+      (setq
+       result
+       (or
+        result
+        (let ((file-attr
+               (tramp-get-file-property
+                v localname
+                (concat "file-attributes-" suffix) nil))
+              (remote-uid
+               (tramp-get-connection-property
+                v (concat "uid-" suffix) nil))
+              (remote-gid
+               (tramp-get-connection-property
+                v (concat "gid-" suffix) nil)))
+          (and
+           file-attr
+           (or
+            ;; Not a symlink
+            (eq t (car file-attr))
+            (null (car file-attr)))
+           (or
+            ;; World accessible.
+            (eq access (aref (nth 8 file-attr) (+ offset 6)))
+            ;; User accessible and owned by user.
+            (and
+             (eq access (aref (nth 8 file-attr) offset))
+             (equal remote-uid (nth 2 file-attr)))
+            ;; Group accessible and owned by user's
+            ;; principal group.
+            (and
+             (eq access (aref (nth 8 file-attr) (+ offset 3)))
+             (equal remote-gid (nth 3 file-attr)))))))))))
+
 (defun tramp-get-inode (vec)
   "Returns the virtual inode number.
 If it doesn't exist, generate a new one."
-- 
1.6.4





reply via email to

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