Added add-backing and remove-backing commands (still missing recursive remove)

This commit is contained in:
2024-02-18 15:23:01 +01:00
parent 444bbc6ebe
commit 5613fbdcab
4 changed files with 186 additions and 3 deletions

View File

@@ -84,6 +84,124 @@ Status DoesBackingExist(const char* backing_identifier, bool* _result) {
return SUCCESS;
}
Status AddBacking(const char* backing_identifier, const char* entry_identifier) {
// Check that the backing does not already exist
bool exists;
Status status = DoesBackingExist(backing_identifier, &exists);
if (status != SUCCESS)
return status;
if (exists) {
Log(LOG_LEVEL_ERROR, "The backing '%s' already exists.", backing_identifier);
return FAILURE;
}
// Check that the entry disk exists
status = DoesEntryDiskExist(entry_identifier, &exists);
if (status != SUCCESS)
return status;
if (!exists) {
Log(LOG_LEVEL_ERROR, "The entry disk '%s' does not exist. Cannot create backing from it.", entry_identifier);
return FAILURE;
}
// Get information about the entry disk
DiskInfo entry_info;
status = GetEntryDiskInfo(entry_identifier, &entry_info);
if (status != SUCCESS)
return status;
// Check that the backing used by the entry actually exists (broken backing chain)
if (entry_info.backing_identifier != NULL) {
status = DoesBackingExist(entry_info.backing_identifier, &exists);
if (status != SUCCESS) {
FreeDiskInfo(&entry_info);
return status;
}
if (!exists) {
Log(LOG_LEVEL_ERROR, "The backing '%s' of the entry disk '%s' does not exist.", entry_info.backing_identifier, entry_identifier);
FreeDiskInfo(&entry_info);
return FAILURE;
}
}
// Copy the entry disk to the backing disk
char* entry_disk_path = NULL;
status = GetEntryDiskPath(entry_identifier, &entry_disk_path);
if (status != SUCCESS) {
FreeDiskInfo(&entry_info);
return status;
}
char* backing_path = NULL;
status = GetBackingPath(backing_identifier, &backing_path);
if (status != SUCCESS) {
free(entry_disk_path);
FreeDiskInfo(&entry_info);
return status;
}
status = CopyFile(entry_disk_path, backing_path);
if (status != SUCCESS) {
free(entry_disk_path);
free(backing_path);
FreeDiskInfo(&entry_info);
return status;
}
free(entry_disk_path);
// If the entry disk has a backing, rebase the backing to the new backing
if (entry_info.backing_identifier != NULL) {
status = RebaseDisk(backing_path, entry_info.backing_identifier);
if (status != SUCCESS) {
RemoveBacking(backing_identifier);
free(backing_path);
FreeDiskInfo(&entry_info);
return status;
}
}
free(backing_path);
FreeDiskInfo(&entry_info);
return SUCCESS;
}
Status RemoveBacking(const char* backing_identifier) {
// Check that the backing exists
bool exists;
Status status = DoesBackingExist(backing_identifier, &exists);
if (status != SUCCESS)
return status;
if (!exists) {
Log(LOG_LEVEL_ERROR, "The backing '%s' does not exist.", backing_identifier);
return FAILURE;
}
// Get the backing path
char* backing_path = NULL;
status = GetBackingPath(backing_identifier, &backing_path);
if (status != SUCCESS)
return status;
// Remove the backing
if (unlink(backing_path) != 0) {
Log(LOG_LEVEL_ERROR, "Failed to remove the backing '%s' (%s).", backing_identifier, strerror(errno));
free(backing_path);
return FAILURE;
}
free(backing_path);
// TODO: Remove all the backings that use this backing as a base
return SUCCESS;
}
Status ListBackings(char*** _backings) {
*_backings = NULL;