00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef FILESYS_STUB
00014
00015 #include "copyright.h"
00016 #include "main.h"
00017 #include "filehdr.h"
00018 #include "openfile.h"
00019 #include "synchdisk.h"
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 OpenFile::OpenFile(int sector)
00030 {
00031 hdr = new FileHeader;
00032 hdr->FetchFrom(sector);
00033 seekPosition = 0;
00034 }
00035
00036
00037
00038
00039
00040
00041 OpenFile::~OpenFile()
00042 {
00043 delete hdr;
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 void
00055 OpenFile::Seek(int position)
00056 {
00057 seekPosition = position;
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 int
00074 OpenFile::Read(char *into, int numBytes)
00075 {
00076 int result = ReadAt(into, numBytes, seekPosition);
00077 seekPosition += result;
00078 return result;
00079 }
00080
00081 int
00082 OpenFile::Write(char *into, int numBytes)
00083 {
00084 int result = WriteAt(into, numBytes, seekPosition);
00085 seekPosition += result;
00086 return result;
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 int
00116 OpenFile::ReadAt(char *into, int numBytes, int position)
00117 {
00118 int fileLength = hdr->FileLength();
00119 int i, firstSector, lastSector, numSectors;
00120 char *buf;
00121
00122 if ((numBytes <= 0) || (position >= fileLength))
00123 return 0;
00124 if ((position + numBytes) > fileLength)
00125 numBytes = fileLength - position;
00126 DEBUG(dbgFile, "Reading " << numBytes << " bytes at " << position << " from file of length " << fileLength);
00127
00128 firstSector = divRoundDown(position, SectorSize);
00129 lastSector = divRoundDown(position + numBytes - 1, SectorSize);
00130 numSectors = 1 + lastSector - firstSector;
00131
00132
00133 buf = new char[numSectors * SectorSize];
00134 for (i = firstSector; i <= lastSector; i++)
00135 kernel->synchDisk->ReadSector(hdr->ByteToSector(i * SectorSize),
00136 &buf[(i - firstSector) * SectorSize]);
00137
00138
00139 bcopy(&buf[position - (firstSector * SectorSize)], into, numBytes);
00140 delete [] buf;
00141 return numBytes;
00142 }
00143
00144 int
00145 OpenFile::WriteAt(char *from, int numBytes, int position)
00146 {
00147 int fileLength = hdr->FileLength();
00148 int i, firstSector, lastSector, numSectors;
00149 bool firstAligned, lastAligned;
00150 char *buf;
00151
00152 if ((numBytes <= 0) || (position >= fileLength))
00153 return 0;
00154 if ((position + numBytes) > fileLength)
00155 numBytes = fileLength - position;
00156 DEBUG(dbgFile, "Writing " << numBytes << " bytes at " << position << " from file of length " << fileLength);
00157
00158 firstSector = divRoundDown(position, SectorSize);
00159 lastSector = divRoundDown(position + numBytes - 1, SectorSize);
00160 numSectors = 1 + lastSector - firstSector;
00161
00162 buf = new char[numSectors * SectorSize];
00163
00164 firstAligned = (position == (firstSector * SectorSize));
00165 lastAligned = ((position + numBytes) == ((lastSector + 1) * SectorSize));
00166
00167
00168 if (!firstAligned)
00169 ReadAt(buf, SectorSize, firstSector * SectorSize);
00170 if (!lastAligned && ((firstSector != lastSector) || firstAligned))
00171 ReadAt(&buf[(lastSector - firstSector) * SectorSize],
00172 SectorSize, lastSector * SectorSize);
00173
00174
00175 bcopy(from, &buf[position - (firstSector * SectorSize)], numBytes);
00176
00177
00178 for (i = firstSector; i <= lastSector; i++)
00179 kernel->synchDisk->WriteSector(hdr->ByteToSector(i * SectorSize),
00180 &buf[(i - firstSector) * SectorSize]);
00181 delete [] buf;
00182 return numBytes;
00183 }
00184
00185
00186
00187
00188
00189
00190 int
00191 OpenFile::Length()
00192 {
00193 return hdr->FileLength();
00194 }
00195
00196 #endif //FILESYS_STUB