Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / lustre / tests / small_write.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #include <stdio.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <errno.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #define GOTO(label, rc)   do { rc; goto label; } while (0)
45
46 int main (int argc, char **argv) {
47         int fd, i, rc = 0;
48         unsigned long bytes, lbytes;
49         struct stat st;
50         char *str, *str2, *readbuf;
51
52         if (argc != 3) {
53                 fprintf(stderr, "usage: %s <filename> <bytes>\n", argv[0]);
54                 GOTO(out, rc = 1);
55         }
56
57         bytes = strtoul(argv[2], NULL, 10);
58         if (!bytes) {
59                 printf("No bytes!\n");
60                 GOTO(out, rc = 2);
61         }
62         if (bytes % 2) {
63                 printf("Need an even number of bytes!\n");
64                 GOTO(out, rc = 3);
65         }
66         lbytes = 3*bytes/2;
67
68         str = malloc(bytes+1);
69         if (!str) {
70                 printf("No enough memory for %lu bytes.\n", bytes);
71                 GOTO(out, rc = 4);
72         }
73         str2 = malloc(lbytes+1);
74         if (!str2) {
75                 printf("No enough memory for %lu bytes.\n", lbytes);
76                 GOTO(out_str, rc = 5);
77         }
78         readbuf = malloc(bytes*2);
79         if (!readbuf) {
80                 printf("No enough memory for %lu bytes.\n", bytes*2);
81                 GOTO(out_str2, rc = 6);
82         }
83
84         for(i=0; i < bytes; i++)
85                 str[i] = 'a' + (i % 26);
86         str[i] = '\0';
87
88         memcpy(str2, str, bytes);
89         memcpy(str2+(bytes/2), str, bytes);
90         str2[lbytes] = '\0';
91
92         if (bytes < 320)
93                 printf("First  String: %s\nSecond String: %s\n", str, str2);
94
95         fd = open(argv[1], O_CREAT|O_RDWR|O_TRUNC, 0700);
96         if (fd == -1) {
97                 printf("Could not open file %s.\n", argv[1]);
98                 GOTO(out_readbuf, rc = 7);
99         }
100
101         rc = write(fd, str, bytes);
102         if (rc != bytes) {
103                 printf("Write failed!\n");
104                 GOTO(out_fd, rc = 8);
105         }
106
107         sleep(1);
108         rc = fstat(fd, &st);
109         if (rc < 0 || st.st_size != bytes) {
110                 printf("bad file %lu size first write %lu != %lu: rc %d\n",
111                        (unsigned long)st.st_ino, (unsigned long)st.st_size,
112                        bytes, rc);
113                 GOTO(out_fd, rc = 9);
114         }
115
116         rc = lseek(fd, bytes / 2, SEEK_SET);
117         if (rc != bytes / 2) {
118                 printf("Seek failed!\n");
119                 GOTO(out_fd, rc = 10);
120         }
121
122         rc = write(fd, str, bytes);
123         if (rc != bytes) {
124                 printf("Write failed!\n");
125                 GOTO(out_fd, rc = 11);
126         }
127
128         rc = fstat(fd, &st);
129         if (rc < 0 || st.st_size != bytes + bytes / 2) {
130                 printf("bad file %lu size second write %lu != %lu: rc %d\n",
131                        (unsigned long)st.st_ino, (unsigned long)st.st_size,
132                        bytes, rc);
133                 GOTO(out_fd, rc = 12);
134         }
135
136         rc = lseek(fd, 0, SEEK_SET);
137         if (rc != 0) {
138                 printf("Seek failed!\n");
139                 GOTO(out_fd, rc = 13);
140         }
141
142         rc = read(fd, readbuf, bytes * 2);
143         if (rc != lbytes) {
144                 printf("Read %d bytes instead of %lu.\n", rc, lbytes);
145                 if (rc == -1)
146                         perror("");
147                 else
148                         printf("%s\n%s\n", readbuf, str2);
149                 rc = fstat(fd, &st);
150                 if (rc < 0 || st.st_size != bytes + bytes / 2) {
151                         printf("bad file size after read %lu != %lu: rc %d\n",
152                                (unsigned long)st.st_size, bytes + bytes / 2,
153                                rc);
154                         GOTO(out_fd, rc = 14);
155                 }
156
157                 GOTO(out_fd, rc = 15);
158         }
159         rc = 0;
160
161         if (bytes < 320)
162                 printf("%s\n%s\n", readbuf, str2);
163         if (strcmp(readbuf, str2)) {
164                 printf("No match!\n");
165                 GOTO(out_fd, rc = 16);
166         }
167
168         printf("Pass!\n");
169 out_fd:
170         close(fd);
171 out_readbuf:
172         free(readbuf);
173 out_str2:
174         free(str2);
175 out_str:
176         free(str);
177 out:
178         return rc;
179 }