Whamcloud - gitweb
LU-14475 log: Rewrite some log messages
[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.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
30 #include <stdio.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #define GOTO(label, rc)   do { rc; goto label; } while (0)
40
41 int main (int argc, char **argv) {
42         int fd, i, rc = 0;
43         unsigned long bytes, lbytes;
44         struct stat st;
45         char *str, *str2, *readbuf;
46
47         if (argc != 3) {
48                 fprintf(stderr, "usage: %s <filename> <bytes>\n", argv[0]);
49                 GOTO(out, rc = 1);
50         }
51
52         bytes = strtoul(argv[2], NULL, 10);
53         if (!bytes) {
54                 printf("No bytes!\n");
55                 GOTO(out, rc = 2);
56         }
57         if (bytes % 2) {
58                 printf("Need an even number of bytes!\n");
59                 GOTO(out, rc = 3);
60         }
61         lbytes = 3*bytes/2;
62
63         str = malloc(bytes+1);
64         if (!str) {
65                 printf("No enough memory for %lu bytes.\n", bytes);
66                 GOTO(out, rc = 4);
67         }
68         str2 = malloc(lbytes+1);
69         if (!str2) {
70                 printf("No enough memory for %lu bytes.\n", lbytes);
71                 GOTO(out_str, rc = 5);
72         }
73         readbuf = malloc(bytes*2);
74         if (!readbuf) {
75                 printf("No enough memory for %lu bytes.\n", bytes*2);
76                 GOTO(out_str2, rc = 6);
77         }
78
79         for(i=0; i < bytes; i++)
80                 str[i] = 'a' + (i % 26);
81         str[i] = '\0';
82
83         memcpy(str2, str, bytes);
84         memcpy(str2+(bytes/2), str, bytes);
85         str2[lbytes] = '\0';
86
87         if (bytes < 320)
88                 printf("First  String: %s\nSecond String: %s\n", str, str2);
89
90         fd = open(argv[1], O_CREAT|O_RDWR|O_TRUNC, 0700);
91         if (fd == -1) {
92                 printf("Could not open file %s.\n", argv[1]);
93                 GOTO(out_readbuf, rc = 7);
94         }
95
96         rc = write(fd, str, bytes);
97         if (rc != bytes) {
98                 printf("Write failed!\n");
99                 GOTO(out_fd, rc = 8);
100         }
101
102         sleep(1);
103         rc = fstat(fd, &st);
104         if (rc < 0 || st.st_size != bytes) {
105                 printf("bad file %lu size first write %lu != %lu: rc %d\n",
106                        (unsigned long)st.st_ino, (unsigned long)st.st_size,
107                        bytes, rc);
108                 GOTO(out_fd, rc = 9);
109         }
110
111         rc = lseek(fd, bytes / 2, SEEK_SET);
112         if (rc != bytes / 2) {
113                 printf("Seek failed!\n");
114                 GOTO(out_fd, rc = 10);
115         }
116
117         rc = write(fd, str, bytes);
118         if (rc != bytes) {
119                 printf("Write failed!\n");
120                 GOTO(out_fd, rc = 11);
121         }
122
123         rc = fstat(fd, &st);
124         if (rc < 0 || st.st_size != bytes + bytes / 2) {
125                 printf("bad file %lu size second write %lu != %lu: rc %d\n",
126                        (unsigned long)st.st_ino, (unsigned long)st.st_size,
127                        bytes, rc);
128                 GOTO(out_fd, rc = 12);
129         }
130
131         rc = lseek(fd, 0, SEEK_SET);
132         if (rc != 0) {
133                 printf("Seek failed!\n");
134                 GOTO(out_fd, rc = 13);
135         }
136
137         rc = read(fd, readbuf, bytes * 2);
138         if (rc != lbytes) {
139                 printf("Read %d bytes instead of %lu.\n", rc, lbytes);
140                 if (rc == -1)
141                         perror("");
142                 else
143                         printf("%s\n%s\n", readbuf, str2);
144                 rc = fstat(fd, &st);
145                 if (rc < 0 || st.st_size != bytes + bytes / 2) {
146                         printf("bad file size after read %lu != %lu: rc %d\n",
147                                (unsigned long)st.st_size, bytes + bytes / 2,
148                                rc);
149                         GOTO(out_fd, rc = 14);
150                 }
151
152                 GOTO(out_fd, rc = 15);
153         }
154         rc = 0;
155
156         if (bytes < 320)
157                 printf("%s\n%s\n", readbuf, str2);
158         if (strcmp(readbuf, str2)) {
159                 printf("No match!\n");
160                 GOTO(out_fd, rc = 16);
161         }
162
163         printf("Pass!\n");
164 out_fd:
165         close(fd);
166 out_readbuf:
167         free(readbuf);
168 out_str2:
169         free(str2);
170 out_str:
171         free(str);
172 out:
173         return rc;
174 }