pit

Owner: IIIlllIIIllI URL: git@github.com:nyangkosense/pit.git

formatting/ add listing function

Commit e4fd7958a7619bc04f8912d6845c11d0ed58ba30 by SM <seb.michalk@gmail.com> on 2025-03-16 17:23:52 +0000
diff --git a/pit.c b/pit.c
index 4826539..55d3432 100644
--- a/pit.c
+++ b/pit.c
@@ -173,18 +173,20 @@ secure_free(void *ptr, size_t size)
 }
 
 /* todo: format this better and more clearly */
-static void
+static void 
 usage(void)
 {
     die("usage: pit [-v] [-h] command [arguments]\n"
         "Commands:\n"
-        "  dig FILE 10                     - create new empty pit file 10 mb size\n"
-        "  key KEY.key                     - generate new encrypted key file\n"
-        "  open FILE KEY.key               - open an existing pit\n"
-        "  close   PATH        		       - close an opened pit\n"
-        "  list               		       - list opened pits\n"
-        "  panic              		       - emergency close all pits (forced)\n"
-        "  example: pit dig container.pit, pit key container.key, pit open container.pit container.key\n");
+        "  dig FILE 10                 - create new empty pit file of 10 MB size\n"
+        "  key KEY.key                 - generate new encrypted key file\n"
+        "  open FILE KEY.key           - open an existing pit\n"
+        "  close PATH                  - close an opened pit\n"
+        "  list                        - list opened pits\n"
+        "  panic                       - emergency close all pits (forced)\n"
+        "  example: pit dig container.pit\n"
+        "           pit key container.key\n"
+        "           pit open container.pit container.key\n");
 }
 
 static void
@@ -725,7 +727,7 @@ open_pit(const char *path, const char *keyfile)
         }
     } else {
         printf("pit: existing filesystem found\n");
-        debug_fs_info(mapper_path);
+        //debug_fs_info(mapper_path);
     }
 
     /* Create mount point */
@@ -752,8 +754,58 @@ open_pit(const char *path, const char *keyfile)
 static int
 list_pits(void)
 {
-    printf("pit: no pits currently mounted\n");
-    return 0;
+    char **mounted_paths = NULL;
+    int count = 0;
+    int ret = 0;
+    int i = 0;
+    
+    if (find_mounted_pits(&mounted_paths, &count) < 0) {
+        fprintf(stderr, "pit: failed to find mounted pits\n");
+        return -1;
+    }
+    
+    if (count == 0) {
+        printf("pit: no pits currently mounted\n");
+    } else {
+        printf("pit: %d pit%s currently mounted:\n", count, count == 1 ? "" : "s");
+        
+        for (i = 0; i < count; i++) {
+            if (mounted_paths[i]) {
+                char device_path[PATH_MAX] = {0};
+                FILE *mtab = fopen("/proc/mounts", "r");
+                if (mtab) {
+                    char line[PATH_MAX];
+                    while (fgets(line, sizeof(line), mtab)) {
+                        char mnt_path[PATH_MAX] = {0};
+                        char dev_path[PATH_MAX] = {0};
+                        sscanf(line, "%s %s", dev_path, mnt_path);
+                        
+                        if (strcmp(mnt_path, mounted_paths[i]) == 0) {
+                            strncpy(device_path, dev_path, PATH_MAX - 1);
+                            break;
+                        }
+                    }
+                    fclose(mtab);
+                }
+                
+                if (device_path[0]) {
+                    const char *mapper_prefix = "/dev/mapper/pit-";
+                    if (strncmp(device_path, mapper_prefix, strlen(mapper_prefix)) == 0) {
+                        printf("  %s -> %s\n", device_path + strlen(mapper_prefix), mounted_paths[i]);
+                    } else {
+                        printf("  %s -> %s\n", device_path, mounted_paths[i]);
+                    }
+                } else {
+                    printf("  %s\n", mounted_paths[i]);
+                }
+                
+                free(mounted_paths[i]);
+            }
+        }
+    }
+    
+    free(mounted_paths);
+    return ret;
 }
 
 static int