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

📄 zfsrename.c

📁 zilog的实时操作系统RZK,可以移植到多种处理器上
💻 C
字号:
/*
 * File       : ZFSRename.c
 * Description: This file contains the implementation of ZFS APIs
 * 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: ZFSRename
//Description: This API renames a existing file
ZFS_STATUS_t ZFSRename( IN INT8 *src_file_path, IN INT8 *dst_file_name )
{
	UINT len = 0 ;
	UINT off = 0 ;
	UINT last_fd_off = 0 ;
	UINT idx ;
	PZFS_DIR_LIST_t pdir_node, pcwd_dir_node ;
	PZFS_VOL_INFO_t pvol_info ;
	PZFS_FIR_t pfir;
	ZFS_FIR_t fir_hdr ;
	UINT8 status ;
	ZFS_STATUS_t ret_status = ZFSERR_SUCCESS;


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

	if( src_file_path == NULL || dst_file_name == NULL )
		return ZFSERR_INVALID_ARGUMENTS ;

	if( !ValidatePath( src_file_path ) )
		return ZFSERR_INVALID_FILEDIR_PATH ;

	if( !ValidateFileDirName( dst_file_name ) )
		return ZFSERR_INVALID_FILE_DIR_NAME ;

	last_fd_off = GetLastFileDirNameOff( src_file_path ) ;
	
	// now check the validity of the dir path given in the argument
	len = strlen( (const INT8 *)src_file_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_ZFSRename ;
		}

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

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

	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
	// i.e till last directory.
	if( off != last_fd_off )
		{
		// the file is not present in the root directory
		pdir_node = GetNodeForPath( pdir_node, src_file_path + off, (last_fd_off - off ) ) ;
		if( pdir_node == NULL )
			{
			ret_status = ZFSERR_INVALID_FILEDIR_PATH ;
			goto err_Label_ZFSRename ;
			}
		}

	// now again check whether the last name is a directory or not. If directory, return error.
	if( GetNodeForPath( pdir_node, src_file_path + last_fd_off, (len - last_fd_off ) ) )
		{
		ret_status = ZFSERR_INVALID_FILEDIR_PATH ;
		goto err_Label_ZFSRename ;
		}

	// Now every thing is valid, just check whether the file is in use or not.

	// search the FIT for the file name
	pfir = SearchFIR( pvol_info, 
						( PZFS_FIT_HDR_t ) pdir_node->sec_num, 
						( src_file_path + last_fd_off ),
						( len - last_fd_off ) ) ;
	if( pfir == NULL )
		{
		// file is not found, return error
		ret_status = ZFSERR_FILE_DIR_DOES_NOT_EXIST ;
		goto err_Label_ZFSRename ;
		}

	if( SearchFIR( pvol_info, 
						( PZFS_FIT_HDR_t ) pdir_node->sec_num, 
						dst_file_name,
						strlen( (const INT8 *)dst_file_name ) ) )
		{
		ret_status = ZFSERR_FILE_DIR_ALREADY_EXISTS ;
		goto err_Label_ZFSRename ;
		}
		

	// No file with new file name exists, just check whether the file is under use (opened)
	// Now file is found, just check out whether the file is opened by any thread or not

	pvol_info->pcfg->pfn_drv_read( pfir, &fir_hdr, sizeof( ZFS_FIR_t ) ) ;
	
	for( idx = 0 ; idx < g_max_or_entries ; idx ++ )
		{
		if( ( g_zfs_or[ idx ].first_secnum == fir_hdr.sec_datanum ) && g_zfs_or[idx].status == ZFS_OR_MAGIC_NUM )
			{
			// file is opened by a thread, just return error
			ret_status = ZFSERR_FILE_DIR_IN_USE ;
			goto err_Label_ZFSRename ;
			}
		}
	
	// No new file exists, just overwrite the previous FIR with new FIR

	// prepare a new FIR
//	InitializeHeader( &fir_hdr, sizeof( ZFS_FIR_t ) ) ;

	memset( &fir_hdr.fir_name[0], 0xFF, ZFS_MAX_FILE_NAME_SIZE ) ;	
	memcpy( &fir_hdr.fir_name[0], dst_file_name, strlen( (const INT8 *)dst_file_name ) ) ;

	if( OverWriteFIR( pvol_info, (PZFS_FIT_HDR_t) pdir_node->sec_num, pfir, &fir_hdr ) == NULL )
		{
		ret_status = ZFSERR_INTERNAL ;
		goto err_Label_ZFSRename ;
		}

	// now every thing is over, just return success
err_Label_ZFSRename:	
	RZKReleaseSemaphore(hSem_FS);
//	EnablePreemption( preempt_status ) ;
	return ret_status ;
}




⌨️ 快捷键说明

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