示例#1
0
/*
 * Fetch the specified file block from the specified inode.
 * Return the number of valid bytes in the block, -1 on error.
 */
int
file_getblock(struct unixfilesystem *fs, int inumber, int blockNum, void *buf)
{

    // First retrieve the inode in question
    struct inode node;
    int inode_iget_status = inode_iget(fs, inumber, &node);
    if (inode_iget_status == -1)
        return -1;

    // Then we want to get the block
    int block = inode_indexlookup(fs, &node, blockNum);
    if (!block)
        return -1;

    // Now retrive the contents of the block and put it into the buffer
    int sector_status = diskimg_readsector(fs->dfd, block, buf);

    // Figure out what the size was supposed to be
    int inodesize = inode_getsize(&node);

    // Now figure out what the valid number of bytes to return is
    int bytesRemaining = inodesize - blockNum * 512;
    if (bytesRemaining < 512) {
        return bytesRemaining;
    }

    return sector_status;

}
示例#2
0
int remove_file(struct unixfilesystem *fs, int inumber)
{
  struct inode in;
  int err = inode_iget(fs, inumber, &in);
  if (err < 0) return -1;
  
  if (!(in.i_mode & IALLOC) || ((in.i_mode & IFMT) == IFDIR))
  {
    /* Not allocated or is a directory */
    fprintf(stderr, "The input path is not a file and cannot be removed");
    return -1;
  }
  
  if (in.i_nlink == 1) // only one link, so overwrite the file
  {
    char zeros[DISKIMG_SECTOR_SIZE] = {0};
    int size = inode_getsize(&in);
    int blockNum = 0;
    
    // overwrite the file with zeros
    for (blockNum = 0; blockNum < size / DISKIMG_SECTOR_SIZE; blockNum++)
    {
      char buf[DISKIMG_SECTOR_SIZE];
      
      int blockAddress = inode_indexlookup(fs, &in, blockNum);
      
      if (blockAddress == -1) {
        fprintf(stderr, "Error looking up block number %d in inode %d", blockNum, inumber);
        return -1;
      }
      
      if (diskimg_writesector(fs->dfd, blockAddress, &zeros) == -1)
      {
        fprintf(stderr, "Error overwriting file");
        return -1;
      }
    }
  }
  else  // multiple links to file, so only remove this link
  {
    in.i_nlink--;
    
  }
  
  return 0;
}
示例#3
0
文件: file.c 项目: jaedongtang/opt
/*
 * Fetch the specified file block from the specified inode.
 * Return the number of valid bytes in the block, -1 on error.
 */
int
file_getblock(struct unixfilesystem *fs, int inumber, int blockNum, void *buf)
{
	//fprintf(stderr, "in file_getblock, size %d, inum: %d\n", cacheMemSizeInKB, inumber);
	struct inode in;
	int getResult = inode_iget(fs, inumber, &in);
	int sectorNum = inode_indexlookup(fs, &in, blockNum);
	int bytesRead = diskimg_readsector(fs->dfd, sectorNum, buf);
	if ((getResult == -1) || (bytesRead == -1) || (sectorNum == -1)) return -1;
	int size = inode_getsize(&in);
	if ((size % BYTES_PER_BLOCK) == 0) {
		return BYTES_PER_BLOCK;
	} else {
		int numblocks = (size / BYTES_PER_BLOCK);
		if (blockNum == numblocks) {
			return size % BYTES_PER_BLOCK;
		} else {
			return BYTES_PER_BLOCK;
		}
	}
}
示例#4
0
文件: file.c 项目: megankanne/CS110
/*
 * Fetch the specified file block from the specified inode.
 * Return the number of valid bytes in the block, -1 on error.
 */
int
file_getblock(struct unixfilesystem *fs, int inumber, int blockNum, void *buf)
{
	// Consider inumber = 17, blocknumber = 5: 
	// – Find the inode
	struct inode inp;
	if(inode_iget(fs, inumber, &inp) < 0){
		printf("Error getting inode %i\n", inumber);
		return -1;
	}
	int filesize = inode_getsize(&inp);
	int partial = (filesize % DISKIMG_SECTOR_SIZE == 0) ? 0 : 1;
	int blocks = (filesize / DISKIMG_SECTOR_SIZE) + partial;
	
	// – Find the sector number
	int sectorNum = inode_indexlookup(fs, &inp, blockNum);
	if(sectorNum == -1){
		printf("Error getting data sector %i\n", blockNum);
		return -1;
	}
	if((sectorNum > (int)fs->superblock.s_fsize) || (sectorNum < fs->superblock.s_isize + INODE_START_SECTOR)){
		printf("sectorNum: %i blockNum: %i blocks: %i inode: %i\n", sectorNum, blockNum, blocks, inumber);
		
	}
	// – Read the data in the sector into the buffer
	if (diskimg_readsector(fs->dfd, sectorNum, buf) != DISKIMG_SECTOR_SIZE) {
	    fprintf(stderr, "Error reading sector %i for block %i of %i\n", sectorNum, blockNum, blocks);
	    return -1;
	}
	//Determine value of valid bytes in the block
	if(blockNum == blocks - 1){
		if(partial) {
			return (filesize % DISKIMG_SECTOR_SIZE);
		}else{
			return DISKIMG_SECTOR_SIZE;
		}
	}else{
		return DISKIMG_SECTOR_SIZE;
	}
}
示例#5
0
文件: file.c 项目: megankanne/CS110
/*
 * Fetch the specified file block from the specified inode.
 * Return the number of valid bytes in the block, -1 on error.
 */
int
file_getblock(struct unixfilesystem *fs, int inumber, int blockNum, void *buf)
{
	//Find the inode
	struct inode inp;
	if(inode_iget(fs, inumber, &inp) < 0){
		printf("Error getting inode %i\n", inumber);
		return -1;
	}
	//Find the sector number for the blockNum
	int sectorNum = inode_indexlookup(fs, &inp, blockNum);
	if(sectorNum == -1){
		printf("Error getting data sector %i\n", blockNum);
		return -1;
	}
	//Read the data in the sector into the buffer
	if (diskimg_readsector(fs->dfd, sectorNum, buf) != DISKIMG_SECTOR_SIZE) {
	    fprintf(stderr, "Error reading sector %i for block %i\n", sectorNum, blockNum);
	    return -1;
	}
	//Return number of valid bytes in the block
	return validBytes(&inp, blockNum);
}