Implemented the add-entry command

This commit is contained in:
2024-02-17 14:38:52 +01:00
parent 8821519a12
commit f0dd2ca5fb
4 changed files with 162 additions and 1 deletions

View File

@@ -199,6 +199,18 @@ Result add_root_entry(const char* entry_id, uint64_t size) {
return SUCCESS;
}
Result add_backed_entry(const char* entry_id, const char* backing_id) {
// TODO: Implement
return FAILURE;
}
Result add_automatic_entry(const char* entry_id) {
// TODO: Implement
return FAILURE;
}
Result remove_entry(const char* entry_id) {
// Check that it exists
bool exists;
@@ -217,6 +229,48 @@ Result remove_entry(const char* entry_id) {
if (result != SUCCESS)
return result;
// Remove all the files in the directory
DIR* dir = opendir(entry_path);
if (dir == NULL) {
log_message(LOG_LEVEL_ERROR, "Failed to open the directory '%s' (%s).", entry_path, strerror(errno));
free(entry_path);
return FAILURE;
}
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
// Skip the current and parent directories
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
// Get the path of the file
char* file_path;
result = format(&file_path, "%s/%s", entry_path, entry->d_name);
if (result != SUCCESS) {
closedir(dir);
free(entry_path);
return result;
}
// Remove the file
if (unlink(file_path) == -1) {
log_message(LOG_LEVEL_ERROR, "Failed to remove the file '%s' (%s).", file_path, strerror(errno));
free(file_path);
closedir(dir);
free(entry_path);
return FAILURE;
}
// Free the entry file path
free(file_path);
}
// Close the directory
closedir(dir);
// Remove the directory
if (rmdir(entry_path) == -1) {
log_message(LOG_LEVEL_ERROR, "Failed to remove the directory '%s' (%s).", entry_path, strerror(errno));