Whamcloud - gitweb
corrected an error made in setepall in test-framework.sh, which affects
[fs/lustre-release.git] / lustre / tests / small_write.c
1 #include <stdio.h>
2 #include <sys/stat.h>
3 #include <sys/types.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #define GOTO(label, rc)   do { rc; goto label; } while (0)
11
12 int main (int argc, char **argv) {
13         int fd, i, rc = 0;
14         unsigned long bytes, lbytes;
15         struct stat st;
16         char *str, *str2, *readbuf;
17
18         if (argc != 3) {
19                 fprintf(stderr, "usage: %s <filename> <bytes>\n", argv[0]);
20                 GOTO(out, rc = 1);
21         }
22
23         bytes = strtoul(argv[2], NULL, 10);
24         if (!bytes) {
25                 printf("No bytes!\n");
26                 GOTO(out, rc = 2);
27         }
28         if (bytes % 2) {
29                 printf("Need an even number of bytes!\n");
30                 GOTO(out, rc = 3);
31         }
32         lbytes = 3*bytes/2;
33
34         str = malloc(bytes+1);
35         if (!str) {
36                 printf("No enough memory for %lu bytes.\n", bytes);
37                 GOTO(out, rc = 4);
38         }
39         str2 = malloc(lbytes+1);
40         if (!str2) {
41                 printf("No enough memory for %lu bytes.\n", lbytes);
42                 GOTO(out_str, rc = 5);
43         }
44         readbuf = malloc(bytes*2);
45         if (!readbuf) {
46                 printf("No enough memory for %lu bytes.\n", bytes*2);
47                 GOTO(out_str2, rc = 6);
48         }
49
50         for(i=0; i < bytes; i++)
51                 str[i] = 'a' + (i % 26);
52         str[i] = '\0';
53
54         memcpy(str2, str, bytes);
55         memcpy(str2+(bytes/2), str, bytes);
56         str2[lbytes] = '\0';
57
58         if (bytes < 320)
59                 printf("First  String: %s\nSecond String: %s\n", str, str2);
60
61         fd = open(argv[1], O_CREAT|O_RDWR|O_TRUNC, 0700);
62         if (fd == -1) {
63                 printf("Could not open file %s.\n", argv[1]);
64                 GOTO(out_readbuf, rc = 7);
65         }
66
67         rc = write(fd, str, bytes);
68         if (rc != bytes) {
69                 printf("Write failed!\n");
70                 GOTO(out_fd, rc = 8);
71         }
72
73         sleep(1);
74         rc = fstat(fd, &st);
75         if (rc < 0 || st.st_size != bytes) {
76                 printf("bad file %lu size first write %lu != %lu: rc %d\n",
77                        (unsigned long)st.st_ino, (unsigned long)st.st_size,
78                        bytes, rc);
79                 GOTO(out_fd, rc = 9);
80         }
81
82         rc = lseek(fd, bytes / 2, SEEK_SET);
83         if (rc != bytes / 2) {
84                 printf("Seek failed!\n");
85                 GOTO(out_fd, rc = 10);
86         }
87
88         rc = write(fd, str, bytes);
89         if (rc != bytes) {
90                 printf("Write failed!\n");
91                 GOTO(out_fd, rc = 11);
92         }
93
94         rc = fstat(fd, &st);
95         if (rc < 0 || st.st_size != bytes + bytes / 2) {
96                 printf("bad file %lu size second write %lu != %lu: rc %d\n",
97                        (unsigned long)st.st_ino, (unsigned long)st.st_size,
98                        bytes, rc);
99                 GOTO(out_fd, rc = 12);
100         }
101
102         rc = lseek(fd, 0, SEEK_SET);
103         if (rc != 0) {
104                 printf("Seek failed!\n");
105                 GOTO(out_fd, rc = 13);
106         }
107
108         rc = read(fd, readbuf, bytes * 2);
109         if (rc != lbytes) {
110                 printf("Read %d bytes instead of %lu.\n", rc, lbytes);
111                 if (rc == -1)
112                         perror("");
113                 else
114                         printf("%s\n%s\n", readbuf, str2);
115                 rc = fstat(fd, &st);
116                 if (rc < 0 || st.st_size != bytes + bytes / 2) {
117                         printf("bad file size after read %lu != %lu: rc %d\n",
118                                (unsigned long)st.st_size, bytes + bytes / 2,
119                                rc);
120                         GOTO(out_fd, rc = 14);
121                 }
122
123                 GOTO(out_fd, rc = 15);
124         }
125         rc = 0;
126
127         if (bytes < 320)
128                 printf("%s\n%s\n", readbuf, str2);
129         if (strcmp(readbuf, str2)) {
130                 printf("No match!\n");
131                 GOTO(out_fd, rc = 16);
132         }
133
134         printf("Pass!\n");
135 out_fd:
136         close(fd);
137 out_readbuf:
138         free(readbuf);
139 out_str2:
140         free(str2);
141 out_str:
142         free(str);
143 out:
144         return rc;
145 }