Skip to content

Commit

Permalink
Merge pull request cathugger#61 from scribblemaniac/checkpointing
Browse files Browse the repository at this point in the history
Add checkpointing support for passphrases
  • Loading branch information
cathugger authored Dec 10, 2021
2 parents 73d2791 + 67868f4 commit 3648c1f
Show file tree
Hide file tree
Showing 3 changed files with 291 additions and 6 deletions.
178 changes: 176 additions & 2 deletions ioutil.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "types.h"
#include "ioutil.h"
#include "vec.h"
#include <stdio.h>

#ifndef _WIN32

Expand Down Expand Up @@ -31,7 +34,7 @@ FH createfile(const char *path,int secret)
int fd;
do {
fd = open(path,O_WRONLY | O_CREAT | O_TRUNC,secret ? 0600 : 0666);
if (fd == -1) {
if (fd < 0) {
if (errno == EINTR)
continue;
return -1;
Expand All @@ -45,7 +48,7 @@ int closefile(FH fd)
int cret;
do {
cret = close(fd);
if (cret == -1) {
if (cret < 0) {
if (errno == EINTR)
continue;
return -1;
Expand All @@ -59,6 +62,122 @@ int createdir(const char *path,int secret)
return mkdir(path,secret ? 0700 : 0777);
}

static int syncwritefile(const char *filename,const char *tmpname,int secret,const u8 *data,size_t datalen)
{
FH f = createfile(tmpname,secret);
if (f == FH_invalid)
return -1;

if (writeall(f,data,datalen) < 0) {
goto failclose;
}

int sret;
do {
sret = fsync(f);
if (sret < 0) {
if (errno == EINTR)
continue;

goto failclose;
}
} while (0);

if (closefile(f) < 0) {
goto failrm;
}

if (rename(tmpname,filename) < 0) {
goto failrm;
}

return 0;

failclose:
(void) closefile(f);
failrm:
remove(tmpname);

return -1;
}

int syncwrite(const char *filename,int secret,const u8 *data,size_t datalen)
{
//fprintf(stderr,"filename = %s\n",filename);

size_t fnlen = strlen(filename);

VEC_STRUCT(,char) tmpnamebuf;
VEC_INIT(tmpnamebuf);
VEC_ADDN(tmpnamebuf,fnlen + 4 /* ".tmp" */ + 1 /* "\0" */);
memcpy(&VEC_BUF(tmpnamebuf,0),filename,fnlen);
strcpy(&VEC_BUF(tmpnamebuf,fnlen),".tmp");
const char *tmpname = &VEC_BUF(tmpnamebuf,0);

//fprintf(stderr,"tmpname = %s\n",tmpname);

int r = syncwritefile(filename,tmpname,secret,data,datalen);

VEC_FREE(tmpnamebuf);

if (r < 0)
return r;

VEC_STRUCT(,char) dirnamebuf;
VEC_INIT(dirnamebuf);
const char *dirname;

for (ssize_t x = ((ssize_t)fnlen) - 1;x >= 0;--x) {
if (filename[x] == '/') {
if (x)
--x;
++x;
VEC_ADDN(dirnamebuf,x + 1);
memcpy(&VEC_BUF(dirnamebuf,0),filename,x);
VEC_BUF(dirnamebuf,x) = '\0';
dirname = &VEC_BUF(dirnamebuf,0);
goto foundslash;
}
}
/* not found slash, fall back to "." */
dirname = ".";

foundslash:
//fprintf(stderr,"dirname = %s\n",dirname);
;

int dirf;
do {
dirf = open(dirname,O_RDONLY);
if (dirf < 0) {
if (errno == EINTR)
continue;

// failed for non-eintr reasons
goto skipdsync; // don't really care enough
}
} while (0);

int sret;
do {
sret = fsync(dirf);
if (sret < 0) {
if (errno == EINTR)
continue;

// failed for non-eintr reasons
break; // don't care
}
} while (0);

(void) closefile(dirf); // don't care

skipdsync:
VEC_FREE(dirnamebuf);

return 0;
}

#else

int writeall(FH fd,const u8 *data,size_t len)
Expand Down Expand Up @@ -99,6 +218,61 @@ int createdir(const char *path,int secret)
return CreateDirectoryA(path,0) ? 0 : -1;
}

static int syncwritefile(const char *filename,const char *tmpname,int secret,const char *data,size_t datalen)
{
FH f = createfile(tmpnamestr,secret)
if (f == FH_invalid)
return -1;

if (writeall(f,data,datalen) < 0) {
goto failclose;
}

if (FlushFileBuffers(f) == 0) {
goto failclose;
}

if (closefile(f) < 0) {
goto failrm;
}

if (MoveFileA(tmpnamestr,filename) == 0) {
goto failrm;
}

return 0;

failclose:
(void) closefile(f);
failrm:
remove(tmpnamestr);

return -1;
}

int syncwrite(const char *filename,int secret,const char *data,size_t datalen)
{
size_t fnlen = strlen(filename);

VEC_STRUCT(,char) tmpnamebuf;
VEC_INIT(tmpnamebuf);
VEC_ADDN(tmpnamebuf,fnlen + 4 /* ".tmp" */ + 1 /* "\0" */);
memcpy(&VEC_BUF(tmpnamebuf,0),filename,fnlen);
strcpy(&VEC_BUF(tmpnamebuf,fnlen),".tmp");
const char *tmpname = &VEC_BUF(tmpnamebuf,0);

int r = syncwritefile(filename,tmpname,secret,data,datalen);

VEC_FREE(tmpnamebuf);

if (r < 0)
return r;

// can't fsync parent dir on windows so just end here

return 0;
}

#endif

int writetofile(const char *path,const u8 *data,size_t len,int secret)
Expand Down
1 change: 1 addition & 0 deletions ioutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ int closefile(FH fd);
int writeall(FH,const u8 *data,size_t len);
int writetofile(const char *path,const u8 *data,size_t len,int secret);
int createdir(const char *path,int secret);
int syncwrite(const char *filename,int secret,const u8 *data,size_t datalen);
Loading

0 comments on commit 3648c1f

Please sign in to comment.