73ce773f5e56 — Oben Sonne 12 years ago
Use more fine-grained return codes

Return codes are now defined by constants. Return codes may be combined
bitwise, which is especially useful when multiple items are processed by
one command (because different errors may occur).
1 files changed, 36 insertions(+), 25 deletions(-)

M gnome-encfs
M gnome-encfs +36 -25
@@ 81,6 81,14 @@ only if gnome-encfs is installed in the 
 
 VERSION="0.1"
 
+# bitwise or'able return codes
+RC_OK = 0
+RC_MOUNT_FAILED = 1
+RC_KEYRING_LOCKED = 2
+RC_UNKNOWN_ITEM = 4
+RC_INVALID_PATH = 8
+RC_MOUNT_POINT_IN_USE = 16
+
 # =============================================================================
 # helper
 # =============================================================================

          
@@ 152,12 160,12 @@ def _options():
 
     return opts
 
-def _exit(ec):
+def _exit(rc):
     """Exit with additional check if autostart file is still needed."""
 
-    if ec != 2:
+    if rc != RC_KEYRING_LOCKED: # getting items requires an unlocked keyring
         _autostart(_get_items(amount="y"))
-    sys.exit(ec)
+    sys.exit(rc)
 
 def _proceed(msg):
     print("Warning: %s" % msg)

          
@@ 259,7 267,7 @@ def list_items(path=None):
 
     if not items and path:
         print(MSG_NO_MATCH)
-        return False
+        return RC_UNKNOWN_ITEM
 
     for item in items:
         epath = item.attributes["encfs-path"]

          
@@ 269,7 277,7 @@ def list_items(path=None):
         print("  mount point    : %s" % mpoint)
         print("  mount at login : %s" % (amount == "y" and "yes" or "no"))
 
-    return True
+    return RC_OK
 
 def add_item(epath, mpoint):
     """Add new EncFS item to keyring."""

          
@@ 292,7 300,7 @@ def add_item(epath, mpoint):
     global _items_cached
     _items_cached = None
 
-    return True
+    return RC_OK
 
 def edit_item(mpoint):
     """Edit EncFS item in keyring."""

          
@@ 302,7 310,7 @@ def edit_item(mpoint):
 
     if not edits:
         print(MSG_NO_MATCH)
-        return False
+        return RC_UNKNOWN_ITEM
 
     for item in edits:
 

          
@@ 341,7 349,7 @@ def edit_item(mpoint):
         global _items_cached
         _items_cached = None
 
-    return True
+    return RC_OK
 
 def remove_item(mpoint):
     """Remove EncFS item from keyring."""

          
@@ 350,7 358,7 @@ def remove_item(mpoint):
 
     if not items:
         print(MSG_NO_MATCH)
-        return False
+        return RC_UNKNOWN_ITEM
 
     for item in items:
         gk.item_delete_sync(KEYRING, item.item_id)

          
@@ 358,7 366,7 @@ def remove_item(mpoint):
     global _items_cached
     _items_cached = None
 
-    return True
+    return RC_OK
 
 def mount_items(path, autostart):
     """Mount selected items.

          
@@ 367,14 375,17 @@ def mount_items(path, autostart):
     point equals `path`. If `autostart` is True, mount only those items where
     auto-mount is set to 'y'.
 
+    Return true if all items have been mounted successfully and false
+    otherwise.
+
     """
     items = _get_items(anypath=path, amount=(autostart and "y" or None))
 
     if not items and path:
         print(MSG_NO_MATCH)
-        return False
+        return RC_UNKNOWN_ITEM
 
-    rc = True
+    rc = 0
 
     for item in items:
         epath = item.attributes["encfs-path"]

          
@@ 382,15 393,16 @@ def mount_items(path, autostart):
         msg = "Mounting %s at %s: " % (epath, mpoint)
         if _is_mounted(mpoint):
             msg += "mount point already in use"
+            rc |= RC_MOUNT_POINT_IN_USE
         elif not os.path.isdir(mpoint):
             msg += "mount point does not exist or is not a directory"
-            rc = False
+            rc |= RC_INVALID_PATH
         else:
             cmd = ["encfs", "-o", "nonempty", "-S", epath, mpoint]
             p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
             p.communicate(input="%s\n" % item.secret)
             msg += p.returncode and "FAILED" or "OK"
-            rc &= not p.returncode
+            rc |= 0 if p.returncode == os.EX_OK else RC_MOUNT_FAILED
 
         print(msg)
 

          
@@ 405,24 417,23 @@ def main():
     opts = _options()
 
     if opts.add:
-        ok = add_item(opts.p1, opts.p2)
+        rc = add_item(opts.p1, opts.p2)
     elif opts.list:
-        ok = list_items(opts.p1)
+        rc = list_items(opts.p1)
     elif opts.mount:
-        ok = mount_items(opts.p1, opts.autostart)
+        rc = mount_items(opts.p1, opts.autostart)
     elif opts.edit:
-        ok = edit_item(opts.p1)
+        rc = edit_item(opts.p1)
     elif opts.remove:
-        ok = remove_item(opts.p1)
+        rc = remove_item(opts.p1)
     else:
         assert False
 
-    return ok
-    
+    return rc
+
 if __name__ == '__main__':
-
     try:
-        ret = int(not main())
+        rc = main()
     except gk.CancelledError:
-        ret = 2
-    _exit(ret)
+        rc = RC_KEYRING_LOCKED
+    _exit(rc)