]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - mkfile/xfs_mkfile.c
2 * Copyright (c) 2000 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/
34 * Make file utility for xfs.
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
47 #define MAXBUFFERSIZE (256 * 1024)
49 static void usage(void);
50 static char *progname
;
53 main(int argc
, char **argv
)
70 int buflen
= 0, nbuflen
;
71 int bufalign
= 0, nbufalign
, bufmin
;
75 progname
= basename(argv
[0]);
76 while ((c
= getopt(argc
, argv
, "npvV")) != EOF
) {
88 printf("%s version %s\n", progname
, VERSION
);
96 if (argc
< optind
+ 2 || errflg
)
101 len
= strlen(argv
[optind
]);
103 if (isalpha(argv
[optind
][len
-1])) {
104 switch (argv
[optind
][len
-1]) {
125 fprintf(stderr
, "unknown size %s\n", argv
[optind
]);
129 argv
[optind
][len
-1] = '\0';
132 size
= atoll(argv
[optind
]) * mult
;
136 while (optind
< argc
) {
138 fprintf(stdout
, "%s %lld bytes %s\n",
139 argv
[optind
], (long long)size
,
144 oflags
= O_CREAT
|O_TRUNC
|O_WRONLY
|(nobytes
? 0 : O_DIRECT
);
146 fd
= open(argv
[optind
], oflags
, 0600);
148 if ( (oflags
& O_DIRECT
)
149 && ( (fd
< 0 && errno
== EINVAL
)
150 || ioctl(fd
, XFS_IOC_DIOINFO
, &da
) < 0)) {
156 fd
= open(argv
[optind
], oflags
, 0600);
160 perror(argv
[optind
]);
172 if ((result
= lseek64(fd
, size
- 1, SEEK_SET
)) < 0LL) {
174 * This check doesn't actually work for 6.2
175 * efs and nfs2, although it should.
177 fprintf(stderr
, "lseek64 error, result = %lld\n",
180 perror(argv
[optind
]);
182 } else if (nobytes
) {
183 if (write(fd
, "", 1) < 0) {
184 perror(argv
[optind
]);
188 flck
.l_whence
= SEEK_SET
;
192 (void)ioctl(fd
, XFS_IOC_RESVSP64
, &flck
);
195 if ( close(fd
) < 0 ) {
196 perror(argv
[optind
]);
197 unlink(argv
[optind
]);
206 if (oflags
& O_DIRECT
) {
207 nbufalign
= da
.d_mem
;
209 if ( da
.d_miniosz
<= MAXBUFFERSIZE
210 && MAXBUFFERSIZE
<= da
.d_maxiosz
)
211 nbuflen
= MAXBUFFERSIZE
;
212 else if (da
.d_maxiosz
< MAXBUFFERSIZE
)
213 nbuflen
= da
.d_maxiosz
;
215 nbuflen
= da
.d_miniosz
;
217 bufmin
= da
.d_miniosz
;
219 nbuflen
= MAXBUFFERSIZE
;
220 nbufalign
= sizeof(long);
224 if (nbuflen
> buflen
|| nbufalign
> bufalign
) {
227 buf
= memalign(nbufalign
, nbuflen
);
230 nbufalign
= bufalign
;
235 lseek64(fd
, 0LL, SEEK_SET
);
237 while (wrote
< size
) {
238 if (size
- wrote
>= buflen
)
241 bytes
= roundup(size
- wrote
, bufmin
);
243 bytes
= size
- wrote
;
245 len
= write(fd
, buf
, bytes
);
248 perror(argv
[optind
]);
249 unlink(argv
[optind
]);
257 if (wrote
> size
&& ftruncate64(fd
, size
) < 0) {
258 perror(argv
[optind
]);
259 unlink(argv
[optind
]);
264 if ( close(fd
) < 0 ) {
265 perror(argv
[optind
]);
266 unlink(argv
[optind
]);
279 fprintf(stderr
, "%s: [-npv] <size> <name1> [<name2>] ...\n", progname
);