]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/pwrite.c
2 * Copyright (c) 2003-2005 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include <xfs/libxfs.h>
39 static cmdinfo_t pwrite_cmd
;
46 " writes a range of bytes (in block size increments) from the given offset\n"
49 " 'pwrite 512 20' - writes 20 bytes at 512 bytes into the open file\n"
51 " Writes into a segment of the currently open file, using either a buffer\n"
52 " filled with a set pattern (0xcdcdcdcd) or data read from an input file.\n"
53 " -S -- use an alternate seed number\n"
54 " -i -- specifies an input file from which to source data to write\n"
55 " -d -- open the input file for direct IO\n"
56 " -s -- skip a number of bytes at the start of the input file\n"
57 " -w -- call fdatasync(2) at the end (included in timing results)\n"
58 " -W -- call fsync(2) at the end (included in timing results)\n"
59 " The writes are performed in sequential blocks starting at offset, with the\n"
60 " blocksize tunable using the -b option (default blocksize is 4096 bytes).\n"
73 ssize_t bytes
, bytes_requested
;
74 long long bar
= min(bs
, count
);
79 if (fd
> 0) { /* input file given, read buffer first */
80 if (read_buffer(fd
, skip
+ *total
, bs
, &bar
, 0, 1) < 0)
83 bytes_requested
= min(bar
, count
);
84 bytes
= pwrite64(file
->fd
, buffer
, bytes_requested
, offset
);
93 if (bytes
< bytes_requested
)
106 off64_t offset
, skip
= 0;
107 long long count
, total
;
108 unsigned int seed
= 0xcdcdcdcd;
109 int blocksize
, sectsize
;
110 struct timeval t1
, t2
;
111 char s1
[64], s2
[64], ts
[64];
112 char *sp
, *infile
= NULL
;
113 int Cflag
, uflag
, dflag
, wflag
, Wflag
;
116 Cflag
= uflag
= dflag
= wflag
= Wflag
= 0;
117 init_cvtnum(&blocksize
, §size
);
118 while ((c
= getopt(argc
, argv
, "b:Cdf:i:s:S:uwW")) != EOF
) {
121 blocksize
= cvtnum(blocksize
, sectsize
, optarg
);
123 printf(_("non-numeric bsize -- %s\n"), optarg
);
138 skip
= cvtnum(blocksize
, sectsize
, optarg
);
140 printf(_("non-numeric skip -- %s\n"), optarg
);
145 seed
= strtoul(optarg
, &sp
, 0);
146 if (!sp
|| sp
== optarg
) {
147 printf(_("non-numeric seed -- %s\n"), optarg
);
161 return command_usage(&pwrite_cmd
);
164 if ( ((skip
|| dflag
) && !infile
) || (optind
!= argc
- 2))
165 return command_usage(&pwrite_cmd
);
166 offset
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
168 printf(_("non-numeric offset argument -- %s\n"), argv
[optind
]);
172 count
= cvtnum(blocksize
, sectsize
, argv
[optind
]);
174 printf(_("non-numeric length argument -- %s\n"), argv
[optind
]);
178 if (alloc_buffer(blocksize
, uflag
, seed
) < 0)
181 c
= IO_READONLY
| (dflag
? IO_DIRECT
: 0);
182 if (infile
&& ((fd
= openfile(infile
, NULL
, c
, 0)) < 0))
185 gettimeofday(&t1
, NULL
);
186 c
= write_buffer(offset
, count
, blocksize
, fd
, skip
, &total
);
195 gettimeofday(&t2
, NULL
);
198 /* Finally, report back -- -C gives a parsable format */
199 timestr(&t2
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
201 cvtstr((double)total
, s1
, sizeof(s1
));
202 cvtstr(tdiv((double)total
, t2
), s2
, sizeof(s2
));
203 printf(_("wrote %lld/%lld bytes at offset %lld\n"),
204 total
, count
, (long long)offset
);
205 printf(_("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n"),
206 s1
, c
, ts
, s2
, tdiv((double)c
, t2
));
207 } else {/* bytes,ops,time,bytes/sec,ops/sec */
208 printf("%lld,%d,%s,%.3f,%.3f\n",
210 tdiv((double)total
, t2
), tdiv((double)c
, t2
));
219 pwrite_cmd
.name
= _("pwrite");
220 pwrite_cmd
.altname
= _("w");
221 pwrite_cmd
.cfunc
= pwrite_f
;
222 pwrite_cmd
.argmin
= 2;
223 pwrite_cmd
.argmax
= -1;
224 pwrite_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
226 _("[-i infile [-d] [-s skip]] [-b bs] [-S seed] [-wW] off len");
228 _("writes a number of bytes at a specified offset");
229 pwrite_cmd
.help
= pwrite_help
;
231 add_command(&pwrite_cmd
);