Пример #1
0
int main()
{
	FileSystem fileSystem;
	int errCode;
	
	//Format filesystem
	errCode = fileSystem.formatDisk();
	if(errCode == 1)
		cout<<"Filesystem formatting failed"<<endl;

	//Create a file and open it
	int file1 = fileSystem.create();
	int fileid1 = fileSystem.open(file1);

	//This is the data we'll write
	byte buffer[] = "Implementing File Systems, a test program";

	//Seek to a large distance so that we actually cause an indirection. As usual, we skip the first 10 blocks, and then write
	//Note that 256 is the block size. So, we seek to skip the first 10 direct blocks and then 10 bytes
	fileSystem.seek(fileid1, 10*256 + 10, SEEK_CUR);

	//Write 20 bytes at that position
	//If you could not write 20 bytes, that is definitely an error
	if(fileSystem.write(fileid1, buffer, 20) != 20)
		cerr<<"Error!"<<endl;

	//Close the file
	fileSystem.close(fileid1);
	
	//Reopen
	fileid1 = fileSystem.open(file1);

	/* Read from file currently does not work. So, commenting out */
	/*
	byte readbuf[16] = "\0";
	fileSystem.read(fileid1, readbuf, 6);

	cout<<"Read"<<readbuf<<"done" <<endl;
	*/

	//Owing to the non-functionality of read(), del() cannot be tested
	//fileSystem.del(fileid1);

	//By our write, we can calculate that the data block is 22
	//free() would free that block and add it to the free list
	//free() is a private function, however to show that it works, all you have to do is modify the FileSystem.h file and uncomment the following statement
	//fileSystem.free(22);
	
	//Also note that inumber() works. It is trivial to verify this
	//Only read() and del() do not work.

	return 0;
}
Пример #2
0
 void run() {
    //we prefer to have pointers so we create these:
    FileSystem* fileSystem = &fs_;
    File* file = &f_;
    Directory* d = &d_;


    xprintf("\n////////////////////////////\n"
            "main > TESTING INIT AND MOUNT\n"
            "//////////////////////\n");

    fileSystem->enableDebugOutput(true);

    //disk must be mounted before anything else can be done
    fileSystem->mountDisk(0);
    xprintf("main > mount done\n");

    //format will create a new fat file system
    //this can be omitted if there already is a fs on the disk
    fileSystem->formatDisk(0);
    xprintf("main > format done\n");


    //setting time changes RODOS system time
    fileSystem->setTime(2012, 03, 03, 07, 31, 12);



    xprintf("\n////////////////////////////\n"
            "main > TESTING DIRECTORY FUNTIONS\n"
            "///////////////////////////\n");

    file->enableDebugOutput(true);

    file->open("/test.txt", file->O_WRITE);
    file->putString("This is my first file!\n");
    file->close();
    xprintf("main > created '/test.txt'\n");



    //this will not work because it does not create intermediate directories
    xprintf("main > create dir '/FirstDir/ScndDir'\n");
    fileSystem->createDirectory("/FirstDir/ScndDir");

    bool isDir;
    bool exists = fileSystem->fileExists("/FirstDir/ScndDir", &isDir);
    if (!exists) {
        xprintf("main > directory '/FirstDir/ScndDir' does not exist -> correct\n");
    } else {
        xprintf("main > directory '/FirstDir/ScndDir' exists -> NOT CORRECT\n");
    }


    //if we create directories one after the other everything will work fine
    xprintf("main > create dir '/FirstDir'\n");
    fileSystem->createDirectory("/FirstDir");

    xprintf("main > create dir '/FirstDir/ScndDir'\n");
    fileSystem->createDirectory("/FirstDir/ScndDir");
    exists = fileSystem->fileExists("/FirstDir/ScndDir", &isDir);
    if (!exists) {
        xprintf("main > directory '/FirstDir/ScndDir' does not exist -> NOT CORRECT\n");
    } else {
        xprintf("main > directory '/FirstDir/ScndDir' exists -> correct\n");
    }


    //fill directory with files
    fileSystem->move("/test.txt", "/FirstDir/test.txt");

    file->open("FirstDir/file2.txt", file->O_WRITE);
    file->putString("This is file2, dude.");
    file->close();

    file->open("FirstDir/file3.txt", file->O_WRITE);
    file->putString("This is file3, bro.");
    file->close();

    //rename entire directory
    fileSystem->move("FirstDir", "MyDir");

    
    
    //open and interate through directory
    d->open("/MyDir");
    d->resetEntryEnumeration();
    xprintf("main > reset entry numeration on  '/MyDir/'\n");

    char nextFileName[100];
    long size;
    d->readNextEntry(nextFileName, 100, &size);

    while (nextFileName[0] != '\0') {
        xprintf("main > next entry in  '/MyDir/' is %s\nsize: %ld\n", nextFileName, size);
        char path[200];
        path[0] = 0;
        xsprintf(path, "/MyDir/%s", nextFileName);

        exists = fileSystem->fileExists(path, &isDir);
        //read content if file
        if (isDir == false) {
            file->enableDebugOutput(true);
            file->open(path, file->O_READ);

            char readStr[200];
            readStr[0] = 0;
            file->getString(readStr, 200);
            xprintf("main > read file > %s\n%s\n", path, readStr);
            file->close();
        }
        nextFileName[0] = 0;

        d->readNextEntry(nextFileName, 100, &size);
    }

    //directories don't need to be closed after use. you can just free them
    
    
   
    //this will remove the disk
    fileSystem->unmountDisk(0);

  
  while(1) {
       suspendCallerUntil();
     }
}