From 444bbc6ebe88379f9a67dcf402f3c6f65af7718b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Sun, 18 Feb 2024 14:56:36 +0100 Subject: [PATCH] Improved the entry listing --- src/backing.c | 16 ++++++++++++++++ src/backing.h | 5 ++++- src/disk.c | 13 ++++++++++++- src/disk.h | 3 ++- src/entry.c | 16 ++++++++++++++++ src/entry.h | 5 ++++- src/sandbox.c | 19 +++++++++++++++---- 7 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/backing.c b/src/backing.c index b051bc8..2aa8245 100644 --- a/src/backing.c +++ b/src/backing.c @@ -196,3 +196,19 @@ Status GetLatestBacking(char** _backing_identifier) { return status; } + +Status GetBackingDiskInfo(const char* backing_identifier, DiskInfo* _info) { + *_info = (DiskInfo){0}; + + // Get the backing path + char* backing_path = NULL; + Status status = GetBackingPath(backing_identifier, &backing_path); + if (status != SUCCESS) + return status; + + // Get the disk info + status = GetDiskInfo(backing_path, _info); + free(backing_path); + + return status; +} diff --git a/src/backing.h b/src/backing.h index a5508bb..9a3c819 100644 --- a/src/backing.h +++ b/src/backing.h @@ -1,6 +1,7 @@ #pragma once #include "utils.h" +#include "disk.h" bool IsBackingIdentifierValid(const char* backing_identifier); int GetBackingIndex(const char* backing_identifier); @@ -14,4 +15,6 @@ Status AddBacking(const char* backing_identifier, const char* entry_identifier); Status RemoveBacking(const char* backing_identifier); Status ListBackings(char*** _backings); -Status GetLatestBacking(char** _backing_identifier); \ No newline at end of file +Status GetLatestBacking(char** _backing_identifier); + +Status GetBackingDiskInfo(const char* backing_identifier, DiskInfo* _info); \ No newline at end of file diff --git a/src/disk.c b/src/disk.c index bd64229..4bbf4bb 100644 --- a/src/disk.c +++ b/src/disk.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -233,9 +234,19 @@ Status GetDiskInfo(const char* path, DiskInfo* _info) { json_object_put(root); + if (_info->backing_file != NULL) { + _info->backing_identifier = strdup(basename(_info->backing_file)); + if (_info->backing_identifier == NULL) { + Log(LOG_LEVEL_ERROR, "Failed to duplicate the backing identifier (%s).", strerror(errno)); + FreeDiskInfo(_info); + return FAILURE; + } + } + return SUCCESS; } void FreeDiskInfo(DiskInfo* info) { free(info->backing_file); -} + free(info->backing_identifier); +} \ No newline at end of file diff --git a/src/disk.h b/src/disk.h index 2e51b55..80c3572 100644 --- a/src/disk.h +++ b/src/disk.h @@ -6,6 +6,7 @@ typedef struct { uint64_t size; uint64_t allocated; char* backing_file; + char* backing_identifier; } DiskInfo; Status CreateRootDisk(const char* path, uint64_t disk_size); @@ -15,4 +16,4 @@ Status TrimDisk(const char* path); Status RebaseDisk(const char* path, const char* backing_file); Status GetDiskInfo(const char* path, DiskInfo* _info); -void FreeDiskInfo(DiskInfo* info); \ No newline at end of file +void FreeDiskInfo(DiskInfo* info); diff --git a/src/entry.c b/src/entry.c index 87c72d2..c32d1af 100644 --- a/src/entry.c +++ b/src/entry.c @@ -489,3 +489,19 @@ Status TrimEntryDisk(const char* entry_identifier) { return status; } + +Status GetEntryDiskInfo(const char* entry_identifier, DiskInfo* _info) { + *_info = (DiskInfo){0}; + + // Get the disk path + char* entry_disk_path = NULL; + Status status = GetEntryDiskPath(entry_identifier, &entry_disk_path); + if (status != SUCCESS) + return status; + + // Get the disk info + status = GetDiskInfo(entry_disk_path, _info); + free(entry_disk_path); + + return status; +} diff --git a/src/entry.h b/src/entry.h index 40b9690..f686a6c 100644 --- a/src/entry.h +++ b/src/entry.h @@ -1,6 +1,7 @@ #pragma once #include "utils.h" +#include "disk.h" bool IsEntryIdentifierValid(const char* entry_identifier); @@ -21,4 +22,6 @@ Status AddRootEntryDisk(const char* entry_identifier, uint64_t disk_size); Status AddBackedEntryDisk(const char* entry_identifier, const char* backing_identifier); Status RemoveEntryDisk(const char* entry_identifier); Status ResetEntryDisk(const char* entry_identifier, const char* backing_identifier); -Status TrimEntryDisk(const char* entry_identifier); \ No newline at end of file +Status TrimEntryDisk(const char* entry_identifier); + +Status GetEntryDiskInfo(const char* entry_identifier, DiskInfo* _info); diff --git a/src/sandbox.c b/src/sandbox.c index b4efbc7..e8ab3e1 100644 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -221,10 +221,21 @@ int CommandListEntries(int argc, char* argv[]) { if (status != SUCCESS) return EXIT_FAILURE; - if (has_disk) - fprintf(stdout, "%zu | %-*s | %s |\n", i, (int)max_length, entries[i], "Has disk"); - else - fprintf(stdout, "%zu | %-*s | %s |\n", i, (int)max_length, entries[i], "No disk "); + if (has_disk) { + DiskInfo info; + status = GetEntryDiskInfo(entries[i], &info); + if (status != SUCCESS) + continue; + + // Format the size + char* size_fmt; + status = FormatSize(info.allocated, &size_fmt); + if (status != SUCCESS) + continue; + + fprintf(stdout, "%zu | %-*s | %-10s | %s%s\n", i, (int)max_length, entries[i], size_fmt, info.backing_identifier == NULL ? "No backing" : "Backed by ", info.backing_identifier == NULL ? "" : info.backing_identifier); + } else + fprintf(stdout, "%zu | %-*s | %s\n", i, (int)max_length, entries[i], "No disk "); } // Free the entries