Go to the source code of this file.
Functions | |
void | usage () |
int | main (int argc, char *argv[]) |
Utility program that preallocates and appends a specified number of pages to the end of a data file. |
int main | ( | int | argc, | |
char * | argv[] | |||
) |
Utility program that preallocates and appends a specified number of pages to the end of a data file.
Usage:
allocFile --append-pages=<number of pages> --pagesize=<pageSize> <filename>
The file must be writable and exclusively lockable.
Definition at line 48 of file AllocFile.cpp.
References usage().
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 }
void usage | ( | ) |
Definition at line 119 of file AllocFile.cpp.
Referenced by main().
00120 { 00121 printf("Usage: allocFile --append-pages=<number of pages> "); 00122 printf("--pagesize=<pageSize> <filename>\n"); 00123 }