mirror of
https://github.com/cathugger/mkp224o.git
synced 2025-04-21 22:39:09 +00:00
cleanup, free memory
This commit is contained in:
parent
1679e51e1b
commit
67868f4126
1 changed files with 85 additions and 54 deletions
139
ioutil.c
139
ioutil.c
|
@ -1,5 +1,6 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "ioutil.h"
|
#include "ioutil.h"
|
||||||
#include "vec.h"
|
#include "vec.h"
|
||||||
|
@ -61,28 +62,14 @@ int createdir(const char *path,int secret)
|
||||||
return mkdir(path,secret ? 0700 : 0777);
|
return mkdir(path,secret ? 0700 : 0777);
|
||||||
}
|
}
|
||||||
|
|
||||||
int syncwrite(const char *filename,int secret,const u8 *data,size_t datalen)
|
static int syncwritefile(const char *filename,const char *tmpname,int secret,const u8 *data,size_t datalen)
|
||||||
{
|
{
|
||||||
//fprintf(stderr,"filename = %s\n",filename);
|
FH f = createfile(tmpname,secret);
|
||||||
|
|
||||||
VEC_STRUCT(,char) tmpname;
|
|
||||||
size_t fnlen = strlen(filename);
|
|
||||||
VEC_INIT(tmpname);
|
|
||||||
VEC_ADDN(tmpname,fnlen + 4 /* ".tmp" */ + 1 /* "\0" */);
|
|
||||||
memcpy(&VEC_BUF(tmpname,0),filename,fnlen);
|
|
||||||
strcpy(&VEC_BUF(tmpname,fnlen),".tmp");
|
|
||||||
const char *tmpnamestr = &VEC_BUF(tmpname,0);
|
|
||||||
|
|
||||||
//fprintf(stderr,"tmpnamestr = %s\n",tmpnamestr);
|
|
||||||
|
|
||||||
FH f = createfile(tmpnamestr,secret);
|
|
||||||
if (f == FH_invalid)
|
if (f == FH_invalid)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (writeall(f,data,datalen) < 0) {
|
if (writeall(f,data,datalen) < 0) {
|
||||||
closefile(f);
|
goto failclose;
|
||||||
remove(tmpnamestr);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int sret;
|
int sret;
|
||||||
|
@ -92,48 +79,76 @@ int syncwrite(const char *filename,int secret,const u8 *data,size_t datalen)
|
||||||
if (errno == EINTR)
|
if (errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
closefile(f);
|
goto failclose;
|
||||||
remove(tmpnamestr);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
} while (0);
|
} while (0);
|
||||||
|
|
||||||
if (closefile(f) < 0) {
|
if (closefile(f) < 0) {
|
||||||
remove(tmpnamestr);
|
goto failrm;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rename(tmpnamestr,filename) < 0) {
|
if (rename(tmpname,filename) < 0) {
|
||||||
remove(tmpnamestr);
|
goto failrm;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
VEC_STRUCT(,char) dirname;
|
return 0;
|
||||||
const char *dirnamestr;
|
|
||||||
|
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) {
|
for (ssize_t x = ((ssize_t)fnlen) - 1;x >= 0;--x) {
|
||||||
if (filename[x] == '/') {
|
if (filename[x] == '/') {
|
||||||
if (x)
|
if (x)
|
||||||
--x;
|
--x;
|
||||||
++x;
|
++x;
|
||||||
VEC_INIT(dirname);
|
VEC_ADDN(dirnamebuf,x + 1);
|
||||||
VEC_ADDN(dirname,x + 1);
|
memcpy(&VEC_BUF(dirnamebuf,0),filename,x);
|
||||||
memcpy(&VEC_BUF(dirname,0),filename,x);
|
VEC_BUF(dirnamebuf,x) = '\0';
|
||||||
VEC_BUF(dirname,x) = '\0';
|
dirname = &VEC_BUF(dirnamebuf,0);
|
||||||
dirnamestr = &VEC_BUF(dirname,0);
|
|
||||||
goto foundslash;
|
goto foundslash;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* not found slash, fall back to "." */
|
/* not found slash, fall back to "." */
|
||||||
dirnamestr = ".";
|
dirname = ".";
|
||||||
|
|
||||||
foundslash:
|
foundslash:
|
||||||
//fprintf(stderr,"dirnamestr = %s\n",dirnamestr);
|
//fprintf(stderr,"dirname = %s\n",dirname);
|
||||||
;
|
;
|
||||||
|
|
||||||
int dirf;
|
int dirf;
|
||||||
do {
|
do {
|
||||||
dirf = open(dirnamestr,O_RDONLY);
|
dirf = open(dirname,O_RDONLY);
|
||||||
if (dirf < 0) {
|
if (dirf < 0) {
|
||||||
if (errno == EINTR)
|
if (errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
|
@ -143,6 +158,7 @@ foundslash:
|
||||||
}
|
}
|
||||||
} while (0);
|
} while (0);
|
||||||
|
|
||||||
|
int sret;
|
||||||
do {
|
do {
|
||||||
sret = fsync(dirf);
|
sret = fsync(dirf);
|
||||||
if (sret < 0) {
|
if (sret < 0) {
|
||||||
|
@ -157,6 +173,7 @@ foundslash:
|
||||||
(void) closefile(dirf); // don't care
|
(void) closefile(dirf); // don't care
|
||||||
|
|
||||||
skipdsync:
|
skipdsync:
|
||||||
|
VEC_FREE(dirnamebuf);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -201,42 +218,56 @@ int createdir(const char *path,int secret)
|
||||||
return CreateDirectoryA(path,0) ? 0 : -1;
|
return CreateDirectoryA(path,0) ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int syncwrite(const char *filename,int secret,const char *data,size_t datalen)
|
static int syncwritefile(const char *filename,const char *tmpname,int secret,const char *data,size_t datalen)
|
||||||
{
|
{
|
||||||
VEC_STRUCT(,char) tmpname;
|
|
||||||
size_t fnlen = strlen(filename);
|
|
||||||
VEC_INIT(tmpname);
|
|
||||||
VEC_ADDN(tmpname,fnlen + 4 /* ".tmp" */ + 1 /* "\0" */);
|
|
||||||
memcpy(&VEC_BUF(tmpname,0),filename,fnlen);
|
|
||||||
strcpy(&VEC_BUF(tmpname,fnlen),".tmp");
|
|
||||||
const char *tmpnamestr = &VEC_BUF(tmpname,0);
|
|
||||||
|
|
||||||
FH f = createfile(tmpnamestr,secret)
|
FH f = createfile(tmpnamestr,secret)
|
||||||
if (f == FH_invalid)
|
if (f == FH_invalid)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (writeall(f,data,datalen) < 0) {
|
if (writeall(f,data,datalen) < 0) {
|
||||||
closefile(f);
|
goto failclose;
|
||||||
remove(tmpnamestr);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (FlushFileBuffers(f) == 0) {
|
if (FlushFileBuffers(f) == 0) {
|
||||||
closefile(f);
|
goto failclose;
|
||||||
remove(tmpnamestr);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (closefile(f) < 0) {
|
if (closefile(f) < 0) {
|
||||||
remove(tmpnamestr);
|
goto failrm;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MoveFileA(tmpnamestr,filename) == 0) {
|
if (MoveFileA(tmpnamestr,filename) == 0) {
|
||||||
remove(tmpnamestr);
|
goto failrm;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
// can't fsync parent dir on windows so just end here
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Reference in a new issue