Implemented some more functions. Next big step : AddBacking
This commit is contained in:
parent
051b95484c
commit
dcdec67476
@ -1,7 +1,15 @@
|
||||
#include "backing.h"
|
||||
|
||||
#include "disk.h"
|
||||
#include "entry.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <libgen.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
bool IsBackingIdentifierValid(const char* backing_identifier) {
|
||||
if (backing_identifier == NULL)
|
||||
@ -22,7 +30,7 @@ bool IsBackingIdentifierValid(const char* backing_identifier) {
|
||||
return false;
|
||||
|
||||
// Check that the identifier starts with a number
|
||||
if (backing_identifier[0] >= '0' && backing_identifier[0] <= '9')
|
||||
if (backing_identifier[0] < '0' || backing_identifier[0] > '9')
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
@ -7,3 +7,9 @@ int GetBackingIndex(const char* backing_identifier);
|
||||
|
||||
Status GetBackingPoolPath(char** _backing_pool_path);
|
||||
Status GetBackingDiskPath(const char* backing_identifier, char** _backing_path);
|
||||
|
||||
Status AddBacking(const char* backing_identifier, const char* entry_identifier);
|
||||
Status RemoveBacking(const char* backing_identifier);
|
||||
|
||||
Status ListBackings(char*** _backings);
|
||||
Status GetLatestBacking(char** _backing_identifier);
|
@ -1,6 +1,7 @@
|
||||
#include "sandbox.h"
|
||||
|
||||
#include "entry.h"
|
||||
#include "backing.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -145,7 +146,12 @@ int CommandAddEntry(int argc, char* argv[]) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return AddEntry(argv[0]);
|
||||
Status status = AddEntry(argv[0]);
|
||||
if (status != SUCCESS)
|
||||
return status;
|
||||
|
||||
Log(LOG_LEVEL_INFO, "Added entry '%s'.", argv[0]);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int CommandRemoveEntry(int argc, char* argv[]) {
|
||||
@ -157,7 +163,12 @@ int CommandRemoveEntry(int argc, char* argv[]) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return RemoveEntry(argv[0]);
|
||||
Status status = RemoveEntry(argv[0]);
|
||||
if (status != SUCCESS)
|
||||
return status;
|
||||
|
||||
Log(LOG_LEVEL_INFO, "Removed entry '%s'.", argv[0]);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int CommandListEntries(int argc, char* argv[]) {
|
||||
@ -187,5 +198,10 @@ int CommandClearEntries(int argc, char* argv[]) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return ClearEntries();
|
||||
Status status = ClearEntries();
|
||||
if (status != SUCCESS)
|
||||
return status;
|
||||
|
||||
Log(LOG_LEVEL_INFO, "Cleared all entries.");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
49
src/utils.c
49
src/utils.c
@ -35,6 +35,7 @@ void Log(LogLevel level, const char* format, ...) {
|
||||
case LOG_LEVEL_INFO:
|
||||
color = "\033[0;36m";
|
||||
level_str = "INFO";
|
||||
break;
|
||||
case LOG_LEVEL_WARNING:
|
||||
color = "\033[0;33m";
|
||||
level_str = "WARNING";
|
||||
@ -323,3 +324,51 @@ Status WriteFile(const char* path, const char* content) {
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
Status CopyFile(const char* source_path, const char* destination_path) {
|
||||
// Open the source file
|
||||
int source_file = open(source_path, O_RDONLY);
|
||||
if (source_file == -1) {
|
||||
Log(LOG_LEVEL_ERROR, "Failed to open the source file %s (%s).", source_path, strerror(errno));
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
// Open the destination file
|
||||
int destination_file = open(destination_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (destination_file == -1) {
|
||||
Log(LOG_LEVEL_ERROR, "Failed to open the destination file %s (%s).", destination_path, strerror(errno));
|
||||
close(source_file);
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
// Copy the file
|
||||
char buffer[4096];
|
||||
ssize_t bytes_read;
|
||||
while ((bytes_read = read(source_file, buffer, sizeof(buffer))) > 0) {
|
||||
ssize_t bytes_written = 0;
|
||||
while (bytes_written < bytes_read) {
|
||||
ssize_t result = write(destination_file, buffer + bytes_written, bytes_read - bytes_written);
|
||||
if (result == -1) {
|
||||
Log(LOG_LEVEL_ERROR, "Failed to write to the destination file %s (%s).", destination_path, strerror(errno));
|
||||
close(source_file);
|
||||
close(destination_file);
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
bytes_written += result;
|
||||
}
|
||||
}
|
||||
|
||||
if (bytes_read == -1) {
|
||||
Log(LOG_LEVEL_ERROR, "Failed to read from the source file %s (%s).", source_path, strerror(errno));
|
||||
close(source_file);
|
||||
close(destination_file);
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
// Close the files
|
||||
close(source_file);
|
||||
close(destination_file);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
@ -28,3 +28,5 @@ Status ReadFileDescriptor(int fd, char** _content);
|
||||
Status WriteFileDescriptor(int fd, const char* content);
|
||||
Status ReadFile(const char* path, char** _content);
|
||||
Status WriteFile(const char* path, const char* content);
|
||||
|
||||
Status CopyFile(const char* source_path, const char* destination_path);
|
Loading…
Reference in New Issue
Block a user