Theodore Ts'o | 3c67a9f | 2010-03-22 21:53:41 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * fallocate - utility to use the fallocate system call |
| 3 | * |
| 4 | * Copyright (C) 2008 Red Hat, Inc. All rights reserved. |
| 5 | * Written by Eric Sandeen <sandeen@redhat.com> |
| 6 | * |
| 7 | * cvtnum routine taken from xfsprogs, |
| 8 | * Copyright (c) 2003-2005 Silicon Graphics, Inc. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope that it would be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software Foundation, |
| 21 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #define _GNU_SOURCE |
| 25 | #define _LARGEFILE_SOURCE |
| 26 | #define _LARGEFILE64_SOURCE |
| 27 | |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/syscall.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <unistd.h> |
| 35 | #include <ctype.h> |
| 36 | |
| 37 | // #include <linux/falloc.h> |
| 38 | #define FALLOC_FL_KEEP_SIZE 0x01 |
| 39 | |
| 40 | void usage(void) |
| 41 | { |
| 42 | printf("Usage: fallocate [-n] [-o offset] -l length filename\n"); |
| 43 | exit(EXIT_FAILURE); |
| 44 | } |
| 45 | |
| 46 | #define EXABYTES(x) ((long long)(x) << 60) |
| 47 | #define PETABYTES(x) ((long long)(x) << 50) |
| 48 | #define TERABYTES(x) ((long long)(x) << 40) |
| 49 | #define GIGABYTES(x) ((long long)(x) << 30) |
| 50 | #define MEGABYTES(x) ((long long)(x) << 20) |
| 51 | #define KILOBYTES(x) ((long long)(x) << 10) |
| 52 | |
| 53 | long long |
| 54 | cvtnum(char *s) |
| 55 | { |
| 56 | long long i; |
| 57 | char *sp; |
| 58 | int c; |
| 59 | |
| 60 | i = strtoll(s, &sp, 0); |
| 61 | if (i == 0 && sp == s) |
| 62 | return -1LL; |
| 63 | if (*sp == '\0') |
| 64 | return i; |
| 65 | if (sp[1] != '\0') |
| 66 | return -1LL; |
| 67 | |
| 68 | c = tolower(*sp); |
| 69 | switch (c) { |
| 70 | case 'k': |
| 71 | return KILOBYTES(i); |
| 72 | case 'm': |
| 73 | return MEGABYTES(i); |
| 74 | case 'g': |
| 75 | return GIGABYTES(i); |
| 76 | case 't': |
| 77 | return TERABYTES(i); |
| 78 | case 'p': |
| 79 | return PETABYTES(i); |
| 80 | case 'e': |
| 81 | return EXABYTES(i); |
| 82 | } |
| 83 | |
| 84 | return -1LL; |
| 85 | } |
| 86 | |
| 87 | int main(int argc, char **argv) |
| 88 | { |
| 89 | int fd; |
| 90 | char *fname; |
| 91 | int opt; |
| 92 | loff_t length = -2LL; |
| 93 | loff_t offset = 0; |
| 94 | int falloc_mode = 0; |
| 95 | int error; |
| 96 | |
| 97 | while ((opt = getopt(argc, argv, "nl:o:")) != -1) { |
| 98 | switch(opt) { |
| 99 | case 'n': |
| 100 | /* do not change filesize */ |
| 101 | falloc_mode = FALLOC_FL_KEEP_SIZE; |
| 102 | break; |
| 103 | case 'l': |
| 104 | length = cvtnum(optarg); |
| 105 | break; |
| 106 | case 'o': |
| 107 | offset = cvtnum(optarg); |
| 108 | break; |
| 109 | default: |
| 110 | usage(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (length == -2LL) { |
| 115 | printf("Error: no length argument specified\n"); |
| 116 | usage(); |
| 117 | } |
| 118 | |
| 119 | if (length <= 0) { |
| 120 | printf("Error: invalid length value specified\n"); |
| 121 | usage(); |
| 122 | } |
| 123 | |
| 124 | if (offset < 0) { |
| 125 | printf("Error: invalid offset value specified\n"); |
| 126 | usage(); |
| 127 | } |
| 128 | |
| 129 | if (optind == argc) { |
| 130 | printf("Error: no filename specified\n"); |
| 131 | usage(); |
| 132 | } |
| 133 | |
| 134 | fname = argv[optind++]; |
| 135 | |
| 136 | /* Should we create the file if it doesn't already exist? */ |
| 137 | fd = open(fname, O_WRONLY|O_DIRECTORY); |
| 138 | if (fd < 0) { |
| 139 | perror("Error opening file"); |
| 140 | exit(EXIT_FAILURE); |
| 141 | } |
| 142 | |
| 143 | error = syscall(SYS_fallocate, fd, falloc_mode, offset, length); |
| 144 | |
| 145 | if (error < 0) { |
| 146 | perror("fallocate failed"); |
| 147 | exit(EXIT_FAILURE); |
| 148 | } |
| 149 | |
| 150 | close(fd); |
| 151 | return 0; |
| 152 | } |