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

📄 nu_file.c

📁 NUcleus plus 支持的文件系统。 是学习文件系统的很好参考资料。
💻 C
📖 第 1 页 / 共 5 页
字号:
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       None.                                                           
*                                                                       
*************************************************************************/
VOID NU_Disk_Abort(CHAR *path)
{
INT16       driveno;


    PC_FS_ENTER()     /* Must be last line in declarations */
    VOID_CHECK_USER() /* Check if a valid user if multitasking */

    if (pc_parsedrive(&driveno, (UINT8 *)path))
    {
        PC_DRIVE_ENTER(driveno, YES)        /* Grab exclusive access to the drive */
        /* Release the drive unconditionally */
        pc_dskfree(driveno, YES);
        PC_DRIVE_EXIT(driveno)
    }
    /* Restore the kernel state */
    PC_FS_EXIT()
    return;
}


/************************************************************************
* FUNCTION                                                              
*                                                                       
*       NU_Make_Dir                                                     
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       Create a sudirectory in the path specified by name. Fails if a  
*       file or directory of the same name already exists or if the path
*       is not found.                                                   
*                                                                       
* AUTHOR                                                                
*                                                                       
*       Takahiro Takahashi
*                                                                       
* INPUTS                                                                
*                                                                       
*       name                                Path name                   
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       NU_SUCCESS                          Make the directory          
*                                            was successfully.          
*       NUF_BAD_USER                        Not a file user.            
*       NUF_BADDRIVE                        Invalid drive specified.    
*       NUF_NOSPC                           No space to create directory
*       NUF_LONGPATH                        Path or directory name too  
*                                            long.                      
*       NUF_INVNAME                         Path or filename includes   
*                                            invalid character.         
*       NUF_ROOT_FULL                       Root directry full.         
*                                            in this disk.              
*       NUF_NOFILE                          The specified file not      
*                                            found.                     
*       NUF_EXIST                           The directory already exists.
*       NUF_NO_BLOCK                        No block buffer available.  
*       NUF_NO_FINODE                       No FINODE buffer available. 
*       NUF_NO_DROBJ                        No DROBJ buffer available.  
*       NUF_IO_ERROR                        IO error occured.           
*       NUF_INTERNAL                        Nucleus FILE internal error.
*                                                                       
*       fs_user->p_errno is set to one of these values                  
*                                                                       
*       PENOENT                             File not found or path to   
*                                            file not found.            
*       PEEXIST                             Exclusive access requested  
*                                            but file already exists.   
*       PENOSPC                             Create failed.              
*                                                                       
*************************************************************************/
STATUS NU_Make_Dir(CHAR *name)
{
DROBJ       *pobj;
DROBJ       *parent_obj;
VOID        *path;
VOID        *filename;
VOID        *fileext;
STATUS      ret_stat;
INT16       driveno;


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

    parent_obj = NULL;
    pobj = NULL;

    fs_user->p_errno = 0;

    pc_parsedrive(&driveno, (UINT8 *)name);
    PC_DRIVE_ENTER(driveno, NO)   /* Register drive in use */

    /* Get out the filename and d:parent */
    ret_stat = pc_parsepath(&path, &filename, &fileext, (UINT8 *)name);
    if (ret_stat != NU_SUCCESS)
    {
        fs_user->p_errno = PENOENT;
    }
    else
    {
        /* Find the parent and make sure it is a directory \ */
        ret_stat = pc_fndnode(driveno, &parent_obj, (UINT8 *)path);
        if ( (ret_stat == NU_SUCCESS) && (pc_isadir(parent_obj)) && (!pc_isavol(parent_obj)) )
        {
            /* Lock the parent */
            PC_INODE_ENTER(parent_obj->finode, YES)

            /* Fail if the directory exists */
            pobj = NULL;
            ret_stat = pc_get_inode(&pobj, parent_obj, (UINT8 *)filename);
            if (ret_stat == NU_SUCCESS)
            {
                fs_user->p_errno = PEEXIST;        /* Exclusive fail */
                pc_freeobj(pobj);
                ret_stat = NUF_EXIST;
            }
            else if (ret_stat == NUF_NOFILE)  /* File not found */
            {
                /* Create Directory */
                ret_stat = pc_mknode(&pobj, parent_obj, (UINT8 *)filename, (UINT8 *)fileext, ADIRENT);
                if (ret_stat != NU_SUCCESS)
                {
                    fs_user->p_errno = PENOSPC;
                }
                else
                {
                    pc_freeobj(pobj);
                }
            }
            PC_INODE_EXIT(parent_obj->finode)
            pc_freeobj(parent_obj);
        }
        else
        {
            fs_user->p_errno = PENOENT;
        }
    }
    PC_DRIVE_EXIT(driveno)
    /* Restore the kernel state */
    PC_FS_EXIT()
    return(ret_stat);
}


/************************************************************************
* FUNCTION                                                              
*                                                                       
*       _synch_file_ptrs                                                
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       Synchronize file pointers. Read write Seek and close all call   
*       here.                                                           
*       This fixes the following BUGS:                                  
*       1. If a file is created and left open and then opened again with
*          a new file handle before any writing takes place. Neither    
*          file will get its fptr_cluster set correctly initially. The  
*          first one to write would get set up correctly but the other  
*          wouldn't.Thus if fptr_cluster is zero we see if we can set it.
*       2. If one file seeked to the end of the file or has written to  
*          the end of the file its file pointer will point beyond the   
*          last cluster in the chain, the next call to write will notice
*          the fptr is beyond the file size and extend the file by      
*          allocating a new cluster to the chain. During this time the  
*          cluster/block and byte offsets are out of synch. If another  
*          instance extends the file during this time the next call to  
*          write will miss this condition since fptr is not >= fsize    
*          any more. To fix this we note in the file when this condition
*          is true AND, afterwards each time we work with the file we   
*          see if the file has grown and adjust the cluster pointer and 
*          block pointer if needed.                                     
*                                                                       
* AUTHOR                                                                
*                                                                       
*       Takahiro Takahashi
*                                                                       
* INPUTS                                                                
*                                                                       
*       *pfile                              Internal file representation.
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       None.                                                           
*                                                                       
*************************************************************************/
VOID _synch_file_ptrs(PC_FILE *pfile)
{
UINT32      clno = 0;


    if (!pfile->fptr_cluster)
    {
        /* Current cluster - note on a new file this will be zero */
        pfile->fptr_cluster = pfile->pobj->finode->fcluster; 

        if (pfile->fptr_cluster)
            pfile->fptr_block = pc_cl2sector(pfile->pobj->pdrive, pfile->fptr_cluster);
        else
            pfile->fptr_block = 0L;
    }
    if (pfile->at_eof)
    {
        if (pfile->fptr_cluster)
        {
            pc_clnext(&clno, pfile->pobj->pdrive, pfile->fptr_cluster);
            if (clno)
            {
                pfile->fptr_cluster = clno;
                pfile->fptr_block = pc_cl2sector(pfile->pobj->pdrive, pfile->fptr_cluster);
                pfile->at_eof = NO;
            }
        }
    }
}


/************************************************************************
* FUNCTION                                                              
*                                                                       
*       NU_Open                                                         
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       Open the file for access as specified in flag. If creating use  
*       mode to set the access permissions on the file.                 
*                                                                       
*       Flag values are                                                 
*                                                                       
*       PO_RDONLY                           Open for read only.         
*       PO_WRONLY                           Open for write only.        
*       PO_RDWR                             Read/write access allowed.  
*                                                                       
*       PO_APPEND                           Seek to eof on each write.  
*       PO_CREAT                            Create the file if it does  
*                                            not exist.                 
*       PO_TRUNC                            Truncate the file if it     
*                                            already exists.            
*       PO_EXCL                             If flag contains            
*                                            (PO_CREAT | PO_EXCL) and   
*                                            the file already exists    
*                                            fail and set               
*                                            fs_user->p_errno to EEXIST.
*                                                                       
*       PO_BINARY                           Ignored. All file access is 
*                                            binary.                    
*       PO_TEXT                             Ignored.                    
*                                                                       
*       PO_NOSHAREANY                       Fail if the file is already 
*                                            open.                      
*       PO_NOSHAREWRITE                     Fail if the file is already 
*                                            open for write.            
*                                                                       
*       Mode values are                                                 
*                                                                       
*       PS_IWRITE                           Write permitted.            
*       PS_IREAD                            Read permitted.             
*                                           (Always true anyway)        
*                                                                       
* AUTHOR                                                                
*                                                                       
*       Takahiro Takahashi
*                                                                       
* INPUTS                                                                
*                                                                       
*       *name                               Open file name              
*       flag                                Open flag                   
*       mode                                Open mode                   
*                                                                       
* OUTPUTS                                                               
*                                                                       

⌨️ 快捷键说明

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