Files
linuxinstall/src/utils.c

496 lines
12 KiB
C
Raw Normal View History

2024-02-15 19:42:22 +01:00
#include "utils.h"
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
2024-02-17 23:59:38 +01:00
#include <stdlib.h>
2024-02-19 16:01:53 +01:00
#include <string.h>
2024-02-17 01:20:20 +01:00
#include <unistd.h>
#include <sys/wait.h>
2024-02-19 16:01:53 +01:00
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <openssl/md5.h>
2024-02-15 19:42:22 +01:00
2024-02-19 16:01:53 +01:00
char _error_buffer[ERROR_BUFFER_SIZE];
2024-02-15 19:42:22 +01:00
2024-02-19 16:01:53 +01:00
result_t success(void) {
return EXIT_SUCCESS;
2024-02-15 19:42:22 +01:00
}
2024-02-19 16:01:53 +01:00
result_t failure(const char* format, ...) {
2024-02-15 19:42:22 +01:00
va_list args;
va_start(args, format);
2024-02-15 19:42:22 +01:00
2024-02-19 16:01:53 +01:00
// Print the error message into the error buffer
vsnprintf(_error_buffer, ERROR_BUFFER_SIZE, format, args);
2024-02-15 19:42:22 +01:00
2024-02-19 16:01:53 +01:00
va_end(args);
2024-02-15 19:42:22 +01:00
2024-02-19 16:01:53 +01:00
return EXIT_FAILURE;
}
2024-02-15 19:42:22 +01:00
2024-02-19 16:01:53 +01:00
const char* error(void) {
return _error_buffer;
2024-02-15 19:42:22 +01:00
}
2024-02-19 16:01:53 +01:00
result_t format(char** _str, const char* format, ...) {
// Initialize the output parameters
*_str = NULL;
2024-02-15 19:42:22 +01:00
2024-02-19 16:01:53 +01:00
// Calculate the length of the formatted string
2024-02-15 19:42:22 +01:00
va_list args;
2024-02-19 16:01:53 +01:00
va_start(args, format);
int length = vsnprintf(NULL, 0, format, args);
2024-02-15 19:42:22 +01:00
va_end(args);
2024-02-19 16:01:53 +01:00
if (length < 0)
return failure("Failed to calculate the length of the formatted string.");
2024-02-15 19:42:22 +01:00
2024-02-19 16:01:53 +01:00
// Allocate memory for the formatted string
errno = 0;
char* str = malloc(length + 1);
if (str == NULL)
return failure("Failed to allocate memory for the formatted string (%s).", strerror(errno));
// Format the string
va_start(args, format);
2024-02-15 19:42:22 +01:00
2024-02-19 16:01:53 +01:00
if (vsnprintf(str, length + 1, format, args) < 0)
return failure("Failed to format the string.");
2024-02-15 19:42:22 +01:00
va_end(args);
2024-02-19 16:01:53 +01:00
// Return the formatted string
*_str = str;
return success();
2024-02-15 19:42:22 +01:00
}
2024-02-17 01:20:20 +01:00
2024-02-19 16:01:53 +01:00
result_t substring(char** _str, const char* str, size_t start, size_t length) {
// Initialize the output parameters
*_str = NULL;
2024-02-18 18:09:53 +01:00
2024-02-19 16:01:53 +01:00
// Check the start index
size_t str_length = strlen(str);
2024-02-18 18:09:53 +01:00
2024-02-19 16:01:53 +01:00
if (start > str_length)
return failure("The start index is out of range.");
if (start + length > str_length)
length = str_length - start;
2024-02-18 18:09:53 +01:00
2024-02-19 16:01:53 +01:00
// Allocate memory for the substring
errno = 0;
char* substr = malloc(length + 1); // +1 for the null terminator
if (substr == NULL)
return failure("Failed to allocate memory for the substring (%s).", strerror(errno));
2024-02-18 18:09:53 +01:00
2024-02-19 16:01:53 +01:00
// Copy the substring
memcpy(substr, str + start, length);
substr[length] = '\0';
2024-02-18 18:09:53 +01:00
2024-02-19 16:01:53 +01:00
// Return the substring
*_str = substr;
return success();
2024-02-18 18:09:53 +01:00
}
2024-02-19 16:01:53 +01:00
result_t format_size(char** _str, uint64_t size) {
// Initialize the output parameters
*_str = NULL;
2024-02-18 14:05:04 +01:00
// Format the size
2024-02-19 16:01:53 +01:00
if (size < 1024ULL)
return format(_str, "%lluB", size);
if (size < 1024ULL * 1024)
return format(_str, "%.2fKiB", size / (1024.0));
if (size < 1024ULL * 1024 * 1024)
return format(_str, "%.2fMiB", size / (1024.0 * 1024.0));
if (size < 1024ULL * 1024 * 1024 * 1024)
return format(_str, "%.2fGiB", size / (1024.0 * 1024.0 * 1024.0));
if (size < 1024ULL * 1024 * 1024 * 1024 * 1024)
return format(_str, "%.2fTiB", size / (1024.0 * 1024.0 * 1024.0 * 1024.0));
if (size < 1024ULL * 1024 * 1024 * 1024 * 1024 * 1024)
return format(_str, "%.2fPiB", size / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0));
return format(_str, "%.2fEiB", size / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0));
2024-02-18 14:05:04 +01:00
}
2024-02-19 16:01:53 +01:00
result_t parse_size(uint64_t* _size, const char* str) {
// Initialize the output parameters
2024-02-18 14:05:04 +01:00
*_size = 0;
// Parse the size
char* endptr;
2024-02-19 16:01:53 +01:00
uint64_t size = strtoull(str, &endptr, 10);
if (endptr == str)
return failure("Failed to parse the size.");
2024-02-18 14:05:04 +01:00
2024-02-19 16:01:53 +01:00
// Check the suffix
2024-02-18 14:05:04 +01:00
if (*endptr == '\0') {
2024-02-19 16:01:53 +01:00
// No suffix
2024-02-18 14:05:04 +01:00
*_size = size;
2024-02-19 16:01:53 +01:00
return success();
2024-02-18 14:05:04 +01:00
}
2024-02-19 16:01:53 +01:00
// Parse the suffix
if (endptr[1] != '\0')
return failure("Invalid size suffix.");
2024-02-18 14:05:04 +01:00
2024-02-19 16:01:53 +01:00
switch (endptr[0]) {
case 'B':
*_size = size;
return success();
case 'K':
*_size = size * 1024;
return success();
case 'M':
*_size = size * 1024 * 1024;
return success();
case 'G':
*_size = size * 1024 * 1024 * 1024;
return success();
case 'T':
*_size = size * 1024 * 1024 * 1024 * 1024;
return success();
case 'P':
*_size = size * 1024 * 1024 * 1024 * 1024 * 1024;
return success();
case 'E':
*_size = size * 1024 * 1024 * 1024 * 1024 * 1024 * 1024;
return success();
default:
return failure("Invalid size suffix.");
}
2024-02-18 14:05:04 +01:00
}
2024-02-19 16:01:53 +01:00
result_t execute(int* _exit_code, char** _stdoutbuf, char** _stderrbuf, const char* executable, ...) {
// Initialize the output parameters
2024-02-17 23:59:38 +01:00
if (_exit_code != NULL)
2024-02-19 16:01:53 +01:00
*_exit_code = 0;
if (_stdoutbuf != NULL)
*_stdoutbuf = NULL;
if (_stderrbuf != NULL)
*_stderrbuf = NULL;
2024-02-17 01:20:20 +01:00
2024-02-19 16:01:53 +01:00
// Count the number of arguments to pass to the executable
int argc = 1; // +1 for the executable itself
2024-02-17 01:20:20 +01:00
va_list args;
va_start(args, executable);
while (va_arg(args, const char*) != NULL)
argc++;
va_end(args);
2024-02-19 16:01:53 +01:00
// Allocate memory for the arguments array
errno = 0;
char** argv = malloc((argc + 1) * sizeof(char*)); // +1 for the NULL terminator
if (argv == NULL)
return failure("Failed to allocate memory for the arguments array (%s).", strerror(errno));
2024-02-17 01:20:20 +01:00
2024-02-17 23:59:38 +01:00
// Fill the arguments array
2024-02-19 16:01:53 +01:00
errno = 0;
2024-02-17 01:20:20 +01:00
argv[0] = strdup(executable);
if (argv[0] == NULL) {
free(argv);
2024-02-19 16:01:53 +01:00
return failure("Failed to duplicate the executable string (%s).", strerror(errno));
2024-02-17 01:20:20 +01:00
}
va_start(args, executable);
for (int i = 1; i < argc; i++) {
argv[i] = strdup(va_arg(args, const char*));
if (argv[i] == NULL) {
for (int j = 0; j < i; j++)
free(argv[j]);
free(argv);
2024-02-19 16:01:53 +01:00
va_end(args);
return failure("Failed to duplicate the argument string (%s).", strerror(errno));
2024-02-17 01:20:20 +01:00
}
}
va_end(args);
argv[argc] = NULL;
2024-02-19 16:01:53 +01:00
// Create pipes for the standard output and standard error of the child process
2024-02-17 01:20:20 +01:00
int stdout_pipe[2];
2024-02-19 16:01:53 +01:00
errno = 0;
if (pipe(stdout_pipe) < 0) {
2024-02-17 01:20:20 +01:00
for (int i = 0; i < argc; i++)
free(argv[i]);
free(argv);
2024-02-19 16:01:53 +01:00
return failure("Failed to create a pipe for the standard output of the child process (%s).", strerror(errno));
2024-02-17 01:20:20 +01:00
}
int stderr_pipe[2];
2024-02-19 16:01:53 +01:00
errno = 0;
if (pipe(stderr_pipe) < 0) {
2024-02-17 01:20:20 +01:00
for (int i = 0; i < argc; i++)
free(argv[i]);
free(argv);
close(stdout_pipe[0]);
close(stdout_pipe[1]);
2024-02-19 16:01:53 +01:00
return failure("Failed to create a pipe for the standard error of the child process (%s).", strerror(errno));
2024-02-17 01:20:20 +01:00
}
2024-02-19 16:01:53 +01:00
// Fork the child process
errno = 0;
2024-02-17 01:20:20 +01:00
pid_t pid = fork();
2024-02-19 16:01:53 +01:00
if (pid < 0) {
2024-02-17 01:20:20 +01:00
for (int i = 0; i < argc; i++)
free(argv[i]);
free(argv);
close(stdout_pipe[0]);
close(stdout_pipe[1]);
close(stderr_pipe[0]);
close(stderr_pipe[1]);
2024-02-19 16:01:53 +01:00
return failure("Failed to fork the child process (%s).", strerror(errno));
2024-02-17 01:20:20 +01:00
}
if (pid == 0) {
2024-02-19 16:01:53 +01:00
// Redirect the standard output and standard error of the child process
2024-02-17 01:20:20 +01:00
close(stdout_pipe[0]);
close(stderr_pipe[0]);
2024-02-19 16:01:53 +01:00
if (dup2(stdout_pipe[1], STDOUT_FILENO) < 0)
exit(EXIT_FAILURE);
if (dup2(stderr_pipe[1], STDERR_FILENO) < 0)
exit(EXIT_FAILURE);
close(stdout_pipe[1]);
2024-02-17 01:20:20 +01:00
close(stderr_pipe[1]);
2024-02-19 16:01:53 +01:00
// Execute the child process
errno = 0;
execvp(executable, argv);
2024-02-17 01:20:20 +01:00
2024-02-19 16:01:53 +01:00
// If the child process reaches this point, it failed to execute
fprintf(stderr, "Failed to execute the child process (%s).\n", strerror(errno));
2024-02-17 01:20:20 +01:00
exit(EXIT_FAILURE);
}
2024-02-19 16:01:53 +01:00
// Free the arguments array
2024-02-17 23:59:38 +01:00
for (int i = 0; i < argc; i++)
free(argv[i]);
free(argv);
2024-02-19 16:01:53 +01:00
// Close the write ends of the pipes
close(stdout_pipe[1]);
close(stderr_pipe[1]);
2024-02-17 23:59:38 +01:00
// Wait for the child process to terminate
2024-02-17 01:20:20 +01:00
int status;
2024-02-19 16:01:53 +01:00
errno = 0;
if (waitpid(pid, &status, 0) < 0) {
close(stdout_pipe[0]);
close(stderr_pipe[0]);
return failure("Failed to wait for the child process to terminate (%s).", strerror(errno));
}
2024-02-17 23:59:38 +01:00
2024-02-19 16:01:53 +01:00
// Read the standard output and standard error of the child process
if (_exit_code != NULL)
*_exit_code = WEXITSTATUS(status);
if (_stdoutbuf != NULL)
read_fd(_stdoutbuf, stdout_pipe[0]);
if (_stderrbuf != NULL)
read_fd(_stderrbuf, stderr_pipe[0]);
2024-02-17 01:20:20 +01:00
2024-02-19 16:01:53 +01:00
// Close the read ends of the pipes
2024-02-17 01:20:20 +01:00
close(stdout_pipe[0]);
close(stderr_pipe[0]);
2024-02-19 16:01:53 +01:00
return success();
2024-02-17 01:20:20 +01:00
}
2024-02-19 16:01:53 +01:00
result_t read_fd(char** _str, int fd) {
// Initialize the output parameters
*_str = NULL;
2024-02-17 01:20:20 +01:00
2024-02-19 16:01:53 +01:00
// Read the file descriptor
2024-02-17 01:20:20 +01:00
size_t length = 0;
2024-02-19 16:01:53 +01:00
size_t capacity = 4096;
// Allocate memory for the output string
errno = 0;
char* str = malloc(capacity + 1); // +1 for the null terminator
if (str == NULL)
return failure("Failed to allocate memory for the output string (%s).", strerror(errno));
ssize_t count;
while (1) {
// Read the file descriptor into the output string
errno = 0;
count = read(fd, str + length, capacity - length);
if (count < 0) {
free(str);
return failure("Failed to read the file descriptor %d (%s).", fd, strerror(errno));
2024-02-17 01:20:20 +01:00
}
2024-02-19 16:01:53 +01:00
// Update the length of the output string
length += count;
if (count == 0)
break;
2024-02-17 23:59:38 +01:00
2024-02-19 16:01:53 +01:00
// Reallocate memory for the output string if necessary
if (length >= capacity) {
capacity *= 2;
2024-02-17 01:20:20 +01:00
2024-02-19 16:01:53 +01:00
errno = 0;
char* new_str = realloc(str, capacity + 1); // +1 for the null terminator
if (new_str == NULL) {
free(str);
return failure("Failed to reallocate memory for the output string (%s).", strerror(errno));
}
str = new_str;
}
2024-02-17 01:20:20 +01:00
}
2024-02-19 16:01:53 +01:00
// Null-terminate the string
str[length] = '\0';
// Return the output string
*_str = str;
return success();
2024-02-17 01:20:20 +01:00
}
2024-02-17 12:34:24 +01:00
2024-02-19 16:01:53 +01:00
result_t write_fd(int fd, const char* str) {
// Write the string to the file descriptor
size_t length = strlen(str);
size_t written = 0;
2024-02-17 12:34:24 +01:00
2024-02-19 16:01:53 +01:00
while (written < length) {
// Try to write the entire string at once
errno = 0;
ssize_t count = write(fd, str + written, length - written);
if (count < 0)
return failure("Failed to write to the file descriptor %d (%s).", fd, strerror(errno));
2024-02-17 12:34:24 +01:00
2024-02-19 16:01:53 +01:00
written += count;
2024-02-17 12:34:24 +01:00
}
2024-02-19 16:01:53 +01:00
return success();
2024-02-17 12:34:24 +01:00
}
2024-02-19 16:01:53 +01:00
result_t read_file(char** _str, const char* path) {
// Initialize the output parameters
*_str = NULL;
2024-02-17 23:59:38 +01:00
2024-02-17 12:34:24 +01:00
// Open the file
2024-02-19 16:01:53 +01:00
errno = 0;
int fd = open(path, O_RDONLY);
if (fd < 0)
return failure("Failed to open the file '%s' (%s).", path, strerror(errno));
2024-02-17 12:34:24 +01:00
2024-02-17 23:59:38 +01:00
// Read the file
2024-02-19 16:01:53 +01:00
result_t result = read_fd(_str, fd);
2024-02-17 12:34:24 +01:00
// Close the file
2024-02-19 16:01:53 +01:00
close(fd);
2024-02-17 12:34:24 +01:00
2024-02-19 16:01:53 +01:00
return result;
2024-02-17 12:34:24 +01:00
}
2024-02-19 16:01:53 +01:00
result_t write_file(const char* path, const char* str) {
2024-02-17 23:59:38 +01:00
// Open the file
2024-02-19 16:01:53 +01:00
errno = 0;
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
return failure("Failed to open the file '%s' (%s).", path, strerror(errno));
2024-02-17 14:21:14 +01:00
2024-02-19 16:01:53 +01:00
// Write the string to the file
result_t result = write_fd(fd, str);
2024-02-17 14:21:14 +01:00
2024-02-17 23:59:38 +01:00
// Close the file
2024-02-19 16:01:53 +01:00
close(fd);
2024-02-17 14:38:52 +01:00
2024-02-19 16:01:53 +01:00
return result;
2024-02-17 14:38:52 +01:00
}
2024-02-19 16:01:53 +01:00
result_t list_files(char*** _files, const char* path, bool (*filter)(const char*)) {
// Initialize the output parameters
*_files = NULL;
2024-02-19 16:01:53 +01:00
// Allocate memory for the files array
size_t length = 0;
size_t capacity = 64;
errno = 0;
char** files = malloc(capacity * sizeof(char*));
if (files == NULL)
return failure("Failed to allocate memory for the files array (%s).", strerror(errno));
// Open the directory
errno = 0;
DIR* dir = opendir(path);
if (dir == NULL) {
free(files);
return failure("Failed to open the directory '%s' (%s).", path, strerror(errno));
}
2024-02-19 16:01:53 +01:00
// Read the directory
struct dirent* entry;
while (1) {
// Read the next entry
errno = 0;
entry = readdir(dir);
if (entry == NULL) {
if (errno != 0) {
for (size_t i = 0; i < length; i++)
free(files[i]);
free(files);
closedir(dir);
return failure("Failed to read the directory '%s' (%s).", path, strerror(errno));
}
2024-02-19 16:01:53 +01:00
break;
}
2024-02-19 16:01:53 +01:00
// Filter the entry
if (filter != NULL && !filter(entry->d_name))
continue;
// Add the entry to the files array
files[length] = strdup(entry->d_name);
if (files[length] == NULL) {
for (size_t i = 0; i < length; i++)
free(files[i]);
free(files);
closedir(dir);
return failure("Failed to allocate memory for the file name.");
}
2024-02-19 16:01:53 +01:00
length++;
// Reallocate memory for the files array if necessary
if (length > capacity) {
capacity *= 2;
errno = 0;
char** new_files = realloc(files, capacity * sizeof(char*));
if (new_files == NULL) {
for (size_t i = 0; i < length; i++)
free(files[i]);
free(files);
closedir(dir);
return failure("Failed to reallocate memory for the files array (%s).", strerror(errno));
}
files = new_files;
}
}
2024-02-19 16:01:53 +01:00
// Close the directory
closedir(dir);
// Null-terminate the files array
files[length] = NULL;
2024-02-19 16:01:53 +01:00
// Return the files array
*_files = files;
return success();
}