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

📄 zfsopen.c

📁 zilog的实时操作系统RZK,可以移植到多种处理器上
💻 C
字号:
/*
 * File       : ZFSOpen.c
 * Description: This file contains the implementation of ZFSOpen API
 * Author     : Mahadev K C
 * Created on : 30-APR-2003
 *
 * Copyright 2004 ZiLOG Inc.  ALL RIGHTS RESERVED.
 *
 * This file contains unpublished confidential and proprietary information
 * of ZiLOG, Inc.
 * NO PART OF THIS WORK MAY BE DUPLICATED, STORED, PUBLISHED OR DISCLOSED 
 * IN ANY FORM WITHOUT THE PRIOR WRITTEN CONSENT OF ZiLOG, INC.
 * This is not a license and no use of any kind of this work is authorized
 * in the absence of a written license granted by ZiLOG, Inc. in ZiLOG's 
 * sole discretion 
 */


#include "ZSysgen.h"
#include "ZTypes.h"
#include "ZThread.h"
#include "ZSemaphore.h"
#include "glextern.h"

extern UINT8 GetTimeData( UINT32 *date_time ) ;
extern RZK_SEMAPHOREHANDLE_t hSem_FS;
ZFS_SEC_ID_t GetLastSecNum( PZFS_VOL_INFO_t pvol_info, PZFS_FIR_t fir_hdr )
{
	ZFS_SEC_ID_t     new_sec ;
	ZFS_SEC_ID_t     prev_sec ;
	ZFS_SEC_HDR_t    sec_hdr ;

	// update the sector num..
	for( new_sec = prev_sec = fir_hdr->sec_datanum ; new_sec != ( ZFS_SEC_ID_t ) FREE_SECTOR ; )
		{
		pvol_info->pcfg->pfn_drv_read( GetSecAddr( pvol_info, new_sec ), &sec_hdr, sizeof(ZFS_SEC_HDR_t ) ) ;
		prev_sec = new_sec ;
		new_sec = sec_hdr.nxtsecnum ;
		}
	return prev_sec ;
}

//Function Name: ZFSOpen
//Description: This API will open a file for reading/writing
ZFS_HANDLE_t ZFSOpen( IN INT8 *filename, IN UINT8 mode, IN UINT8 type )
{

	UINT             preempt_status ;
	UINT             len                  = 0 ;
	UINT             off                  = 0 ;
	UINT             last_fd_off          = 0 ;
	UINT             idx ;
	UINT8            status ;
	ZFS_STATUS_t     errnum ;
	PZFS_DIR_LIST_t  pdir_node ;
	PZFS_DIR_LIST_t  pcwd_dir_node ;
	PZFS_VOL_INFO_t  pvol_info ;
	PZFS_FIR_t       pfir ;
	ZFS_FIR_t        fir_hdr ;
	ZFS_SEC_HDR_t    sec_hdr ;
	PZFS_OPEN_REC_t  old_or ;
	PZFS_OPEN_REC_t  new_or               = ( PZFS_OPEN_REC_t ) NULL ;
	PZFS_OPEN_REC_t  pzfs_or ;
	ZFS_SEC_ID_t     last_secnum ;

	// disable preemption
//	preempt_status = DisablePreemption() ;
	RZKAcquireSemaphore(hSem_FS,INFINITE_SUSPEND);
	if( GetCwdInfoForCurThread( &pvol_info, &pcwd_dir_node )  != ZFS_TRUE )
		{
		errnum = ZFSERR_INVALID_VOLUME ;
		goto err_ZFSOpen_EnablePreemption;
		}
	
	RZKReleaseSemaphore(hSem_FS);

	// if ZFS is not initialized, return error
	if( !IsZFSInited() )
		{
		errnum = ZFSERR_NOT_INITIALIZED ;
		goto err_ZFSOpen ;
		}

	// validate the arguments
	if( filename == NULL || ( mode > ZFS_READ_WRITE || mode < ZFS_READ ) || 
			( type > ZFS_MODE_BINARY ) )
		{
		errnum = ZFSERR_INVALID_ARGUMENTS ;
		goto err_ZFSOpen ;
		}
	
	if( !ValidatePath( filename ) )
		{
		errnum = ZFSERR_INVALID_FILEDIR_PATH ;
		goto err_ZFSOpen ;
		}

	last_fd_off = GetLastFileDirNameOff( filename ) ;

	// validate file directory name
	if( !ValidateFileDirName( filename + last_fd_off ) )
		{
		errnum = ZFSERR_INVALID_FILEDIR_PATH ;
		goto err_ZFSOpen ;
		}
		
	// now check the validity of the dir path given in the argument
	len = strlen( (const INT8 *)filename ) ;

	// disable preemption
//	preempt_status = DisablePreemption() ;
	RZKAcquireSemaphore(hSem_FS,INFINITE_SUSPEND);

	// now validated, disable preemption, find the absolute path with reduced length
	status = IsAbsPath( filename, &pvol_info, &off, &pdir_node ) ;
	if( status != ZFS_TRUE )
		{
		errnum = ZFSERR_INVALID_FILEDIR_PATH ;
		goto err_ZFSOpen_EnablePreemption ;
		}

	if( !IsVolumeValid(pvol_info) )
		{
		errnum = ZFSERR_INVALID_VOLUME ;
		goto err_ZFSOpen_EnablePreemption;
		}

	if( pdir_node == NULL )
		{
		// get the CWD_INFO table
		pdir_node = pcwd_dir_node ;
		}
	else
		{
		// it is an absolute path, just check whether the file name part exist or not.
		if( len == last_fd_off )
			{
			// it is an error: only volume name is present, no file name or directory name is present.
			errnum = ZFSERR_INVALID_FILEDIR_PATH ;
			goto err_ZFSOpen_EnablePreemption ;
			}
		}
			
	// this is relative path, get the directory node of the relative path with reduced length
	pdir_node = GetNodeForPath( pdir_node, filename + off, ( last_fd_off - off ) ) ;
	if( pdir_node == NULL )
		{
		errnum = ZFSERR_FILE_DIR_DOES_NOT_EXIST ;
		goto err_ZFSOpen_EnablePreemption ;
		}

	// now directory is found, just check whether the file is a "." and ".." or not
	if( strcmp( (INT8 *)(filename + last_fd_off), "." ) == 0 || 
			strcmp( (const INT8 *)(filename + last_fd_off), ".." ) == 0 )
		{
		errnum = ZFSERR_INVALID_FILE_DIR_NAME ;
		goto err_ZFSOpen_EnablePreemption ;
		}


	// now directory is found, just check whether the file exists or not
	pfir = SearchFIR( pvol_info, ( PZFS_FIT_HDR_t ) pdir_node->sec_num, filename + last_fd_off, ( len-last_fd_off ) ) ;


	if( pfir != NULL )
		{
		// read the FIR
		pvol_info->pcfg->pfn_drv_read( pfir, &fir_hdr, sizeof( ZFS_FIR_t) ) ;
		}

	if( pfir != NULL && (~((UINT8)fir_hdr.fir_type_status) & ZFS_DIR_TYPE) )
		{
		errnum = ZFSERR_INVALID_FILE_DIR_NAME ;
		goto err_ZFSOpen_EnablePreemption ;
		}

	if( ( mode & ( ZFS_READ | ZFS_APPEND | ZFS_READ_WRITE ) && pfir == NULL ) )
		{
		errnum = ZFSERR_FILE_DIR_DOES_NOT_EXIST ;
		goto err_ZFSOpen_EnablePreemption ;
		}

	if( pfir != NULL )
		{

		old_or = ( PZFS_OPEN_REC_t ) NULL ;
		// now check whether any thread opened this file.
		// No directory with new name exists, just check whether any thread opened a file in this directory or not
		pzfs_or = &g_zfs_or[0];
		for( idx = 0 ; idx < g_max_or_entries ; idx ++, pzfs_or++ )
			{
			if( pzfs_or->status == ZFS_OR_MAGIC_NUM && 
					pzfs_or->first_secnum == fir_hdr.sec_datanum &&
					pzfs_or->pfir_file == pfir )
				{
				old_or = pzfs_or ;
				break ;
				}
			}

		// if any thread opened this file, and mode is READ_WRITE, WRITE and APPEND return an error
		if( old_or && ( mode & (ZFS_READ_WRITE| ZFS_WRITE | ZFS_APPEND ) ) )
			{
			errnum = ZFSERR_FILE_IS_ALREADY_OPEN ;
			goto err_ZFSOpen_EnablePreemption ;
			}		
		}


	// just get an empty OR and write all the data onto it.
	new_or = AllocateOR() ;
	if( new_or == NULL )
		{
		errnum = ZFSERR_MAX_FILE_OPEN_COUNT_REACHED ;
		goto err_ZFSOpen_EnablePreemption ;
		}
	
	// now allocate a OR for this 
	if( mode == ZFS_READ )
		{

		last_secnum = GetLastSecNum( pvol_info, &fir_hdr ) ;

		new_or->last_secnum = last_secnum ;
		new_or->first_secnum = new_or->cur_secnum = fir_hdr.sec_datanum ;
		if( fir_hdr.size == FREE_SECTOR )
			new_or->size = 0 ;
		else
			new_or->size = fir_hdr.size ;
		new_or->offset = 0 ;
		goto success_ZFSOpen ;

		}
	else if( mode == ZFS_APPEND )
		{

		last_secnum = GetLastSecNum( pvol_info, &fir_hdr ) ;
		new_or->last_secnum = new_or->cur_secnum = last_secnum ;
		new_or->first_secnum = fir_hdr.sec_datanum ;
		if( fir_hdr.size == FREE_SECTOR )
			new_or->size = 0 ;
		else
		new_or->size = fir_hdr.size; 
		new_or->offset = new_or->size ;
		goto success_ZFSOpen ;

		}
	else if( mode == ZFS_READ_WRITE )
		{
			
		last_secnum = GetLastSecNum( pvol_info, &fir_hdr ) ;
		new_or->last_secnum = last_secnum ;
		new_or->first_secnum = new_or->cur_secnum = fir_hdr.sec_datanum ;
		if( fir_hdr.size == FREE_SECTOR )
			new_or->size = 0 ;
		else
			new_or->size = fir_hdr.size ;
		new_or->offset = 0 ;
		goto success_ZFSOpen ;

		}
	else if( mode == ZFS_WRITE )
		{
		if( pfir == NULL )
			{
			// check the count in the directory
			if( pdir_node->fd_cnt == ZFS_MAX_FILE_DIR_IN_A_DIR )
				{
				errnum = ZFSERR_FILE_DIR_COUNT_LIMIT_REACHED ;
				goto err_ZFSOpen_EnablePreemption ;
				}
			// allocate a FIR for the new file and write it.
			pfir = AllocFIR( pvol_info, ( PZFS_FIT_HDR_t ) pdir_node->sec_num ) ;
			if( pfir == NULL )
				{
				errnum = ZFSERR_DATAMEDIA_FULL ;
				goto err_ZFSOpen_EnablePreemption ;
				}
			InitializeHeader( &fir_hdr, sizeof( ZFS_FIR_t ) ) ;
			memcpy( fir_hdr.fir_name, filename + last_fd_off, strlen( (const INT8 *)(filename + last_fd_off)) ) ;
			fir_hdr.fir_type_status = ~(ZFS_FILE_TYPE | ZFS_FIR_ALLOCATED );
			fir_hdr.size = 0 ;
			fir_hdr.filecs = 0 ;
			WriteFIR( pvol_info, pfir, &fir_hdr ) ;

			// just get an empty OR and write all the data onto it.
			new_or->size = 0 ;
			new_or->offset = 0 ;
			new_or->first_secnum = new_or->cur_secnum = new_or->last_secnum = (ZFS_SEC_ID_t) (FREE_SECTOR) ;

			pdir_node->fd_cnt++;
			goto success_ZFSOpen ;

			}
		else
			{
			PZFS_SEC_HDR_t pcur_sec ;
			PZFS_SEC_HDR_t prev_addr ;
			// now file is already present, just truncate the file and open in write mode

			// free the sectors that this file occupied
			for( pcur_sec = ( PZFS_SEC_HDR_t ) fir_hdr.sec_datanum ; 
				pcur_sec != ( ZFS_SEC_ID_t ) FREE_SECTOR ; )
				{
				prev_addr = ( PZFS_SEC_HDR_t ) GetSecAddr( pvol_info, pcur_sec ) ;
				if( FreeSector( pvol_info, pcur_sec ) == ZFS_FALSE )
					{
//					// This condition should not arise at all.
					}
				pvol_info->pcfg->pfn_drv_read( prev_addr, &sec_hdr, sizeof( ZFS_SEC_HDR_t ) ) ;
				pcur_sec = ( PZFS_SEC_HDR_t ) sec_hdr.nxtsecnum ;
				}

			InitializeHeader( &fir_hdr, sizeof( ZFS_FIR_t ) ) ;
			memcpy( fir_hdr.fir_name, filename + last_fd_off, strlen( (const INT8 *)(filename + last_fd_off)) ) ;
			fir_hdr.fir_type_status &= ~( ZFS_FILE_TYPE | ZFS_FIR_ALLOCATED );
			fir_hdr.size = 0 ;
			fir_hdr.filecs = 0 ;

			pfir = OverWriteFIR( pvol_info, ( PZFS_FIT_HDR_t ) pdir_node->sec_num, pfir, &fir_hdr) ;
			if( pfir == NULL )
				{
				// error
				errnum = ZFSERR_INTERNAL;
				goto err_ZFSOpen_EnablePreemption ;
				}
			
			new_or->size = 0 ;
			new_or->offset = 0 ;
			new_or->first_secnum = new_or->cur_secnum = new_or->last_secnum = (ZFS_SEC_ID_t) FREE_SECTOR ;
			goto success_ZFSOpen ;

			}
	
		}

err_ZFSOpen_EnablePreemption:
	RZKReleaseSemaphore(hSem_FS);
//	EnablePreemption( preempt_status ) ;

err_ZFSOpen:
	preempt_status = DisablePreemption() ;
	// got the OR, change the status to 0x00
	if( new_or )
		new_or->status = ZFS_OR_INVALID_MAGIC_NUM ;
	EnablePreemption( preempt_status ) ;
	SetFSData( errnum ) ;
	return NULL ;

success_ZFSOpen:

	// store the reqiured information in the OPEN_REC structure
	strcpy( (INT8 *)new_or->name, (const INT8 *)filename + last_fd_off ) ;
	new_or->pdir_node = pdir_node ;
	new_or->pfir_file = pfir ;
	new_or->pvol = pvol_info ;
	new_or->attr = type ;
	new_or->mode = mode ;
	new_or->filecs = fir_hdr.filecs ;

	RZKReleaseSemaphore(hSem_FS);
//	EnablePreemption( preempt_status ) ;

	SetFSData( ZFSERR_SUCCESS ) ;
	return new_or ;
}


⌨️ 快捷键说明

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