Implemented the creation of backing disks
This commit is contained in:
46
src/utils.c
46
src/utils.c
@@ -359,3 +359,49 @@ bool delete_directory(const char* directory, int level) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool copy_file(const char* source, const char* destination, mode_t mode, uid_t uid, gid_t gid) {
|
||||
// Open the source file
|
||||
FILE* source_file = fopen(source, "r");
|
||||
if (source_file == NULL) {
|
||||
log_msg(LOG_ERROR, "Failed to open file '%s' for reading (%s).", source, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Open the destination file
|
||||
FILE* destination_file = fopen(destination, "w");
|
||||
if (destination_file == NULL) {
|
||||
log_msg(LOG_ERROR, "Failed to open file '%s' for writing (%s).", destination, strerror(errno));
|
||||
fclose(source_file);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Copy the file
|
||||
char buffer[4096];
|
||||
size_t bytes_read;
|
||||
while ((bytes_read = fread(buffer, 1, sizeof(buffer), source_file)) > 0)
|
||||
if (fwrite(buffer, 1, bytes_read, destination_file) != bytes_read) {
|
||||
log_msg(LOG_ERROR, "Failed to write to file '%s' (%s).", destination, strerror(errno));
|
||||
fclose(source_file);
|
||||
fclose(destination_file);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Close the files
|
||||
fclose(source_file);
|
||||
fclose(destination_file);
|
||||
|
||||
// Change the mode of the destination file
|
||||
if (chmod(destination, mode) != 0) {
|
||||
log_msg(LOG_ERROR, "Failed to change the mode of file '%s' (%s).", destination, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Change the owner of the destination file
|
||||
if (chown(destination, uid, gid) != 0) {
|
||||
log_msg(LOG_ERROR, "Failed to change the owner of file '%s' (%s).", destination, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user