⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rdwt.c

📁 基于UNIX6的文件系统模拟程序
💻 C
字号:
#include <stdio.h>#include "filesys.h"unsigned int read(fileid,buf,size)int fileid;char *buf;unsigned int size;{	unsigned long off;	int block,block_off,i,j;	struct inode *inode;	char *temp_buf;	inode=sys_ofile[user[user_id].u_ofile[fileid]].f_inode;	if(!(sys_ofile[user[user_id].u_ofile[fileid]].f_flag&FREAD))	{		printf("\nthe file is not opened for read\n");		return 0;	}	temp_buf = buf;	off = sys_ofile[user[user_id].u_ofile[fileid]].f_off;	if((off+size)>inode->di_size)		size=inode->di_size-off;	block_off=off%BLOCKSIZ;	block=off/BLOCKSIZ;	if(block_off+size < BLOCKSIZ)	{		fseek(fd,DATASTART+inode->di_addr[block]*BLOCKSIZ+block_off,SEEK_SET);		fread(buf,1,size,fd);		return size;	}	fseek(fd,DATASTART+inode->di_addr[block]*BLOCKSIZ+block_off,SEEK_SET);	fread(temp_buf,1,BLOCKSIZ-block_off,fd);	temp_buf += BLOCKSIZ-block_off;	j=(inode->di_size-off-block_off)/BLOCKSIZ;	for(i=0;i<(size-block_off)/BLOCKSIZ;i++)	{		fseek(fd,DATASTART+inode->di_addr[j+i]*BLOCKSIZ,SEEK_SET);		fread(temp_buf,1,BLOCKSIZ,fd);		temp_buf += BLOCKSIZ;	}	block_off=(size-block_off)%BLOCKSIZ;	block=inode->di_addr[off+size/BLOCKSIZ+1];	fseek(fd,DATASTART+block*BLOCKSIZ,SEEK_SET);	fread(temp_buf,1,BLOCKSIZ,fd);		sys_ofile[user[user_id].u_ofile[fileid]].f_off += size;	return size;}unsigned int write(fileid,buf,size)int fileid;char * buf;unsigned int size;{	unsigned long off;	int block,block_off,i,j;	struct inode *inode;	char *temp_buf,*a;	inode = sys_ofile[user[user_id].u_ofile[fileid]].f_inode;


//	printf("in write mode%d",inode->di_mode);	if(!sys_ofile[user[user_id].u_ofile[fileid]].f_flag&FWRITE)	{		printf("\nthe file is not opened for write\n");		return 0;	}	temp_buf = buf;	off= sys_ofile[user[user_id].u_ofile[fileid]].f_off;	block_off=off%BLOCKSIZ;	block=off/BLOCKSIZ;

	if(0 == off&&inode->di_size==0)
		inode->di_addr[0] = balloc();	if(block_off+size<BLOCKSIZ)	{		fseek(fd,DATASTART+inode->di_addr[block]*BLOCKSIZ+block_off,SEEK_SET);		fwrite(temp_buf,1,size,fd);
		//the below is just for debug
		//fseek(fd,DATASTART+inode->di_addr[block]*BLOCKSIZ+block_off,SEEK_SET);
		//a = malloc(20);
		//fread(a,1,size,fd);
		//printf("fffffffff:%s",a);
		inode->di_size += size;
		sys_ofile[user[user_id].u_ofile[fileid]].f_off += size;//		return size;	}	fseek(fd,DATASTART+inode->di_addr[block]*BLOCKSIZ+block_off,SEEK_SET);	fwrite(temp_buf,1,BLOCKSIZ-block_off,fd);	temp_buf+=BLOCKSIZ-block_off;	for(i=0;i<(size-BLOCKSIZ+block_off)/BLOCKSIZ;i++)//size-block_off	{		inode->di_addr[block+1+i]=balloc();		fseek(fd,DATASTART+inode->di_addr[block+1+i]*BLOCKSIZ,SEEK_SET);		fwrite(temp_buf,1,BLOCKSIZ,fd);		temp_buf += BLOCKSIZ;	}	block_off = (size-BLOCKSIZ+block_off)%BLOCKSIZ;	block=inode->di_addr[(off+size)/BLOCKSIZ+1]=balloc();	fseek(fd,DATASTART+block*BLOCKSIZ,SEEK_SET);	fwrite(temp_buf,1,block_off,fd);
		inode->di_size += size;	sys_ofile[user[user_id].u_ofile[fileid]].f_off += size;//	return size;}	/**/	int seek(fileid,offset,origin)
unsigned int fileid;
long offset;
int origin;
{
	struct inode *inode;
	inode = sys_ofile[user[user_id].u_ofile[fileid]].f_inode;
	switch(origin)
	{
		/* 0 is from the head,2 is from the end,1 is from the current position*/
	case 0:
		{
			if(offset>inode->di_size)
				sys_ofile[user[user_id].u_ofile[fileid]].f_off = inode->di_size;
			else
				sys_ofile[user[user_id].u_ofile[fileid]].f_off = offset;
			break;
		}
	case 1:
		{
			if(offset+offset>inode->di_size)
				sys_ofile[user[user_id].u_ofile[fileid]].f_off = inode->di_size;
			else
				sys_ofile[user[user_id].u_ofile[fileid]].f_off = offset+offset;
			break;
		}
	case 2:
		{
			if(offset>inode->di_size)
				sys_ofile[user[user_id].u_ofile[fileid]].f_off = 0;
			else
				sys_ofile[user[user_id].u_ofile[fileid]].f_off = inode->di_size-offset;
			break;
		}
	default:
		break;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -