]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - mkfile/xfs_mkfile.c
2 * Copyright (c) 2000-2003 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.
40 #define MAXBUFFERSIZE (256 * 1024)
42 static char *progname
;
47 fprintf(stderr
, _("%s: [-npv] <size> <name1> [<name2>] ...\n"),
53 openfd(char *name
, int oflags
)
57 fd
= open(name
, oflags
, 0600);
63 if (!platform_test_xfs_fd(fd
)) {
64 fprintf(stderr
, _("%s: "
65 "file [\"%s\"] is not on an XFS filesystem\n"),
73 main(int argc
, char **argv
)
91 int buflen
= 0, nbuflen
;
92 int bufalign
= 0, nbufalign
, bufmin
;
96 progname
= basename(argv
[0]);
97 setlocale(LC_ALL
, "");
98 bindtextdomain(PACKAGE
, LOCALEDIR
);
101 while ((c
= getopt(argc
, argv
, "npvV")) != EOF
) {
113 printf(_("%s version %s\n"), progname
, VERSION
);
121 if (argc
< optind
+ 2 || errflg
)
126 len
= strlen(argv
[optind
]);
128 if (isalpha((int)argv
[optind
][len
-1])) {
129 switch ((int)argv
[optind
][len
-1]) {
150 fprintf(stderr
, _("unknown size %s\n"), argv
[optind
]);
154 argv
[optind
][len
-1] = '\0';
157 size
= strtoll(argv
[optind
], NULL
, 10) * mult
;
161 while (optind
< argc
) {
163 fprintf(stdout
, _("%s %lld bytes %s\n"),
164 argv
[optind
], (long long)size
,
166 ? _("(pre-allocated)")
169 oflags
= O_CREAT
|O_TRUNC
|O_WRONLY
|(nobytes
? 0 : O_DIRECT
);
170 fname
= argv
[optind
];
172 if ((fd
= openfd(fname
, oflags
)) == -1) {
178 if ( (oflags
& O_DIRECT
)
179 && ( (fd
< 0 && errno
== EINVAL
)
180 || xfsctl(fname
, fd
, XFS_IOC_DIOINFO
, &da
) < 0)) {
186 if ((fd
= openfd(argv
[optind
], oflags
)) == -1) {
199 if ((result
= lseek64(fd
, size
- 1, SEEK_SET
)) < 0LL) {
200 fprintf(stderr
, _("lseek64 error, result = %lld\n"),
203 perror(argv
[optind
]);
205 } else if (nobytes
) {
206 if (write(fd
, "", 1) < 0) {
207 perror(argv
[optind
]);
211 flck
.l_whence
= SEEK_SET
;
215 xfsctl(fname
, fd
, XFS_IOC_RESVSP64
, &flck
);
216 if (oflags
& O_DIRECT
) {
217 nbufalign
= da
.d_mem
;
219 if ( da
.d_miniosz
<= MAXBUFFERSIZE
220 && MAXBUFFERSIZE
<= da
.d_maxiosz
)
221 nbuflen
= MAXBUFFERSIZE
;
222 else if (da
.d_maxiosz
< MAXBUFFERSIZE
)
223 nbuflen
= da
.d_maxiosz
;
225 nbuflen
= da
.d_miniosz
;
227 bufmin
= da
.d_miniosz
;
229 nbuflen
= MAXBUFFERSIZE
;
230 nbufalign
= sizeof(long);
234 if (nbuflen
> buflen
|| nbufalign
> bufalign
) {
237 buf
= memalign(nbufalign
, nbuflen
);
240 nbufalign
= bufalign
;
245 lseek64(fd
, 0LL, SEEK_SET
);
247 while (wrote
< size
) {
248 if (size
- wrote
>= buflen
)
251 bytes
= roundup(size
- wrote
, bufmin
);
253 bytes
= size
- wrote
;
255 len
= write(fd
, buf
, bytes
);
258 perror(argv
[optind
]);
259 unlink(argv
[optind
]);
267 if (wrote
> size
&& ftruncate64(fd
, size
) < 0) {
268 perror(argv
[optind
]);
269 unlink(argv
[optind
]);
274 if ( close(fd
) < 0 ) {
275 perror(argv
[optind
]);
276 unlink(argv
[optind
]);