Compare commits

..

3 commits

Author SHA1 Message Date
5f8d6b0c0b More robust handling of bad compressor cmd
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 4s
2024-07-18 22:32:04 +09:00
72d2db378f Do not create archive file on failure 2024-07-18 22:09:03 +09:00
7046dfb1e5 More robust cleanup of temporary file 2024-07-18 22:08:48 +09:00
2 changed files with 65 additions and 4 deletions

View file

@ -72,6 +72,17 @@ int write_list_datas_fn(void *data, void *ud) {
return 0; return 0;
} }
void cleanup_temp_filename_delete(char **tmpfilename) {
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
if (tmpfilename && *tmpfilename) {
unlink(*tmpfilename);
*tmpfilename = NULL;
}
#endif
}
int write_files_fn(void *data, void *ud) { int write_files_fn(void *data, void *ud) {
const SDArchiverFileInfo *file_info = data; const SDArchiverFileInfo *file_info = data;
SDArchiverState *state = ud; SDArchiverState *state = ud;
@ -163,6 +174,41 @@ int write_files_fn(void *data, void *ud) {
close(pipe_into_cmd[0]); close(pipe_into_cmd[0]);
close(pipe_outof_cmd[1]); close(pipe_outof_cmd[1]);
int compressor_status;
int compressor_return_val;
int compressor_ret = waitpid(compressor_pid, &compressor_status, WNOHANG);
if (compressor_ret == compressor_pid) {
// Status is available.
if (WIFEXITED(compressor_status)) {
compressor_return_val = WEXITSTATUS(compressor_status);
if (compressor_return_val == 127) {
// Exec failed.
fprintf(stderr,
"WARNING: Exec failed (exec exit code 127)! Invalid "
"compressor cmd?\n");
return 1;
} else if (compressor_return_val == 0) {
// Immediately halted.
fprintf(stderr,
"WARNING: Exec failed (exec exit code 0)! Invalid "
"compressor cmd?\n");
return 1;
} else {
// Other status returned.
fprintf(stderr,
"WARNING: Exec failed (exec exit code %d)! Invalid "
"compressor cmd?\n",
compressor_return_val);
return 1;
}
}
} else if (compressor_ret == 0) {
// Probably still running, continue on.
} else {
// Error.
return 1;
}
// Write file to pipe, and read from other pipe. // Write file to pipe, and read from other pipe.
char write_buf[1024]; char write_buf[1024];
char read_buf[1024]; char read_buf[1024];
@ -315,6 +361,8 @@ 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.
@ -362,7 +410,6 @@ int write_files_fn(void *data, void *ud) {
// Cleanup. // Cleanup.
free_FILE_helper(&tmp_fd); free_FILE_helper(&tmp_fd);
unlink(temp_filename);
#endif #endif
} else { } else {
uint16_t u16; uint16_t u16;
@ -1204,13 +1251,13 @@ int simple_archiver_de_compress(int pipe_fd_in[2], int pipe_fd_out[2],
posix_spawn_file_actions_destroy(&file_actions); posix_spawn_file_actions_destroy(&file_actions);
close(pipe_fd_in[0]); close(pipe_fd_in[0]);
close(pipe_fd_out[1]); close(pipe_fd_out[1]);
return 3; return 4;
} else if (posix_spawn_file_actions_addclose(&file_actions, pipe_fd_out[0]) != } else if (posix_spawn_file_actions_addclose(&file_actions, pipe_fd_out[0]) !=
0) { 0) {
posix_spawn_file_actions_destroy(&file_actions); posix_spawn_file_actions_destroy(&file_actions);
close(pipe_fd_in[0]); close(pipe_fd_in[0]);
close(pipe_fd_out[1]); close(pipe_fd_out[1]);
return 3; return 5;
} }
__attribute__((cleanup( __attribute__((cleanup(
@ -1223,7 +1270,7 @@ int simple_archiver_de_compress(int pipe_fd_in[2], int pipe_fd_out[2],
close(pipe_fd_in[0]); close(pipe_fd_in[0]);
close(pipe_fd_out[1]); close(pipe_fd_out[1]);
posix_spawn_file_actions_destroy(&file_actions); posix_spawn_file_actions_destroy(&file_actions);
return 4; return 6;
} }
posix_spawn_file_actions_destroy(&file_actions); posix_spawn_file_actions_destroy(&file_actions);

View file

@ -18,6 +18,13 @@
#include <stdio.h> #include <stdio.h>
#include "platforms.h"
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
#include <unistd.h>
#endif
#include "archiver.h" #include "archiver.h"
#include "parser.h" #include "parser.h"
@ -78,6 +85,13 @@ int main(int argc, const char **argv) {
fprintf(stderr, " %s\n", error_str); fprintf(stderr, " %s\n", error_str);
} }
fclose(file); fclose(file);
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
if (ret != SDAS_SUCCESS) {
unlink(parsed.filename);
}
#endif
} else if ((parsed.flags & 3) == 2) { } else if ((parsed.flags & 3) == 2) {
FILE *file = fopen(parsed.filename, "rb"); FILE *file = fopen(parsed.filename, "rb");
if (!file) { if (!file) {