]> git.scottworley.com Git - overonion/blobdiff - reverse_lib.c
Automate overonion test
[overonion] / reverse_lib.c
index 20837ee5f65fe95b237436b0d1685fd2d497a69d..f55bedf7b990458bee727df66f14a0961116beec 100644 (file)
@@ -1,6 +1,7 @@
-#define _GNU_SOURCE
 #define _FILE_OFFSET_BITS 64
 
+#include "temp_file.h"
+
 #include <err.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <sysexits.h>
 #include <unistd.h>
 
+static off_t ceil_div(off_t dividend, off_t divisor) {
+  return (dividend - 1) / divisor + 1;
+}
+
 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");
 
@@ -19,36 +26,28 @@ void reverse_file(const char* input_filename, FILE* output_stream) {
   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);
 
-  for (off_t p = stats.st_size - 1; p >= 0; p--) {
-    if (fputc(m[p], output_stream) == EOF) errx(EX_IOERR, "Could not write");
+    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 (munmap(m, map_size) == -1) err(EX_IOERR, "Could not unmap chunk %lld of %lld", chunk, num_chunks);
   }
 
-  if (munmap(m, map_size) == -1) err(EX_IOERR, "Could not unmap input");
   if (close(fd) == -1) err(EX_IOERR, "Could not close input");
 }
 
-/* Create a temporary file in $TMPDIR, or /tmp if TMPDIR is not set.
- * Caller must free temp_filename and fclose temp_file.  Succeeds or terminates
- * the process. */
-static void make_temporary_file(char** temp_filename, FILE** temp_file) {
-  char* TMPDIR = getenv("TMPDIR");
-  if (TMPDIR == NULL) {
-    TMPDIR = "/tmp";
-  }
-  if (asprintf(temp_filename, "%s/reverse.XXXXXX", TMPDIR) == -1) {
-    errx(EX_OSERR, "Could not assemble temporary filename");
-  }
-  int fd = mkstemp(*temp_filename);
-  if (fd == -1) err(EX_IOERR, "Could not make a temporary file");
-  *temp_file = fdopen(fd, "w");
-  if (*temp_file == NULL) err(EX_IOERR, "Could not open temporary file");
-}
-
 /* Copy data from input to output until EOF is reached. */
 static void copy(FILE* input, FILE* output) {
   for (;;) {