Added commands for managing disks

This commit is contained in:
2024-02-18 14:05:04 +01:00
parent a149a838d1
commit 3e08d6a438
8 changed files with 403 additions and 35 deletions

View File

@@ -49,7 +49,7 @@ Status GetBackingPoolPath(char** _backing_pool_path) {
return Format(_backing_pool_path, "/var/lib/sandbox/backings");
}
Status GetBackingDiskPath(const char* backing_identifier, char** _backing_path) {
Status GetBackingPath(const char* backing_identifier, char** _backing_path) {
// Check that the identifier is valid, as it will be used in a path
if (!IsBackingIdentifierValid(backing_identifier)) {
Log(LOG_LEVEL_ERROR, "Invalid backing identifier '%s'.", backing_identifier);
@@ -72,7 +72,7 @@ Status GetBackingDiskPath(const char* backing_identifier, char** _backing_path)
Status DoesBackingExist(const char* backing_identifier, bool* _result) {
// Get the backing path
char* backing_path = NULL;
Status status = GetBackingDiskPath(backing_identifier, &backing_path);
Status status = GetBackingPath(backing_identifier, &backing_path);
if (status != SUCCESS)
return status;
@@ -83,3 +83,116 @@ Status DoesBackingExist(const char* backing_identifier, bool* _result) {
return SUCCESS;
}
Status ListBackings(char*** _backings) {
*_backings = NULL;
// Open the backing pool directory
char* backing_pool_path = NULL;
Status status = GetBackingPoolPath(&backing_pool_path);
if (status != SUCCESS)
return status;
DIR* dir = opendir(backing_pool_path);
if (dir == NULL) {
Log(LOG_LEVEL_ERROR, "Failed to open the backing pool directory %s (%s).", backing_pool_path, strerror(errno));
free(backing_pool_path);
return FAILURE;
}
free(backing_pool_path);
// Allocate memory for at least one backing (the NULL terminator)
*_backings = malloc(sizeof(char*));
if (*_backings == NULL) {
Log(LOG_LEVEL_ERROR, "Failed to allocate memory for the backings list (%s).", strerror(errno));
closedir(dir);
return FAILURE;
}
(*_backings)[0] = NULL;
// Read the files in the backing pool directory
size_t count = 0;
struct dirent* file;
while ((file = readdir(dir)) != NULL) {
if (strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)
continue;
// Check if the backing exists (this checks for validity)
bool exists;
Status status = DoesBackingExist(file->d_name, &exists);
if (status != SUCCESS || !exists)
continue;
// Allocate memory for the new backing list
char** new_backings = realloc(*_backings, (count + 2) * sizeof(char*)); // +2 for the new entry and the NULL terminator
if (new_backings == NULL) {
Log(LOG_LEVEL_ERROR, "Failed to reallocate memory for the backing list (%s).", strerror(errno));
closedir(dir);
for (size_t i = 0; i < count; i++)
free((*_backings)[i]);
free(*_backings);
return FAILURE;
}
*_backings = new_backings;
// Duplicate the file name and add it to the list
(*_backings)[count] = strdup(file->d_name);
if ((*_backings)[count] == NULL) {
Log(LOG_LEVEL_ERROR, "Failed to duplicate the backing name (%s).", strerror(errno));
closedir(dir);
for (size_t i = 0; i < count; i++)
free((*_backings)[i]);
free(*_backings);
return FAILURE;
}
// Add the NULL terminator
(*_backings)[count + 1] = NULL;
count++;
}
closedir(dir);
return SUCCESS;
}
Status GetLatestBacking(char** _backing_identifier) {
// List the backings
char** backings = NULL;
Status status = ListBackings(&backings);
if (status != SUCCESS)
return status;
// Find the latest backing
int latest_identifier_index = -1;
int latest_index = -1;
for (int i = 0; backings[i] != NULL; i++) {
int index = GetBackingIndex(backings[i]);
if (index > latest_index) {
latest_identifier_index = i;
latest_index = index;
}
}
// Return the latest backing
if (latest_identifier_index == -1) {
*_backing_identifier = NULL;
} else {
*_backing_identifier = strdup(backings[latest_identifier_index]);
if (*_backing_identifier == NULL) {
Log(LOG_LEVEL_ERROR, "Failed to duplicate the backing identifier (%s).", strerror(errno));
status = FAILURE;
}
}
// Free the backings list
for (int i = 0; backings[i] != NULL; i++)
free(backings[i]);
free(backings);
return status;
}