File sponge.c changed (mode: 100644) (index dcd4aba..a02e0e7) |
... |
... |
static void write_buff_out(char* buff, size_t length, FILE *fd) { |
199 |
199 |
} |
} |
200 |
200 |
} |
} |
201 |
201 |
|
|
202 |
|
static void copy_tmpfile(FILE *tmpfile, FILE *outfd) { |
|
|
202 |
|
static void copy_tmpfile(FILE *tmpfile, FILE *outfile) { |
203 |
203 |
char buf[BUFF_SIZE]; |
char buf[BUFF_SIZE]; |
204 |
204 |
if (fseek(tmpfile, 0, SEEK_SET)) { |
if (fseek(tmpfile, 0, SEEK_SET)) { |
205 |
205 |
perror("could to seek to start of temporary file"); |
perror("could to seek to start of temporary file"); |
|
... |
... |
static void copy_tmpfile(FILE *tmpfile, FILE *outfd) { |
207 |
207 |
exit(1); |
exit(1); |
208 |
208 |
} |
} |
209 |
209 |
while (fread(buf, BUFF_SIZE, 1, tmpfile) > 0) { |
while (fread(buf, BUFF_SIZE, 1, tmpfile) > 0) { |
210 |
|
write_buff_out(buf, BUFF_SIZE, outfd); |
|
|
210 |
|
write_buff_out(buf, BUFF_SIZE, outfile); |
211 |
211 |
} |
} |
212 |
212 |
if (ferror(tmpfile)) { |
if (ferror(tmpfile)) { |
213 |
213 |
perror("read temporary file"); |
perror("read temporary file"); |
|
... |
... |
static void copy_tmpfile(FILE *tmpfile, FILE *outfd) { |
215 |
215 |
exit(1); |
exit(1); |
216 |
216 |
} |
} |
217 |
217 |
fclose(tmpfile); |
fclose(tmpfile); |
218 |
|
fclose(outfd); |
|
|
218 |
|
fclose(outfile); |
219 |
219 |
} |
} |
220 |
220 |
|
|
221 |
221 |
FILE *open_tmpfile(void) { |
FILE *open_tmpfile(void) { |
|
... |
... |
int main (int argc, char **argv) { |
247 |
247 |
char *buf, *bufstart, *outname = NULL; |
char *buf, *bufstart, *outname = NULL; |
248 |
248 |
size_t bufsize = BUFF_SIZE; |
size_t bufsize = BUFF_SIZE; |
249 |
249 |
size_t bufused = 0; |
size_t bufused = 0; |
250 |
|
FILE *tmpfile = 0; |
|
|
250 |
|
FILE *outfile, *tmpfile = 0; |
251 |
251 |
ssize_t i = 0; |
ssize_t i = 0; |
252 |
252 |
size_t mem_available = default_sponge_size(); |
size_t mem_available = default_sponge_size(); |
253 |
253 |
|
|
|
... |
... |
int main (int argc, char **argv) { |
293 |
293 |
/* write whatever we have in memory to tmpfile */ |
/* write whatever we have in memory to tmpfile */ |
294 |
294 |
if (bufused) |
if (bufused) |
295 |
295 |
write_buff_tmp(bufstart, bufused, tmpfile); |
write_buff_tmp(bufstart, bufused, tmpfile); |
296 |
|
fclose(tmpfile); |
|
|
296 |
|
free(bufstart); |
|
297 |
|
if (fflush(tmpfile) != 0) { |
|
298 |
|
perror("fflush"); |
|
299 |
|
exit(1); |
|
300 |
|
} |
297 |
301 |
|
|
298 |
302 |
if (outname) { |
if (outname) { |
299 |
303 |
/* If it's a regular file, or does not yet exist, |
/* If it's a regular file, or does not yet exist, |
300 |
304 |
* attempt a fast rename of the temp file. */ |
* attempt a fast rename of the temp file. */ |
301 |
|
if ((stat(outname, &statbuf) == 0 && |
|
302 |
|
S_ISREG(statbuf.st_mode)) || |
|
303 |
|
errno == ENOENT) { |
|
304 |
|
if (rename(tmpname, outname) != 0) { |
|
305 |
|
/* Slow copy. */ |
|
306 |
|
FILE *outfd = fopen(outname, "w"); |
|
307 |
|
if (outfd < 0) { |
|
308 |
|
perror("error opening output file"); |
|
309 |
|
exit(1); |
|
310 |
|
} |
|
311 |
|
copy_tmpfile(tmpfile, outfd); |
|
|
305 |
|
if (((lstat(outname, &statbuf) == 0 && |
|
306 |
|
S_ISREG(statbuf.st_mode) && |
|
307 |
|
! S_ISLNK(statbuf.st_mode) |
|
308 |
|
) || errno == ENOENT) && |
|
309 |
|
rename(tmpname, outname) == 0) { |
|
310 |
|
/* Fix renamed file mode. */ |
|
311 |
|
if (errno != ENOENT && |
|
312 |
|
chmod(outname, statbuf.st_mode) != 0) { |
|
313 |
|
perror("chmod"); |
|
314 |
|
exit(1); |
312 |
315 |
} |
} |
|
316 |
|
return(0); |
|
317 |
|
} |
|
318 |
|
|
|
319 |
|
/* Fall back to slow copy. */ |
|
320 |
|
outfile = fopen(outname, "w"); |
|
321 |
|
if (!outfile) { |
|
322 |
|
perror("error opening output file"); |
|
323 |
|
exit(1); |
313 |
324 |
} |
} |
|
325 |
|
copy_tmpfile(tmpfile, outfile); |
314 |
326 |
} |
} |
315 |
327 |
else { |
else { |
316 |
328 |
copy_tmpfile(tmpfile, stdout); |
copy_tmpfile(tmpfile, stdout); |
317 |
329 |
} |
} |
318 |
330 |
} |
} |
319 |
331 |
else { |
else { |
320 |
|
FILE *outfd = stdout; |
|
|
332 |
|
outfile = stdout; |
321 |
333 |
if (outname) { |
if (outname) { |
322 |
|
outfd = fopen(outname, "w"); |
|
323 |
|
if (outfd < 0) { |
|
|
334 |
|
outfile = fopen(outname, "w"); |
|
335 |
|
if (!outfile) { |
324 |
336 |
perror("error opening output file"); |
perror("error opening output file"); |
325 |
337 |
exit(1); |
exit(1); |
326 |
338 |
} |
} |
327 |
339 |
} |
} |
328 |
340 |
if (bufused) |
if (bufused) |
329 |
|
write_buff_out(bufstart, bufused, outfd); |
|
330 |
|
fclose(outfd); |
|
|
341 |
|
write_buff_out(bufstart, bufused, outfile); |
|
342 |
|
fclose(outfile); |
331 |
343 |
} |
} |
332 |
344 |
|
|
333 |
345 |
return 0; |
return 0; |