chiark / gitweb /
debugging for thing that crashed
[innduct.git] / contrib / reset-cnfs.c
1 /* Quick and Dirty Hack to reset a CNFS buffer without having to DD the
2  * Entire Thing from /dev/zero again. */
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <errno.h>
6 #include <fcntl.h>
7
8 #include <stdio.h>
9
10 /* uncomment the below for LARGE_FILES support */ 
11 /* #define LARGE_FILES */
12
13 int main(int argc, char *argv[])
14 {
15     int fd;
16     int i, j;
17     char buf[512];
18 #ifdef LARGE_FILES 
19     struct stat64 st; 
20 #else 
21     struct stat st; 
22 #endif
23     int numwr;
24
25     bzero(buf, sizeof(buf));
26     for (i = 1; i < argc; i++) {
27 #ifdef LARGE_FILES 
28         if ((fd = open(argv[i], O_LARGEFILE | O_RDWR, 0664)) < 0) 
29 #else 
30         if ((fd = open(argv[i], O_RDWR, 0664)) < 0) 
31 #endif
32             fprintf(stderr, "Could not open file %s: %s\n", argv[i], strerror(errno));
33         else {
34 #ifdef LARGE_FILES 
35             if (fstat64(fd, &st) < 0) { 
36 #else 
37             if (fstat(fd, &st) < 0) { 
38 #endif
39                 fprintf(stderr, "Could not stat file %s: %s\n", argv[i], strerror(errno));
40             } else {
41                 /* each bit in the bitfield is 512 bytes of data.  Each byte
42                  * has 8 bits, so calculate as 512 * 8 bytes of data, plus
43                  * fuzz. buf has 512 bytes in it, therefore containing data for
44                  * (512 * 8) * 512 bytes of data. */
45                 numwr = (st.st_size / (512*8) / sizeof(buf)) + 50;
46                 printf("File %s: %u %u\n", argv[i], st.st_size, numwr);
47                 for (j = 0; j < numwr; j++) {
48                     if (!(j % 100))
49                         printf("\t%d/%d\n", j, numwr);
50                     write(fd, buf, sizeof(buf));
51                 }
52             }
53             close(fd);
54         }
55     }
56 }