1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-12-24 04:39:51 +05:30
gists/c-programming/reverse-ramdisk.c

304 lines
6.0 KiB
C
Raw Normal View History

2023-12-27 20:05:59 +05:30
/*
C programming idea: Handling temporary files like memory allocations (allocating -> creating empty file, using -> locking for R/W, freeing -> deleting).
2023-12-30 18:31:21 +05:30
20% AI, 80% human (the code is tested and reviewed)
2023-12-27 20:05:59 +05:30
Warning: The current result is quick and dirty. Not for educational or
production purposes.
Warning: The functions are not thread-safe.
GCC/Clang/TCC: Compile with -DTEST to set macro TEST as defined, with -DDEBUG
to enable debug mode
To-Do: Add thread-safe versions of functions (use postfix `_r`)
2023-12-27 20:05:59 +05:30
*/
#include <errno.h>
#include <inttypes.h>
2023-12-27 23:55:39 +05:30
#include <math.h>
2023-12-27 20:05:59 +05:30
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2023-12-28 02:32:52 +05:30
#if _POSIX_VERSION >= 200112L
2023-12-28 02:32:52 +05:30
# include <fcntl.h>
# include <unistd.h>
# define IS_POSIX 1
#else
# define IS_POSIX 0
#endif
2023-12-27 20:05:59 +05:30
#ifdef DEBUG
# define line_fail(x) printf("failed on line %d\n", __LINE__ + x)
#else
# define line_fail(x)
#endif
#define retreat(s) do { \
perror(s); \
exit(EXIT_FAILURE); \
} while (0)
#if IS_POSIX
typedef struct {
int ID;
char* file_path;
int file;
} TempFile;
#else
2023-12-27 20:05:59 +05:30
typedef struct {
int ID;
char* file_path;
FILE* file;
} TempFile;
#endif
2023-12-27 20:05:59 +05:30
TempFile* temp_files = NULL;
size_t num_temp_files = 0;
int tf_alloc(size_t n, size_t type_size);
int tf_free(int ID);
int tf_write(int ID, size_t offset, void* src, size_t data_size);
int tf_read(int ID, size_t offset, void* dest, size_t data_size);
2023-12-27 20:05:59 +05:30
int tf_alloc(size_t n, size_t type_size) {
// Create an empty file
size_t len_digit;
if (num_temp_files == 0)
len_digit = 1;
else
len_digit = (size_t) ceil(log((double) num_temp_files + 1.) / log(10.));
size_t file_path_len = len_digit + strlen("tf_.tmp");
char* file_path = malloc((file_path_len + 1) * sizeof(char));
if (file_path == NULL) {
line_fail(-2);
return -1;
}
int res = snprintf(file_path, file_path_len + 1, "tf_%" PRIuMAX ".tmp",
(uintmax_t) num_temp_files);
if ((size_t) res != file_path_len) {
line_fail(-2);
return -1;
}
#if IS_POSIX
int file = open(file_path, O_RDWR | O_CREAT);
if (file == -1) {
#else
2023-12-27 20:05:59 +05:30
FILE* file = fopen(file_path, "w+b");
if (file == NULL) {
#endif
2023-12-27 20:05:59 +05:30
line_fail(-2);
return -1;
}
// Allocate memory for the TempFile struct
TempFile* temp_file = malloc(sizeof(TempFile));
2023-12-27 20:05:59 +05:30
if (temp_file == NULL) {
line_fail(-2);
return -1;
}
// Assign the ID, file path, file handler
2023-12-27 20:05:59 +05:30
temp_file->ID = num_temp_files;
temp_file->file_path = strdup(file_path);
temp_file->file = file;
// Allocate/reallocate memory for the temp_files structure
if (temp_files == NULL)
temp_files = malloc(sizeof(TempFile));
else
temp_files = realloc(temp_files,
(size_t) num_temp_files * sizeof(TempFile));
2023-12-27 20:05:59 +05:30
if (temp_files == NULL) {
line_fail(-2);
return -1;
}
// Add the temp file to the array
temp_files[num_temp_files++] = *temp_file;
2023-12-27 20:05:59 +05:30
return temp_file->ID;
}
int tf_free(int ID) {
size_t index = (size_t) ID;
2023-12-27 20:05:59 +05:30
#if IS_POSIX
close(temp_files[index].file);
#else
2023-12-27 20:05:59 +05:30
fclose(temp_files[index].file);
#endif
2023-12-27 20:05:59 +05:30
// Delete the file
if (remove(temp_files[index].file_path) != 0) {
line_fail(-1);
2023-12-27 20:05:59 +05:30
return -1;
}
free(temp_files[index].file_path);
// Shift the remaining temp files in the array
2023-12-28 02:39:16 +05:30
for (int i = index; i < num_temp_files - 1; i++)
temp_files[(size_t) i] = temp_files[(size_t) i + 1];
2023-12-27 20:05:59 +05:30
num_temp_files--;
// Reallocate memory for the temp_files array
temp_files = realloc(temp_files, num_temp_files * sizeof(TempFile));
2023-12-27 20:05:59 +05:30
if (temp_files == NULL && num_temp_files > 0) {
line_fail(-2);
2023-12-27 20:05:59 +05:30
return -1;
}
return 0;
}
int tf_write(int ID, size_t offset, void* src, size_t data_size) {
size_t index = (size_t) ID;
2023-12-27 20:05:59 +05:30
#if IS_POSIX
// Check file handler for -1
int file = temp_files[index].file;
if (file == -1)
#else
// Check file handler for NULL
2023-12-27 20:05:59 +05:30
FILE* file = temp_files[index].file;
if (file == NULL)
#endif
2023-12-27 20:05:59 +05:30
return -1;
// Set the position
#if IS_POSIX
if (lseek(file, offset, SEEK_SET) == -1) {
#else
if (fseek(file, offset, SEEK_SET) == -1) {
#endif
line_fail(-1);
return -1;
}
2023-12-27 20:05:59 +05:30
// Write the data to the file
#if IS_POSIX
ssize_t bytes_written = write(file, src, data_size);
#else
size_t bytes_written = fwrite(src, 1, data_size, file);
#endif
2023-12-27 20:05:59 +05:30
if (
#if IS_POSIX
(size_t)
#endif
bytes_written != data_size) {
errno = EIO;
2023-12-27 20:05:59 +05:30
return -1;
}
2023-12-27 20:05:59 +05:30
#if IS_POSIX
if (fsync(file) == -1) {
line_fail(-1);
return -1;
}
#else
fflush(file);
#endif
2023-12-27 20:05:59 +05:30
return 0;
}
int tf_read(int ID, size_t offset, void* dest, size_t data_size) {
size_t index = (size_t) ID;
2023-12-27 20:05:59 +05:30
#if IS_POSIX
int file = temp_files[index].file;
if (file == -1)
#else
2023-12-27 20:05:59 +05:30
FILE* file = temp_files[index].file;
if (file == NULL)
#endif
2023-12-27 20:05:59 +05:30
return -1;
// Read the data from the file
void* src = malloc(data_size);
if (src == NULL) {
#if IS_POSIX
close(file);
#else
2023-12-27 20:05:59 +05:30
fclose(file);
#endif
line_fail(-7);
2023-12-27 20:05:59 +05:30
return -1;
}
memset(src, 0, data_size); // clear destination
// Set the position
#if IS_POSIX
if (lseek(file, offset, SEEK_SET) == -1) {
#else
if (fseek(file, offset, SEEK_SET) == -1) {
#endif
line_fail(-1);
return -1;
}
// read bytes
#if IS_POSIX
ssize_t bytes_read = read(file, src, data_size);
#else
size_t bytes_read = fread(src, 1, data_size, file);
#endif
memcpy(dest, src, data_size);
2023-12-27 20:05:59 +05:30
free(src); // Free the allocated memory
2023-12-27 20:05:59 +05:30
if (
#if IS_POSIX
(size_t)
#endif
bytes_read != data_size) {
2023-12-27 20:05:59 +05:30
errno = EIO;
return -1;
}
#ifdef DEBUG
printf("Read: ID = %d, src = %016" PRIXPTR ", size = %zu -> '",
2023-12-27 20:05:59 +05:30
ID, (uintptr_t)dest, data_size);
for (size_t i = 0; i < data_size; i++)
printf("0x%02" PRIX8 " ", *((uint8_t*)((uint8_t*)dest + i)));
2023-12-27 20:05:59 +05:30
printf("'\n");
fflush(stdout);
#endif
2023-12-27 20:05:59 +05:30
return 0;
}
#ifdef TEST
int main(void) {
2023-12-27 20:05:59 +05:30
int ID = tf_alloc(4, sizeof(int));
if (ID == -1)
retreat("tf_alloc");
int test_data[4] = {123, 456, 789, -123};
for (size_t i = 0; i < 4; i++)
if (tf_write(ID, i * sizeof(int), &test_data[i], sizeof(int)) == -1)
retreat("tf_write");
// round-trip
test_data[0] = 111;
test_data[1] = 222;
test_data[2] = 333;
test_data[3] = 444;
for (size_t i = 0; i < 4; i++)
if (tf_read(ID, i * sizeof(int), &test_data[i], sizeof(int)) == -1)
retreat("tf_read");
printf("Values: %d %d %d %d\n",
test_data[0], test_data[1], test_data[2], test_data[3]);
tf_free(ID);
return 0;
}
#endif