io: directory: implement temporary directories and delete-on-close

This commit is contained in:
2025-08-09 19:40:11 +01:00
parent 655d8b1881
commit 8bdb770ae5
5 changed files with 127 additions and 22 deletions

View File

@@ -1,3 +1,4 @@
#include "misc.h"
#include "posix.h"
#include <blue/core/random.h>
@@ -118,24 +119,6 @@ b_result b_file_open(
return B_RESULT_SUCCESS;
}
static void generate_tmp_filename(char *out, size_t len)
{
static const char *alphabet
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
"01234567"
"89+=-_.";
static const size_t alphabet_len = 67;
b_random_ctx *ctx = b_random_global_ctx();
for (size_t i = 0; i < len; i++) {
int v = b_random_next_int64(ctx) % alphabet_len;
out[i] = alphabet[v];
}
out[len - 1] = 0;
}
b_result b_file_open_temp(enum b_file_mode mode, struct b_file **out)
{
mode |= B_FILE_DELETE_ON_CLOSE;
@@ -144,7 +127,7 @@ b_result b_file_open_temp(enum b_file_mode mode, struct b_file **out)
char path[128];
while (1) {
generate_tmp_filename(name, sizeof name);
z__b_io_generate_tmp_filename(name, sizeof name);
snprintf(path, sizeof path, "/tmp/%s", name);
struct b_path *rpath = b_path_create_from_cstr(path);
@@ -423,7 +406,7 @@ enum b_status b_file_swap_shadow(struct b_file *main_file, struct b_file *shadow
while (1) {
char tmp_name[16];
generate_tmp_filename(tmp_name, sizeof tmp_name);
z__b_io_generate_tmp_filename(tmp_name, sizeof tmp_name);
struct b_path *tmp_name_p = b_path_create_from_cstr(tmp_name);
const struct b_path *parts[] = {