This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
/
files.c
383 lines (330 loc) · 7.96 KB
/
files.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
Tomato Firmware
Copyright (C) 2006-2009 Jonathan Zarate
*/
#define _BSD_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <dirent.h>
#include <bcmnvram.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/file.h>
#include "shutils.h"
#include "shared.h"
int f_exists(const char *path) // note: anything but a directory
{
struct stat st;
return (stat(path, &st) == 0) && (!S_ISDIR(st.st_mode));
}
int d_exists(const char *path) // directory only
{
struct stat st;
return (stat(path, &st) == 0) && (S_ISDIR(st.st_mode));
}
unsigned long f_size(const char *path) // 4GB-1 -1 = error
{
struct stat st;
if (stat(path, &st) == 0) return st.st_size;
return (unsigned long)-1;
}
int f_read_excl(const char *path, void *buffer, int max)
{
int f;
int n;
if ((f = open(path, O_RDONLY)) < 0) return -1;
flock(f, LOCK_EX);
n = read(f, buffer, max);
flock(f, LOCK_UN);
close(f);
return n;
}
int f_read(const char *path, void *buffer, int max)
{
int f;
int n;
if ((f = open(path, O_RDONLY)) < 0) return -1;
n = read(f, buffer, max);
close(f);
return n;
}
int f_write_excl(const char *path, const void *buffer, int len, unsigned flags, unsigned cmode)
{
static const char nl = '\n';
int f, fl;
int r = -1;
mode_t m;
m = umask(0);
if (cmode == 0) cmode = 0666;
if ((fl = open(ACTION_LOCK_FILE, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600)) >= 0) { // own the lock
if (( f = open(path, (flags & FW_APPEND) ? (O_WRONLY|O_CREAT|O_APPEND) : (O_WRONLY|O_CREAT|O_TRUNC), cmode)) >= 0) {
flock(f, LOCK_EX);
if ((buffer == NULL) || ((r = write(f, buffer, len)) == len)) {
if (flags & FW_NEWLINE) {
if (write(f, &nl, 1) == 1) ++r;
}
}
flock(f, LOCK_UN);
close(f);
}
close(fl);
unlink(ACTION_LOCK_FILE);
}
umask(m);
return r;
}
/**
* Write @buffer, length len, to file specified by @path.
* @path:
* @buffer:
* @len:
* @flags: Combination of FW_APPEND, FW_NEWLINE, FW_SILENT, etc.
* @cmode:
* @return:
* >0: writted length
* -1: open file error or -EINVAL
* -errno: errno of write file error
*/
int f_write(const char *path, const void *buffer, int len, unsigned flags, unsigned cmode)
{
static const char nl = '\n';
const void *b;
int f, ret = 0;
size_t wlen;
ssize_t r;
mode_t m;
m = umask(0);
if (cmode == 0) cmode = 0666;
if ((f = open(path, (flags & FW_APPEND) ? (O_WRONLY|O_CREAT|O_APPEND) : (O_WRONLY|O_CREAT|O_TRUNC), cmode)) >= 0) {
if ((buffer == NULL)) {
if (flags & FW_NEWLINE) {
if (write(f, &nl, 1) == 1)
ret = 1;
}
} else {
for (b = buffer, wlen = len; wlen > 0;) {
r = write(f, b, wlen);
if (r < 0) {
ret = -errno;
if (!(flags & FW_SILENT)) {
_dprintf("%s: Write [%s] to [%s] failed! errno %d (%s)\n",
__func__, b, path, errno, strerror(errno));
}
break;
} else {
ret += r;
b += r;
wlen -= r;
}
}
}
close(f);
} else {
ret = -1;
}
umask(m);
return ret;
}
int f_read_string(const char *path, char *buffer, int max)
{
if (max <= 0) return -1;
int n = f_read(path, buffer, max - 1);
buffer[(n > 0) ? n : 0] = 0;
return n;
}
int f_write_string(const char *path, const char *buffer, unsigned flags, unsigned cmode)
{
return f_write(path, buffer, strlen(buffer), flags, cmode);
}
static int _f_read_alloc(const char *path, char **buffer, int max, int z)
{
unsigned long n;
*buffer = NULL;
if (max >= 0) {
if ((n = f_size(path)) != (unsigned long)-1) {
if (n < max) max = n;
if ((!z) && (max == 0)) return 0;
if ((*buffer = malloc(max + z)) != NULL) {
if ((max = f_read(path, *buffer, max)) >= 0) {
if (z) *(*buffer + max) = 0;
return max;
}
free(buffer);
}
}
}
return -1;
}
int f_read_alloc(const char *path, char **buffer, int max)
{
return _f_read_alloc(path, buffer, max, 0);
}
int f_read_alloc_string(const char *path, char **buffer, int max)
{
return _f_read_alloc(path, buffer, max, 1);
}
static int _f_wait_exists(const char *name, int max, int invert)
{
while (max-- > 0) {
if (f_exists(name) ^ invert) return 1;
sleep(1);
}
return 0;
}
int f_wait_exists(const char *name, int max)
{
return _f_wait_exists(name, max, 0);
}
int f_wait_notexists(const char *name, int max)
{
return _f_wait_exists(name, max, 1);
}
int
check_if_dir_exist(const char *dirpath)
{
return d_exists(dirpath);
}
int
check_if_dir_empty(const char *dirpath)
{
DIR *dir;
struct dirent *dirent;
int found=0;
if((dir=opendir(dirpath))!=NULL) {
while ((dirent=readdir(dir))!=NULL) {
if(strcmp(dirent->d_name, ".") && strcmp(dirent->d_name, "..")) {
found=1;
break;
}
}
closedir(dir);
}
return found;
}
int
check_if_file_exist(const char *filepath)
{
/* Note: f_exists() checks not S_ISREG, but !S_ISDIR
struct stat st;
return (stat(path, &st) == 0) && (S_ISREG(st.st_mode));
*/
return f_exists(filepath);
}
/* Test whether we can write to a directory.
* @return:
* 0 not writable
* -1 invalid parameter
* otherwise writable
*/
int check_if_dir_writable(const char *dir)
{
char tmp[PATH_MAX];
FILE *fp;
int ret = 0;
if (!dir || *dir == '\0')
return -1;
sprintf(tmp, "%s/.test_dir_writable", dir);
if ((fp = fopen(tmp, "w")) != NULL) {
fclose(fp);
unlink(tmp);
ret = 1;
}
return ret;
}
/* Serialize using fcntl() calls
*/
/* when lock file has been re-opened by the same process,
* it can't be closed, because it release original lock,
* that have been set earlier. this results in file
* descriptors leak.
* one way to avoid it - check if the process has set the
* lock already via /proc/locks, but it seems overkill
* with al of related file ops and text searching. there's
* no kernel API for that.
* maybe need different lock kind? */
#define LET_FD_LEAK
int file_lock(const char *tag)
{
struct flock lock;
char path[64];
int lockfd;
pid_t pid, err;
#ifdef LET_FD_LEAK
pid_t lockpid;
#else
struct stat st;
#endif
snprintf(path, sizeof(path), "/var/lock/%s.lock", tag);
#ifndef LET_FD_LEAK
pid = getpid();
/* check if we already hold a lock */
if (stat(path, &st) == 0 && !S_ISDIR(st.st_mode) && st.st_size > 0) {
FILE *fp;
char line[100], *ptr, *value;
char id[sizeof("XX:XX:4294967295")];
if ((fp = fopen("/proc/locks", "r")) == NULL)
goto error;
snprintf(id, sizeof(id), "%02x:%02x:%ld",
major(st.st_dev), minor(st.st_dev), st.st_ino);
while ((value = fgets(line, sizeof(line), fp)) != NULL) {
strtok_r(line, " ", &ptr);
if ((value = strtok_r(NULL, " ", &ptr)) && strcmp(value, "POSIX") == 0 &&
(value = strtok_r(NULL, " ", &ptr)) && /* strcmp(value, "ADVISORY") == 0 && */
(value = strtok_r(NULL, " ", &ptr)) && strcmp(value, "WRITE") == 0 &&
(value = strtok_r(NULL, " ", &ptr)) && atoi(value) == pid &&
(value = strtok_r(NULL, " ", &ptr)) && strcmp(value, id) == 0)
break;
}
fclose(fp);
if (value != NULL) {
syslog(LOG_DEBUG, "Error locking %s: %d %s", path, 0, "Already locked");
return -1;
}
}
#endif
if ((lockfd = open(path, O_CREAT | O_RDWR, 0666)) < 0)
goto error;
#ifdef LET_FD_LEAK
pid = getpid();
/* check if we already hold a lock */
if (read(lockfd, &lockpid, sizeof(pid_t)) == sizeof(pid_t) &&
lockpid == pid) {
/* don't close the file here as that will release all locks */
syslog(LOG_DEBUG, "Error locking %s: %d %s", path, 0, "Already locked");
return -1;
}
#endif
memset(&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
lock.l_pid = pid;
while (fcntl(lockfd, F_SETLKW, &lock) < 0) {
if (errno != EINTR)
goto close;
}
if (lseek(lockfd, 0, SEEK_SET) < 0 ||
write(lockfd, &pid, sizeof(pid_t)) < 0)
goto close;
return lockfd;
close:
err = errno;
close(lockfd);
errno = err;
error:
syslog(LOG_DEBUG, "Error locking %s: %d %s", path, errno, strerror(errno));
return -1;
}
void file_unlock(int lockfd)
{
if (lockfd < 0) {
errno = EBADF;
syslog(LOG_DEBUG, "Error unlocking %d: %d %s", lockfd, errno, strerror(errno));
return;
}
ftruncate(lockfd, 0);
close(lockfd);
}