Implemented the creation of backing disks
This commit is contained in:
@@ -9,6 +9,8 @@
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
char* get_backings_path(void) {
|
||||
@@ -182,3 +184,44 @@ char* find_latest_backing(void) {
|
||||
|
||||
return latest_backing;
|
||||
}
|
||||
|
||||
bool create_backing(const char* disk, const char* description) {
|
||||
// Create the backing disk name
|
||||
uint64_t timestamp = (uint64_t)time(NULL);
|
||||
char* backing_name = NULL;
|
||||
|
||||
if (description == NULL || strlen(description) == 0)
|
||||
backing_name = format("%lu", timestamp);
|
||||
else
|
||||
backing_name = format("%lu-%s", timestamp, description);
|
||||
|
||||
if (backing_name == NULL) {
|
||||
log_msg(LOG_ERROR, "Failed to create the backing disk name.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the backing disk path
|
||||
char* backings_path = get_backings_path();
|
||||
if (backings_path == NULL) {
|
||||
free(backing_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
char* backing_path = format("%s/%s", backings_path, backing_name);
|
||||
free(backing_name);
|
||||
free(backings_path);
|
||||
|
||||
if (backing_path == NULL)
|
||||
return false;
|
||||
|
||||
// Copy the disk to the backing disk
|
||||
if (!copy_file(disk, backing_path, 0755, 0, 0)) {
|
||||
free(backing_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Free the backing disk path
|
||||
free(backing_path);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user