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

📄 fal.c

📁 NUcleus plus 支持的文件系统。 是学习文件系统的很好参考资料。
💻 C
📖 第 1 页 / 共 4 页
字号:
    return (-1);
#endif

#if NT_FILE_SYSTEM
    *ltime = (FAL_TIME)(time ((FAL_TIME*)ltime));

    if (ltime != NU_NULL)
        return (NU_SUCCESS);
    else
        return (-1);
#endif

#if IMF_INCLUDED
    p_errno = NU_UNAVAILABLE;
    return (-1);
#endif
}

/************************************************************************
*
*  FUNCTION
*
*      FAL_Ctime
*
*  DESCRIPTION
*
*      Time function-  The user will have to implement their own time
*      data functions for Nucleus File
*      NT File System returns true if the difference between the time
*      of the last change of file status information and the time find
*      was started is ltime 24-hour periods.
*
*  INPUTS
*
*      *ltime      Total seconds in time from a specified date.
*      *buffer     Pointer to the buffer to be filled in with the time
*                  in character format.
*
************************************************************************/
INT FAL_Ctime(FAL_TIME *ltime, CHAR *buffer)
{
    /* Remove Warnings */
#if (NUCLEUS_FILE_INCLUDED || NUCLEUS_FILE2_INCLUDED || IMF_INCLUDED)
    UNUSED_PARAMETER(ltime);
    UNUSED_PARAMETER(buffer);
#endif

#if (NUCLEUS_FILE_INCLUDED || NUCLEUS_FILE2_INCLUDED)
    /* A time function will need to be imlemented for your particular
     * target.
     */
    return (NU_UNAVAILABLE);
#endif

#if NT_FILE_SYSTEM
    memcpy(buffer, ctime ((CONST FAL_TIME *)ltime), CTIME_BUFFER_SIZE);

    if (buffer != NU_NULL)
        return (NU_SUCCESS);
    else
        return (-1);
#endif

#if IMF_INCLUDED
    return (NU_UNAVAILABLE);
#endif
}

/************************************************************************
*
*  FUNCTION
*
*      FAL_Local_Time
*
*  DESCRIPTION
*
*      Time function -  The user will have to implement their own time
*      data functions.
*
*  INPUTS
*
*      *timer      Total seconds in time from a specified date.
*      *tm         Pointer to the structure that will be filled in by
*                  the function.
*
************************************************************************/
INT FAL_Local_Time(FAL_TIME *timer, FAL_LOCAL *tm)
{
    /* Remove Warnings */
#if (NUCLEUS_FILE_INCLUDED || NUCLEUS_FILE2_INCLUDED || IMF_INCLUDED)
    UNUSED_PARAMETER(timer);
    UNUSED_PARAMETER(tm);
#endif

#if (NT_FILE_SYSTEM)
    UNUSED_PARAMETER(tm);
#endif

#if (NUCLEUS_FILE_INCLUDED || NUCLEUS_FILE2_INCLUDED)
    fs_user->p_errno = NU_UNAVAILABLE;
    return (-1);
#endif

#if NT_FILE_SYSTEM
    *tm = *(localtime ((CONST FAL_TIME *)timer));

    if (tm != NU_NULL)
        return (NU_SUCCESS);
    else
        return (-1);
#endif

#if IMF_INCLUDED
    p_errno = NU_UNAVAILABLE;
    return (-1);
#endif
}

/************************************************************************
*
*  FUNCTION
*
*      FAL_Store_Name
*
*  DESCRIPTION
*
*      Stores the file name according to the file system's
*      implementation standard.
*
*  SUPPORTED BY
*
*      Nucleus File
*      NT File System
*      In-Memory File System
*
*  INPUTS
*
*      *name       Pointer to the buffer that will hold the name.
*      *r_file     Pointer to the file structure from which to extract
*                  the name.
*
*  OUTPUTS
*
*      NU_SUCCESS
*
************************************************************************/
INT FAL_Store_Name(CHAR *name, FAL_DIR *r_file)
{
    INT temp = 0;
    INT i = 0;

    /* Remove Warnings */
#if (IMF_INCLUDED || NT_FILE_SYSTEM)
    UNUSED_PARAMETER(i);
#if IMF_INCLUDED
    UNUSED_PARAMETER(temp);
#endif
#endif

#if  NUCLEUS_FILE_INCLUDED
    /*  With Nucleus FILE the file name is split up in two
     *  parts.  The filename and the extension are both
     *  Null Terminated.  This handles this problem.
     */
    while(r_file->fname[temp] != ' ' && r_file->fname[temp] != '\0')
    {
        name[temp] = r_file->fname[temp];
        temp++;
    }

    if (r_file->fext[0] != ' ')
    {
        name[temp] = '.';
        temp++;

        while(r_file->fext[i] != ' ' && r_file->fext[i] != '\0')
        {
            name[temp] = r_file->fext[i];
            temp++;
            i++;
        }
    }

    name[temp] = '\0';
    return (NU_SUCCESS);
#endif

#if  NUCLEUS_FILE2_INCLUDED
    /*  With Nucleus FILEv2 the long file name contains
     *  both the filename and extension.  It is
     *  Null Terminated.
     */

    /* this is a directory */
    if (r_file->fext[0] == ' ')
        i = 1;
        
     while(r_file->lfname[temp] != '\0')
    {
        name[temp] = r_file->lfname[temp];
        temp++;
    }

    if (i == 1)
    {
        while( name[temp-1] == ' ')
            temp--;
    }

    name[temp] = '\0';
    return (NU_SUCCESS);
#endif

#if NT_FILE_SYSTEM
    /* With NT, the name and extension are not split up */
    while(r_file->name[temp] != '\0')
    {
        name[temp] = r_file->name[temp];
        temp++;
    }

    name[temp] = '\0';

    return (NU_SUCCESS);
#endif

#if IMF_INCLUDED
    return (IMF_Store_Name (name, r_file));
#endif
}

/************************************************************************
*
*  FUNCTION
*
*      FAL_Sys_Init
*
*  DESCRIPTION
*
*      Performs initialization functions for the given file system
*
*  SUPPORTED BY
*
*      Nucleus File
*      In-Memory File System
*
*  INPUTS
*
*      None
*
*  OUTPUTS
*
*      If Successful   -   NU_SUCCESS
*      If Unsuccessful -   -1
*
************************************************************************/
INT FAL_Sys_Init(INT ram_disk)
{
    INT     i;
    STATUS  status;
    CHAR    default_disk[3];

    default_disk[0] = (CHAR)('A' + ram_disk);
    default_disk[1] = ':';
    default_disk[2] = 0;

    Default_Drive = ram_disk;

    /* Remove Warnings */
#if (NT_FILE_SYSTEM || IMF_INCLUDED)
    UNUSED_PARAMETER(ram_disk);
#endif

#if (NT_FILE_SYSTEM || IMF_INCLUDED || NUCLEUS_FILE2_INCLUDED)
    UNUSED_PARAMETER(default_disk);
#endif

#if (NUCLEUS_FILE_INCLUDED || NUCLEUS_FILE2_INCLUDED || NT_FILE_SYSTEM)
    status = NU_SUCCESS;
    i = 0;

    UNUSED_PARAMETER(status);
    UNUSED_PARAMETER(i);
#endif

#if NUCLEUS_FILE_INCLUDED

    if (NUFP_Initialize() != NU_SUCCESS)
        return (-1);

    /* initialize memory; allocate tables needed by Nucleus File system
       this only has to be done once system wide.  This routine is found
       in the file pc_memry.c and calls various routines that may need
       to be ported to your environment. */
    if (!pc_memory_init())
        return (-1);

    /* Each task must register as a Nucleus user */
    if (!NU_Become_File_User())
         return (-1);

    /* Initialize and format the disk. */
    if (!RAM_disk_init())
        return (-1);

    /*  Prepare the RAM disk for use by this task.  */
    if (!NU_Open_Disk (default_disk))
        return (-1);

    if (NU_Set_Default_Drive ((INT16)Default_Drive) == 0)
        return (-1);

    /*  Set the Current Directory to the Root */
    if(!NU_Set_Current_Dir (default_disk))
        return (-1);

    return (NU_SUCCESS);

#endif

#if NUCLEUS_FILE2_INCLUDED

    /* This function call will initialize the Nucleus File System */
    status = file_init();

    if (status != NU_SUCCESS)
        return(-1);

    return (NU_SUCCESS);

#endif

#if NT_FILE_SYSTEM
    return (NU_SUCCESS);
#endif

#if IMF_INCLUDED
    for (i = 0; i < MAX_FILES; i++)
    {
        status = NU_Allocate_Memory (&System_Memory, (VOID *)&file_desc_ary[i].start,
                 MAX_FILE_SIZE, NU_NO_SUSPEND);

        if(status != NU_SUCCESS)
        {
            p_errno = status;
            return (-1);
        }

        file_desc_ary[i].end = file_desc_ary[i].start + MAX_FILE_SIZE;
        file_desc_ary[i].length = 0;
        file_desc_ary[i].max_length = MAX_FILE_SIZE;
        file_desc_ary[i].file_ptr = file_desc_ary[i].start;
        file_desc_ary[i].eof_ptr = file_desc_ary[i].start;
        file_desc_ary[i].used = 0;
        file_desc_ary[i].open = 0;

        UTL_Zero(file_desc_ary[i].name, (NAME_LENGTH + 1));
    }
    return (NU_SUCCESS);
#endif
}

/************************************************************************
*
*  FUNCTION
*
*      FAL_Object_File_Length
*
*  DESCRIPTION
*
*      Returns the length of the file associated with the file.
*
*  SUPPORTED BY
*
*      Nucleus File
*      NT File System
*      In-Memory File System
*
*  INPUTS
*
*      *file       Pointer to the file structure of the file of which
*                  to get the length.
*
*  OUTPUTS
*
*      The Length of the File
*
************************************************************************/
UINT32 FAL_Object_File_Length(FAL_DIR *file)
{
#if  (NUCLEUS_FILE_INCLUDED || NUCLEUS_FILE2_INCLUDED)
    return (file->fsize);
#endif

#if NT_FILE_SYSTEM
    return (file->size);
#endif

#if IMF_INCLUDED
    return (file->length);
#endif
}

/************************************************************************
*
*  FUNCTION
*
*      FAL_Handle_File_Length
*
*  DESCRIPTION
*
*      Returns the length of the file associated with the file
*      handle.
*
*  SUPPORTED BY
*
*      Nucleus File
*      NT File System
*      In-Memory File System
*
*  INPUTS
*
*      file_desc       File descriptor associated with the file.
*
*  OUTPUTS
*
*      The Length of the File
*
************************************************************************/
UINT32 FAL_Handle_File_Length(FAL_FILE file_desc)
{
#if (NUCLEUS_FILE_INCLUDED || NUCLEUS_FILE2_INCLUDED)
    UINT32  data_length = 0;
    INT32   original_location;

    /* Save off the current location of the file pointer - seek 0 bytes
     * from the current position.
     */
    original_location = NU_Seek(file_desc, 0, PSEEK_CUR);

    if (original_location >= 0)
    {
        /* Get the end location of the file pointer - seek to the end of
         * the file.
         */ 
        data_length = (UINT32)NU_Seek(file_desc, 0, PSEEK_END);

        /* Restore the original position of the file pointer - seek 
         * original_location bytes from the beginning of the file.
         */
        NU_Seek(file_desc, original_location, PSEEK_SET);
    }

    return (data_length);
#endif

#if NT_FILE_SYSTEM
    UINT32  data_length;

    if ( (data_length = _filelength(file_desc)) < 0)
        data_length = 0;

    return (data_length);
#endif

#if IMF_INCLUDED
    return (file_desc_ary[file_desc].length);
#endif
}

/************************************************************************
*
*  FUNCTION
*
*      FAL_Disk_Space
*
*  DESCRIPTION
*
*      Returns the amount of space available on the current disk.
*
*  SUPPORTED BY
*
*      Nucleus File
*      NT File System
*      In-Memory File System
*
*  INPUTS
*
*      None
*
*  OUTPUTS
*
*      The amount of free space on the current disk.
*
************************************************************************/
UINT32 FAL_Disk_Space(VOID)
{
#if (NUCLEUS_FILE_INCLUDED || NUCLEUS_FILE2_INCLUDED)
    CHAR    drive;
#endif

#if NT_FILE_SYSTEM
    UINT32  sectors_per_cluster[1];
    UINT32  bytes_per_sector[1];
    UINT32  free_clusters[1];
    UINT32  total_clusters[1];
#endif

#if  NUCLEUS_FILE_INCLUDED    
    drive = (CHAR)('A' + FAL_Get_Curr_Drive());

    return (NU_FreeSpace (&drive));
#endif

#if  NUCLEUS_FILE2_INCLUDED
    UINT8   sectors_per_cluster[1];
    UINT16  bytes_per_sector[1];
    UINT32  free_clusters[1];
    UINT32  total_clusters[1];
    
    drive = (CHAR)('A' + FAL_Get_Curr_Drive());

    if (NU_FreeSpace (&drive, sectors_per_cluster,
        bytes_per_sector, free_clusters, total_clusters) < 0)
        return (0);

    return ((*sectors_per_cluster) * (*bytes_per_sector) * (*free_clusters));
#endif

#if NT_FILE_SYSTEM
    if (GetDiskFreeSpace(NU_NULL, sectors_per_cluster, bytes_per_sector,
                     free_clusters, total_clusters) < 0)
        return (0);

    return ((*sectors_per_cluster) * (*bytes_per_sector) * (*free_clusters));
#endif

#if IMF_INCLUDED
    return (MAX_FILE_SIZE);
#endif
}

/************************************************************************
*
*  FUNCTION
*
*      FAL_Get_Curr_Drive
*
*  DESCRIPTION
*
*      Returns the current drive.
*      0 = A
*      1 = B ...
*
*  SUPPORTED BY

⌨️ 快捷键说明

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