Source

gnome-encfs / gnome-encfs

Diff from to

gnome-encfs

 
 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
 # =============================================================================
 
     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)
 
     if not items and path:
         print(MSG_NO_MATCH)
-        return False
+        return RC_UNKNOWN_ITEM
 
     for item in items:
         epath = item.attributes["encfs-path"]
         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."""
     global _items_cached
     _items_cached = None
 
-    return True
+    return RC_OK
 
 def edit_item(mpoint):
     """Edit EncFS item in keyring."""
 
     if not edits:
         print(MSG_NO_MATCH)
-        return False
+        return RC_UNKNOWN_ITEM
 
     for item in edits:
 
         global _items_cached
         _items_cached = None
 
-    return True
+    return RC_OK
 
 def remove_item(mpoint):
     """Remove EncFS item from keyring."""
 
     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)
     global _items_cached
     _items_cached = None
 
-    return True
+    return RC_OK
 
 def mount_items(path, autostart):
     """Mount selected items.
     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"]
         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)
 
     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)