Whamcloud - gitweb
LU-15152 tests: auster reports wrong testsuite status
[fs/lustre-release.git] / lustre / tests / ll_sparseness_verify.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  *
29  * lustre/tests/ll_sparseness_verify.c
30  *
31  * The companion to ll_sparseness_write; walk all the bytes in the file.
32  * the bytes at the offsets specified on the command line must be '+', as
33  * previously written by ll_sparseness_write.  All other bytes must be 0.
34  */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <sys/types.h>
42 #include <sys/wait.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <stdarg.h>
46
47 #define BUFSIZE (1024 * 1024)
48
49 void error(char *fmt, ...)
50 {
51         va_list ap;
52
53         va_start(ap, fmt);
54         vfprintf(stderr, fmt, ap);
55         va_end(ap);
56         exit(1);
57 }
58
59 int compare_offsets(const void *a, const void *b)
60 {
61         off_t *A = (off_t *)a;
62         off_t *B = (off_t *)b;
63
64         return *A - *B;
65 }
66
67 int main(int argc, char **argv)
68 {
69         unsigned int num_offsets, cur_off = 0, i;
70         off_t *offsets, pos = 0, end_of_buf = 0;
71         char *end, *buf;
72         struct stat st;
73         ssize_t ret;
74         int fd;
75
76         if (argc < 3)
77                 error("Usage: %s <filename> <offset> [ offset ... ]\n",
78                       argv[0]);
79
80         fd = open(argv[1], O_RDONLY);
81         if (fd < 0)
82                 error("couldn't open %s: %s\n", argv[1], strerror(errno));
83
84         buf = malloc(BUFSIZE);
85         if (!buf)
86                 error("can't allocate buffer\n");
87
88         num_offsets = argc - 2;
89         offsets = calloc(sizeof(offsets[0]), num_offsets);
90         for (i = 0; i < num_offsets; i++) {
91                 offsets[i] = strtoul(argv[i + 2], &end, 10);
92                 if (*end)
93                         error("couldn't parse offset '%s'\n", argv[i + 2]);
94         }
95         qsort(offsets, num_offsets, sizeof(offsets[0]), compare_offsets);
96
97         if (fstat(fd, &st) < 0)
98                 error("stat: %s\n", strerror(errno));
99
100         for (i = 0; pos < st.st_size; i++, pos++) {
101                 if (pos == end_of_buf) {
102                         ret = read(fd, buf, BUFSIZE);
103                         if (ret < 0)
104                                 error("read(): %s\n", strerror(errno));
105                         end_of_buf = pos + ret;
106                         if (end_of_buf > st.st_size)
107                                 error("read %d bytes past file size?\n",
108                                       end_of_buf - st.st_size);
109                         i = 0;
110                 }
111
112                 /* check for 0 when we aren't at a given offset */
113                 if (cur_off >= num_offsets || pos != offsets[cur_off]) {
114                         if (buf[i] != 0)
115                                 error("found char 0x%x at pos %lu instead of 0x0\n",
116                                       buf[i], (long)pos);
117                         continue;
118                 }
119
120                 /* the command line asks us to check for + at this offset */
121                 if (buf[i] != '+')
122                         error("found char 0x%x at pos %lu instead of '.'\n",
123                               buf[i], (long)pos);
124
125                 /* skip over duplicate offset arguments */
126                 while (cur_off < num_offsets && offsets[cur_off] == pos)
127                         cur_off++;
128         }
129         /* don't bother freeing or closing.. */
130         return 0;
131 }