-
Notifications
You must be signed in to change notification settings - Fork 4
/
file.c
283 lines (246 loc) · 5.52 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
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
/*
* C Implementation: file
*
* Description:
*
*
* Author: Milan Svoboda <milan.svoboda@centrum.cz>, (C) 2005
* (C) 2009, 2011 Ulrich Hecht <uli@suse.de>
*
* Copyright: See COPYING file that comes with this distribution
*
*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syslog.h>
#include <sys/types.h>
#include <unistd.h>
#include "direct_compress.h"
#include "globals.h"
#include "log.h"
#include "structs.h"
#include "file.h"
#include "utils.h"
compressor_t *find_compressor(const header_t *fh)
{
if (fh->type > sizeof(compressors) / sizeof(compressors[0]))
return NULL;
return compressors[fh->type];
}
compressor_t *find_compressor_name(const char *name)
{
int i;
for (i = 0; i < sizeof(compressors) / sizeof(compressors[0]); i++)
if (compressors[i] && !strcmp(name, compressors[i]->name))
return compressors[i];
return NULL;
}
compressor_t *file_compressor(const header_t *fh)
{
compressor_t *compressor;
if ((fh->id[0] != '\037') || (fh->id[1] != '\135') || (fh->id[2] != '\211'))
return NULL;
compressor = find_compressor(fh);
if (!compressor)
return NULL;
return compressor;
}
int file_write_header(int fd, compressor_t *compressor, off_t size)
{
header_t fh;
assert(fd >= 0);
assert(compressor);
assert(size != (off_t) -1);
fh.id[0] = '\037';
fh.id[1] = '\135';
fh.id[2] = '\211';
fh.type = compressor->type;
fh.size = to_le64(size);
DEBUG_("writing header to %d at %zd\n", fd, lseek(fd, 0, SEEK_CUR));
return write(fd, &fh, sizeof(fh));
}
/**
* Returns type of compression and real size of the file if file is compressed.
* If the header is invalid, the size will not be touched, and the compressor
* will be set to NULL.
*
* @param fd
* @param **compressor
* @param *size
*
* @return 0 - compressor and size assigned or invalid header
* -1 - file i/o error
*/
int file_read_header_fd(int fd, compressor_t **compressor, off_t *size)
{
int r;
header_t fh;
assert(fd >= 0);
assert(compressor);
assert(size);
DEBUG_("reading header from %d at %zd\n", fd, lseek(fd, 0, SEEK_CUR));
r = read(fd, &fh, sizeof(fh));
if (r == -1)
return r;
if (r == sizeof(fh))
{
fh.size = from_le64(fh.size);
*compressor = file_compressor(&fh);
if (*compressor)
*size = fh.size;
}
return 0;
}
/**
* Try to acquire compressor type and uncompressed file size from header.
*
* @return 0 - compressor and size retrieved or invalid header
* -1 - i/o error
*/
int file_read_header_name(const char *name, compressor_t **compressor, off_t *size)
{
int r;
int x;
int fd;
int err = 0; // Only to shut-up compiler's warning about uninitialized use
assert(name);
assert(compressor);
assert(size);
fd = file_open(name, O_RDONLY);
if (fd == FAIL)
return FAIL;
r = file_read_header_fd(fd, compressor, size);
if (r == FAIL)
err = errno;
x = close(fd);
if (x == FAIL)
err = errno;
if ((r == FAIL) || (x == FAIL))
{
errno = err;
return FAIL;
}
return 0;
}
char *file_create_temp(int *fd_temp)
{
int i = 0;
int fd = -1;
int temp_len;
char *temp;
struct timespec delay;
temp_len = sizeof(TEMP) + 7;
temp = (char *)malloc(temp_len);
if (temp == NULL)
{
CRIT_("No memory!");
//exit(EXIT_FAILURE);
*fd_temp = -1;
return 0;
}
while (fd == -1)
{
memcpy(temp, TEMP "XXXXXX", sizeof(TEMP) + sizeof("XXXXXX") - 1);
fd = mkstemp(temp);
// Mkstemp may fail. This protects us before some misterious deadlock
// that may happen.
//
if (i++ > 50)
{
CRIT_("Unable to create temp file");
//exit(EXIT_FAILURE);
free(temp);
*fd_temp = -1;
return 0;
}
if (fd == -1)
{
delay.tv_sec = 1,
delay.tv_nsec = 0,
nanosleep(&delay, NULL);
}
}
*fd_temp = fd;
return temp;
}
/**
* "Force" opens a file, ie opens a file even if we dont have access.
*
* @param entry
* @param mode
* @return
*/
#define ALLMODES S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
int file_open(const char *filename, int mode)
{
int fd;
int newmode;
struct stat buf;
// Try to open file, if we dont have access, force it.
//
fd = open(filename, mode);
if (fd == FAIL && (errno == EACCES || errno == EPERM))
{
DEBUG_("fail with EACCES or EPERM");
// TODO: chmod failing in the start here shouldn't be critical, but size will be invalid if it fails.
// Grab old file info.
//
if (stat(filename,&buf) == FAIL)
{
return FAIL;
}
newmode = buf.st_mode | ALLMODES;
if (chmod(filename,newmode) == FAIL)
{
return FAIL;
}
fd = open(filename, mode);
if (fd == FAIL)
{
return FAIL;
}
if (chmod(filename,buf.st_mode) == FAIL)
{
return FAIL;
}
} else { DEBUG_("fail"); }
return fd;
}
inline void file_close(int *fd)
{
assert(fd);
assert(*fd > FAIL);
if (close(*fd) == -1)
{
CRIT_("Failed to close fd!");
//exit(EXIT_FAILURE);
};
*fd = -1;
}
int is_compressible(const char *filename)
{
char **ext;
for (ext = incompressible; *ext != NULL; ext++)
if (strcasestr(filename, *ext))
return FALSE;
if (user_incompressible)
for (ext = user_incompressible; *ext != NULL; ext++)
if (strcasestr(filename, *ext))
return FALSE;
return TRUE;
}
int is_excluded(const char *filename)
{
char **dir;
if (user_exclude_paths) {
for (dir = user_exclude_paths; *dir != NULL; dir++) {
if (strncmp(filename, *dir, strlen(*dir)) == 0)
return TRUE;
}
}
return FALSE;
}