Fix robust cleanup of temporary compressed file
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 4s
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 4s
This commit is contained in:
parent
5f8d6b0c0b
commit
d30c30995f
1 changed files with 15 additions and 6 deletions
|
@ -72,13 +72,19 @@ int write_list_datas_fn(void *data, void *ud) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cleanup_temp_filename_delete(char **tmpfilename) {
|
void cleanup_temp_filename_delete(void ***ptrs_array) {
|
||||||
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
|
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
|
||||||
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
|
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
|
||||||
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
|
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
|
||||||
if (tmpfilename && *tmpfilename) {
|
if (ptrs_array && *ptrs_array) {
|
||||||
unlink(*tmpfilename);
|
if ((*ptrs_array)[1]) {
|
||||||
*tmpfilename = NULL;
|
free_FILE_helper((FILE **)(*ptrs_array)[1]);
|
||||||
|
}
|
||||||
|
if ((*ptrs_array)[0]) {
|
||||||
|
unlink((char *)((*ptrs_array)[0]));
|
||||||
|
}
|
||||||
|
free(*ptrs_array);
|
||||||
|
*ptrs_array = NULL;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -130,6 +136,10 @@ int write_files_fn(void *data, void *ud) {
|
||||||
}
|
}
|
||||||
__attribute__((cleanup(free_FILE_helper))) FILE *tmp_fd =
|
__attribute__((cleanup(free_FILE_helper))) FILE *tmp_fd =
|
||||||
fopen(temp_filename, "wb");
|
fopen(temp_filename, "wb");
|
||||||
|
__attribute__((cleanup(cleanup_temp_filename_delete))) void **ptrs_array =
|
||||||
|
malloc(sizeof(void *) * 2);
|
||||||
|
ptrs_array[0] = temp_filename;
|
||||||
|
ptrs_array[1] = &tmp_fd;
|
||||||
if (!tmp_fd) {
|
if (!tmp_fd) {
|
||||||
// Unable to create temp file.
|
// Unable to create temp file.
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -361,8 +371,7 @@ int write_files_fn(void *data, void *ud) {
|
||||||
// Get compressed file length.
|
// Get compressed file length.
|
||||||
// Compressed file should be at "temp_filename".
|
// Compressed file should be at "temp_filename".
|
||||||
tmp_fd = fopen(temp_filename, "rb");
|
tmp_fd = fopen(temp_filename, "rb");
|
||||||
__attribute__((cleanup(cleanup_temp_filename_delete))) char
|
|
||||||
*temp_filename_cleanup_reference = temp_filename;
|
|
||||||
long end;
|
long end;
|
||||||
if (fseek(tmp_fd, 0, SEEK_END) != 0) {
|
if (fseek(tmp_fd, 0, SEEK_END) != 0) {
|
||||||
// Error seeking.
|
// Error seeking.
|
||||||
|
|
Loading…
Reference in a new issue