📄 file.c
字号:
num = 0;
else if( (fp->fcb->file_length - curp - (BLOCKSIZE - n)) % BLOCKSIZE )
num = (fp->fcb->file_length - curp - (BLOCKSIZE - n)) / BLOCKSIZE + 1;
else
num = (fp->fcb->file_length - curp - (BLOCKSIZE - n)) / BLOCKSIZE;
/*m is the num of block and n is the offset in the m+1 block
which is occupied by remained data */
size = size - ( BLOCKSIZE - n );
m = size / BLOCKSIZE;
n = size % BLOCKSIZE;
/*remained data will be written in the i+1 subfcb block*/
if( m + 1 + j + 1 < NUM2 ){
for( s = 1; s < m + 1; s++ ){
if( num != 0 ){
blkid = *((unsigned int*)((SUBFCB_S*)p+1) + j + s);
num--;
}
else{
if( (blkid = get_block( fp->fcb->partition )) < 0 ) goto UNLOCK;
*((unsigned int*)((SUBFCB_S*)p+1) + j + s) = blkid;
}
write_block( &fp->link, blkid, 0, BLOCKSIZE , buffer, NORMAL_WRITE );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
if( num != 0 ){
blkid = *((unsigned int*)((SUBFCB_S*)p+1) + j + s);
num--;
}
else{
if( (blkid = get_block( fp->fcb->partition )) < 0 ) goto UNLOCK;
*((unsigned int*)((SUBFCB_S*)p+1) + j + s) = blkid;
}
write_block( &fp->link, blkid, 0, n , buffer, NORMAL_WRITE );
count += n;
if( fp->fcb->file_length < curp + count )
fp->fcb->file_length = curp + count;
// return count;
goto UNLOCK;
}
/*remained data will be written in the i+1 subfcb block and the i+2...subfcb block*/
else{
/*write in the i+1 subfcb block*/
for( s = 1; s <= NUM2 - ( j + 1 ); s++ ){
if( num != 0 ){
blkid = *((unsigned int*)((SUBFCB_S*)p+1) + j + s);
num--;
}
else{
if( (blkid = get_block( fp->fcb->partition )) < 0 ) goto UNLOCK;
*((unsigned int*)((SUBFCB_S*)p+1) + j + s) = blkid;
}
write_block( &fp->link, blkid, 0, BLOCKSIZE , buffer, NORMAL_WRITE );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
/* create the fcb list and write the data
s + 1 is num of subfcb
t + 1 is block num in last subfcb*/
s = (m - (NUM2 - (j + 1))) / NUM2;
t = (m - (NUM2 - (j + 1))) % NUM2;
/* write s subfcbs */
for( i = 0; i < s; i++ ){
if( num != 0 ){
if( ((FCB_S*)q)->nextfcb == NULL ){
p = (unsigned int *)read_FCB(((FCB_S*)q)->nextblk, NULL);
((FCB_S*)q)->nextfcb = (SUBFCB_S*)p;
}
else
p = ((FCB_S*)q)->nextfcb;
}
else{
if ( (p = (unsigned int*)new_SUBFCB( fp->fcb )) == NULL ) goto UNLOCK;
((FCB_S*)q)->nextfcb = (SUBFCB_S*)p;
((FCB_S*)q)->nextblk = ((SUBFCB_S*)p)->blkid;
}
q = p;
((FCB_S*)q)->flag = BE_USED;
for( u = 0; u < NUM2; u++ ){
if( num != 0 ){
blkid = *((unsigned int*)((SUBFCB_S*)q+1) + u);
num--;
}
else{
if( (blkid = get_block( fp->fcb->partition )) < 0 ) goto UNLOCK;
*((unsigned int*)((SUBFCB_S*)q+1) + u) = blkid;
}
write_block( &fp->link, blkid, 0, BLOCKSIZE , buffer, NORMAL_WRITE );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
}
/*write t blocks in the s+1 sub*/
if( num != 0 ){
if( ((FCB_S*)q)->nextfcb == NULL ){
p = (unsigned int *)read_FCB(((FCB_S*)q)->nextblk, NULL);
((FCB_S*)q)->nextfcb = (SUBFCB_S*)p;
}
else
p = ((FCB_S*)q)->nextfcb;
}
else{
if( (p = (unsigned int*)new_SUBFCB( fp->fcb )) == NULL )goto UNLOCK;
((FCB_S*)q)->nextfcb = (SUBFCB_S*)p;
((FCB_S*)q)->nextblk = ((SUBFCB_S*)p)->blkid;
}
q = p;
((FCB_S*)q)->flag = BE_USED;
for( i = 0; i < t; i++ ){
if( num != 0 ){
blkid = *((unsigned int*)((SUBFCB_S*)q+1) + i);
num--;
}
else{
if( (blkid = get_block( fp->fcb->partition )) < 0 ) goto UNLOCK;
*((unsigned int*)((SUBFCB_S*)q+1) + i) = blkid;
}
write_block( &fp->link, blkid, 0, BLOCKSIZE , buffer, NORMAL_WRITE );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
/*write the t+1 block in the s+1 sub*/
if( num != 0 ){
blkid = *((unsigned int*)((SUBFCB_S*)q+1) + i);
num--;
}else{
if( (blkid = get_block( fp->fcb->partition )) < 0 ) goto UNLOCK;
*((unsigned int*)((SUBFCB_S*)q+1) + i) = blkid;
}
write_block( &fp->link, blkid, 0, n , buffer, NORMAL_WRITE );
count += n;
if( fp->fcb->file_length < curp + count )
fp->fcb->file_length = curp + count;
// return count;
goto UNLOCK;
}
}
}
UNLOCK:
// write_block( &fp->link, fp->fcb->blkid, 0, BLOCKSIZE, fp->fcb, NORMAL_WRITE );
// fp->curp += count;
curp += count;
set_curp( fp, curp );
/* UNLOCK should be added here!*/
FS_UNLOCK( FILE_S_LOCK_ID );
return count;
}
/* this function is only used in this C file and only used by read_FILE( ) */
int read_cycle( FILE_S* fp, char* buffer, unsigned int size, unsigned short m, unsigned short n )
{
unsigned int blkid,count;
unsigned short i,j,s,t,u;
unsigned int *p,*q;
count = 0;
if( m < NUM1 ){/* m < NUM1 follow */
blkid = *(unsigned int *)((unsigned int *)(fp->fcb + 1) + m);
/* the data to be readed in the m+1 block */
if( n + size < BLOCKSIZE ){
read_block( &fp->link, blkid, n, size, buffer );
count += size;
return count;
}
/* the data to be readed will exceed the m+1 block */
else{
read_block( &fp->link, blkid, n, BLOCKSIZE - n , buffer );
buffer += ( BLOCKSIZE - n );
count += ( BLOCKSIZE - n );
size = size - ( BLOCKSIZE - n );
/*i is the num of block and j is the offset in the i+1 block
which is occupied by remained data */
i = size / BLOCKSIZE;
j = size % BLOCKSIZE;
/*remained data will be readed in the fcb block*/
if( m + 1 + i + 1 < NUM1 ){
for( n = 1; n < i + 1; n++ ){
blkid = *(unsigned int*)((unsigned int *)(fp->fcb + 1) + m + n);
read_block( &fp->link, blkid, 0, BLOCKSIZE , buffer );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
blkid = *(unsigned int *)((unsigned int *)(fp->fcb + 1) + m + n);
read_block( &fp->link, blkid, 0, j , buffer );
count += j;
return count;
}
/*remained data will be readed in the fcb block and subfcb block */
else{
/* readed in the fcb block */
for( n = 1; n <= NUM1 - ( m + 1 ); n++ ){
blkid = *(unsigned int *)((unsigned int *)(fp->fcb + 1) + m + n);
read_block( &fp->link, blkid, 0, BLOCKSIZE , buffer );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
/* create the fcb list and read the data
s + 1 is num of subfcb
t + 1 is block num in last subfcb*/
s = (i - (NUM1 - (m + 1))) / NUM2;
t = (i - (NUM1 - (m + 1))) % NUM2;
/*read s subfcbs*/
q = (unsigned int *)fp->fcb;
for( n = 0; n < s; n++ ){
if( ((FCB_S*)q)->nextfcb == NULL ){
p = (unsigned int*)read_FCB( ((FCB_S*)q)->nextblk, NULL );
((FCB_S*)q)->nextfcb = (struct _subfcb_s *)p;
}
else
p = ((FCB_S*)q)->nextfcb;
q = p;
for( u = 0; u < NUM2; u++ ){
blkid = *(unsigned int *)((unsigned int*)((SUBFCB_S*)p + 1) + u);
read_block( &fp->link, blkid, 0, BLOCKSIZE , buffer );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
}
/*read t blocks in the s+1 subfcb*/
if( ((FCB_S*)q)->nextfcb == NULL ){
p = (unsigned int*)read_FCB( ((FCB_S*)q)->nextblk, NULL );
((FCB_S*)q)->nextfcb = (struct _subfcb_s *)p;
}
else
p = ((FCB_S*)q)->nextfcb;
for( n = 0; n < t; n++ ){
blkid = *(unsigned int *)((unsigned int*)((SUBFCB_S*)p + 1) + n);
read_block( &fp->link, blkid, 0, BLOCKSIZE , buffer );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
/*read the t+1 block in the s+1 subfcb*/
blkid = *(unsigned int *)((unsigned int*)((SUBFCB_S*)p + 1) + n);
read_block( &fp->link, blkid, 0, j , buffer );
count += j;
return count;
}
}
}
/* m > NUM1 follow */
else{
/*i is num of whole subfcb will be excced
j is num of block will be exceed in the last subfcb */
i = (m - NUM1) / NUM2;
j = (m - NUM1) % NUM2;
/* create the fcb list */
q = (unsigned int *)fp->fcb;
for( s = 0; s <= i; s++ ){
if( ((FCB_S*)q)->nextfcb == NULL){
p = (unsigned int*)read_FCB( ((FCB_S*)q)->nextblk, NULL );
((FCB_S*)q)->nextfcb = (struct _subfcb_s *)p;
}
else
p = ((FCB_S*)q)->nextfcb;
q = p;
}
blkid = *(unsigned int *)((unsigned int*)((SUBFCB_S*)p + 1) + j);
/* the data to be readed in the j+1 block */
if( n + size < BLOCKSIZE ){
read_block( &fp->link, blkid, n, size, buffer );
count += size;
return count;
}
/* the data to be readed will exceed the j+1 block */
else{
read_block( &fp->link, blkid, n, BLOCKSIZE - n , buffer );
buffer += ( BLOCKSIZE - n );
count += ( BLOCKSIZE - n );
/*m is the num of block and n is the offset in the m+1 block
which is occupied by remained data */
size = size - ( BLOCKSIZE - n );
m = size / BLOCKSIZE;
n = size % BLOCKSIZE;
/*remained data will be readed in the i+1 subfcb block*/
if( m + 1 + j + 1 < NUM2 ){
for( s = 1; s < m + 1; s++ ){
blkid = *(unsigned int *)((unsigned int*)((SUBFCB_S*)p + 1) + j + s);
read_block( &fp->link, blkid, 0, BLOCKSIZE , buffer );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
blkid = *(unsigned int *)((unsigned int*)((SUBFCB_S*)p + 1) + j + s);
read_block( &fp->link, blkid, 0, n , buffer );
count += n;
return count;
}
/*remained data will be written in the i+1 subfcb block and the i+2...subfcb block*/
else{
/*read in the i+1 subfcb block*/
for( s = 1; s <= NUM2 - ( j + 1 ); s++ ){
blkid = *(unsigned int *)((unsigned int*)((SUBFCB_S*)p + 1) + j + s);
read_block( &fp->link, blkid, 0, BLOCKSIZE , buffer );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
/* create the fcb list and read the data
s + 1 is num of subfcb
t + 1 is block num in last subfcb*/
s = (m - (NUM2 - (j + 1))) / NUM2;
t = (m - (NUM2 - (j + 1))) % NUM2;
for( i = 0; i < s; i++ ){
if( ((FCB_S*)q)->nextfcb == NULL ){
p = (unsigned int*)read_FCB( ((FCB_S*)q)->nextblk, NULL );
((FCB_S*)q)->nextfcb = (struct _subfcb_s *)p;
}
else
p = ((FCB_S*)q)->nextfcb;
q = p;
for( u = 0; u < NUM2; u++ ){
blkid = *(unsigned int *)((unsigned int*)((SUBFCB_S*)p + 1) + u);
read_block( &fp->link, blkid, 0, BLOCKSIZE , buffer );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
}
/*write t blocks in the s+1 sub*/
if( ((FCB_S*)q)->nextfcb == NULL ){
p = (unsigned int*)read_FCB( ((FCB_S*)q)->nextblk, NULL );
((FCB_S*)q)->nextfcb = (struct _subfcb_s *)p;
}
else
p = ((FCB_S*)q)->nextfcb;
for( i = 0; i < t; i++ ){
blkid = *(unsigned int *)((unsigned int*)((SUBFCB_S*)p + 1) + i);
read_block( &fp->link, blkid, 0, BLOCKSIZE , buffer );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
/*write the t+1 block in the s+1 sub*/
blkid = *(unsigned int *)((unsigned int*)((SUBFCB_S*)p + 1) + i);
read_block( &fp->link, blkid, 0, n , buffer );
count += n;
return count;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -