libsrc/blockio.cxx

Go to the documentation of this file.
00001 /**********************************************************************
00002  * Copyright 2002 Jeff Rush <jrush@taupro.com>
00003  * Original Copyright 1979-2002 Udanax.com
00004  *
00005  * This file is part of the Udanax xanalogical storage system.
00006  *
00007  * Udanax is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * Udanax is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with Udanax; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  **********************************************************************/
00021 
00030 /* Modification History:
00031  * $Log: blockio.cxx,v $
00032  * Revision 1.3  2004/09/11 13:59:21  jrush
00033  * Changed all fprintf's to stderr to use the Nana library L() macro.  Also
00034  * removed a 2-3 minor compiler warnings.
00035  *
00036  * Revision 1.2  2004/09/04 16:02:17  jrush
00037  * Added Doxygen headers before each and every function definition.
00038  *
00039  * Revision 1.1  2004/08/28 13:35:26  jrush
00040  * Catching up CVS to various incomplete versions on local disk.
00041  *
00042  *
00043  */
00044 
00045 #include <stdio.h>
00046 #include <memory.h>
00047 #include <stdlib.h>
00048 #include <unistd.h>
00049 #include <sys/types.h>
00050 #include <sys/stat.h>
00051 #include <fcntl.h>
00052 #include <netinet/in.h>
00053 #include <errno.h>
00054 
00055 #include "udanax.h"
00056 
00064     FileBlockIO::OpenStatus
00065 FileBlockIO::open(
00066     const char *filename)
00067 {
00068     // Try to Open an Existing File
00069     blockfd = ::open(filename, O_RDWR, 0);
00070     if (blockfd >= 0)
00071         return OS_OPENED; // Existing Media was Found and Opened
00072 
00073     errno = 0;
00074 
00075     // Else Try to Create a New File
00076     blockfd = creat(filename, 0660);
00077     if (blockfd >= 0)
00078         return OS_CREATED; // No Media Found so a New One was Created
00079 
00080     // Error: Unable to Open or Create the File
00081     perror("FileBlockIO::open");
00082     return OS_FAILED;
00083 }
00084 
00092     void
00093 FileBlockIO::close()
00094 {
00095     if (::close(blockfd) != 0) {
00096         perror("FileBlockIO::close");
00097         assert(0); // close failed
00098     }
00099 }
00100 
00108     bool
00109 FileBlockIO::get(
00110     void     *buf,
00111     int       buflen,
00112     BlockNum  blockno)
00113 {
00114     L("FileBlockIO::get(buf=0x%08lX, buflen=%d, blockno=%d) blocksize=%d\n", buf, buflen, blockno, blocksize);
00115     assert(buf != NULL);
00116     assert(blockno != 0);
00117     assert( goodblock(blockno) );
00118 //    assert(buflen <= blocksize);
00119 
00120 //OBSOLETE    if (!enffileread) {
00121 //OBSOLETE        if (close(enffiledes) != 0)
00122 //OBSOLETE            perror("close failed in readloaf");
00123 //OBSOLETE
00124 //OBSOLETE        if ((enffiledes = open("enf.enf",  O_RDWR, 0)) == -1) {
00125 //OBSOLETE            perror("open");
00126 //OBSOLETE            assert(0); // open
00127 //OBSOLETE        }
00128 //OBSOLETE        enffileread = true;
00129 //OBSOLETE    }
00130 
00131     if (lseek(blockfd, (long) blockno * blocksize, 0) < 0) {
00132         perror("FileBlockIO::get");
00133         return false;
00134     }
00135 
00136     int nbytes = read(blockfd, buf, buflen);
00137     assert(nbytes > 0); // Incorrect Usage of Assertion
00138 
00139     stats.blocks_read++;
00140     return true;
00141 }
00142 
00150     bool
00151 FileBlockIO::put(
00152     void     *buf,
00153     int       buflen,
00154     BlockNum  blockno)
00155 {
00156     L("FileBlockIO::put(buf=0x%08lX, buflen=%d, blockno=%d) blocksize=%d\n", buf, buflen, blockno, blocksize);
00157     assert(buf != NULL);
00158     assert(blockno != 0);
00159     assert( goodblock(blockno) );
00160     assert(buflen <= blocksize);
00161 
00162     if (lseek(blockfd, (long) blockno * blocksize, 0) < 0) {
00163         perror("FileBlockIO::put");
00164         return false;
00165     }
00166 
00167     int nbytes = write(blockfd, buf, buflen);
00168     assert(nbytes >= 0); // Incorrect Usage of Assert
00169 
00170     stats.blocks_written++;
00171     return true;
00172 }

Generated on Sun Aug 21 04:18:13 2005 for Udanax-Green by doxygen1.3.4