Whamcloud - gitweb
b=15967
[fs/lustre-release.git] / lustre / tests / ll_sparseness_write.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #ifndef _GNU_SOURCE
5 #define _GNU_SOURCE
6 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16
17 #define BUFSIZE (1024*1024)   
18
19 /* Function: pwrite character '+' to <filename> at <offset> (man pwrite)
20  * Return:   0 success
21  *           1 failure */
22 int main(int argc, char**argv)
23 {
24         int p_size;
25         unsigned int offset;
26         char *filename;
27         int fd;
28         char buf[] = "+++";
29         char *end;
30
31         if(argc != 3) {
32                 fprintf(stderr, "Usage: %s <filename> <offset>(KB)\n", argv[0]);
33                 exit(1);
34         }
35
36         filename = argv[1];
37         offset = strtoul(argv[2], &end, 10);
38         if (*end) {
39                 fprintf(stderr, "<offset> parameter should be integer\n");
40                 exit(1);
41         }
42
43         fd = open(filename, O_CREAT|O_RDWR, 0644);
44         if (fd == -1) {
45                 fprintf(stderr, "Opening %s fails (%s)\n", 
46                         filename, strerror(errno));
47                 return 1;
48         }
49
50         /* write the character '+' at offset */
51         p_size = pwrite(fd, buf, 1, offset);
52         if (p_size != 1) {
53                 fprintf(stderr, "pwrite %s fails (%s)\n", 
54                         filename, strerror(errno));
55                 close(fd);
56                 return 1;
57         }
58                 
59         close(fd);
60         return 0;
61 }