Implemented some entry functions
This commit is contained in:
parent
a2339d7515
commit
d13a2e3053
103
src/entry.c
103
src/entry.c
@ -1,10 +1,12 @@
|
||||
#include "entry.h"
|
||||
|
||||
#include "utils.h"
|
||||
#include "disk.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
bool is_entry_id_valid(const char* entry_id) {
|
||||
@ -62,3 +64,104 @@ Result entry_exists(const char* entry_id, bool* out_exists) {
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
Result add_root_entry(const char* entry_id, uint64_t size) {
|
||||
// Check that the entry does not exist
|
||||
bool exists;
|
||||
Result result = entry_exists(entry_id, &exists);
|
||||
if (result != SUCCESS)
|
||||
return result;
|
||||
|
||||
if (exists) {
|
||||
log_message(LOG_LEVEL_ERROR, "Entry '%s' already exists.", entry_id);
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
// Get the path of the entry
|
||||
char* path;
|
||||
result = get_entry_path(entry_id, &path);
|
||||
if (result != SUCCESS)
|
||||
return result;
|
||||
|
||||
// Create the directory
|
||||
int return_code = mkdir(path, 0755);
|
||||
|
||||
// Free the path as it is no longer needed
|
||||
free(path);
|
||||
|
||||
// Check if the directory was created
|
||||
if (return_code != 0) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to create the entry '%s' (%s).", entry_id, strerror(errno));
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
// Get the disk path
|
||||
char* disk_path;
|
||||
result = get_entry_disk_path(entry_id, &disk_path);
|
||||
if (result != SUCCESS) {
|
||||
remove_entry(entry_id);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Create the disk
|
||||
result = create_root_disk(disk_path, size, 0644);
|
||||
|
||||
// Free the disk path as it is no longer needed
|
||||
free(disk_path);
|
||||
|
||||
// Remove the entry if the disk could not be created
|
||||
if (result != SUCCESS) {
|
||||
remove_entry(entry_id);
|
||||
return result;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
Result remove_entry(const char* entry_id) {
|
||||
// Remove the disk
|
||||
char* disk_path;
|
||||
Result result = get_entry_disk_path(entry_id, &disk_path);
|
||||
if (result == SUCCESS) {
|
||||
unlink(disk_path);
|
||||
free(disk_path);
|
||||
}
|
||||
|
||||
// Get the path of the entry
|
||||
char* path;
|
||||
result = get_entry_path(entry_id, &path);
|
||||
if (result != SUCCESS)
|
||||
return result;
|
||||
|
||||
// Remove the directory
|
||||
int return_code = rmdir(path);
|
||||
|
||||
// Free the path as it is no longer needed
|
||||
free(path);
|
||||
|
||||
// Check if the directory was removed
|
||||
if (return_code != 0) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to remove the entry '%s' (%s).", entry_id, strerror(errno));
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
Result get_entry_disk_path(const char* entry_id, char** out_path) {
|
||||
*out_path = NULL;
|
||||
|
||||
// Get the path of the entry
|
||||
char* path;
|
||||
Result result = get_entry_path(entry_id, &path);
|
||||
if (result != SUCCESS)
|
||||
return result;
|
||||
|
||||
// Format the disk path
|
||||
result = format(out_path, "%s/disk", path);
|
||||
|
||||
// Free the path as it is no longer needed
|
||||
free(path);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user