Whamcloud - gitweb
b=3031
[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                        (unsigned long)st.st_ino, (unsigned long)st.st_size,
76                        bytes, rc);
77                 return 1;
78         }
79
80         rc = lseek(fd, bytes / 2, SEEK_SET);
81         if (rc != bytes / 2) {
82                 printf("Seek failed!\n");
83                 return 1;
84         }
85
86         rc = write(fd, str, bytes);
87         if (rc != bytes) {
88                 printf("Write failed!\n");
89                 return 1;
90         }
91
92         rc = fstat(fd, &st);
93         if (rc < 0 || st.st_size != bytes + bytes / 2) {
94                 printf("bad file %lu size second write %lu != %lu: rc %d\n",
95                        (unsigned long)st.st_ino, (unsigned long)st.st_size,
96                        bytes, rc);
97                 return 1;
98         }
99
100         rc = lseek(fd, 0, SEEK_SET);
101         if (rc != 0) {
102                 printf("Seek failed!\n");
103                 return 1;
104         }
105
106         rc = read(fd, readbuf, bytes * 2);
107         if (rc != lbytes) {
108                 printf("Read %d bytes instead of %lu.\n", rc, lbytes);
109                 if (rc == -1)
110                         perror("");
111                 else
112                         printf("%s\n%s\n", readbuf, str2);
113                 rc = fstat(fd, &st);
114                 if (rc < 0 || st.st_size != bytes + bytes / 2) {
115                         printf("bad file size after read %lu != %lu: rc %d\n",
116                                (unsigned long)st.st_size, bytes + bytes / 2,
117                                rc);
118                         return 1;
119                 }
120
121                 return 1;
122         }
123
124         fd = close(fd);
125         if (fd == -1)
126                 return 1;
127
128         if (bytes < 320)
129                 printf("%s\n%s\n", readbuf, str2);
130         if (strcmp(readbuf, str2)) {
131                 printf("No match!\n");
132                 return 1;
133         }
134
135         printf("Pass!\n");
136         return 0;
137 }