Added add_backing
This commit is contained in:
158
src/backing.c
158
src/backing.c
@@ -9,6 +9,7 @@
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <libgen.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
result_t check_backing_identifier(const char* backing) {
|
||||
@@ -69,10 +70,16 @@ result_t check_backing_exists(const char* backing) {
|
||||
}
|
||||
|
||||
result_t get_backing_pool_path(char** _path) {
|
||||
// Initialize the output parameters
|
||||
*_path = NULL;
|
||||
|
||||
return format(_path, "/var/lib/sandbox/backings");
|
||||
}
|
||||
|
||||
result_t get_backing_path(char** _path, const char* backing) {
|
||||
// Initialize the output parameters
|
||||
*_path = NULL;
|
||||
|
||||
// Check the backing identifier
|
||||
result_t result = check_backing_identifier(backing);
|
||||
if (result != success())
|
||||
@@ -92,6 +99,9 @@ result_t get_backing_path(char** _path, const char* backing) {
|
||||
}
|
||||
|
||||
result_t get_backing_default_path(char** _path) {
|
||||
// Initialize the output parameters
|
||||
*_path = NULL;
|
||||
|
||||
// Get the backing pool path
|
||||
char* pool_path;
|
||||
result_t result = get_backing_pool_path(&pool_path);
|
||||
@@ -105,6 +115,148 @@ result_t get_backing_default_path(char** _path) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result_t get_backing_temp_path(char** _path) {
|
||||
// Initialize the output parameters
|
||||
*_path = NULL;
|
||||
|
||||
// Get the backing pool path
|
||||
char* pool_path;
|
||||
result_t result = get_backing_pool_path(&pool_path);
|
||||
if (result != success())
|
||||
return result;
|
||||
|
||||
// Format the backing path
|
||||
result = format(_path, "%s/temp", pool_path);
|
||||
|
||||
free(pool_path);
|
||||
return result;
|
||||
}
|
||||
|
||||
result_t add_backing(char** _backing, const char* container) {
|
||||
// Initialize the output parameters
|
||||
*_backing = NULL;
|
||||
|
||||
// Get the container path
|
||||
char* path;
|
||||
result_t result = get_container_path(&path, container);
|
||||
if (result != success())
|
||||
return result;
|
||||
|
||||
// Import the container as a backing
|
||||
result = import_backing(_backing, path);
|
||||
|
||||
free(path);
|
||||
return result;
|
||||
}
|
||||
|
||||
result_t import_backing(char** _backing, const char* path) {
|
||||
// Initialize the output parameters
|
||||
*_backing = NULL;
|
||||
|
||||
// Get the temporary backing path
|
||||
char* temp_path;
|
||||
result_t result = get_backing_temp_path(&temp_path);
|
||||
if (result != success())
|
||||
return result;
|
||||
|
||||
// Copy the disk to the temporary backing path
|
||||
result = copy_file(path, temp_path);
|
||||
if (result != success()) {
|
||||
free(temp_path);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Get the disk info to know if the disk is backed
|
||||
disk_info_t info;
|
||||
result = get_disk_info(&info, temp_path);
|
||||
if (result != success()) {
|
||||
remove(temp_path);
|
||||
free(temp_path);
|
||||
return result;
|
||||
}
|
||||
|
||||
if (info.backing_path != NULL) {
|
||||
// Get the backing identifier of the parent
|
||||
char* backing = strdup(basename(info.backing_path));
|
||||
if (backing == NULL) {
|
||||
free_disk_info(&info);
|
||||
remove(temp_path);
|
||||
free(temp_path);
|
||||
return failure("Failed to get the backing identifier of the parent.");
|
||||
}
|
||||
|
||||
// Check that the backing exists
|
||||
result = check_backing_exists(backing);
|
||||
if (result != success()) {
|
||||
free_disk_info(&info);
|
||||
remove(temp_path);
|
||||
free(temp_path);
|
||||
free(backing);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Reback the disk to the parent
|
||||
result = reback_disk(temp_path, backing);
|
||||
if (result != success()) {
|
||||
free_disk_info(&info);
|
||||
remove(temp_path);
|
||||
free(temp_path);
|
||||
free(backing);
|
||||
return result;
|
||||
}
|
||||
|
||||
free(backing);
|
||||
}
|
||||
|
||||
free_disk_info(&info);
|
||||
|
||||
// Get the md5 hash of the disk as the backing identifier
|
||||
char* backing;
|
||||
result = md5sum(&backing, temp_path);
|
||||
if (result != success()) {
|
||||
remove(temp_path);
|
||||
free(temp_path);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Check if the backing already exists
|
||||
result = check_backing_exists(backing);
|
||||
if (result == success()) {
|
||||
result = failure("Backing '%s' already exists.", backing);
|
||||
free(backing);
|
||||
remove(temp_path);
|
||||
free(temp_path);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Get the path of the final backing
|
||||
char* backing_path;
|
||||
result = get_backing_path(&backing_path, backing);
|
||||
if (result != success()) {
|
||||
free(backing);
|
||||
remove(temp_path);
|
||||
free(temp_path);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Move the temporary backing to the final backing path
|
||||
errno = 0;
|
||||
if (rename(temp_path, backing_path) != 0) {
|
||||
result = failure("Failed to move the temporary backing to the final backing path (%s).", strerror(errno));
|
||||
free(backing);
|
||||
free(backing_path);
|
||||
remove(temp_path);
|
||||
free(temp_path);
|
||||
return result;
|
||||
}
|
||||
|
||||
free(backing_path);
|
||||
free(temp_path);
|
||||
|
||||
*_backing = backing;
|
||||
return result;
|
||||
}
|
||||
|
||||
result_t remove_backing(const char* backing) {
|
||||
// Check that the backing exists
|
||||
result_t result = check_backing_exists(backing);
|
||||
@@ -137,6 +289,9 @@ bool backing_filter(const char* file) {
|
||||
}
|
||||
|
||||
result_t list_backings(char*** _backings) {
|
||||
// Initialize the output parameters
|
||||
*_backings = NULL;
|
||||
|
||||
// Get the backing pool path
|
||||
char* pool_path;
|
||||
result_t result = get_backing_pool_path(&pool_path);
|
||||
@@ -151,6 +306,9 @@ result_t list_backings(char*** _backings) {
|
||||
}
|
||||
|
||||
result_t get_default_backing(char** _backing) {
|
||||
// Initialize the output parameters
|
||||
*_backing = NULL;
|
||||
|
||||
// Get the backing default path
|
||||
char* path;
|
||||
result_t result = get_backing_default_path(&path);
|
||||
|
Reference in New Issue
Block a user