Whamcloud - gitweb
LU-6142 tests: Fix style issues for ll_sparseness_verify.c
[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  * Lustre is a trademark of Sun Microsystems, Inc.
29  *
30  * lustre/tests/ll_sparseness_verify.c
31  *
32  * The companion to ll_sparseness_write; walk all the bytes in the file.
33  * the bytes at the offsets specified on the command line must be '+', as
34  * previously written by ll_sparseness_write.  All other bytes must be 0.
35  */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <stdarg.h>
47
48 #define BUFSIZE (1024 * 1024)
49
50 void error(char *fmt, ...)
51 {
52         va_list ap;
53
54         va_start(ap, fmt);
55         vfprintf(stderr, fmt, ap);
56         va_end(ap);
57         exit(1);
58 }
59
60 int compare_offsets(const void *a, const void *b)
61 {
62         off_t *A = (off_t *)a;
63         off_t *B = (off_t *)b;
64
65         return *A - *B;
66 }
67
68 int main(int argc, char **argv)
69 {
70         unsigned int num_offsets, cur_off = 0, i;
71         off_t *offsets, pos = 0, end_of_buf = 0;
72         char *end, *buf;
73         struct stat st;
74         ssize_t ret;
75         int fd;
76
77         if (argc < 3)
78                 error("Usage: %s <filename> <offset> [ offset ... ]\n",
79                       argv[0]);
80
81         fd = open(argv[1], O_RDONLY);
82         if (fd < 0)
83                 error("couldn't open %s: %s\n", argv[1], strerror(errno));
84
85         buf = malloc(BUFSIZE);
86         if (!buf)
87                 error("can't allocate buffer\n");
88
89         num_offsets = argc - 2;
90         offsets = calloc(sizeof(offsets[0]), num_offsets);
91         for (i = 0; i < num_offsets; i++) {
92                 offsets[i] = strtoul(argv[i + 2], &end, 10);
93                 if (*end)
94                         error("couldn't parse offset '%s'\n", argv[i + 2]);
95         }
96         qsort(offsets, num_offsets, sizeof(offsets[0]), compare_offsets);
97
98         if (fstat(fd, &st) < 0)
99                 error("stat: %s\n", strerror(errno));
100
101         for (i = 0; pos < st.st_size; i++, pos++) {
102                 if (pos == end_of_buf) {
103                         ret = read(fd, buf, BUFSIZE);
104                         if (ret < 0)
105                                 error("read(): %s\n", strerror(errno));
106                         end_of_buf = pos + ret;
107                         if (end_of_buf > st.st_size)
108                                 error("read %d bytes past file size?\n",
109                                       end_of_buf - st.st_size);
110                         i = 0;
111                 }
112
113                 /* check for 0 when we aren't at a given offset */
114                 if (cur_off >= num_offsets || pos != offsets[cur_off]) {
115                         if (buf[i] != 0)
116                                 error("found char 0x%x at pos %lu instead of 0x0\n",
117                                       buf[i], (long)pos);
118                         continue;
119                 }
120
121                 /* the command line asks us to check for + at this offset */
122                 if (buf[i] != '+')
123                         error("found char 0x%x at pos %lu instead of '.'\n",
124                               buf[i], (long)pos);
125
126                 /* skip over duplicate offset arguments */
127                 while (cur_off < num_offsets && offsets[cur_off] == pos)
128                         cur_off++;
129         }
130         /* don't bother freeing or closing.. */
131         return 0;
132 }