Skip to content

Commit

Permalink
ConfigFile: Attempt to create path on write
Browse files Browse the repository at this point in the history
  • Loading branch information
Zirias committed Jul 26, 2024
1 parent d42ef28 commit 72ee094
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/bin/xmoji/configfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#define TMPSUFX ".tmp"
Expand Down Expand Up @@ -190,13 +191,33 @@ PSC_Event *ConfigFile_changed(ConfigFile *self)
return self->changed;
}

static int ensurepath(char *current)
{
int rc = 0;
char *sep = strrchr(current, '/');
if (!sep || sep == current) return rc;
*sep = 0;
struct stat st;
if (stat(current, &st) < 0)
{
rc = ensurepath(current);
}
else if (S_ISDIR(st.st_mode)) goto done;
else rc = -1;
if (rc == 0) rc = mkdir(current, 0777);
done:
*sep = '/';
return rc;
}

int ConfigFile_write(ConfigFile *self)
{
int rc = -1;
size_t nmlen = strlen(self->path);
char *tmpnm = PSC_malloc(nmlen + sizeof TMPSUFX);
memcpy(tmpnm, self->path, nmlen);
memcpy(tmpnm+nmlen, TMPSUFX, sizeof TMPSUFX);
if (ensurepath(tmpnm) < 0) goto done;
FILE *f = fopen(tmpnm, "w");
if (!f) goto done;
for (size_t i = 0; i < self->nkeys; ++i)
Expand Down

0 comments on commit 72ee094

Please sign in to comment.