Added backing signatures

This commit is contained in:
2024-02-17 14:21:14 +01:00
parent fd7b056457
commit 8821519a12
5 changed files with 105 additions and 10 deletions

View File

@@ -343,6 +343,60 @@ Result write_file(const char* path, const char* string) {
return result;
}
Result copy_file(const char* src_path, const char* dst_path, mode_t mode) {
// Open the source file
int src_fd = open(src_path, O_RDONLY);
if (src_fd == -1) {
log_message(LOG_LEVEL_ERROR, "Failed to open the source file '%s' (%s).", src_path, strerror(errno));
return FAILURE;
}
// Open the destination file
int dst_fd = open(dst_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
if (dst_fd == -1) {
log_message(LOG_LEVEL_ERROR, "Failed to open the destination file '%s' (%s).", dst_path, strerror(errno));
close(src_fd);
return FAILURE;
}
// Copy the file
char buffer[4096];
ssize_t bytes_read;
while ((bytes_read = read(src_fd, buffer, sizeof(buffer))) > 0) {
ssize_t bytes_written = 0;
while (bytes_written < bytes_read) {
ssize_t result = write(dst_fd, buffer + bytes_written, bytes_read - bytes_written);
if (result == -1) {
log_message(LOG_LEVEL_ERROR, "Failed to write to the destination file '%s' (%s).", dst_path, strerror(errno));
close(src_fd);
close(dst_fd);
return FAILURE;
}
bytes_written += result;
}
}
// Check if an error occurred
if (bytes_read == -1) {
log_message(LOG_LEVEL_ERROR, "Failed to read from the source file '%s' (%s).", src_path, strerror(errno));
close(src_fd);
close(dst_fd);
return FAILURE;
}
// Close the files
close(src_fd);
close(dst_fd);
return SUCCESS;
}
Result format_size(uint64_t size, char** out_string) {
*out_string = NULL;