Whamcloud - gitweb
LU-14475 log: Rewrite some log messages
[fs/lustre-release.git] / lustre / tests / writeme.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) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdbool.h>
38
39 void usage(char *prog)
40 {
41         printf("usage: %s [-s] [-b <bytes>] filename\n", prog);
42         exit(1);
43 }
44
45 int main(int argc, char **argv)
46 {
47         bool limit_write = false, do_sync = false;
48         int c, per_write, fd, rc;
49         unsigned long bytes = 0;
50         char buf[4096];
51         char *endptr = NULL;
52
53         while ((c = getopt(argc, argv, "sb:")) != -1) {
54                 switch (c) {
55                 case 's':
56                         do_sync = true;
57                         break;
58                 case 'b':
59                         limit_write = true;
60                         bytes = strtoul(optarg, &endptr, 10);
61                         if (endptr != NULL && *endptr != '\0')
62                                 usage(argv[0]);
63                         break;
64                 default:
65                         usage(argv[0]);
66                         break;
67                 }
68         }
69
70         if (argc - optind != 1)
71                 usage(argv[0]);
72
73         memset(buf, 0, 4096);
74         fd = open(argv[optind], O_RDWR | O_CREAT, 0600);
75         if (fd == -1) {
76                 printf("Error opening %s\n", argv[1]);
77                 exit(1);
78         }
79
80         /* Even 0 bytes, write at least once */
81         if (limit_write) {
82                 do {
83                         per_write = bytes > 4096 ? 4096 : bytes;
84                         rc = write(fd, buf, per_write);
85                         if (rc > 0)
86                                 bytes -= rc;
87                         else if (rc < 0)
88                                 break;
89                 } while (bytes > 0);
90
91                 return rc >= 0 ? 0 : rc;
92         }
93
94         for (rc = 0; ;) {
95                 sprintf(buf, "write %d\n", rc);
96                 rc = write(fd, buf, sizeof(buf));
97                 if (do_sync)
98                         sync();
99                 sleep(1);
100         }
101
102         return 0;
103 }