📄 readwe.c
字号:
/************************************************/
/* */
/* readwrite.c */
/* */
/************************************************/
#include <stdio.h>
#include "filsys.h"
int read( int fh, char * buf, unsigned int nbyte )
{
unsigned long off;
int block, block_off, i, sys_ip;
struct inode * inode;
// char * temp_buf;
sys_ip = user[user_id].u_ofile[fh];
inode = sys_ofile[sys_ip].f_inode;
if( !( sys_ofile[sys_ip].f_flag & O_RDONLY ))
{
printf( "The file is not opened for read\n" );
return -1;
}
off = sys_ofile[sys_ip].f_off;
block = off / BLOCKSIZ; /* 从inode->i_addr[block]号盘块读起 */
block_off = off % BLOCKSIZ; /* block_off是在block号盘块中的偏移量 */
/* 读写指针在文件末尾,读出字数为0 */
if( off == inode->i_size )
return 0;
/* 读文件不用跨越盘块,即在一盘块中就可读出nbyte字 */
if( block_off + nbyte < BLOCKSIZ )
{
fseek( fd,
DATASTART + inode->i_addr[block] * BLOCKSIZ + block_off,
SEEK_SET );
fread( buf, 1, nbyte, fd );
// free( temp_buf );
return nbyte;
}
/* 读文件跨越盘块,即所读的文件有多个盘块 */
/* 先读block盘块 */
fseek( fd,
DATASTART + inode->i_addr[block] * BLOCKSIZ + block_off,
SEEK_SET );
fread( buf, 1, BLOCKSIZ - block_off, fd );
buf += BLOCKSIZ - block_off;
block++;
/* 所读的文件没有到末尾 */
if( nbyte + off < inode->i_size )
{
for( i = 0; i < ( nbyte - block_off ) / BLOCKSIZ; i++ )
{
fseek( fd,
DATASTART + inode->i_addr[i + block] * BLOCKSIZ,
SEEK_SET );
fread( buf, 1, BLOCKSIZ, fd );
buf += BLOCKSIZ;
}
// block_off = ( nbyte - block_off ) % BLOCKSIZ;
// block = inode->i_addr[off + nbyte / BLOCKSIZ + 1];
fseek( fd, DATASTART + inode->i_addr[i + block] * BLOCKSIZ, SEEK_SET );
fread( buf, 1, ( nbyte - block_off ) % BLOCKSIZ, fd );
/* 置文件读/写指针位置 */
sys_ofile[sys_ip].f_off += nbyte;
return nbyte;
}
/* 所读的文件到末尾 */
for( i = block; i < inode->i_size / BLOCKSIZ; i++ )
{
fseek( fd,
DATASTART + inode->i_addr[i] * BLOCKSIZ,
SEEK_SET );
fread( buf, 1, BLOCKSIZ, fd );
buf += BLOCKSIZ;
}
fseek( fd, DATASTART + inode->i_addr[i] * BLOCKSIZ, SEEK_SET );
fread( buf, 1, inode->i_size % BLOCKSIZ, fd );
sys_ofile[sys_ip].f_off = inode->i_size;
return ( inode->i_size - off );
}
int write( int fh, char * buf, unsigned int nbyte )
{
unsigned long off;
int block, block_off, i, sys_ip;
struct inode * inode;
char * temp_buf;
sys_ip = user[user_id].u_ofile[fh];
inode = sys_ofile[sys_ip].f_inode;
if( !( sys_ofile[sys_ip].f_flag & O_WRONLY ))
{
printf( "The file is not opened for write\n" );
return -1;
}
off = sys_ofile[sys_ip].f_off;
block = off / BLOCKSIZ; /* 从inode->i_addr[block]号盘块起开始写 */
block_off = off % BLOCKSIZ; /* block_off是在block号盘块中的偏移量 */
// temp_buf = buf;
/* 写文件不用跨越盘块,即在一盘块中就可写入nbyte字 */
if( block_off + nbyte < BLOCKSIZ )
{
fseek( fd,
DATASTART + inode->i_addr[block] * BLOCKSIZ + block_off,
SEEK_SET );
fwrite( buf, 1, nbyte, fd );
if( inode->i_size < nbyte )
inode->i_size = nbyte;
return nbyte;
}
/* 写文件跨越盘块,即所写的文件有多个盘块 */
/* 先往block盘块写入 */
fseek( fd,
DATASTART + inode->i_addr[block] * BLOCKSIZ + block_off,
SEEK_SET );
fwrite( temp_buf, 1, BLOCKSIZ - block_off, fd );
buf += BLOCKSIZ - block_off;
/* 所写的文件没有到末尾 */
if( nbyte + off < inode->i_size )
{
for( i = 0; i < ( nbyte - block_off ) / BLOCKSIZ; i++ )
{
fseek( fd,
DATASTART + inode->i_addr[i + block] * BLOCKSIZ,
SEEK_SET );
fwrite( buf, 1, BLOCKSIZ, fd );
buf += BLOCKSIZ;
}
// block_off = ( nbyte - block_off ) % BLOCKSIZ;
// block = inode->i_addr[off + nbyte / BLOCKSIZ + 1];
fseek( fd, DATASTART + inode->i_addr[i + block] * BLOCKSIZ, SEEK_SET );
fwrite( buf, 1, ( nbyte - block_off ) % BLOCKSIZ, fd );
/* 置文件读/写指针位置 */
sys_ofile[sys_ip].f_off += nbyte;
if( inode->i_size < nbyte )
inode->i_size = nbyte;
return nbyte;
}
/* 所读的文件到末尾 */
/* 写到文件尾 */
for( i = block; i < inode->i_size / BLOCKSIZ; i++ )
{
fseek( fd,
DATASTART + inode->i_addr[i] * BLOCKSIZ,
SEEK_SET );
fwrite( buf, 1, BLOCKSIZ, fd );
buf += BLOCKSIZ;
}
/* 申请空间再写 */
for( ; i < ( nbyte + off ) / BLOCKSIZ; i++ )
{
inode->i_addr[i] = balloc();
fseek( fd,
DATASTART + inode->i_addr[i] * BLOCKSIZ,
SEEK_SET );
fwrite( temp_buf, 1, BLOCKSIZ, fd );
buf += BLOCKSIZ;
}
fseek( fd, DATASTART + inode->i_addr[i] * BLOCKSIZ, SEEK_SET );
fwrite( buf, 1, ( nbyte + off ) % BLOCKSIZ, fd );
inode->i_size = nbyte + off;
sys_ofile[sys_ip].f_off = inode->i_size;
if( inode->i_size < nbyte )
inode->i_size = nbyte;
return nbyte;
/*
for( i = 0; i < ( nbyte - block_off ) / BLOCKSIZ; i++ )
{
inode->i_addr[block + 1 - i] = balloc();
fseek( fd,
DATASTART + inode->i_addr[block + 1 + i] * BLOCKSIZ,
SEEK_SET );
fwrite( temp_buf, 1, BLOCKSIZ, fd );
buf += BLOCKSIZ;
}
block_off = ( nbyte - block_off ) % BLOCKSIZ;
block = inode->i_addr[off+nbyte / BLOCKSIZ + 1] = balloc();
fseek( fd, DATASTART + block * BLOCKSIZ, SEEK_SET );
fwrite( buf, 1, block_off, fd );
sys_ofile[user[user_id].u_ofile[fh]].f_off += nbyte;
return nbyte;*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -