Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

scratch dir

  • Loading branch information...
commit eea3fa3ba30b0becab17188e654bf1acca27a542 1 parent c33eea9
Keith Rarick authored December 17, 2012
5  Readme.md
Source Rendered
@@ -23,6 +23,9 @@ put a `+` in front of the _ctcheck command (as in the sample
23 23
 makefile) and run make with its `-jN` flag, for example
24 24
 `make -j16 check`, CT will run tests concurrently (and
25 25
 hopefully in parallel).
  26
+- A scratch directory can be obtained by calling ctdir()
  27
+inside the test. This directory will be removed by the test
  28
+runner after the test finishes.
26 29
 
27 30
 ## Terminal Output
28 31
 
@@ -38,7 +41,7 @@ cc -Werror -Wall   -c -o ct/ct.o ct/ct.c
38 41
 cc -Werror -Wall   -c -o msg.o msg.c
39 42
 cc   ct/_ctcheck.o ct/ct.o msg.o msg-test.o   -o ct/_ctcheck
40 43
 ct/_ctcheck
41  
-E..FEF
  44
+E...FEF
42 45
 
43 46
 cttestexit: error (exit status 2)
44 47
 
56  ct/ct.c
@@ -6,14 +6,17 @@
6 6
 #include <stdio.h>
7 7
 #include <stdarg.h>
8 8
 #include <sys/types.h>
  9
+#include <sys/stat.h>
9 10
 #include <unistd.h>
10 11
 #include <sys/wait.h>
11 12
 #include <fcntl.h>
  13
+#include <dirent.h>
12 14
 #include <errno.h>
13 15
 #include "internal.h"
14 16
 #include "ct.h"
15 17
 
16 18
 
  19
+static T *curtest;
17 20
 static int rjobfd = -1, wjobfd = -1;
18 21
 
19 22
 
@@ -39,6 +42,14 @@ ctfail(void)
39 42
 }
40 43
 
41 44
 
  45
+char *
  46
+ctdir(void)
  47
+{
  48
+    mkdir(curtest->dir, 0700);
  49
+    return curtest->dir;
  50
+}
  51
+
  52
+
42 53
 static void
43 54
 die(int code, int err, char *msg)
44 55
 {
@@ -99,6 +110,8 @@ start(T *t)
99 110
         die(1, errno, "tmpfile");
100 111
     }
101 112
     t->fd = fileno(out);
  113
+    strcpy(t->dir, TmpDirPat);
  114
+    mktemp(t->dir);
102 115
     t->pid = fork();
103 116
     if (t->pid < 0) {
104 117
         die(1, errno, "fork");
@@ -113,6 +126,7 @@ start(T *t)
113 126
         if (dup2(1, 2) == -1) {
114 127
             die(3, errno, "dup2");
115 128
         }
  129
+        curtest = t;
116 130
         t->f();
117 131
         _exit(0);
118 132
     }
@@ -152,6 +166,47 @@ copyfd(FILE *out, int in)
152 166
 }
153 167
 
154 168
 
  169
+// Removes path and all of its children.
  170
+// Writes errors to stderr and keeps going.
  171
+// If path doesn't exist, rmtree returns silently.
  172
+static void
  173
+rmtree(char *path)
  174
+{
  175
+    int r = unlink(path);
  176
+    if (r == 0 || errno == ENOENT) {
  177
+        return; // success
  178
+    }
  179
+    int unlinkerr = errno;
  180
+
  181
+    DIR *d = opendir(path);
  182
+    if (!d) {
  183
+        if (errno == ENOTDIR) {
  184
+            fprintf(stderr, "ct: unlink: %s\n", strerror(unlinkerr));
  185
+        } else {
  186
+            perror("ct: opendir");
  187
+        }
  188
+        fprintf(stderr, "ct: path %s\n", path);
  189
+        return;
  190
+    }
  191
+    struct dirent *ent;
  192
+    while ((ent = readdir(d))) {
  193
+        if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
  194
+            continue;
  195
+        }
  196
+        int n = strlen(path) + 1 + strlen(ent->d_name);
  197
+        char s[n];
  198
+        sprintf(s, "%s/%s", path, ent->d_name);
  199
+        rmtree(s);
  200
+    }
  201
+    closedir(d);
  202
+    r = rmdir(path);
  203
+    if (r == -1) {
  204
+        perror("ct: rmdir");
  205
+        fprintf(stderr, "ct: path %s\n", path);
  206
+    }
  207
+}
  208
+
  209
+
155 210
 static int
156 211
 report(T t[])
157 212
 {
@@ -159,6 +214,7 @@ report(T t[])
159 214
 
160 215
     putchar('\n');
161 216
     for (; t->f; t++) {
  217
+        rmtree(t->dir);
162 218
         if (!t->status) {
163 219
             continue;
164 220
         }
5  ct/ct.h
... ...
@@ -1,5 +1,6 @@
1  
-void ctfail(void);
2  
-void ctlogpn(char*, int, char*, ...) __attribute__((format(printf, 3, 4)));
  1
+char *ctdir(void);
  2
+void  ctfail(void);
  3
+void  ctlogpn(char*, int, char*, ...) __attribute__((format(printf, 3, 4)));
3 4
 #define ctlog(...) ctlogpn(__FILE__, __LINE__, __VA_ARGS__)
4 5
 #define assert(x) do if (!(x)) {\
5 6
 	ctlog("%s", "test: " #x);\
4  ct/gen
@@ -26,9 +26,9 @@ gen() {
26 26
 
27 27
     printf 'T ctmain[] = {\n'
28 28
     for t in "$@"
29  
-    do printf '    {%s, "%s", 0, 0, 0},\n' $t $t
  29
+    do printf '    {%s, "%s", 0, 0, 0, {}},\n' $t $t
30 30
     done
31  
-    printf '    {0, 0, 0, 0, 0},\n'
  31
+    printf '    {0, 0, 0, 0, 0, {}},\n'
32 32
     printf '};\n'
33 33
 }
34 34
 
3  ct/internal.h
... ...
@@ -1,3 +1,5 @@
  1
+#define TmpDirPat "/tmp/ct.XXXXXX"
  2
+
1 3
 typedef struct T T;
2 4
 
3 5
 struct T {
@@ -6,6 +8,7 @@ struct T {
6 8
     int  status;
7 9
     int  fd;
8 10
     int  pid;
  11
+    char dir[sizeof TmpDirPat];
9 12
 };
10 13
 
11 14
 extern T ctmain[];
9  msg-test.c
... ...
@@ -1,5 +1,7 @@
1 1
 #include <string.h>
2 2
 #include <stdlib.h>
  3
+#include <unistd.h>
  4
+#include <fcntl.h>
3 5
 #include <stdio.h>
4 6
 #include "msg.h"
5 7
 #include "ct/ct.h"
@@ -44,6 +46,13 @@ cttestsegfault()
44 46
 }
45 47
 
46 48
 void
  49
+cttesttmpdir()
  50
+{
  51
+    assert(chdir(ctdir()) == 0);
  52
+    assert(open("x", O_CREAT|O_RDWR, 0777));
  53
+}
  54
+
  55
+void
47 56
 cttestexit()
48 57
 {
49 58
     exit(2);

0 notes on commit eea3fa3

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