-
Notifications
You must be signed in to change notification settings - Fork 1
/
File.c
234 lines (200 loc) · 4.92 KB
/
File.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <glob.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "File.h"
#include "localptr.h"
ArrayU8 * File_load_data(String * filename)
{
long len;
long n;
int procflag = 0;
FILE *f = fopen(s(filename), "rb");
if (!f) {
// perror(s(filename));
return 0L;
}
if (strncmp("/proc/", s(filename), strlen("/proc/")) == 0) {
len = 32768;
procflag = 1;
}
else {
fseek(f, 0, SEEK_END);
len = ftell(f);
fseek(f, 0, SEEK_SET);
}
ArrayU8 *a = ArrayU8_new();
ArrayU8_extend(a, len);
n = fread(a->data, 1, len, f);
if (n < len && !procflag) {
fprintf(stderr, "warning: only read %ld of %ld expected bytes from %s\n", n, len, s(filename));
}
fclose(f);
return a;
}
String * File_load_text(String * filename)
{
ArrayU8 *a = File_load_data(filename);
if (a) {
/* Add 1 extra byte so can add null-termination byte when converting to string. */
ArrayU8_extend(a, a->len+1);
a->data[a->len-1] = 0;
return ArrayU8_to_string(&a);
}
else {
return String_value_none();
}
}
static char base64map[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
String * File_load_base64(String * filename)
{
int i;
ArrayU8 *a = File_load_data(filename);
if (!a) {
return String_value_none();
}
String * result = String_new("");
int digits[4] = {};
char values[4];
for (i=0; i < a->len; i++) {
switch (i%3) {
case 0: digits[0] = a->data[i] >> 2; digits[1] = (a->data[i] & 0x3) << 4;
values[0] = base64map[digits[0]];
break;
case 1: digits[1] |= a->data[i] >> 4; digits[2] = (a->data[i] & 0xf) << 2;
values[1] = base64map[digits[1]];
break;
case 2: digits[2] |= a->data[i] >> 6; digits[3] = (a->data[i] & 0x3f);
values[2] = base64map[digits[2]];
values[3] = base64map[digits[3]];
String_append_bytes(result, values, 4);
break;
}
}
switch(i%3) {
case 0: break;
case 1:
values[1] = base64map[digits[1]];
String_append_bytes(result, values, 2);
String_cat1(result, "==");
break;
case 2:
values[2] = base64map[digits[2]];
String_append_bytes(result, values, 3);
String_cat1(result, "=");
break;
}
return result;
}
String_list *Files_glob(String *path, String *pattern)
{
glob_t g;
int i;
String_list *slst = String_list_new();
//fprintf(stderr, "%s\n", s(path));
String *full_pattern = String_sprintf("%s/%s", s(path), s(pattern));
int rc = glob(s(full_pattern), 0,
NULL,
&g);
if (rc == 0) {
for (i=0; i < g.gl_pathc; i++) {
String *tmp = String_new(g.gl_pathv[i]);
String_list_add(slst, &tmp);
}
globfree(&g);
}
return slst;
}
int File_exists(String *path)
{
return access(s(path), R_OK) == 0 ? 1 : 0;
}
int File_load_int(String * path, int * result)
{
int rc = 0;
String * contents = File_load_text(path);
if (String_is_none(contents)) {
fprintf(stderr, "Could not read number from %s\n", s(path));
rc = -1;
}
else {
String_trim_right(contents);
if (sscanf(s(contents), "%d", result) != 1) {
fprintf(stderr, "%s does not seem to contain an integer\n", s(path));
rc = -1;
}
}
String_clear(&contents);
return rc;
}
int File_make_dir(String * path)
{
struct stat st;
int rc;
rc = stat(s(path), &st);
if (rc == 0) {
if (S_ISDIR(st.st_mode)) {
/* Already exists and is a directory. My decision is that for
CTI semantics, this is not cause for warning or error. */
return 0;
}
else {
fprintf(stderr, "%s exists but is not a directory!\n", s(path));
return -1;
}
}
return mkdir(s(path), 0777);
}
int File_set_owner_perms(String * path, int uid, int gid, int mode)
{
return 0;
}
int File_copy(String * oldpath, String * newpath)
{
/* I'm not totally happy with this. I might be able to expose errors
better if I used lower level read/write. Also, it might be nice
if it made use of SourceSink.c instead of redudantly doing similar
things here. */
int ret = 0;
FILE * fin = fopen(s(oldpath), "rb");
if (!fin) {
perror(s(oldpath));
return 1;
}
FILE * fout = fopen(s(newpath), "wb");
if (!fout) {
perror(s(newpath));
fclose(fin);
return 1;
}
while (1) {
uint8_t block[32000];
int rn = fread(block, 1, sizeof(block), fin);
// fprintf(stderr, "read %d\n", rn);
if (rn == 0) {
break;
}
int wn = fwrite(block, 1, rn, fout);
// fprintf(stderr, "wrote %d\n", wn);
if (wn != rn) {
ret = 1;
break;
}
}
/* Since stdio buffers, errors like ENOSPC (disk full) won't show up
until a flush is attempted. */
if (fflush(fout) != 0) {
fprintf(stderr, "%s ", s(newpath));
perror("fflush");
ret = 1;
}
if (fclose(fout) != 0) {
fprintf(stderr, "%s ", s(newpath));
perror("fclose");
ret = 1;
}
fclose(fin);
return ret;
}