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

📄 zfsmkdir.c

📁 zilog的实时操作系统RZK,可以移植到多种处理器上
💻 C
字号:
/*
 * File       : ZFSMkDir.c
 * Description: This file contains the implementation of ZFSMkdir 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 RZK_SEMAPHOREHANDLE_t hSem_FS;

//Function Name: ZFSMkdir
//Description: This API creates a directory in a given path
ZFS_STATUS_t ZFSMkdir( IN INT8 *path, IN INT8 *dir_name )
{
	UINT len = 0 ;
	UINT off = 0 ;
	PZFS_DIR_LIST_t pdir_node, pcwd_dir_node ;
	PZFS_VOL_INFO_t pvol_info ;
	PZFS_FIR_t pfir;
	ZFS_FIR_t fir_hdr ;
	PZFS_FIR_t new_fir = ( PZFS_FIR_t ) NULL ;
	PZFS_DIR_LIST_t new_dir_node = ( PZFS_DIR_LIST_t )NULL ;
	ZFS_SEC_ID_t new_sec_num = NULL ;
	UINT8 status ;
	ZFS_STATUS_t ret_status = ZFSERR_SUCCESS ;


	// if ZFS is not initialized, return error
	if( !IsZFSInited() )
		return ZFSERR_NOT_INITIALIZED ;

	if( path == NULL || dir_name == NULL )
		return ZFSERR_INVALID_ARGUMENTS ;

	if( !ValidatePath( path ) )
		return ZFSERR_INVALID_FILEDIR_PATH ;

	if( !ValidateFileDirName( dir_name ) )
		return ZFSERR_INVALID_FILE_DIR_NAME ;
	
	// now check the validity of the dir path given in the argument
	len = strlen( (const INT8 *)path ) ;

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

	if( GetCwdInfoForCurThread( &pvol_info, &pcwd_dir_node )  != ZFS_TRUE )
		{
		ret_status = ZFSERR_INVALID_VOLUME ;
		goto err_Label_ZFSMkdir;
		}
	
	// now validated, disable preemption, find the absolute path with reduced length
	status = IsAbsPath( path, &pvol_info, &off, &pdir_node ) ;
	if( status != ZFS_TRUE )
		{
		ret_status = ZFSERR_INVALID_FILEDIR_PATH ;
		goto err_Label_ZFSMkdir;
		}

	if( !IsVolumeValid(pvol_info) )
		{
		ret_status = ZFSERR_INVALID_VOLUME ;
		goto err_Label_ZFSMkdir;
		}
	

	if( pdir_node == NULL )
		{
		// get the CWD_INFO table
		pdir_node = pcwd_dir_node ;
		}
	
	// this is relative path, get the directory node of the relative path with reduced length
	pdir_node = GetNodeForPath( pdir_node, path + off, ( len - off ) ) ;
	if( pdir_node == NULL )
		{
		ret_status = ZFSERR_FILE_DIR_DOES_NOT_EXIST ;
		goto err_Label_ZFSMkdir;
		}

	// check whether a directory/file exists with the same name
	pfir = SearchFIR( pvol_info, ( PZFS_FIT_HDR_t ) pdir_node->sec_num, dir_name, strlen( (const INT8 *)dir_name ) ) ;

	if( pfir != NULL )
		{
		ret_status = ZFSERR_FILE_DIR_ALREADY_EXISTS ;
		goto err_Label_ZFSMkdir;
		}

	// no File or directory name exists with the same name, so check whether number of file/directory is present
	if( pdir_node->fd_cnt == ZFS_MAX_FILE_DIR_IN_A_DIR )
		{
		ret_status = ZFSERR_FILE_DIR_COUNT_LIMIT_REACHED ;
		goto err_Label_ZFSMkdir;
		}

	// try to allocate a new dir_list node for this directory
	new_dir_node = AllocDirNode( ) ;
// this code is not needed - Mahadev check and then delete
// 1
	if( new_dir_node == NULL )
		{
		// number of RAM buffer for DIR_LIST is over, just return an error
		ret_status = ZFSERR_DIR_COUNT_LIMIT_REACHED ;
		goto err_Label_ZFSMkdir;
		}
// 1
	// now every thing is valid, just create a directory FIR and allocate a FIT to it.

	new_fir = AllocFIR( pvol_info, ( PZFS_FIT_HDR_t ) pdir_node->sec_num ) ;
	if( new_fir == NULL )
		{
		// no space exists, just return an error
		// do an error check
		ret_status = ZFSERR_DATAMEDIA_FULL ;
		goto err_Label_ZFSMkdir;
		}
	new_sec_num = AllocSector( pvol_info, ( ZFS_SEC_ID_t ) 0, ZFS_SEC_TYPE_FIT ) ;
	if( new_sec_num == 0 )
		{
		// do an error check
		ret_status = ZFSERR_DATAMEDIA_FULL ;
		goto err_Label_ZFSMkdir;
		}

	InitializeHeader( &fir_hdr, sizeof( ZFS_FIR_t ) ) ;
	
	memcpy( &fir_hdr.fir_name[0], dir_name, strlen( (const INT8 *)dir_name ) ) ;
	fir_hdr.sec_datanum = new_sec_num ;
	fir_hdr.fir_type_status = ~(ZFS_DIR_TYPE | ZFS_FIR_ALLOCATED) ;

	WriteFIR( pvol_info, new_fir, &fir_hdr ) ;

	// now update the dir list
	strcpy( (INT8 *)&new_dir_node->dir_name[0], (const INT8 *)&dir_name[0] ) ;
	new_dir_node->sec_num = new_sec_num ;
	new_dir_node->fd_cnt = 0 ;

	// add a sub directory node to the dir list
	if( !AddSubDirNode( pdir_node, new_dir_node ) )
		{
		ret_status = ZFSERR_INTERNAL ;
		goto err_Label_ZFSMkdir;
		}

	// Now every thing for a directory is added, just return from here with success.
	goto succ_Label_ZFSMkdir;

err_Label_ZFSMkdir:
	if( new_dir_node != NULL )
		{
		// free the dir node
		memset( new_dir_node, 0x00, sizeof( ZFS_DIR_LIST_t ) ) ;
		}
	if( new_fir != NULL )
		{
		// free the FIR
		FreeFIR( pvol_info, new_fir ) ;
		}

	if( new_sec_num != 0 )
		{
		FreeSector( pvol_info, new_sec_num ) ;
		}

succ_Label_ZFSMkdir:
	RZKReleaseSemaphore(hSem_FS);
//	EnablePreemption( preempt_status ) ;
	return ret_status ;

}


⌨️ 快捷键说明

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