1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <fcntl.h>
18#include <libgen.h>
19#include <stdio.h>
20#include <unistd.h>
21#include <stdlib.h>
22
23#if defined(__linux__)
24#include <linux/fs.h>
25#elif defined(__APPLE__) && defined(__MACH__)
26#include <sys/disk.h>
27#endif
28
29#ifndef USE_MINGW /* O_BINARY is windows-specific flag */
30#define O_BINARY 0
31#endif
32
33static void usage(char *path)
34{
35	fprintf(stderr, "%s -l <len>\n", basename(path));
36	fprintf(stderr, "    <filename>\n");
37}
38
39int main(int argc, char **argv)
40{
41	int opt;
42	const char *filename = NULL;
43	int fd;
44	int exitcode;
45	long long len;
46	while ((opt = getopt(argc, argv, "l:")) != -1) {
47		switch (opt) {
48		case 'l':
49			len = atoll(optarg);
50			break;
51		default: /* '?' */
52			usage(argv[0]);
53			exit(EXIT_FAILURE);
54		}
55	}
56
57
58	if (optind >= argc) {
59		fprintf(stderr, "Expected filename after options\n");
60		usage(argv[0]);
61		exit(EXIT_FAILURE);
62	}
63
64	filename = argv[optind++];
65
66	if (optind < argc) {
67		fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
68		usage(argv[0]);
69		exit(EXIT_FAILURE);
70	}
71
72	if (strcmp(filename, "-")) {
73		fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
74		if (fd < 0) {
75			perror("open");
76			return EXIT_FAILURE;
77		}
78	} else {
79		fd = STDOUT_FILENO;
80	}
81
82        exitcode = make_f2fs_sparse_fd(fd, len, NULL, NULL);
83
84	close(fd);
85	if (exitcode && strcmp(filename, "-"))
86		unlink(filename);
87	return exitcode;
88}
89