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 <stdlib.h> 21#include <string.h> 22#include <unistd.h> 23 24#if defined(__linux__) 25#include <linux/fs.h> 26#elif defined(__APPLE__) && defined(__MACH__) 27#include <sys/disk.h> 28#endif 29 30#include "make_f2fs.h" 31 32#ifndef USE_MINGW /* O_BINARY is windows-specific flag */ 33#define O_BINARY 0 34#endif 35 36static void usage(char *path) 37{ 38 fprintf(stderr, "%s -l <len>\n", basename(path)); 39 fprintf(stderr, " <filename>\n"); 40} 41 42int main(int argc, char **argv) 43{ 44 int opt; 45 const char *filename = NULL; 46 int fd; 47 int exitcode; 48 long long len; 49 while ((opt = getopt(argc, argv, "l:")) != -1) { 50 switch (opt) { 51 case 'l': 52 len = atoll(optarg); 53 break; 54 default: /* '?' */ 55 usage(argv[0]); 56 exit(EXIT_FAILURE); 57 } 58 } 59 60 61 if (optind >= argc) { 62 fprintf(stderr, "Expected filename after options\n"); 63 usage(argv[0]); 64 exit(EXIT_FAILURE); 65 } 66 67 filename = argv[optind++]; 68 69 if (optind < argc) { 70 fprintf(stderr, "Unexpected argument: %s\n", argv[optind]); 71 usage(argv[0]); 72 exit(EXIT_FAILURE); 73 } 74 75 if (strcmp(filename, "-")) { 76 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); 77 if (fd < 0) { 78 perror("open"); 79 return EXIT_FAILURE; 80 } 81 } else { 82 fd = STDOUT_FILENO; 83 } 84 85 exitcode = make_f2fs_sparse_fd(fd, len, NULL, NULL); 86 87 close(fd); 88 if (exitcode && strcmp(filename, "-")) 89 unlink(filename); 90 return exitcode; 91} 92