1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
13 #define T1 "write data before unlink\n"
14 #define T2 "write data after unlink\n"
17 int main(int argc, char **argv)
23 if (argc < 2 || argc > 3) {
24 fprintf(stderr, "usage: %s filename [filename2]\n", argv[0]);
34 fprintf(stderr, "opening\n");
35 fd = open(fname, O_RDWR | O_TRUNC | O_CREAT, 0644);
37 fprintf(stderr, "open (normal) %s\n", strerror(errno));
41 fprintf(stderr, "writing\n");
42 rc = write(fd, T1, strlen(T1) + 1);
43 if (rc != strlen(T1) + 1) {
44 fprintf(stderr, "write (normal) %s (rc %d)\n",
50 fprintf (stderr, "unlinking %s\n", fname2);
53 fprintf(stderr, "unlink %s\n", strerror(errno));
57 printf("unlink %s and press enter\n", fname);
61 if (access(fname, F_OK) == 0) {
62 fprintf(stderr, "%s still exists\n", fname2);
66 fprintf(stderr, "resetting fd offset\n");
67 rc = lseek(fd, 0, SEEK_SET);
69 fprintf(stderr, "seek %s\n", strerror(errno));
73 if (access(fname, F_OK) == 0) {
74 fprintf(stderr, "%s still exists\n", fname);
78 fprintf(stderr, "fstat...\n");
81 fprintf(stderr, "fstat (unlink) %s\n", strerror(errno));
85 fprintf(stderr, "st_nlink = %d\n", (int)st.st_nlink);
87 fprintf(stderr, "reading\n");
88 rc = read(fd, buf, strlen(T1) + 1);
89 if (rc != strlen(T1) + 1) {
90 fprintf(stderr, "read (unlink) %s (rc %d)\n",
95 fprintf(stderr, "comparing data\n");
96 if (memcmp(buf, T1, strlen(T1) + 1) ) {
97 fprintf(stderr, "FAILURE: read wrong data after unlink\n");
101 fprintf(stderr, "truncating\n");
102 rc = ftruncate(fd, 0);
104 fprintf(stderr, "truncate (unlink) %s\n", strerror(errno));
108 fprintf(stderr, "seeking\n");
109 rc = lseek(fd, 0, SEEK_SET);
111 fprintf(stderr, "seek (after unlink trunc) %s\n",
116 fprintf(stderr, "writing again\n");
117 rc = write(fd, T2, strlen(T2) + 1);
118 if (rc != strlen(T2) + 1) {
119 fprintf(stderr, "write (after unlink trunc) %s (rc %d)\n",
120 strerror(errno), rc);
124 fprintf(stderr, "seeking\n");
125 rc = lseek(fd, 0, SEEK_SET);
127 fprintf(stderr, "seek (before unlink read) %s\n",
132 fprintf(stderr, "reading again\n");
133 rc = read(fd, buf, strlen(T2) + 1);
134 if (rc != strlen(T2) + 1) {
135 fprintf(stderr, "read (after unlink rewrite) %s (rc %d)\n",
136 strerror(errno), rc);
140 fprintf(stderr, "comparing data again\n");
141 if (memcmp(buf, T2, strlen(T2) + 1)) {
142 fprintf(stderr, "FAILURE: read wrong data after rewrite\n");
146 fprintf(stderr, "closing\n");
149 fprintf(stderr, "close (unlink) %s\n", strerror(errno));
153 fprintf(stderr, "SUCCESS - goto beer\n");