#define _FILE_OFFSET_BITS 64
-#define BUFFER_SIZE 4096
+#include "temp_file.h"
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-#include <unistd.h>
#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include <sysexits.h>
+#include <unistd.h>
-static void write_all(int fd, const void *buf, size_t count) {
- const char* cbuf = buf;
- size_t written = 0;
- while (written < count) {
- int ret = write(fd, &cbuf[written], count - written);
- if (ret == -1) {
- err(EX_IOERR, "Could not write");
- }
- written += ret;
- }
+static off_t ceil_div(off_t dividend, off_t divisor) {
+ return (dividend - 1) / divisor + 1;
}
-void reverse_file(const char* input_filename) {
+void reverse_file(const char* input_filename, FILE* output_stream) {
+ const off_t mmap_chunk_size = 512 << 20;
+
int fd = open(input_filename, O_RDONLY);
- if (fd == -1) {
- err(EX_NOINPUT, "Could not open specified file");
- }
+ if (fd == -1) err(EX_NOINPUT, "Could not open specified file");
struct stat stats;
- if (fstat(fd, &stats) == -1) {
- err(EX_NOINPUT, "Could not stat input");
- }
+ if (fstat(fd, &stats) == -1) err(EX_NOINPUT, "Could not stat input");
long page_size = sysconf(_SC_PAGE_SIZE);
- off_t pages = (stats.st_size - 1) / page_size + 1;
- long map_size = pages * page_size;
- char *m = mmap(NULL, map_size, PROT_READ, MAP_SHARED, fd, 0);
- if (m == MAP_FAILED) {
- err(EX_NOINPUT, "Could not mmap input");
- }
+ off_t num_chunks = ceil_div(stats.st_size, mmap_chunk_size);
+ for (off_t chunk = num_chunks - 1; chunk >= 0; chunk--) {
+ off_t start_offset = chunk * mmap_chunk_size;
+ off_t end_offset = (chunk + 1) * mmap_chunk_size;
+ if (end_offset > stats.st_size) {
+ end_offset = stats.st_size;
+ }
+ off_t pages = ceil_div(end_offset - start_offset, page_size);
+ long map_size = pages * page_size;
+ char *m = mmap(NULL, map_size, PROT_READ, MAP_SHARED, fd, start_offset);
+ if (m == MAP_FAILED) err(EX_NOINPUT, "Could not mmap chunk %lld of %lld", chunk, num_chunks);
- char buf[BUFFER_SIZE];
- off_t buf_offset = 0;
- for (off_t p = stats.st_size - 1; p >= 0; p--) {
- buf[buf_offset++] = m[p];
- if (buf_offset >= BUFFER_SIZE) {
- write_all(1, buf, buf_offset);
- buf_offset = 0;
+ for (off_t p = (end_offset - start_offset) - 1; p >= 0; p--) {
+ if (fputc(m[p], output_stream) == EOF) errx(EX_IOERR, "Could not write");
}
- }
- if (buf_offset) {
- write_all(1, buf, buf_offset);
- }
- if (munmap(m, map_size) == -1) {
- err(EX_IOERR, "Could not unmap input");
+ if (munmap(m, map_size) == -1) err(EX_IOERR, "Could not unmap chunk %lld of %lld", chunk, num_chunks);
}
- if (close(fd) == -1) {
- err(EX_IOERR, "Could not close input");
+
+ if (close(fd) == -1) err(EX_IOERR, "Could not close input");
+}
+
+/* Copy data from input to output until EOF is reached. */
+static void copy(FILE* input, FILE* output) {
+ for (;;) {
+ int c = fgetc(input);
+ if (c == EOF) {
+ if (ferror(input)) errx(EX_IOERR, "Could not read");
+ if (!feof(input)) errx(EX_IOERR, "Unexpected end of file");
+ break;
+ }
+ if (fputc(c, output) == EOF) errx(EX_IOERR, "Could not write");
}
}
+
+void reverse_stream(FILE* input_stream, FILE* output_stream) {
+ char* temp_filename;
+ FILE* temp_file;
+ make_temporary_file(&temp_filename, &temp_file);
+
+ copy(input_stream, temp_file);
+ if (fclose(temp_file) != 0) err(EX_IOERR, "Could not close temporary file");
+
+ reverse_file(temp_filename, output_stream);
+
+ if (unlink(temp_filename) == -1) err(EX_IOERR, "Could not remove temporary file");
+ free(temp_filename);
+}