Implemented creation of root disks
This commit is contained in:
55
src/utils.c
55
src/utils.c
@@ -7,6 +7,7 @@
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
@@ -287,3 +288,57 @@ Result read_fd(int fd, char** out_string) {
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
Result read_file(const char* path, char** out_string) {
|
||||
*out_string = NULL;
|
||||
|
||||
// Open the file
|
||||
int fd = open(path, O_RDONLY);
|
||||
if (fd == -1) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to open the file '%s' (%s).", path, strerror(errno));
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
// Read the file
|
||||
Result result = read_fd(fd, out_string);
|
||||
|
||||
// Close the file
|
||||
close(fd);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Result write_fd(int fd, const char* string) {
|
||||
// Write the string to the file descriptor
|
||||
size_t length = strlen(string);
|
||||
ssize_t bytes_written = 0;
|
||||
|
||||
while (bytes_written < length) {
|
||||
ssize_t result = write(fd, string + bytes_written, length - bytes_written);
|
||||
if (result == -1) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to write to the file descriptor (%s).", strerror(errno));
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
bytes_written += result;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
Result write_file(const char* path, const char* string) {
|
||||
// Open the file
|
||||
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (fd == -1) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to open the file '%s' (%s).", path, strerror(errno));
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
// Write the string to the file
|
||||
Result result = write_fd(fd, string);
|
||||
|
||||
// Close the file
|
||||
close(fd);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
Reference in New Issue
Block a user