Whamcloud - gitweb
merge b_devel into HEAD, which will become 0.7.3
[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 int main (int argc, char **argv) {
11         int fd, i, rc;
12         unsigned long bytes, lbytes;
13         struct stat st;
14         char *str, *str2, *readbuf;
15
16         if (argc != 3) {
17                 fprintf(stderr, "usage: %s <filename> <bytes>\n", argv[0]);
18                 return 1;
19         }
20
21         bytes = strtoul(argv[2], NULL, 10);
22         if (!bytes) {
23                 printf("No bytes!\n");
24                 return 1;
25         }
26         if (bytes % 2) {
27                 printf("Need an even number of bytes!\n");
28                 return 1;
29         }
30         lbytes = 3*bytes/2;
31
32         str = malloc(bytes+1);
33         if (!str) {
34                 printf("No enough memory for %lu bytes.\n", bytes);
35                 return 1;
36         }
37         str2 = malloc(lbytes+1);
38         if (!str) {
39                 printf("No enough memory for %lu bytes.\n", lbytes);
40                 return 1;
41         }
42         readbuf = malloc(bytes*2);
43         if (!str) {
44                 printf("No enough memory for %lu bytes.\n", bytes*2);
45                 return 1;
46         }
47
48         for(i=0; i < bytes; i++)
49                 str[i] = 'a' + (i % 26);
50         str[i] = '\0';
51
52         memcpy(str2, str, bytes);
53         memcpy(str2+(bytes/2), str, bytes);
54         str2[lbytes] = '\0';
55
56         if (bytes < 320)
57                 printf("First  String: %s\nSecond String: %s\n", str, str2);
58
59         fd = open(argv[1], O_CREAT|O_RDWR|O_TRUNC, 0700);
60         if (fd == -1) {
61                 printf("Could not open file %s.\n", argv[1]);
62                 return 1;
63         }
64
65         rc = write(fd, str, bytes);
66         if (rc != bytes) {
67                 printf("Write failed!\n");
68                 return 1;
69         }
70
71         sleep(1);
72         rc = fstat(fd, &st);
73         if (rc < 0 || st.st_size != bytes) {
74                 printf("bad file %lu size first write %lu != %lu: rc %d\n",
75                        st.st_ino, st.st_size, bytes, rc);
76                 return 1;
77         }
78
79         rc = lseek(fd, bytes / 2, SEEK_SET);
80         if (rc != bytes / 2) {
81                 printf("Seek failed!\n");
82                 return 1;
83         }
84
85         rc = write(fd, str, bytes);
86         if (rc != bytes) {
87                 printf("Write failed!\n");
88                 return 1;
89         }
90
91         rc = fstat(fd, &st);
92         if (rc < 0 || st.st_size != bytes + bytes / 2) {
93                 printf("bad file %lu size second write %lu != %lu: rc %d\n",
94                        st.st_ino, st.st_size, bytes, rc);
95                 return 1;
96         }
97
98         rc = lseek(fd, 0, SEEK_SET);
99         if (rc != 0) {
100                 printf("Seek failed!\n");
101                 return 1;
102         }
103
104         rc = read(fd, readbuf, bytes * 2);
105         if (rc != lbytes) {
106                 printf("Read %d bytes instead of %lu.\n", rc, lbytes);
107                 if (rc == -1)
108                         perror("");
109                 else
110                         printf("%s\n%s\n", readbuf, str2);
111                 rc = fstat(fd, &st);
112                 if (rc < 0 || st.st_size != bytes + bytes / 2) {
113                         printf("bad file size after read %lu != %lu: rc %d\n",
114                                st.st_size, bytes + bytes / 2, rc);
115                         return 1;
116                 }
117
118                 return 1;
119         }
120
121         fd = close(fd);
122         if (fd == -1)
123                 return 1;
124
125         if (bytes < 320)
126                 printf("%s\n%s\n", readbuf, str2);
127         if (strcmp(readbuf, str2)) {
128                 printf("No match!\n");
129                 return 1;
130         }
131
132         printf("Pass!\n");
133         return 0;
134 }