AllocFile.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/util/AllocFile.cpp#8 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2005-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 // Portions Copyright (C) 1999-2009 John V. Sichi
00008 //
00009 // This program is free software; you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by the Free
00011 // Software Foundation; either version 2 of the License, or (at your option)
00012 // any later version approved by The Eigenbase Project.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 // End AllocFile.cpp

Generated on Mon Jun 22 04:00:21 2009 for Fennel by  doxygen 1.5.1