00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #define _FILE_OFFSET_BITS 64
00025
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <fcntl.h>
00030 #include <errno.h>
00031 #include <unistd.h>
00032 #include <sys/file.h>
00033
00034 void usage();
00035
00047 int
00048 main(int argc, char *argv[])
00049 {
00050 if (argc != 4) {
00051 printf("Invalid number of arguments\n");
00052 usage();
00053 exit(EINVAL);
00054 }
00055
00056 char *fileName = NULL;
00057 int nPages = 0;
00058 int pageSize = 0;
00059 for (uint argIdx = 1; argIdx < argc; argIdx++) {
00060 if (strncmp(argv[argIdx], "--", 2) != 0) {
00061 fileName = argv[argIdx];
00062 } else {
00063 if (strncmp(&(argv[argIdx][2]), "append-pages=", 13) == 0) {
00064 nPages = atoi(&(argv[argIdx][15]));
00065 } else if (strncmp(&(argv[argIdx][2]), "pagesize=", 9) == 0) {
00066 pageSize = atoi(&(argv[argIdx][11]));
00067 } else {
00068 printf("Invalid argument %s\n", argv[argIdx]);
00069 usage();
00070 exit(EINVAL);
00071 }
00072 }
00073 }
00074 if (fileName == NULL) {
00075 printf("Filename argument not specified\n");
00076 usage();
00077 exit(EINVAL);
00078 }
00079 if (nPages <= 0) {
00080 printf("Invalid number of pages argument: %d\n", nPages);
00081 exit(EINVAL);
00082 }
00083 if (pageSize <= 0) {
00084 printf("Invalid pagesize argument: %d\n", pageSize);
00085 exit(EINVAL);
00086 }
00087
00088 int access = O_LARGEFILE;
00089 int permission = S_IRUSR;
00090 access |= O_RDWR;
00091 permission |= S_IWUSR;
00092
00093 int handle = open(fileName, access, permission);
00094 if (handle < 0) {
00095 printf("Failed to open file '%s'\n", fileName);
00096 exit(errno);
00097 }
00098 if (flock(handle, LOCK_EX | LOCK_NB) < 0) {
00099 printf("Failed to acquire exclusive lock on file '%s'\n", fileName);
00100 close(handle);
00101 exit(errno);
00102 }
00103 off_t offset = lseek(handle, 0, SEEK_END);
00104 if (offset == -1) {
00105 printf("File seek on file '%s' failed\n", fileName);
00106 close(handle);
00107 exit(errno);
00108 }
00109 int rc = posix_fallocate(handle, offset, ((off_t) nPages * pageSize));
00110 if (rc != 0) {
00111 printf("File allocation failed for file '%s'\n", fileName);
00112 close(handle);
00113 exit(rc);
00114 }
00115 close(handle);
00116 exit(0);
00117 }
00118
00119 void usage()
00120 {
00121 printf("Usage: allocFile --append-pages=<number of pages> ");
00122 printf("--pagesize=<pageSize> <filename>\n");
00123 }
00124
00125