Whamcloud - gitweb
LU-1538 tests: standardize test script init – failover
[fs/lustre-release.git] / lustre / tests / ll_sparseness_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  * Lustre is a trademark of Sun Microsystems, Inc.
29  */
30
31 #ifndef _GNU_SOURCE
32 #define _GNU_SOURCE
33 #endif
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <sys/wait.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43
44 #define BUFSIZE (1024 * 1024)
45
46 /*
47  * Function: pwrite character '+' to <filename> at <offset> (man pwrite)
48  * Return:   0 success
49  *           1 failure
50  */
51 int main(int argc, char**argv)
52 {
53         int p_size;
54         unsigned int offset;
55         char *filename;
56         int fd;
57         char buf[] = "+++";
58         char *end;
59
60         if (argc != 3) {
61                 fprintf(stderr, "Usage: %s <filename> <offset>(KB)\n", argv[0]);
62                 exit(1);
63         }
64
65         filename = argv[1];
66         offset = strtoul(argv[2], &end, 10);
67         if (*end) {
68                 fprintf(stderr, "<offset> parameter should be integer\n");
69                 exit(1);
70         }
71
72         fd = open(filename, O_CREAT|O_RDWR, 0644);
73         if (fd == -1) {
74                 fprintf(stderr, "Opening %s fails (%s)\n",
75                         filename, strerror(errno));
76                 return 1;
77         }
78
79         /* write the character '+' at offset */
80         p_size = pwrite(fd, buf, 1, offset);
81         if (p_size != 1) {
82                 fprintf(stderr, "pwrite %s fails (%s)\n",
83                         filename, strerror(errno));
84                 close(fd);
85                 return 1;
86         }
87         close(fd);
88         return 0;
89 }