1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
class Sdisk
{
public :
Sdisk(string diskname);
Sdisk(string diskname, int numberofblocks, int blocksize);
int getblock(int blocknumber, string& buffer);
int putblock(int blocknumber, string buffer);
int getblocksize(int blocksize) const;
string getname(string diskname);
private :
string diskname; // file name of pseudo-disk
int numberofblocks; // number of blocks on disk
int blocksize; // block size in bytes
};
Sdisk::Sdisk(string discname)
{
if (diskname == discname)
{
fstream iofile;
string disc = diskname + ".spc";
iofile.open(disc.c_str());
iofile >> numberofblocks >> blocksize;
iofile.close();
}
else
{
cout << "No disk exist. Error code 1" << endl;
}
}
Sdisk::Sdisk(string diskname, int numberofblocks, int blocksize) //default constructor
{
this -> diskname = diskname;
this -> numberofblocks = numberofblocks;
this -> blocksize = blocksize;
string spcfile = diskname + ".spc";
string datfile = diskname + ".dat";
ofstream outfile;
outfile.open(spcfile.c_str());
outfile << numberofblocks << " " << blocksize;
outfile.close();
outfile.open(datfile.c_str());
for(int i = 0; i < numberofblocks * blocksize; i++)
{
outfile.put('#');
}
long length = outfile.tellp();
cout << "File size is: " << length << endl;
outfile.close();
}
int Sdisk::getblock(int blocknumber, string& buffer)
{
ifstream enterdat;
string opendat = diskname + ".dat";
enterdat.open(opendat.c_str());
enterdat.seekg(blocksize * blocknumber, ios::beg);
for(int i = 0; i < blocksize; i++)
{
char a = enterdat.get();
buffer += a;
}
enterdat.close();
}
int Sdisk::getblocksize(int blocksize) const
{
return blocksize;
}
class Filesys: public Sdisk
{
private:
int rootsize; //maximum number of entries in ROOT
int fatsize; //number of blocks occupied by FAT
vector<string> filenames; // filenames in root
vector<int> firstblock; // firstblocks in ROOT
vector<int> fat;
public:
Filesys( string filename); //creates an Sdrive
int fsclose(); //
int fssyncout (); //
void buildfilenames(); //
void buildfat(); //
int newfile ( string file); //
int rmfile ( string file);//
int getfirstblock( string file);//
int addblock ( string file , string block);
int delblock( string file , int blocknumber);
int readblock( string file , int blocknumber , string& buffer);
int writeblock ( string file , int blocknumber , string buffer);
int nextblock( string file , int blocknumber) ;
int getrootsize();
int getfatsize();
int getblocksize()const;
string getdiskname() const;
};
int Filesys::getblocksize() const
{
int tmp = Sdisk::getblocksize();
return tmp;
}
string Filesys::getdiskname() const
{
string tmp = Sdisk::getname();
return tmp;
}
Filesys::Filesys( string filename) : Sdisk( filename)
{
rootsize = 10; //number of files available in root
fatsize = 36 ; //getnumberofblocks();
for(int i = 0 ; i<rootsize; i++)
{
filenames.push_back("xxxxx");
firstblock.push_back(0);
}
fat.push_back(3);
for(int i = 1; i < fatsize; i++)
{
fat.push_back(1);
}
for(int i = 3; i < fatsize; i++)
{
fat[i] = i + 1;
}
fat[fat.size() - 1] = 0;
string tmp1 = filename + ".dat";
fstream iofile2;
iofile2.open(tmp1.c_str(), ios::in | ios::out);
char c;
c = iofile2.get();
if(c == '!')
{
cout << filename << " has a file system" << endl;
}
if(x == '#')
{
cout << "Disk" << filename << " already exists." << endl;
cout << "Putting file system on " << getdiskname() << endl;
string buffer;
for(int i = 0; i < 36; i++)
{
buffer += '!';
}
Sdisk::putblock(0, buffer);
}
iofile2.close();
}
int Filesys::fclose()
{
fssyncout();
}
int Filesys::newfile(string file)
{
cout << "File " << file << " is being added to the disk: " << getdiskname() << endl;
if(file.length() > 5)
{
cout << "Error 1 File: " << file << " is too large." << endl;
return 0;
}
for(int i = 0; i < filenames.size(); i++)
{
if( filenames[i] == file)
{
cout << "ERROR 2 File: "<< file <<" already exists on disk named: "<< getdiskname() << endl;
return 0;
}
}
for(int i = 0; i < filenames.size(); i++)
if(filenames[i] == "xxxxx")
{
filenames[i] = file;
cout << "SUCCESS! " << file << " added to disk: " << getdiskname() << endl;
break;
}
fssyncout();
} // end of newfile
int Filesys::rmfile(string file)
{
cout << "Deleting: " << file << " from disk: " << getdiskname() << endl;
for(int i=0; i < filenames.size(); i++)
{
if(filenames[i] == file)
{
if(firstblock[i] == 0)
{
filenames[i] = "xxxxx";
fssyncout();
return 1;
}
}
}
return 0;
}//end of remove file
int Filesys::getfirstblock(string file)
{
for(int i=0; i < filenames.size(); i++)
{
if(filenames[i] == file)
{
return firstblock[i];
}
}
return 0;
}//end of get the first block
int Filesys::addblock(string file, string block)
{
//check if disk is full
if(fat[0] == 0)
{
cout<<getdiskname()<<" is full"<<endl;
return -1;
}
for(int i = 0; i < filenames.size(); i++)
{
if(filenames[i] == file)
{
cout << file << " is located in root." << endl;
return firstblock[i];
}
else
{
cout << file << " not in files" << endl;
return 0;
}
}
int nextblock = getfirstblock(file);
if(nextblock == 0)
{
for(int i = 0; i < filenames.size(); i++)
if(filenames[i] == file)
{
firstblock[i] = fat[0];
break;
}
}
else
{
while(fat[nextblock] != 0)
{
nextblock = fat[nextblock];
}
fat[nextblock] = fat[0];
}
//update fat
int allocate = fat[0];
fat[0] = fat[fat[0]];
fat[allocate] = 0;
} //end addingblock
int Filesys::delblock(string file, int blocknumber)
{
} // end delblock
int Filesys::readblock(string file, int blocknumber, string &buffer)
{
}
int Filesys::writeblock(string file, int blocknumber, string buffer)
{
}//end writeblock
int Filesys::nextblock(string file, int blocknumber)
{
}//end of nextblock
int Filesys::getrootsize()
{
return rootsize;
}
int Filesys:getfatsize()
{
return fatsize;
}
//NEED TO ADD more here
// You can use this to test your Sdisk class
int main()
{
Sdisk disk1("test1",16,32);
string block1, block2, block3, block4;
for (int i=1; i<=32; i++) block1=block1+"1";
for (int i=1; i<=32; i++) block2=block2+"2";
disk1.putblock(4,block1);
disk1.getblock(4,block3);
cout << "Should be 32 1s : ";
cout << block3 << endl;
disk1.putblock(8,block2);
disk1.getblock(8,block4);
cout << "Should be 32 2s : ";
cout << block4 << endl;
}
|