Added signatures for the backing system.

This commit is contained in:
Alexei KADIR 2024-02-16 23:26:29 +01:00
parent 332f5ccfd0
commit 66caca4a7f
4 changed files with 53 additions and 53 deletions

View File

@ -4,3 +4,36 @@
#include <stdint.h>
#define BACKING_POOL "/var/lib/sandbox/backings"
/// @brief Checks whether the backing pool exists and is a directory.
/// @return True if the backing pool exists and is a directory, false otherwise.
bool check_backing_pool(void);
/// @brief Checks whether the backing disk id is valid.
/// @param backing_id The backing disk id to check.
/// @return True if the backing disk id is valid, false otherwise.
bool is_backing_id_valid(const char* backing_id);
/// @brief Checks whether the backing disk exists.
/// @param backing_id The backing disk id to check.
/// @return True if the backing disk exists, false otherwise.
bool backing_exists(const char* backing_id);
/// @brief Adds a backing disk to the backing pool.
/// @param backing_id The backing disk id to add.
/// @param entry_id The entry from which the backing disk should be added.
/// @return True if the backing disk was added, false otherwise.
bool add_backing(const char* backing_id, const char* entry_id);
/// @brief Removes a backing disk from the backing pool, as well as any backing disks that depend on it.
/// @param backing_id The backing disk id to remove.
/// @return True if the backing disk was removed, false otherwise.
bool remove_backing(const char* backing_id);
/// @brief Lists all backing disks in the backing pool.
/// @return An array of backing disk ids or NULL if an error occurred. The result must be freed with free(), as well as each individual backing disk id.
char** list_backings(void);
/// @brief Clears all backing disks from the backing pool.
/// @return True if all backing disks were removed, false otherwise.
bool clear_backings(void);

View File

@ -22,6 +22,8 @@ int main(int argc, char* argv[]) {
int consumed = 2;
// TODO: Implement log level parameter
switch (command->id) {
case CMD_HELP:
return cmd_help(argc - consumed, argv + consumed);

View File

@ -276,53 +276,3 @@ char* read_file(int fd) {
return data;
}
bool copy_file(const char* src, const char* dst, mode_t mode) {
// Open the source file
int src_fd = open(src, O_RDONLY);
if (src_fd == -1) {
log_message(LOG_ERROR, "Failed to open the source file %s (%s).", src, strerror(errno));
return false;
}
// Open the destination file
int dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode);
if (dst_fd == -1) {
log_message(LOG_ERROR, "Failed to open the destination file %s (%s).", dst, strerror(errno));
close(src_fd);
return false;
}
// Copy the file
char buffer[4096];
ssize_t n;
while ((n = read(src_fd, buffer, sizeof(buffer))) > 0) {
if (write(dst_fd, buffer, n) != n) {
log_message(LOG_ERROR, "Failed to write to the destination file %s (%s).", dst, strerror(errno));
close(src_fd);
close(dst_fd);
return false;
}
}
// Check for read errors
if (n < 0) {
log_message(LOG_ERROR, "Failed to read the source file %s (%s).", src, strerror(errno));
close(src_fd);
close(dst_fd);
return false;
}
// Close the files
close(src_fd);
close(dst_fd);
return true;
}

View File

@ -11,12 +11,27 @@ typedef enum {
LOG_ERROR,
} LogLevel;
/// @brief Formats a string using printf-style formatting.
/// @param fmt The format string.
/// @param ... The arguments to format into the string.
/// @return The formatted string or NULL if an error occurred. The result must be freed with free().
char* format(const char* fmt, ...);
/// @brief Logs a message to the console.
/// @param level The log level of the message.
/// @param fmt The format string of the message.
/// @param ... The arguments to format into the message.
void log_message(LogLevel level, const char* fmt, ...);
/// @brief Executes a command and returns the exit code as well as the output and error streams.
/// @param outb The output stream of the command. May be NULL if the output is not needed.
/// @param errb The error stream of the command. May be NULL if the error is not needed.
/// @param file The file to execute.
/// @param ... The arguments to pass to the file.
/// @return The exit code of the command.
int execute(char** outb, char** errb, const char* file, ...);
char* read_file(int fd);
bool copy_file(const char* src, const char* dst, mode_t mode);
/// @brief Reads the contents of a file into a string. The result might contain null bytes.
/// @param fd The file descriptor of the file to read.
/// @return The contents of the file or NULL if an error occurred. The result must be freed with free().
char* read_file(int fd);