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

📄 nu_file.c

📁 NUcleus plus 支持的文件系统。 是学习文件系统的很好参考资料。
💻 C
📖 第 1 页 / 共 5 页
字号:
/************************************************************************
*                                                                       
*       Copyright (c) 2001 by Accelerated Technology, Inc.              
*                                                                       
*  PROPRIETARY RIGHTS of Accelerated Technology are  involved in        
*  the subject matter of this material.  All manufacturing,             
*  reproduction, use, and sales rights pertaining to this subject       
*  matter are  governed by the license agreement.  The recipient of     
*     this software implicitly accepts the terms of the license.        
*                                                                       
*                                                                       
*************************************************************************

*************************************************************************
* FILE NAME                                     VERSION                 
*                                                                       
*       NU_FILE.C                               FILE  2.3              
*                                                                       
* COMPONENT                                                             
*                                                                       
*       Nucleus File                                                    
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       This file contains Nuclues File32 application interface.        
*                                                                       
* DATA STRUCTURES                                                       
*                                                                       
*       None.                                                           
*                                                                       
* FUNCTIONS                                                             
*                                                                       
*       NU_Open_Disk                        Open a disk for business.   
*       NU_Close_Disk                       Flush all buffers for a disk
*                                            and free all core.         
*       NU_Disk_Abort                       Abort all operations on a   
*                                            disk.                      
*       NU_Open                             Open a file.                
*       NU_Read                             Read bytes from a file.     
*       NU_Write                            Write Bytes to a file.      
*       NU_Seek                             Move the file pointer.      
*       NU_Close                            Close a file and flush the  
*                                            file allocation table.     
*       NU_Set_Attributes                   Set a file attributes.      
*       NU_Get_Attributes                   Get a file attributes.      
*       NU_Flush                            Flush an open file.         
*       NU_Truncate                         Truncate an open file.      
*       NU_Rename                           Rename a file.              
*       NU_Delete                           Delete a file.              
*       NU_Make_Dir                         Create a directory.         
*       NU_Remove_Dir                       Delete a directory.         
*       pc_fat_size                         Calculate blocks required   
*                                            for a volume's Allocation  
*                                            Table.                     
*       NU_Format                           Create a file system.       
*       NU_FreeSpace                        Calculate and return the    
*                                            free space on a disk.      
*       NU_Get_First                        Get stats on the first file 
*                                            to match a pattern.        
*       NU_Get_Next                         Get stats on the next file  
*                                            to match a pattern.        
*       NU_Done                             Free resources used by      
*                                            NU_Get_First/NU_Get_Next.  
*       NU_Set_Default_Drive                Set the default drive number.
*       NU_Get_Default_Drive                Get the default drive number.
*       NU_Set_Current_Dir                  Set the current working     
*                                            directory.                 
*       NU_Current_Dir                      Get string representation of
*                                            current working dir.       
*                                                                       
* DEPENDENCIES                                                          
*                                                                       
*       pcdisk.h                            File common definitions     
*                                                                       
*************************************************************************/

#include        "file\pcdisk.h"

extern _PC_BDEVSW       pc_bdevsw[];
extern UNSIGNED         *NUF_Drive_Pointers[];
extern INT              NUF_Fat_Type[];

static INT pc_l_pwd(UINT8 *, DROBJ *);
static INT pc_gm_name(UINT8 *path, DROBJ *pmom, DROBJ *pdotdot);


/************************************************************************
* FUNCTION                                                              
*                                                                       
*       NU_Open_Disk                                                    
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       Given a path spec containing a valid drive specifier open the   
*       disk by reading all of the block zero information and converting
*       it to native byte order.                                        
*                                                                       
* AUTHOR                                                                
*                                                                       
*       Takahiro Takahashi
*                                                                       
* INPUTS                                                                
*                                                                       
*       path                                Driver charactor            
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       NU_SUCCESS                          Disk initialized was        
*                                            successfully.              
*       NUF_BAD_USER                        Not a file user.            
*       NUF_BADDRIVE                        Invalid drive specified.    
*       NUF_FATCORE                         Fat cache table too small.  
*       NUF_NO_PARTITION                    No partition in disk.       
*       NUF_FORMAT                          Not formatted this disk.    
*       NUF_NO_MEMORY                       Can't allocate internal     
*                                            buffer.                    
*       NUF_IO_ERROR                        Driver returned error.      
*       NUF_INTERNAL                        Nucleus FILE internal error.
*       Other error code                    See the driver error code.  
*                                                                       
*************************************************************************/
STATUS NU_Open_Disk(CHAR *path)
{
INT16       driveno;
STATUS      ret_val;
DDRIVE      *pdr;
DROBJ       *pcwd;
INT         open_needed = NU_TRUE;


    PC_FS_ENTER()     /* Must be last line in declarations */
    CHECK_USER(INT, NUF_BAD_USER) /* Check if a valid user if multitasking */

    ret_val = NU_SUCCESS;

    /* Get drive no */
    if (pc_parsedrive(&driveno, (UINT8 *)path))
    {
        PC_DRIVE_ENTER(driveno, YES)        /* Grab exclusive access to the drive */

        /* Check drive exist ( if there is check driver service ) */
        if (pc_bdevsw[driveno].dskchk_proc)
            ret_val = pc_bdevsw[driveno].dskchk_proc(driveno);
        if (ret_val == NU_SUCCESS)
        {
            pdr = (DDRIVE *)NUF_Drive_Pointers[driveno];
            if (pdr)
            {
                /* Don't do anything on reopens */
                if (pdr->opencount > 0)
                {
                    pdr->opencount++;
                    open_needed = NU_FALSE;
                }
            }
        }
        else
        {
            open_needed = NU_FALSE;
        }
        if (open_needed == NU_TRUE)
        {
            /* Open the disk */
            if ( !pc_bdevsw[driveno].open_proc(driveno) )
            {
                pc_report_error(PCERR_INITDEV);

                if (NUF_Fat_Type[driveno] == NUF_NO_PARTITION)
                    ret_val = NUF_NO_PARTITION;
                else
                    ret_val = NUF_IO_ERROR;
            }
            else
            {
                /* File system initialize */
                ret_val = pc_dskinit(driveno);
            }
            if (ret_val == NU_SUCCESS)
            {
                /* Find the drive */
                pdr = pc_drno2dr(driveno);
                /* Set the current root directory */
                pcwd = pc_get_root(pdr);
                fs_user->lcwd[driveno] = pcwd;
            }
        }
        PC_DRIVE_EXIT(driveno)
    }
    else
    {
        ret_val = NUF_BADDRIVE;
    }

    /* Restore the kernel state */
    PC_FS_EXIT()
    return(ret_val);
}


/************************************************************************
* FUNCTION                                                              
*                                                                       
*       NU_Close_Disk                                                   
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       Given a path name containing a valid drive specifier. Flush the 
*       file allocation table and purge any buffers or objects          
*       associated with the drive.                                      
*                                                                       
* AUTHOR                                                                
*                                                                       
*       Takahiro Takahashi
*                                                                       
* INPUTS                                                                
*                                                                       
*       path                                Driver charactor            
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       NU_SUCCESS                          If service is successful.   
*       NUF_BAD_USER                        Not a file user.            
*       NUF_BADDRIVE                        Invalid drive specified.    
*       NUF_NOT_OPENED                      Drive not opened.           
*       NUF_IO_ERROR                        IO_error occured.           
*       NUF_INTERNAL                        Nucleus FILE internal error.
*                                                                       
*************************************************************************/
INT NU_Close_Disk(CHAR *path)
{
INT16       driveno;
STATUS      ret_val;


    PC_FS_ENTER()     /* Must be last line in declarations */
    CHECK_USER(INT, NUF_BAD_USER) /* Check if a valid user if multitasking */

    ret_val = NU_SUCCESS;

    if (pc_parsedrive(&driveno, (UINT8 *)path))
    {
        PC_DRIVE_ENTER(driveno, YES)        /* Grab exclusive access to the drive */
        ret_val = pc_idskclose(driveno);
        PC_DRIVE_EXIT(driveno)
    }
    else
    {
        ret_val = NUF_BADDRIVE;
    }
    /* Restore the kernel state */
    PC_FS_EXIT()

    return(ret_val);
}


/************************************************************************
* FUNCTION                                                              
*                                                                       
*       NU_Disk_Abort                                                   
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       If an application senses that there are problems with a disk, it
*       should call NU_Disk_Abort("D:"). This will cause all resources  
*       associated with that drive to be freed, but no disk writes will 
*       be attempted. All file descriptors associated with the drive    
*       become invalid. After correcting the problem call               
*       NU_Open_Disk("D:") to re-mount the disk and re-open your files. 
*                                                                       
* AUTHOR                                                                
*                                                                       
*       Takahiro Takahashi
*                                                                       
* INPUTS                                                                
*                                                                       
*       path                                Driver charactor            

⌨️ 快捷键说明

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