📄 file.c
字号:
/*************************************************************************
* Copyright 2002 National ASIC Center, All right Reserved
*
* FILE NAME: file.c
* PROGRAMMER: DSA
* Date of Creation: 2002/08/05
* DESCRIPTION: This module is about operation of
* imformation of files which have been opened
*
* NOTE: In this module,we don't check the parameter
here, sizeof( FCB_S ) and sizeof( SUBFCB_S ) must be 4n
*
* FUNCTIONS LIST:
* -------------------------------------------------------------------------
* new_FILE()
* del_FILE()
* read_FILE()
* write_FILE()
*-------------------------------------------------------------------------
* MODIFICATION HISTORY
*
* 2002/08/05 by DSA Creation of this file
*
******************************************************************************/
//#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <filesys\fs.h>
#include "file.h"
#include "block.h"
#include "iobuf.h"
#include "fcb.h"
#include "multsk.h"
#define NUM1 ((BLOCKSIZE - sizeof(FCB_S)) / 4)
#define NUM2 ((BLOCKSIZE - sizeof(SUBFCB_S)) / 4)
int read_cycle( FILE_S* fp, char* buffer, unsigned int size, unsigned short m, unsigned short n );
//----------------------------
/*unsigned long get_curp( unsigned long curp )
*{
* return curp;
*}
*/
//----------------------------
int open_file( char* path, FILE_S* fp, int mode )
{
FCB_S* p;
p = (FCB_S*)get_FCB( path, mode );
if( (int)p < 0 ){
return FS_ERROR;
}
// p = (SUBFCB_S*)read_FCB( p->blkid, NULL );
get_fscb_item( fp );
/* follow we fill the struct of FILE */
fp->flag = BE_USED;
// fp->curp = 0;
fp->magic = FILE_MAGIC;
fp->fcb = p;
// fp->link = NULL;
INIT_LIST_HEAD( &fp->link );
memcpy( fp->path, path, strlen( path ) );
fp->use = 1;
/*fp->curfcb = ; here it is no use*/
/*fp->curblkid = q->blkid; here it is no use*/
return FS_OK;
}
int close_file( FILE_S* fp )
{
del_RAMFCB( fp->fcb, &fp->link );
// sync_link( &fp->link );
return FS_OK;
}
int read_file( FILE_S* fp, char* buffer, unsigned int size )
{
unsigned int curp;
unsigned short m,n;
if( fp->magic != FILE_MAGIC ) return FS_ERR_MAGIC;
/* LOCK should be added here! */
FS_LOCK( FILE_S_LOCK_ID );
// curp = (unsigned int)get_curp( fp->curp );02-8-24 16:31
curp = (unsigned int)get_curp( fp );
/* curp is error */
if( curp >= fp->fcb->file_length ){
FS_UNLOCK( FILE_S_LOCK_ID );
return FS_ERR_PAR;
}
/* file length is not enough */
else if( curp + size > fp->fcb->file_length ){
m = curp / BLOCKSIZE;
n = curp % BLOCKSIZE;
size = fp->fcb->file_length - curp;
}
else if( curp + size <= fp->fcb->file_length ){/* file length is enough*/
m = curp / BLOCKSIZE;
n = curp % BLOCKSIZE;
}
/* transfer read_cycle( ) */
size = read_cycle( fp, buffer, size, m, n );
curp += size;
set_curp( fp, curp );
/* UNLOCK should be added here!*/
FS_UNLOCK( FILE_S_LOCK_ID );
return size;
}/* end of read_FILE( ) */
int write_file( FILE_S* fp, char* buffer, unsigned int size )
{
unsigned int curp;
unsigned int num,count;
unsigned short m,n,i,j,s,t,u,v;
unsigned int *p,*q;
int blkid;
if( fp->magic != FILE_MAGIC ) return FS_ERR_MAGIC;
/* LOCK should be added here! */
FS_LOCK( FILE_S_LOCK_ID );
/* judge the curp and file_length */
// curp = (unsigned int)get_curp( fp->curp );//02-8-24 16:31
curp = (unsigned int)get_curp( fp );
if( curp > fp->fcb->file_length ){
/* UNLOCK should be added here! */
FS_UNLOCK( FILE_S_LOCK_ID );
return FS_ERR_PAR;
}
count = 0;
/*curp <= fp->fcb->file_length*/
/* m is the num of block, n is the offset in the m+1 block */
m = curp / BLOCKSIZE;
n = curp % BLOCKSIZE;
if( m < NUM1 ){
if( (blkid = *((unsigned int *)(fp->fcb + 1) + m)) == 0 ){
if( (blkid = get_block( fp->fcb->partition )) < 0 ) goto UNLOCK;
*(unsigned int *)((unsigned int *)(fp->fcb + 1) + m) = blkid;
}
fp->fcb->flag = BE_USED;
/* the data to be written in the m+1 block */
if( n + size < BLOCKSIZE ){
write_block( &fp->link, blkid, n, size, buffer, NORMAL_WRITE );
if( fp->fcb->file_length < curp + size )
fp->fcb->file_length = curp + size;
count += size;
// return count;
goto UNLOCK;
}
/* the data to be written will exceed the m+1 block */
else{
write_block( &fp->link, blkid, n, BLOCKSIZE - n, buffer, NORMAL_WRITE );
count = (BLOCKSIZE - n);
buffer += (BLOCKSIZE - n);
/* num is the old block */
if((signed int)(fp->fcb->file_length - curp - (BLOCKSIZE - n)) < 0 )
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;
/*i is the num of block and j is the offset in the i+1 block
which is occupied by remained data */
size = size - ( BLOCKSIZE - n );
i = size / BLOCKSIZE;
j = size % BLOCKSIZE;
/*remained data will be written in the fcb block*/
if( m + 1 + i + 1 < NUM1 ){
for( v = 1; v < i + 1; v++ ){
if( num != 0 ){
blkid = *(unsigned int *)((unsigned int *)(fp->fcb + 1) + (m + v));
num--;
}
else{
if( (blkid = get_block( fp->fcb->partition )) < 0 ) goto UNLOCK;
*(unsigned int *)((unsigned int *)(fp->fcb + 1) + (m + v)) = blkid;
}
write_block( &fp->link, blkid, 0, BLOCKSIZE , buffer, NORMAL_WRITE );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
if( num != 0 ){
blkid = *(unsigned int *)((unsigned int *)(fp->fcb + 1) + (m + v));
num--;
}
else{
if( (blkid = get_block( fp->fcb->partition )) < 0 ) goto UNLOCK;
*(unsigned int *)((unsigned int *)(fp->fcb + 1) + (m + v)) = blkid;
}
write_block( &fp->link, blkid, 0, j , buffer, NORMAL_WRITE );
count += j;
if( fp->fcb->file_length < curp + count )
fp->fcb->file_length = curp + count;
// return count;
goto UNLOCK;
}
/*remained data will be written in the fcb block and subfcb block */
else{
/* written in the fcb block */
for( n = 1; n <= NUM1 - (m + 1); n++ ){
if( num != 0 ){
blkid = *(unsigned int *)((unsigned int *)(fp->fcb + 1) + (m + n));
num--;
}
else{
if( (blkid = get_block( fp->fcb->partition )) < 0 ) goto UNLOCK;
*(unsigned int *)((unsigned int *)(fp->fcb + 1) + (m + n)) = 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 = (i - (NUM1 - (m + 1))) / NUM2;
t = (i - (NUM1 - (m + 1))) % NUM2;
/*write s subfcbs*/
p = (unsigned int*)fp->fcb;
for( n = 0; n < s; n++ ){
if( num != 0 ){
if( ((FCB_S*)p)->nextfcb == NULL ){
q = (unsigned int *)read_FCB(((FCB_S*)p)->nextblk, NULL);
((FCB_S*)p)->nextfcb = (SUBFCB_S*)q;
}
else
q = ((FCB_S*)p)->nextfcb;
}
else{
if( (q = (unsigned int*)new_SUBFCB( fp->fcb )) == NULL )goto UNLOCK;
((FCB_S*)p)->nextfcb = (SUBFCB_S*)q;
((FCB_S*)p)->nextblk = ((SUBFCB_S*)q)->blkid;
}
p = q;
((FCB_S*)p)->flag = BE_USED;
for( u = 0; u < NUM2; u++ ){
if( num != 0 ){
blkid = *((unsigned int*)((SUBFCB_S*)p + 1) + u);
num--;
}
else{
if( (blkid = get_block( fp->fcb->partition )) < 0 ) goto UNLOCK;
*((unsigned int*)((SUBFCB_S*)p + 1) + u) = blkid;
}
write_block( &fp->link, blkid, 0, BLOCKSIZE , buffer ,NORMAL_WRITE );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
}
/*write t blocks in the s+1 subfcb*/
if( num != 0 ){
if( ((FCB_S*)p)->nextfcb == NULL ){
q = (unsigned int *)read_FCB(((FCB_S*)p)->nextblk, NULL);
((FCB_S*)p)->nextfcb = (SUBFCB_S*)q;
}
else
q = ((FCB_S*)p)->nextfcb;
}
else{
if( (q = (unsigned int*)new_SUBFCB( fp->fcb )) == NULL )goto UNLOCK;
((FCB_S*)p)->nextfcb = (SUBFCB_S*)q;
((FCB_S*)p)->nextblk = ((SUBFCB_S*)q)->blkid;
}
p = q;
((FCB_S*)p)->flag = BE_USED;
for( n = 0; n < t; n++){
if( num != 0 ){
blkid = *((unsigned int*)((SUBFCB_S*)p + 1) + n);
num--;
}
else{
if( (blkid = get_block( fp->fcb->partition )) < 0 ) goto UNLOCK;
*((unsigned int*)((SUBFCB_S*)p + 1) + n) = blkid;
}
write_block( &fp->link, blkid, 0, BLOCKSIZE , buffer, NORMAL_WRITE );
buffer += BLOCKSIZE;
count += BLOCKSIZE;
}
/*write the t+1 block in the s+1 subfcb*/
if( num != 0 ){
blkid = *((unsigned int*)((SUBFCB_S*)p + 1) + n);
num--;
}
else{
if( (blkid = get_block( fp->fcb->partition )) < 0 ) goto UNLOCK;
*((unsigned int*)((SUBFCB_S*)p + 1) + n) = blkid;
}
write_block( &fp->link, blkid, 0, j, buffer, NORMAL_WRITE );
count += j;
if( fp->fcb->file_length < curp + count )
fp->fcb->file_length = curp + count;
// return count;
goto UNLOCK;
}
}
}
/* 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;
}
if( (blkid = *((unsigned int*)((SUBFCB_S*)p+1) + j)) == 0 ){
if( (blkid = get_block( fp->fcb->partition )) < 0 ) goto UNLOCK;
*((unsigned int*)((SUBFCB_S*)p+1) + j) = blkid;
}
((FCB_S*)q)->flag = BE_USED;
/* the data to be written in the j+1 block */
if( n + size < BLOCKSIZE ){
write_block( &fp->link, blkid, n, size, buffer, NORMAL_WRITE );
if( fp->fcb->file_length < curp + size )
fp->fcb->file_length = curp + size;
count += size;
// return count;
goto UNLOCK;
}
/* the data to be written will exceed the j+1 block */
else{
write_block( &fp->link, blkid, n, BLOCKSIZE - n , buffer, NORMAL_WRITE );
buffer += ( BLOCKSIZE - n );
count += ( BLOCKSIZE - n );
/* num is the old block */
if((signed int)(fp->fcb->file_length - curp - (BLOCKSIZE - n)) < 0 )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -