Example #1
0
int fileSys::delBlock(string file, int block_number){
	int i,first_blk = getFirstBlock(file);
	
	if(check_n_pad(file) == 0)
		return 0;

	if(check_block(block_number, file) == 0)
		return 0;
	
	//check if the block_number belongs to the file
	if(first_blk == block_number){
		for(i = 0; i < firstBlock.size(); i++){
			if(firstBlock[i] == block_number){
				firstBlock[i] = fat[block_number];
				fat[block_number] = fat[0];
				fat[0] = block_number;
				break; 
			}
		}
	}else{
		for(i = 0; i < fat.size();i++){
			if(fat[i] == block_number){
				fat[i] = fat[block_number];
				fat[block_number] = fat[0];
				fat[0] = block_number;
				break;
			}
		}
	}

	fsSynch();
	return 1;
}
Example #2
0
int Filesys::delBlock(std::string file, int blockNumber) {
    int nextBlock = getFirstBlock(file);
    // Can't delete block of empty file.
    if (nextBlock == 0) { // File doesn't exist or is empty.
        throw std::invalid_argument("Cannot delete block.");
        return 0;
    } else if (nextBlock == blockNumber) { // Block to del is first block.
        for (int i = 0; i < fileName.size(); i++) {
            if (fileName[i] == file) {
                firstBlock[i] = fat[blockNumber];
            }
        }
    } else { // Block to del is not first block.
        while (fat[nextBlock] != blockNumber && fat[nextBlock] != 0) {
            nextBlock = fat[nextBlock];
        }
        if (fat[nextBlock] != 0) {
            fat[nextBlock] = fat[blockNumber];
        } else {
            throw std::invalid_argument("Block not in file.");
            return 0;
        }
    }
    fat[blockNumber] = fat[0];
    fat[0] = blockNumber;
    fssynch();
}
Example #3
0
int Filesys::addBlock(std::string file, std::string block) {
    int allocate;
    int fileBlock = getFirstBlock(file);
    if (fileBlock == -1) { // No available blocks; fs full.
        throw std::invalid_argument("Cannot add block. File doesn't exist.");
        return 0;
    } else if (fileBlock == 0) { // No blocks in file, OK to add.
        allocate = fat[0];
        if (allocate == 0) { // Disk is full.
            throw std::invalid_argument("Disk is full, cannot add block.");
            return -1;
        } else {
            fat[0] = fat[fat[0]];
            fat[allocate] = 0;
            // Update root
            for (int i = 0; i < fileName.size(); i++) {
                if (fileName[i] == file) {
                    firstBlock[i] = allocate;
                }
            }
        }
    } else { // File already has blocks. 
        allocate = fat[0];
        fat[0] = fat[fat[0]];
        fat[allocate] = 0;
        // Follow links to change previous last block.
        while (fat[fileBlock] != 0) {
            fileBlock = fat[fileBlock];
        }
        fat[fileBlock] = allocate;
    }
    fssynch();
    putBlock(allocate, block);
    return allocate;
}
Example #4
0
int Filesys::checkBlock(std::string file, int blockNumber) {
    int nextBlock = getFirstBlock(file);
    if (nextBlock == blockNumber) {
        return 1;
    } else {
        while (fat[nextBlock] != 0) {
            nextBlock = fat[nextBlock];
            if (nextBlock == blockNumber) {
                return 1;
            }
        }
        // When here blockNumber is not a part of the file.
        return 0;
    }
}
Example #5
0
int fileSys::check_block(int block, string file){
   
	int next_block, first_blk = getFirstBlock(file);
   bool found = false;

   next_block = first_blk;
   while(next_block != 0){
		if(next_block == block){
         found = true;
         break;
      }
      next_block = fat[next_block];
   }
   if(found == false){
      cout << "getBlock(): Hey! That block number does not belong to that file. (Block: "<< block << ")" << endl;
      return 0;
   }
	return 1;
}
Example #6
0
int Filesys::rmFile(std::string file) {
    for (int i = 0; i < fileName.size(); i++) {
        if (fileName[i] == file) {
            // Don't remove if firstblock is not empty.
            if (getFirstBlock(file) != 0) {
                throw std::invalid_argument("Cannot remove non-empty file.");
                return 0;
            } else {
                // File is empty and thus removeable.
                fileName[i] = "xxxxx";
                fssynch();
                return 1;
            }
        }
    }
    // File doesn't exist.
    throw std::invalid_argument("Cannot delete file that doesn't exist.");
    return 0;
}
Example #7
0
//returns -1 if disk is full and -2 if file wasnt found
int fileSys::addBlock(string file, string buffer){
	int first_blk = getFirstBlock(file), new_blk = fat[0];
	bool file_found = false;
	if(check_n_pad(file) == 0)
		return -1;
	
	if(new_blk == 0){
		cout << "Disk is full!\n";
		return -1;
	}
	
	if(first_blk == 0){//then empty file
		
		for(int i = 0; i < fileName.size(); i++){
			if(fileName[i] == file){
				firstBlock[i] = new_blk;
				fat[0] = fat[new_blk];
				fat[new_blk] = 0;
				file_found = true;
				break;
			}
		}
		if(file_found == false){
			cout << "addBlock(): File was not found!\n";
			return -2;
		}
	}else{
		int next_blk = first_blk;
		
		while(fat[next_blk] != 0)
			next_blk = fat[next_blk];
		fat[next_blk] = new_blk;
		fat[0] = fat[new_blk];
		fat[new_blk] = 0;
	}
	
	disk.putblock(new_blk, buffer);
	fsSynch();
	return new_blk;
}//addblock