Implemented some more functions. Next big step : AddBacking

This commit is contained in:
2024-02-18 00:28:42 +01:00
parent 051b95484c
commit dcdec67476
5 changed files with 85 additions and 4 deletions

View File

@@ -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;
}