This repository has been archived by the owner on Mar 2, 2023. It is now read-only.
forked from cathugger/mkp224o
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioutil.c
111 lines (98 loc) · 1.85 KB
/
ioutil.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdint.h>
#include <string.h>
#include "types.h"
#include "ioutil.h"
#ifndef _WIN32
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int writeall(FH fd,const u8 *data,size_t len)
{
ssize_t wrote;
while (len) {
wrote = write(fd,data,len);
if (wrote == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
continue;
return -1;
}
len -= (size_t) wrote;
data += wrote;
}
return 0;
}
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 (errno == EINTR)
continue;
return -1;
}
} while (0);
return fd;
}
int closefile(FH fd)
{
int cret;
do {
cret = close(fd);
if (cret == -1) {
if (errno == EINTR)
continue;
return -1;
}
} while (0);
return 0;
}
int createdir(const char *path,int secret)
{
return mkdir(path,secret ? 0700 : 0777);
}
#else
int writeall(FH fd,const u8 *data,size_t len)
{
DWORD wrote;
BOOL success;
while (len) {
success = WriteFile(fd,data,
len <= (DWORD)-1 ? (DWORD)len : (DWORD)-1,&wrote,0);
if (!success)
return -1;
data += wrote;
if (len >= wrote)
len -= wrote;
else
len = 0;
}
return 0;
}
FH createfile(const char *path,int secret)
{
// XXX no support for non-ascii chars
// XXX don't know how to handle secret argument
(void) secret;
return CreateFileA(path,GENERIC_WRITE,0,0,CREATE_ALWAYS,0,0);
}
int closefile(FH fd)
{
return CloseHandle(fd) ? 0 : -1;
}
int createdir(const char *path,int secret)
{
// XXX don't know how to handle secret argument
return CreateDirectoryA(path,0) ? 0 : -1;
}
#endif
int writetofile(const char *path,const u8 *data,size_t len,int secret)
{
FH fd = createfile(path,secret);
int wret = writeall(fd,data,len);
int cret = closefile(fd);
if (cret == -1)
return -1;
return wret;
}