00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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
00069 blockfd = ::open(filename, O_RDWR, 0);
00070 if (blockfd >= 0)
00071 return OS_OPENED;
00072
00073 errno = 0;
00074
00075
00076 blockfd = creat(filename, 0660);
00077 if (blockfd >= 0)
00078 return OS_CREATED;
00079
00080
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);
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
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
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);
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);
00169
00170 stats.blocks_written++;
00171 return true;
00172 }