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>
#define TEMP_FILENAME_CMP "simple_archiver_compressed_%u.tmp"
+int is_sig_pipe_occurred = 0;
+
+void handle_sig_pipe(int sig) {
+ if (sig == SIGPIPE) {
+ is_sig_pipe_occurred = 1;
+ }
+}
+
typedef struct SDArchiverInternalToWrite {
void *buf;
uint64_t size;
return 1;
}
+ // Handle SIGPIPE.
+ signal(SIGPIPE, handle_sig_pipe);
+
int pipe_into_cmd[2];
int pipe_outof_cmd[2];
pid_t compressor_pid;
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) {
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;
// 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;
}
}
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);
// Nop.
} else {
// Read error.
+ fprintf(stderr,
+ "WARNING: Failed to read from compressor! Invalid "
+ "compressor cmd?\n");
return 1;
}
} else {
}
}
+ 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;
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;
// 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 {
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) {
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;
}
}
// 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) {
// 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) {
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;