Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

cachedel: remove file from fs cache

  • Loading branch information...
commit 5ae3cb45c3983372e709017fedea9d7a54774b99 1 parent 1afccbc
Julius Plenz authored February 05, 2012

Showing 2 changed files with 45 additions and 0 deletions. Show diff stats Hide diff stats

  1. 1  Makefile
  2. 44  cachedel.c
1  Makefile
... ...
@@ -1,5 +1,6 @@
1 1
 default:
2 2
 	gcc -Wall -o cachestats cachestats.c
  3
+	gcc -Wall -o cachedel cachedel.c
3 4
 	gcc -Wall -fPIC -c -o nocache.o nocache.c
4 5
 	gcc -Wall -fPIC -c -o fcntl_helpers.o fcntl_helpers.c
5 6
 	gcc -Wall -shared -Wl,-soname,nocache.so.1 -ldl -o nocache.so nocache.o fcntl_helpers.o
44  cachedel.c
... ...
@@ -0,0 +1,44 @@
  1
+#include <sys/types.h>
  2
+#include <sys/stat.h>
  3
+#include <fcntl.h>
  4
+#include <error.h>
  5
+#include <stdio.h>
  6
+#include <stdlib.h>
  7
+
  8
+int exiterr(const char *s)
  9
+{
  10
+    perror(s);
  11
+    exit(-1);
  12
+}
  13
+
  14
+int main(int argc, char *argv[])
  15
+{
  16
+    int fd;
  17
+    struct stat st;
  18
+
  19
+    if(argc != 2) {
  20
+        fprintf(stderr, "usage: %s <file> "
  21
+            "-- call fadvise(DONTNEED) on file", argv[0]);
  22
+        exit(1);
  23
+    }
  24
+
  25
+    fd = open(argv[1], O_RDONLY);
  26
+    if(fd == -1)
  27
+        exiterr("open");
  28
+
  29
+    if(fstat(fd, &st) == -1)
  30
+        exiterr("fstat");
  31
+    if(!S_ISREG(st.st_mode)) {
  32
+        fprintf(stderr, "%s: S_ISREG: not a regular file", argv[1]);
  33
+        return EXIT_FAILURE;
  34
+    }
  35
+    if(st.st_size == 0) {
  36
+        fprintf(stderr, "%s: file size is 0!\n", argv[1]);
  37
+        return EXIT_FAILURE;
  38
+    }
  39
+
  40
+    if(posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) == -1)
  41
+        exiterr("posix_fadvise");
  42
+
  43
+    return EXIT_SUCCESS;
  44
+}

0 notes on commit 5ae3cb4

Please sign in to comment.
Something went wrong with that request. Please try again.