added missing progs/mapper2.c file
authorErez Zadok <ezk@cs.sunysb.edu>
Wed, 30 Mar 2011 03:46:35 +0000 (23:46 -0400)
committerErez Zadok <ezk@cs.sunysb.edu>
Wed, 30 Mar 2011 03:46:35 +0000 (23:46 -0400)
Signed-off-by: Erez Zadok <ezk@cs.sunysb.edu>
progs/mapper2.c [new file with mode: 0644]

diff --git a/progs/mapper2.c b/progs/mapper2.c
new file mode 100644 (file)
index 0000000..7022e60
--- /dev/null
@@ -0,0 +1,181 @@
+/*
+ * Copyright (c) 1997-2007 Erez Zadok <ezk@cs.stonybrook.edu>
+ * Copyright (c) 2001-2007 Stony Brook University
+ * Copyright (c) 2003-2007 The Research Foundation of SUNY
+ *
+ * For specific licensing information, see the COPYING file distributed with
+ * this package, or get one from
+ * ftp://ftp.filesystems.org/pub/fistgen/COPYING.
+ *
+ * This Copyright notice must be kept intact and distributed with all
+ * fistgen sources INCLUDING sources generated by fistgen.
+ *
+ *
+ * File: mapper.c
+ *
+ * Usage: mapper -r file
+ * Usage: mapper -w file
+ *     -r will open file for reading only
+ *     -w will open file for reading anf writing, and will change the file
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#define BASE 0
+
+#ifndef MAP_FAILED
+#define MAP_FAILED ((void *)-1)
+#endif /* not MAP_FAILED */
+
+void usage(void)
+{
+  fprintf(stderr, "Usage: mapper -r filename\n");
+  fprintf(stderr, "       mapper -w filename\n");
+  exit(1);
+}
+
+int
+main(int argc, char *argv[])
+{
+  int fd;
+  caddr_t pa;
+  int rw_flag = -1;            /* 0=read, 1=read+write */
+  char *filename, byte;
+  int i, size;
+#if 0
+  struct stat buf;
+#endif
+
+  i=0;
+  /* check input args */
+  if (argc != 3)
+    usage();
+  if (strcmp(argv[1], "-w") == 0)
+    rw_flag = 1;
+  else if (strcmp(argv[1], "-r") == 0)
+    rw_flag = 0;
+  else
+    usage();
+
+  filename = argv[2];
+
+#if 0
+  /* find size of file */
+  if (stat(filename, &buf) < 0) {
+    perror("stat");
+    exit(1);
+  }
+  size = buf.st_size;
+#else
+  size = 3;
+#endif
+  /* open input file */
+  if (rw_flag)
+    fd = open(filename, O_RDWR);
+  else
+    fd = open(filename, O_RDONLY);
+  if (fd < 0) {
+    perror(filename);
+    exit(1);
+  }
+  printf("opened file %s\n", filename);
+
+  /* mmap the input file */
+  pa = mmap((caddr_t) 0,
+           size,
+           (rw_flag ? (PROT_READ | PROT_WRITE) : PROT_READ),
+           MAP_SHARED,
+           fd,
+           BASE);
+  if (pa == MAP_FAILED) {
+    perror("mmap");
+    exit(1);
+  }
+  printf("file is mapped with address %p\n", pa);
+
+#if 0
+  /* now read every 1024-th byte */
+  for (i=0; i<size; i+=1024) {
+    byte = pa[i];
+    printf("byte 0 is decimal %03d (\'%c\')\n",
+          byte, isprint(byte) ? byte : '.');
+  }
+
+  /* for a read-write test, modify the first byte */
+  if (rw_flag)
+    pa[0] += 1;
+
+
+  /* msync the file, but only if we're modifying the file */
+  if (rw_flag)
+    if (msync(pa, size, MS_SYNC) < 0) {
+      perror("msync");
+      exit(1);
+    }
+  printf("file is msynchronized\n");
+#endif
+
+  /* unmap the file */
+  if (munmap(pa, size) < 0) {
+    perror("munmap");
+    exit(1);
+  }
+  printf("file is unmapped\n");
+
+  close(fd);
+  printf("closed file %s\n", filename);
+
+  /*
+   * Now, if we need to do a read-write test, then we reopen the file and
+   * check if the file's content (first bytes) is what we expect it to be.
+   */
+  if (!rw_flag)
+    goto out;
+  /* re-open file (readonly now) */
+  fd = open(filename, O_RDONLY);
+  if (fd < 0) {
+    perror(filename);
+    exit(1);
+  }
+  printf("re-opened file %s\n", filename);
+
+  /* re-mmap the input file */
+  pa = mmap((caddr_t) 0,
+           size,
+           PROT_READ,
+           MAP_SHARED,
+           fd,
+           BASE);
+  if (pa == MAP_FAILED) {
+    perror("mmap");
+    exit(1);
+  }
+  printf("file is re-mapped with address %p\n", pa);
+  /* check if bytes match */
+  if (pa[0] != (byte+1)) {
+    fprintf(stderr,
+           "bytes mismatched in re-mapped file, old=%03d new=%03d!\n",
+           byte+1, pa[0]);
+    exit(1);
+  }
+  /* unmap the file */
+  if (munmap(pa, size) < 0) {
+    perror("munmap");
+    exit(1);
+  }
+  printf("file is unmapped again\n");
+
+  close(fd);
+  printf("closed file %s again\n", filename);
+
+out:
+  exit(0);
+}