示例#1
0
int tfs_writeByte(fileDescriptor FD, unsigned char data) {
	int i, fd, size, firstBlock, numBlocks, currBlock, tempFP;
	int found = 0;
	char buff[BLOCKSIZE];
	char *fileName;
	drt_t *temp = dynamicResourceTable;

	if(disk_mount)
		fd = openDisk(disk_mount, 0);
	else
		return ERR_FILENOTMOUNTED;

	while(temp){
		if(temp->id == FD){
			break;
		}
		temp = temp->next;
	}

	if (!temp)
		return ERR_BADFILE;

	if(!temp->access)
		return ERR_READONLY;

	fileName = temp->fileName;


	/* looking for inode block */
	for(i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE && !found; i++){

		if(readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;

		if(buff[0] == 2){
			if(!strcmp(fileName, buff+4)){
				found = 1;
				firstBlock = buff[2];
				size = (buff[13] << 8) || buff[14];
				numBlocks = (int)ceil((double)size / (double)BLOCKSIZE);
				break;
			}
		}
	}
	if(!found)
		return ERR_NOINODEFOUND;

	currBlock = (int)floor(((double)temp->fileptr+1) / (double)BLOCKSIZE);
	tempFP = temp->fileptr - (BLOCKSIZE * currBlock);
	readBlock(fd,currBlock+firstBlock,buff);
	buff[tempFP+4] = data;
	time_t now = time(0);
	memcpy(buff+19, &now, sizeof(time_t)); // set access time
	memcpy(buff+23, &now, sizeof(time_t)); // set modification time
	writeBlock(fd,currBlock+firstBlock,buff);
	if(DEBUG)
		printf("Wrote %c to byte at block %d\n",buff[tempFP+4],currBlock+firstBlock);
	close(fd);
	return 1;
}
示例#2
0
/*
 * disk access
 *       devnm   device name (possibly with the partition number)
 *       blk     start block number
 *               if device name has a partition number, then the block number in that partition
 *               if there is no partition number in the disk anme, the block number in the entire disk
 *       nblk    number of blocks
 *       buf     buffer (* )
 *       wrt     FALSE : read
 *               TRUE  : write
 *       return value error code
 *       argument marked with (* ) may be an address specified from external sources.
 */
EXPORT ER rwDisk( const UB *devnm, W blk, W nblk, void *buf, BOOL wrt )
{
	DISKCB	*dcb;
	W	pno, nb;
	ER	err;

        /* initialize disk */
	pno = openDisk(devnm, &dcb);
	if ( pno < E_OK ) return pno;

	nb = dcb->part[pno].nblk;

        /* range check of the block number */
	if ( blk < 0 || blk >= nb
	  || nblk <= 0 || nblk > nb - blk ) return E_PAR;

        /* convert the relative block number in a partiton to a block number in the entire disk */
	blk += dcb->part[pno].sblk;

        /* read from, or write to disk */
	err = (*dcb->rwdisk)(dcb, blk, nblk, buf, wrt);
	if ( err < E_OK ) return err;

	return E_OK;
}
示例#3
0
int tfs_mount(char *filename) {
    int ndx = 0;
    char buf[BLOCKSIZE];

	if(fs_mounted) {
        return MOUNT_EXISTS;      //FS Already mounted
    }
    
    if(filename < 0) {
        return BAD_FILENAME;
    }

    disk_num = openDisk(filename, 0);
    fs_mounted = 1;

    for(ndx = 0; ndx < num_blocks; ndx++) {
        readBlock(disk_num, ndx, buf);
        if(buf[1] != MAGIC_NUMBER) {
            return CORRUPTED_DATA;          // File system not of type "tiny" or is corrupted
        }
    }

    OFTable_Init(&oftable, num_blocks);

    return SUCCESS;
}
示例#4
0
int tfs_mkfs(char *filename, int nBytes){

	int i, diskNum;
	char* templateBlk = calloc(1, BLOCKSIZE);

	templateBlk[0] = 4;
	templateBlk[1] = 0x45;

	diskNum = openDisk(filename, nBytes);

	if(diskNum < 0)
		return ERR_MKDSK;

	for(i = 0; i < BLOCKSIZE; i++)
		writeBlock(diskNum, i, templateBlk);

	/* SUPER BLOCK */
	templateBlk[0] = 1;
	templateBlk[2] = 1;
	writeBlock(diskNum, 0, templateBlk);

        if(DEBUG)
            printf("Made File System with super block\n");

	free(templateBlk);
	close(diskNum);
	return 1;
}
示例#5
0
int main(int argc, char** argv)
{
	try
	{
		struct fuse_operations ops;
		struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
	
		if (argc < 3)
		{
			showHelp(argv[0]);
			return 1;
		}
	
		openDisk(argv[1]);
	
		memset(&ops, 0, sizeof(ops));
	
		ops.getattr = hfs_getattr;
		ops.open = hfs_open;
		ops.read = hfs_read;
		ops.release = hfs_release;
		//ops.opendir = hfs_opendir;
		ops.readdir = hfs_readdir;
		ops.readlink = hfs_readlink;
		//ops.releasedir = hfs_releasedir;
		ops.getxattr = hfs_getxattr;
		ops.listxattr = hfs_listxattr;
	
		for (int i = 0; i < argc; i++)
		{
			if (i == 1)
				;
			else
				fuse_opt_add_arg(&args, argv[i]);
		}
		fuse_opt_add_arg(&args, "-oro");
		fuse_opt_add_arg(&args, "-s");
	
		std::cout << "Everything looks OK, disk mounted\n";

		return fuse_main(args.argc, args.argv, &ops, 0);
	}
	catch (const std::exception& e)
	{
		std::cerr << "Error: " << e.what() << std::endl;
		std::cerr << std::endl;

		std::cerr << "Possible reasons:\n"
			"1) The file is corrupt.\n"
			"2) The file is not really a supported disk image, and needs further processing. "
			"The author has seen files with a .dmg extension, which were actually XAR archives "
			"containing a DMG.\n"
			"3) There is a bug in darling-dmg.\n";

		return 1;
	}
}
示例#6
0
uint16 ScummDiskImage::extractIndex(Common::WriteStream *out) {
	int i;
	uint16 reslen = 0;

	openDisk(1);

	if (_game.platform == Common::kPlatformApple2GS) {
		File::seek(142080);
	} else {
		File::seek(0);
	}

	// skip signature
	fileReadUint16LE();

	// write expected signature
	if (_game.platform == Common::kPlatformApple2GS) {
		reslen += write_word(out, 0x0032);
	} else {
		reslen += write_word(out, 0x0132);
	}

	// copy object flags
	for (i = 0; i < _numGlobalObjects; i++)
		reslen += write_byte(out, fileReadByte());

	// copy room offsets
	for (i = 0; i < _numRooms; i++) {
		_roomDisks[i] = fileReadByte();
		reslen += write_byte(out, _roomDisks[i]);
	}
	for (i = 0; i < _numRooms; i++) {
		_roomSectors[i] = fileReadByte();
		reslen += write_byte(out, _roomSectors[i]);
		_roomTracks[i] = fileReadByte();
		reslen += write_byte(out, _roomTracks[i]);
	}
	for (i = 0; i < _numCostumes; i++)
		reslen += write_byte(out, fileReadByte());
	for (i = 0; i < _numCostumes; i++)
		reslen += write_word(out, fileReadUint16LE());

	for (i = 0; i < _numScripts; i++)
		reslen += write_byte(out, fileReadByte());
	for (i = 0; i < _numScripts; i++)
		reslen += write_word(out, fileReadUint16LE());

	for (i = 0; i < _numSounds; i++)
		reslen += write_byte(out, fileReadByte());
	for (i = 0; i < _numSounds; i++)
		reslen += write_word(out, fileReadUint16LE());

	return reslen;
}
示例#7
0
bool ScummDiskImage::open(const Common::String &filename) {
	uint16 signature;

	// check signature
	openDisk(1);

	if (_game.platform == Common::kPlatformApple2GS) {
		File::seek(142080);
	} else {
		File::seek(0);
	}

	signature = fileReadUint16LE();
	if (signature != 0x0A31) {
		error("ScummDiskImage::open(): signature not found in disk 1");
		return false;
	}

	extractIndex(0); // Fill in resource arrays

	if (_game.features & GF_DEMO)
		return true;

	openDisk(2);

	if (_game.platform == Common::kPlatformApple2GS) {
		File::seek(143104);
		signature = fileReadUint16LE();
		if (signature != 0x0032)
			error("Error: signature not found in disk 2");
	} else {
		File::seek(0);
		signature = fileReadUint16LE();
		if (signature != 0x0132)
			error("Error: signature not found in disk 2");
	}


	return true;
}
示例#8
0
time_t tfs_readFileInfo(fileDescriptor FD) {

	int i, fd;
	int found = 0;
	char buff[BLOCKSIZE];
	char *fileName;
	time_t fileCreationTime = 0;
	drt_t *temp = dynamicResourceTable;

	if(disk_mount)
		fd = openDisk(disk_mount, 0);
	else
		return ERR_FILENOTMOUNTED;

	while(temp){
		if(temp->id == FD){
			break;
		}
		temp = temp->next;
	}

	if (!temp)
		return ERR_BADFILE;

	if(!temp->access)
		return ERR_READONLY;

	fileName = temp->fileName;

	/* looking for inode block */
	for(i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE && !found; i++){

		if(readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;

		if(buff[0] == 2){
			if(!strcmp(fileName, buff+4)){
				found = 1;
				break;
			}
		}
	}

	if(!found)
		return ERR_NOINODEFOUND;
	else
		memcpy(&fileCreationTime, buff+15, sizeof(time_t));

	return fileCreationTime;

}
示例#9
0
int main(int argc, char** argv)
{
	try
	{
		struct fuse_operations ops;
		struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
	
		if (argc < 3)
		{
			showHelp(argv[0]);
			return 1;
		}
	
		openDisk(argv[1]);
	
		memset(&ops, 0, sizeof(ops));
	
		ops.getattr = hfs_getattr;
		ops.open = hfs_open;
		ops.read = hfs_read;
		ops.release = hfs_release;
		//ops.opendir = hfs_opendir;
		ops.readdir = hfs_readdir;
		ops.readlink = hfs_readlink;
		//ops.releasedir = hfs_releasedir;
		ops.getxattr = hfs_getxattr;
		ops.listxattr = hfs_listxattr;
	
		for (int i = 0; i < argc; i++)
		{
			if (i == 1)
				;
			else
				fuse_opt_add_arg(&args, argv[i]);
		}
		fuse_opt_add_arg(&args, "-oro");
		fuse_opt_add_arg(&args, "-s");
	
		std::cout << "Everything looks OK, disk mounted\n";

		return fuse_main(args.argc, args.argv, &ops, 0);
	}
	catch (const std::exception& e)
	{
		std::cerr << "Error: " << e.what() << std::endl;
		return 1;
	}
}
示例#10
0
int tfs_seek(fileDescriptor FD, int offset)
{
	int i, fd, found = 0;
	char buff[BLOCKSIZE];
	char *fileName;

	if(disk_mount)
		fd = openDisk(disk_mount, 0);
	else
		return ERR_FILENOTMOUNTED;

	drt_t *temp = dynamicResourceTable;

	while(temp){
		if(temp->id == FD){
			break;
		}
		temp = temp->next;
	}

	if (!temp)
		return ERR_BADFILE;

	temp->fileptr = offset;
	fileName = temp->fileName;

	/* looking for inode block */
	for(i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE && !found; i++){
		if(readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;
		if(buff[0] == 2){
			if(!strcmp(fileName, buff+4)){
				found = 1;
				time_t now = time(0);
				memcpy(buff+19, &now, sizeof(time_t)); // set access time
				writeBlock(fd, i, buff);
				break;
			}
		}
	}

	if(DEBUG)
		printf("Seeked pointer to index %d\n",temp->fileptr);
	return 1;
}
示例#11
0
int tfs_defrag() {
	int i, j, fd, firstFree = 0;
	char buff[BLOCKSIZE], tempBuff[BLOCKSIZE];
	int size;

	if(disk_mount)
		fd = openDisk(disk_mount, 0);
	else
		return ERR_FILENOTMOUNTED;

	for(i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE; i++){
		readBlock(fd, i, buff);
		if(buff[0] == 4 && firstFree == 0)
			firstFree = i;
		else if(buff[0] == 4) {
			continue;
		}
		else if(firstFree != 0){
			if(buff[0] == 2) {
				size = (buff[13] << 8) & 0xFF00;
				size = size | (buff[14] & 0x00FF); 
				for(j = buff[2]; j < buff[2] + size; j++) {
					readBlock(fd, j, tempBuff);
					tempBuff[3] = (char)firstFree;
					writeBlock(fd, j, tempBuff);
				}
			}
			else if(buff[0] == 3){
				readBlock(fd, buff[3], tempBuff);
				if(tempBuff[2] == i){
					tempBuff[2] = firstFree;
				}
				writeBlock(fd, buff[3], tempBuff);
			}
			writeBlock(fd, firstFree, buff);
			buff[0] = 4;
			writeBlock(fd, i, buff);
			firstFree++;
		}
	}

	return 1;
}
示例#12
0
int tfs_displayFragments() {

	int i, fd, count = 0;
	char buff[BLOCKSIZE];

	if(disk_mount)
		fd = openDisk(disk_mount, 0);
	else
		return ERR_FILENOTMOUNTED;
	for(i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE; i++){

		if(readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;

		if(buff[0] == 1){
			printf("|S|");
			count++;
		}
		else if(buff[0] == 2){
			printf("|I|");
			count++;
		}
		else if(buff[0] == 3){
			printf("|D|");
			count++;
		}
		else if(buff[0] == 4){
			printf("| |");
			count++;
		}
		if(count == 4 || i == DEFAULT_DISK_SIZE / BLOCKSIZE - 1) {
			printf("\n");
			count = 0;
		}
		else
			printf(" -> ");
	}
	printf("\n");
	return 1;
}
示例#13
0
uint16 ScummDiskImage::extractResource(Common::WriteStream *out, int res) {
	const int AppleSectorOffset[36] = {
		0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 256,
		272, 288, 304, 320, 336, 352, 368,
		384, 400, 416, 432, 448, 464,
		480, 496, 512, 528, 544, 560
	};
	const int C64SectorOffset[36] = {
		0,
		0, 21, 42, 63, 84, 105, 126, 147, 168, 189, 210, 231, 252, 273, 294, 315, 336,
		357, 376, 395, 414, 433, 452, 471,
		490, 508, 526, 544, 562, 580,
		598, 615, 632, 649, 666
	};
	int i;
	uint16 reslen = 0;

	openDisk(_roomDisks[res]);

	if (_game.platform == Common::kPlatformApple2GS) {
		File::seek((AppleSectorOffset[_roomTracks[res]] + _roomSectors[res]) * 256);
	} else {
		File::seek((C64SectorOffset[_roomTracks[res]] + _roomSectors[res]) * 256);
	}

	for (i = 0; i < _resourcesPerFile[res]; i++) {
		uint16 len;
		do {
			// Note: len might be 0xFFFF for padding in zak-c64-german
			len = fileReadUint16LE();
			reslen += write_word(out, len);
		} while (len == 0xFFFF);

		for (len -= 2; len > 0; len--)
			reslen += write_byte(out, fileReadByte());
	}

	return reslen;
}
示例#14
0
/*
 * obtain disk information
 *       devnm   device name (possibly with the partition number)
 *       blksz   return block size (* )
 *       tblks   return the number of all blocks (* )
 *       return value error code
 *       if there is a partition number to the device name, return the specific information to that partition.
 *       argument marked with (* ) may be an address specified from external sources.
 */
EXPORT ER infoDisk( const UB *devnm, W *blksz, W *tblks )
{
	DISKCB	*dcb;
	W	pno, buf, n;

        /* initialize disk */
	pno = openDisk(devnm, &dcb);
	if ( pno < E_OK ) return pno;

        /* obtain disk information
         *       since there are addresses specified from external source, use writeMem()
	 */
	buf = dcb->part[pno].nblk;
	n = writeMem((UW)tblks, &buf, sizeof(W), sizeof(W));
	if ( n < sizeof(W) ) return E_MACV;

	buf = dcb->blksz;
	n = writeMem((UW)blksz, &buf, sizeof(W), sizeof(W));
	if ( n < sizeof(W) ) return E_MACV;

	return E_OK;
}
示例#15
0
int tfs_mkfs(char *filename, int nBytes) {
	int numBytes, open_fd;

   	numBytes = nBytes ? nBytes: DEFAULT_DISK_SIZE;  
	num_blocks = numBytes / BLOCKSIZE;
    
    if(filename < 0) {
        return BAD_FILENAME;
    }

   	open_fd = openDisk(filename, numBytes);

	if (numBytes < BLOCKSIZE * 2 && numBytes != 0) {
		return NOT_ENOUGH_BLOCKS;
	}

	if (open_fd < 0) {
		return BAD_FILE;
	}

	SuperBlock superblock;
	init_Superblock(&superblock, 1, num_blocks);
	if (writeBlock(open_fd, 0, &superblock) < 0) {
		return WRITE_ERROR;
	}
	
	Directory dir;
    Init_Directory(&dir, "/");
	if (writeBlock(open_fd, 1, &dir) < 0) {
		return WRITE_ERROR;
	}

	if (sb_initFreeblocks(open_fd, num_blocks) < 0) {
		return WRITE_ERROR;
	}
 
    close(open_fd);
	return SUCCESS;
}
示例#16
0
int tfs_makeRW(char *name) {

	int found = 0;
	int i = 0;
	int fd;
	char buff[BLOCKSIZE];

	if(disk_mount)
		fd = openDisk(disk_mount, 0);
	else
		return ERR_FILENOTMOUNTED;

	/* Loop over inode blocks */
	for(i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE && !found; i++){
		if(readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;
		if(buff[0] == 2){
			if(!strcmp(name, buff+4)){
				found = 1;
				buff[3] = 1; // Set RW byte
				time_t now = time(0);
				memcpy(buff+19, &now, sizeof(time_t)); // set access time
				memcpy(buff+23, &now, sizeof(time_t)); // set modification time
				writeBlock(fd, i, buff);
				break;
			}
		}
	}

	if(!found)
		return ERR_NOINODEFOUND;
	else {
		if(DEBUG)
			printf("%s access changes to Read-Write(%d)\n",name, buff[3]);
		return 1;
	}
}
示例#17
0
文件: boot.c 项目: imgtec/t-kernel
/*
 * Loading of the primary bootloader (PBOOT).
 *      return value     boot partition number
 */
LOCAL W loadPBoot( const UB *devnm, DISKCB **dcb_p )
{
	W	retry = 2;
	W	pno;
	DISKCB	*dcb;
	ER	err;

	while ( --retry >= 0 ) {

		/* opening of a disk device */
		pno = openDisk(devnm, &dcb);
		if ( pno < E_OK ) {
			err = pno;
			if ( err == E_IO ) continue; /* retry */
			return err;
		}

                /* If there is no partition specification in the device name, we assume the boot partition. */
		if ( pno == 0 ) pno = dcb->boot;

                /* read the boot block inside the target partiion */
		err = (*dcb->rwdisk)(dcb, dcb->part[pno].sblk, 1,
						PBootAddr, FALSE);
		if ( err < E_OK ) {
			if ( err == E_IO ) continue; /* retry */
			return err;
		}

                /* check the signature in the boot block */
		if ( *(UH*)(PBootAddr + 510) != BootSignature ) return E_BOOT;

		*dcb_p = dcb;
		return pno;
	}

	return E_IO;
}
示例#18
0
int tfs_mount(char *filename) {
	int i, diskNum;

	if (disk_mount)
		tfs_unmount();

	char buff[BLOCKSIZE];
	
	diskNum = openDisk(filename, 0);
	if (diskNum < 0)
		return ERR_BADFS;

	readBlock(diskNum, 0, buff);

	if (buff[1] != 0x45)   
		return ERR_BADFS;

	disk_mount = filename;

        if(DEBUG)
            printf("Mounted disk %s\n",disk_mount);
        close(diskNum);
	return 1;
}
示例#19
0
int tfs_writeFile(fileDescriptor FD, char *buffer, int size)
{
	int i,startIndex, endIndex, j, fd, numBlocks, found = 1, inode;
	char buff[BLOCKSIZE];
	char *fileName;
	drt_t *temp = dynamicResourceTable;

	if(disk_mount)
		fd = openDisk(disk_mount, 0);
	else
		return ERR_FILENOTMOUNTED;

	numBlocks = ceil((double)size / (double)BLOCKSIZE);

	while(temp){
		if(temp->id == FD){
		    break;
		}
		temp = temp->next;
	}
	if (!temp)
		return ERR_BADFILE;

	if(!temp->access)
		return ERR_READONLY;
	
        fileName = temp->fileName;

	found = 0;
	/* find inode */
	for(i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE && !found; i++){
		if(readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;
		if(buff[0] == 2){
			if(!strcmp(fileName, buff+4)){
				found = 1;
				inode = i;
                                break;
			}
		}
	}
	if(!found)
		return ERR_NOINODEFOUND;
	found = 1;

	/* find first free "numBlocks" block occurences to write to */
	for (i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE; i++) {
		if (readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;
		if (buff[0] == 4){
			for(j = i; j < i+numBlocks-1; j++){
				if (readBlock(fd, j, buff) < 0)
					return ERR_NOMORESPACE;  
				if (buff[0] != 4)
					found = 0;
			}
			if(found)
				break;
		}
	}
	if(!found)
		return ERR_NOFREEBLOCKFOUND;

	startIndex = i;
	endIndex = j;
	found = 0;

	/* setting template to make (write) file extents*/
	buff[0] = 3;
	buff[1] = 0x45;
	buff[3] = (char)inode;

	for(i = startIndex; i <= endIndex; i++){
		if(i != endIndex)
			buff[2] = i+1;
		else
			buff[2] = 0;

		strncpy(buff+4,buffer,BLOCKSIZE-4);
		writeBlock(fd, i, buff); 
	}

        if(DEBUG)
            printf("Write to free blocks starting at index %d and ending at %d. Wrote to the file extent -> %s\n",startIndex, endIndex,buff+4);

	/* inode update (1st block occurrence and size) */
	readBlock(fd, inode, buff);
	buff[2] = startIndex;
	buff[13] = (numBlocks & 0xFF00)>>8;
	buff[14] = numBlocks & 0x00FF;
	time_t now = time(0);
	memcpy(buff+19, &now, sizeof(time_t)); // set access time
	memcpy(buff+23, &now, sizeof(time_t)); // set modification time
	writeBlock(fd, inode, buff);
	
        if(DEBUG)
            printf("Updated inode block %d to point to index %d and have size %d %d\n",inode, buff[2],buff[13],buff[14]);
        
        close(fd);

	return 1;
}
示例#20
0
int tfs_deleteFile(fileDescriptor FD)
{
	int i, fd, size, firstBlock, numBlocks;
	int found = 0;
	char buff[BLOCKSIZE];
	char *fileName;
	drt_t *temp = dynamicResourceTable;
	
	if(disk_mount)
		fd = openDisk(disk_mount, 0);
	else
		return ERR_FILENOTMOUNTED;
	

	while(temp){
		if(temp->id == FD){
			break;
		}
		temp = temp->next;
	}
	
	if (!temp)
		return ERR_BADFILE;

	fileName = temp->fileName;

	/* looking for inode block */
	for(i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE && !found; i++){
		if(readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;
		if(buff[0] == 2){
			if(!strcmp(fileName, buff+4)){
				if (buff[3] == 0)
					return ERR_READONLY;
				found = 1;
				firstBlock = buff[2];
				size = (buff[13] << 8) & 0xFF00;
				size = size | (buff[14] & 0x00FF); 
				numBlocks = size;
				break;
			}
		}
	}
	if(!found)
		return ERR_NOINODEFOUND;

	buff[0] = 4;

	/*deleting inode */
	writeBlock(fd, i,buff);

        if(DEBUG)
            printf("Deleted inode block at index %d\n",i);
	/*deleting file extents*/
	for(i = firstBlock; i < firstBlock + numBlocks; i++) {
		writeBlock(fd, i, buff);    
	}

        if(DEBUG)
            printf("Deleted file extents from index %d to %d\n",firstBlock, firstBlock+numBlocks);
	removeDrtNode(FD);
	close(fd);

	return 1;
}
示例#21
0
int tfs_rename(fileDescriptor FD, char *name){
	int i, fd,inodeLoc;
	int found = 0;
	char buff[BLOCKSIZE];
	char *fileName;
	drt_t *temp = dynamicResourceTable;

	if(disk_mount)
		fd = openDisk(disk_mount, 0);
	else
		return ERR_FILENOTMOUNTED;

	while(temp){
		if(temp->id == FD){
			break;
		}
		temp = temp->next;
	}

	if (temp == NULL)
		return ERR_BADFILE;

	fileName = temp->fileName;

	/* change name in inode block */

	for(i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE && !found; i++){
		if(readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;
		if(buff[0] == 2){
			if(!strcmp(fileName, buff+4)){
				found = 1;
				break;
			}
		}
	}
	inodeLoc = i;
	if(!found)
		return ERR_NOINODEFOUND;

	/* looking for inode block */
	if(DEBUG)
		printf("Inode block name changes from %s to ",buff+4);
	strcpy(buff+4,name);
	time_t now = time(0);
	memcpy(buff+19, &now, sizeof(time_t)); // set access time
	memcpy(buff+23, &now, sizeof(time_t)); // set modification time
	writeBlock(fd,inodeLoc,buff);
	if(DEBUG)
		printf("%s\n",buff+4);

	/* change name in drt */
	if(DEBUG)
		printf("Drt name changed from %s to ",temp->fileName);
	temp->fileName = name;
	if(DEBUG)
		printf("%s\n",temp->fileName);

	close(fd);
	return 1;
}
示例#22
0
fileDescriptor tfs_openFile(char *name) {
	int fd = ERR_BADFILE;
	char buff[BLOCKSIZE];
	int i = 0;
	int found = 0;

	/* check if we have a mounted disk */
	if(disk_mount)
		if (checkIfAlreadyOpen(name) >= 0)
			return checkIfAlreadyOpen(name);
		else
			fd = openDisk(disk_mount, 0);
	else
		return ERR_FILENOTMOUNTED;

	/* Loop over inode blocks to see if file already exists */
	for(i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE && !found; i++){
		if(readBlock(fd, i, buff) < 0)
			return ERR_NOMORESPACE;
		if(buff[0] == 2){
			if(!strcmp(name, buff+4)){
				found = 1;
				break;
			}
		}
	}

	time_t now = time(0);

	if (!found) {
		/* find first free location to place an inode block */
		for (i = 0; i < DEFAULT_DISK_SIZE / BLOCKSIZE; i++) {
			if (readBlock(fd, i, buff) < 0)
				return ERR_NOMORESPACE;
			if (buff[0] == 4)
				break;
		}
		/* buff is now a template of the inode block
		 * i is the block number of the soon to be inode
		 * label it as an inode
		 * opy name to front end
		 */
		buff[0] = 2; // set as inode block
		buff[3] = 1; // set as RW by default
		memcpy(buff+4, name, strlen(name));

		memcpy(buff+15, &now, sizeof(time_t)); /* set creation time */
		memcpy(buff+19, &now, sizeof(time_t)); /* set access time */
		memcpy(buff+23, &now, sizeof(time_t)); /* set modification time */
                writeBlock(fd, i, buff);

		if(DEBUG)
			printf("Created inode block with filename %s\n",buff+4);
	} else {
		memcpy(buff+19, &now, sizeof(time_t)); // set access time
	}

	/* add a new entry in drt that refers to this filename
	 *     returns a fileDescriptor (temp->id) */
	drt_t *temp;
	if (dynamicResourceTable == NULL) {
		temp = calloc(1, sizeof(drt_t));
		temp->fileName = name;
		temp->access = 1;
		dynamicResourceTable = temp;

		if(DEBUG)
			printf("Created first dynamic resource table node with filename %s\n",temp->fileName);

	} else {
		temp = addDrtNode(name, dynamicResourceTable, buff[3]);
	}


	close(fd);

	return temp->id;
}