linuxinstall/src/utils.h

136 lines
7.5 KiB
C
Raw Normal View History

2024-02-15 19:42:22 +01:00
#pragma once
2024-02-19 16:01:53 +01:00
#include <stdlib.h>
#include <stdint.h>
2024-02-17 23:59:38 +01:00
#include <stdbool.h>
2024-02-15 19:42:22 +01:00
2024-02-19 16:01:53 +01:00
#define ERROR_BUFFER_SIZE 4096
/// @brief The global error buffer. It can be accessed using the error() function.
2024-02-19 19:44:36 +01:00
extern char _ERROR_BUFFER[];
2024-02-15 19:42:22 +01:00
/// @brief The level of importance of a log message.
typedef enum {
/// @brief A debug message. These messages are used for debugging purposes, and are not meant to be seen by the user.
LOG_LEVEL_DEBUG,
/// @brief An info message. These messages are used to inform the user about the progress of the program.
LOG_LEVEL_INFO,
/// @brief A warning message. These messages are used to warn the user about potential issues.
LOG_LEVEL_WARNING,
/// @brief An error message. These messages are used to inform the user that a critical error has occurred.
LOG_LEVEL_ERROR,
} log_level_t;
/// @brief The minimum log level of the program.
extern log_level_t _LOG_LEVEL;
/// @brief The result of an operation.
2024-02-19 16:01:53 +01:00
typedef int result_t;
2024-02-15 19:42:22 +01:00
/// @brief Sets the log level of the program.
/// @param level The log level to set.
void set_log_level(log_level_t level);
/// @brief Logs a message with the given level, and the given format string and arguments.
/// @param level The level of the message.
/// @param format The format string to use for the message.
/// @param ... The arguments to use for the format string.
void log_message(log_level_t level, const char* format, ...);
2024-02-19 19:44:36 +01:00
/// @brief Returns a generic success result.
/// @return The success result.
2024-02-19 16:01:53 +01:00
result_t success(void);
2024-02-19 19:44:36 +01:00
/// @brief Returns a generic failure result, with an error message formatted using the given format string and arguments. The error message is stored in a global buffer, and can be retrieved using the error() function.
/// @param format The format string to use for the error message.
/// @param ... The arguments to use for the format string.
/// @return The failure result.
2024-02-19 16:01:53 +01:00
result_t failure(const char* format, ...);
2024-02-19 19:44:36 +01:00
/// @brief Returns the error message from the last failure result.
/// @return The error message from the last failure result.
2024-02-19 16:01:53 +01:00
const char* error(void);
2024-02-19 19:44:36 +01:00
/// @brief Formats a string using the given format string and arguments, and stores the result in the given string pointer.
/// @param _str The string pointer to store the resulting string in. The caller is responsible for freeing the string.
2024-02-19 19:44:36 +01:00
/// @param format The format string to use for the string.
/// @param ... The arguments to use for the format string.
/// @return The result of the operation.
2024-02-19 16:01:53 +01:00
result_t format(char** _str, const char* format, ...);
2024-02-19 19:44:36 +01:00
/// @brief Extracts a substring from the given string, starting at the given index and with the given length, and stores the resulting string in the given string pointer.
/// @param _str The string pointer to store the resulting string in. The caller is responsible for freeing the string.
2024-02-19 19:44:36 +01:00
/// @param str The string to extract the substring from.
/// @param start The index to start the substring at.
/// @param length The length of the substring. If the length goes beyond the end of the string, the substring will be truncated to the end of the string.
/// @return The result of the operation.
2024-02-19 16:01:53 +01:00
result_t substring(char** _str, const char* str, size_t start, size_t length);
2024-02-17 14:21:14 +01:00
/// @brief Formats a size in bytes to a human-readable string, and stores the resulting string in the given string pointer.
/// @param _str The string pointer to store the resulting string in. The caller is responsible for freeing the string.
2024-02-19 19:44:36 +01:00
/// @param size The size in bytes to format.
/// @return The result of the operation.
2024-02-19 16:01:53 +01:00
result_t format_size(char** _str, uint64_t size);
2024-02-19 19:44:36 +01:00
/// @brief Parses a size from the given string, and stores the resulting integer in the given size pointer.
/// @param _size The size pointer to store the resulting integer in.
2024-02-19 19:44:36 +01:00
/// @param str The string to parse the size from.
/// @return The result of the operation.
2024-02-19 16:01:53 +01:00
result_t parse_size(uint64_t* _size, const char* str);
2024-02-18 14:05:04 +01:00
/// @brief Executes the given command, and stores the resulting exit code in the given exit code pointer, and the standard output and standard error in the given string pointers.
/// @param _exit_code The exit code pointer to store the resulting exit code in.
/// @param _stdoutbuf The standard output string pointer to store the resulting string in. The caller is responsible for freeing the string. This parameter can be NULL if the standard output is not needed.
/// @param _stderrbuf The standard error string pointer to store the resulting string in. The caller is responsible for freeing the string. This parameter can be NULL if the standard error is not needed.
/// @param working_directory The working directory to execute the command in. If this parameter is NULL, the command will be executed in the current working directory.
2024-02-19 19:44:36 +01:00
/// @param executable The command to execute.
/// @param ... The arguments to pass to the command. The last argument must be NULL. No need to pass the executable as the first argument.
/// @return The result of the operation.
result_t execute(int* _exit_code, char** _stdoutbuf, char** _stderrbuf, const char* working_directory, const char* executable, ...);
2024-02-17 14:38:52 +01:00
// @brief Reads the given file descriptor, and stores the resulting string in the given string pointer. The string can contain null characters.
// @param _str The string pointer to store the resulting string in. The caller is responsible for freeing the string.
2024-02-19 19:44:36 +01:00
// @param fd The file descriptor to read from.
// @return The result of the operation.
2024-02-19 16:01:53 +01:00
result_t read_fd(char** _str, int fd);
2024-02-19 19:44:36 +01:00
// @brief Writes the given string to the given file descriptor.
// @param fd The file descriptor to write to.
// @param str The string to write.
// @return The result of the operation.
2024-02-19 16:01:53 +01:00
result_t write_fd(int fd, const char* str);
2024-02-19 19:44:36 +01:00
/// @brief Reads the given file, and stores the resulting string in the given string pointer. The string can contain null characters.
/// @param _str The string pointer to store the resulting string in. The caller is responsible for freeing the string.
2024-02-19 19:44:36 +01:00
/// @param path The path to the file to read.
/// @return The result of the operation.
2024-02-19 16:01:53 +01:00
result_t read_file(char** _str, const char* path);
2024-02-19 19:44:36 +01:00
/// @brief Writes the given string to the given file.
/// @param path The path to the file to write to.
/// @param str The string to write.
/// @return The result of the operation.
2024-02-19 16:01:53 +01:00
result_t write_file(const char* path, const char* str);
2024-02-19 19:44:36 +01:00
/// @brief Copies the given file to the given destination.
/// @param src The path to the file to copy.
/// @param dst The path to the destination to copy the file to.
/// @return The result of the operation.
2024-02-19 18:18:18 +01:00
result_t copy_file(const char* src, const char* dst);
/// @brief Lists the files in the given directory, and stores the resulting array in the given string array pointer. A filter can be provided to only include files that match the filter.
/// @param _files The string array pointer to store the resulting array in. The caller is responsible for freeing the strings and the array.
2024-02-19 19:44:36 +01:00
/// @param path The path to the directory to list the files in.
/// @param filter The filter to use to only include files that match the filter. This parameter can be NULL if no filter is needed.
/// @return The result of the operation.
2024-02-19 16:01:53 +01:00
result_t list_files(char*** _files, const char* path, bool (*filter)(const char*));
2024-02-19 18:18:18 +01:00
/// @brief Calculates the MD5 checksum of the given file, and stores the resulting hash in the given string pointer.
/// @param _md5 The string pointer to store the resulting hash in. The caller is responsible for freeing the string.
2024-02-19 19:44:36 +01:00
/// @param path The path to the file to calculate the MD5 checksum of.
/// @return The result of the operation.
2024-02-19 18:18:18 +01:00
result_t md5sum(char** _md5, const char* path);