Compare commits
No commits in common. "613354034dbab2bf74ab475d2636cd7dfcf3b5f1" and "d30c30995f04e996bf19b90c8c34b7934466531a" have entirely different histories.
613354034d
...
d30c30995f
2 changed files with 22 additions and 131 deletions
148
src/archiver.c
148
src/archiver.c
|
@ -28,7 +28,6 @@
|
|||
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <spawn.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
|
@ -40,18 +39,6 @@
|
|||
|
||||
#define TEMP_FILENAME_CMP "simple_archiver_compressed_%u.tmp"
|
||||
|
||||
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
|
||||
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
|
||||
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
|
||||
int is_sig_pipe_occurred = 0;
|
||||
|
||||
void handle_sig_pipe(int sig) {
|
||||
if (sig == SIGPIPE) {
|
||||
is_sig_pipe_occurred = 1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
typedef struct SDArchiverInternalToWrite {
|
||||
void *buf;
|
||||
uint64_t size;
|
||||
|
@ -158,9 +145,6 @@ int write_files_fn(void *data, void *ud) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
// Handle SIGPIPE.
|
||||
signal(SIGPIPE, handle_sig_pipe);
|
||||
|
||||
int pipe_into_cmd[2];
|
||||
int pipe_outof_cmd[2];
|
||||
pid_t compressor_pid;
|
||||
|
@ -193,8 +177,6 @@ int write_files_fn(void *data, void *ud) {
|
|||
// Failed to spawn compressor.
|
||||
close(pipe_into_cmd[1]);
|
||||
close(pipe_outof_cmd[0]);
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to start compressor cmd! Invalid cmd?\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -209,19 +191,31 @@ int write_files_fn(void *data, void *ud) {
|
|||
// Status is available.
|
||||
if (WIFEXITED(compressor_status)) {
|
||||
compressor_return_val = WEXITSTATUS(compressor_status);
|
||||
fprintf(stderr,
|
||||
"WARNING: Exec failed (exec exit code %d)! Invalid "
|
||||
"compressor cmd?\n",
|
||||
compressor_return_val);
|
||||
return 1;
|
||||
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.
|
||||
fprintf(stderr,
|
||||
"WARNING: Exec failed (exec exit code unknown)! Invalid "
|
||||
"compressor cmd?\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -235,13 +229,6 @@ int write_files_fn(void *data, void *ud) {
|
|||
size_t read_count;
|
||||
ssize_t ret;
|
||||
while (!write_done || !read_done) {
|
||||
if (is_sig_pipe_occurred) {
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to write to compressor (SIGPIPE)! Invalid "
|
||||
"compressor cmd?\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Read from file.
|
||||
if (!write_done) {
|
||||
if (!write_again) {
|
||||
|
@ -254,16 +241,10 @@ int write_files_fn(void *data, void *ud) {
|
|||
write_again = 1;
|
||||
} else {
|
||||
// Error during write.
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to write to compressor! Invalid "
|
||||
"compressor cmd?\n");
|
||||
return 1;
|
||||
}
|
||||
} else if ((size_t)ret != write_count) {
|
||||
// Error during write.
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to write to compressor! Invalid "
|
||||
"compressor cmd?\n");
|
||||
return 1;
|
||||
} else {
|
||||
write_again = 0;
|
||||
|
@ -277,9 +258,6 @@ int write_files_fn(void *data, void *ud) {
|
|||
// fprintf(stderr, "write_done\n");
|
||||
} else if (ferror(file_fd)) {
|
||||
// Error during read file.
|
||||
fprintf(
|
||||
stderr,
|
||||
"WARNING: Failed to write to compressor (failed to read)!\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
@ -292,9 +270,6 @@ int write_files_fn(void *data, void *ud) {
|
|||
read_count = fwrite(read_buf, 1, ret, tmp_fd);
|
||||
if (read_count != (size_t)ret) {
|
||||
// Write to tmp_fd error.
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to read from compressor! Invalid "
|
||||
"compressor cmd?\n");
|
||||
return 1;
|
||||
} else {
|
||||
// fprintf(stderr, "Written %zd bytes to tmp_fd.\n", read_count);
|
||||
|
@ -309,9 +284,6 @@ int write_files_fn(void *data, void *ud) {
|
|||
// Nop.
|
||||
} else {
|
||||
// Read error.
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to read from compressor! Invalid "
|
||||
"compressor cmd?\n");
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
|
@ -320,13 +292,6 @@ int write_files_fn(void *data, void *ud) {
|
|||
}
|
||||
}
|
||||
|
||||
if (is_sig_pipe_occurred) {
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to write to compressor (SIGPIPE)! Invalid "
|
||||
"compressor cmd?\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
waitpid(compressor_pid, NULL, 0);
|
||||
|
||||
uint16_t u16;
|
||||
|
@ -730,8 +695,6 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
|
|||
fprintf(stderr, "[%10u/%10u]\n", state->count, state->max);
|
||||
if (simple_archiver_list_get(filenames, write_files_fn, state)) {
|
||||
// Error occurred.
|
||||
fprintf(stderr, "Error ocurred writing file(s) to archive.\n");
|
||||
return SDAS_FAILED_TO_WRITE;
|
||||
}
|
||||
state->out_f = NULL;
|
||||
|
||||
|
@ -1027,18 +990,10 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
|
||||
simple_archiver_helper_make_dirs((const char *)out_f_name);
|
||||
out_f = fopen(out_f_name, "wb");
|
||||
__attribute__((
|
||||
cleanup(cleanup_temp_filename_delete))) void **ptrs_array =
|
||||
malloc(sizeof(void *) * 2);
|
||||
ptrs_array[0] = out_f_name;
|
||||
ptrs_array[1] = &out_f;
|
||||
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
|
||||
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
|
||||
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
|
||||
if (is_compressed) {
|
||||
// Handle SIGPIPE.
|
||||
signal(SIGPIPE, handle_sig_pipe);
|
||||
|
||||
int pipe_into_cmd[2];
|
||||
int pipe_outof_cmd[2];
|
||||
pid_t decompressor_pid;
|
||||
|
@ -1073,9 +1028,6 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
// Failed to spawn compressor.
|
||||
close(pipe_into_cmd[1]);
|
||||
close(pipe_outof_cmd[0]);
|
||||
fprintf(
|
||||
stderr,
|
||||
"WARNING: Failed to start decompressor cmd! Invalid cmd?\n");
|
||||
return SDAS_INTERNAL_ERROR;
|
||||
}
|
||||
} else {
|
||||
|
@ -1085,9 +1037,6 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
// Failed to spawn compressor.
|
||||
close(pipe_into_cmd[1]);
|
||||
close(pipe_outof_cmd[0]);
|
||||
fprintf(
|
||||
stderr,
|
||||
"WARNING: Failed to start decompressor cmd! Invalid cmd?\n");
|
||||
return SDAS_INTERNAL_ERROR;
|
||||
}
|
||||
}
|
||||
|
@ -1096,30 +1045,6 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
close(pipe_into_cmd[0]);
|
||||
close(pipe_outof_cmd[1]);
|
||||
|
||||
int decompressor_status;
|
||||
int decompressor_return_val;
|
||||
int decompressor_ret =
|
||||
waitpid(decompressor_pid, &decompressor_status, WNOHANG);
|
||||
if (decompressor_ret == decompressor_pid) {
|
||||
// Status is available.
|
||||
if (WIFEXITED(decompressor_status)) {
|
||||
decompressor_return_val = WEXITSTATUS(decompressor_status);
|
||||
fprintf(stderr,
|
||||
"WARNING: Exec failed (exec exit code %d)! Invalid "
|
||||
"decompressor cmd?\n",
|
||||
decompressor_return_val);
|
||||
return SDAS_INTERNAL_ERROR;
|
||||
}
|
||||
} else if (decompressor_ret == 0) {
|
||||
// Probably still running, continue on.
|
||||
} else {
|
||||
// Error.
|
||||
fprintf(stderr,
|
||||
"WARNING: Exec failed (exec exit code unknown)! Invalid "
|
||||
"decompressor cmd?\n");
|
||||
return SDAS_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
uint64_t compressed_file_size = u64;
|
||||
int write_again = 0;
|
||||
int write_pipe_done = 0;
|
||||
|
@ -1128,13 +1053,6 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
char recv_buf[1024];
|
||||
size_t amount_to_read;
|
||||
while (!write_pipe_done || !read_pipe_done) {
|
||||
if (is_sig_pipe_occurred) {
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to write to decompressor (SIGPIPE)! "
|
||||
"Invalid decompressor cmd?\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Read from file.
|
||||
if (!write_pipe_done) {
|
||||
if (!write_again && compressed_file_size != 0) {
|
||||
|
@ -1164,16 +1082,10 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
write_again = 1;
|
||||
} else {
|
||||
// Error.
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to write to decompressor! Invalid "
|
||||
"decompressor cmd?\n");
|
||||
return SDAS_INTERNAL_ERROR;
|
||||
}
|
||||
} else {
|
||||
// Should be unreachable, error.
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to write to decompressor! Invalid "
|
||||
"decompressor cmd?\n");
|
||||
return SDAS_INTERNAL_ERROR;
|
||||
}
|
||||
}
|
||||
|
@ -1188,15 +1100,9 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
// Success.
|
||||
} else if (ferror(out_f)) {
|
||||
// Error.
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to read from decompressor! Invalid "
|
||||
"decompressor cmd?\n");
|
||||
return SDAS_INTERNAL_ERROR;
|
||||
} else {
|
||||
// Invalid state, error.
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to read from decompressor! Invalid "
|
||||
"decompressor cmd?\n");
|
||||
return SDAS_INTERNAL_ERROR;
|
||||
}
|
||||
} else if (read_ret == -1) {
|
||||
|
@ -1204,9 +1110,6 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
// No bytes to read yet.
|
||||
} else {
|
||||
// Error.
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to read from decompressor! Invalid "
|
||||
"decompressor cmd?\n");
|
||||
return SDAS_INTERNAL_ERROR;
|
||||
}
|
||||
} else if (read_ret == 0) {
|
||||
|
@ -1216,21 +1119,11 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
free_FILE_helper(&out_f);
|
||||
} else {
|
||||
// Invalid state (unreachable?), error.
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to read from decompressor! Invalid "
|
||||
"decompressor cmd?\n");
|
||||
return SDAS_INTERNAL_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_sig_pipe_occurred) {
|
||||
fprintf(stderr,
|
||||
"WARNING: Failed to write to decompressor (SIGPIPE)! "
|
||||
"Invalid decompressor cmd?\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
waitpid(decompressor_pid, NULL, 0);
|
||||
} else {
|
||||
uint64_t compressed_file_size = u64;
|
||||
|
@ -1269,7 +1162,6 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
return SDAS_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
ptrs_array[0] = NULL;
|
||||
fprintf(stderr, " Extracted.\n");
|
||||
#endif
|
||||
} else {
|
||||
|
|
|
@ -90,7 +90,6 @@ int main(int argc, const char **argv) {
|
|||
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
|
||||
if (ret != SDAS_SUCCESS) {
|
||||
unlink(parsed.filename);
|
||||
return 3;
|
||||
}
|
||||
#endif
|
||||
} else if ((parsed.flags & 3) == 2) {
|
||||
|
@ -98,7 +97,7 @@ int main(int argc, const char **argv) {
|
|||
if (!file) {
|
||||
fprintf(stderr, "ERROR: Failed to open \"%s\" for reading!\n",
|
||||
parsed.filename);
|
||||
return 4;
|
||||
return 3;
|
||||
}
|
||||
|
||||
int ret = simple_archiver_parse_archive_info(file, 0, NULL);
|
||||
|
@ -113,7 +112,7 @@ int main(int argc, const char **argv) {
|
|||
if (!file) {
|
||||
fprintf(stderr, "ERROR: Failed to open \"%s\" for reading!\n",
|
||||
parsed.filename);
|
||||
return 5;
|
||||
return 3;
|
||||
}
|
||||
|
||||
__attribute__((cleanup(simple_archiver_free_state)))
|
||||
|
|
Loading…
Reference in a new issue