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

📄 osfilesys.c

📁 osal for Vxworks,c code
💻 C
字号:
/*** File   : osfilesys.c** Author : Nicholas Yanchik / GSFC Code 582*//****************************************************************************************                                    INCLUDE FILES****************************************************************************************/#include "vxWorks.h"#include "stdio.h"#include "stdlib.h"#include "string.h"#include "ioLib.h"#include "ramDrv.h"#include "dosFsLib.h"#include "drv/hdisk/ataDrv.h"#include "dirent.h"#include "stat.h"#include "cbioLib.h"#include "ramDiskCbio.h"#include "common_types.h"#include "osapi.h"/****************************************************************************************                                   GLOBAL DATA****************************************************************************************/int32 OS_check_name_length(const char *path);int32 OS_NameChange(char* name);/* ** This is the volume table reference. It is defined in the BSP/startup code for the board*/extern OS_VolumeInfo_t OS_VolumeTable [NUM_TABLE_ENTRIES]; /****************************************************************************************                                    Filesys API****************************************************************************************//*** System Level API *//*---------------------------------------------------------------------------------------    Name: OS_mkfs    Purpose: Makes a RAM disk on the target with a dos file system        Returns: OS_FS_ERR_INVALID_POINTER if devname is NULL             OS_FS_DRIVE_NOT_CREATED if the OS calls to create the the drive failed             OS_FS_SUCCESS on creating the disk    Note: if address == 0, then a malloc will be called to create the disk---------------------------------------------------------------------------------------*/int32 OS_mkfs (char *address, char *devname, char *volname, uint32 blocksize,                uint32 numblocks){    int i;    char local_volname[OS_MAX_PATH_LEN];    uint32 ReturnCode;            BLK_DEV *pBlkDev;    DOS_VOL_DESC *pVolDesc;    BLK_DEV      *ataBlkDev;    DOS_VOL_DESC *ataVolDesc;        if ( devname == NULL || volname == NULL )        return OS_FS_ERR_INVALID_POINTER;    /* find an open entry in the Volume Table */    for (i = 0; i < NUM_TABLE_ENTRIES; i++)    {        if (OS_VolumeTable[i].FreeFlag == TRUE && OS_VolumeTable[i].IsMounted == FALSE            && strcmp(OS_VolumeTable[i].DeviceName, devname) == 0)            break;    }    if (i >= NUM_TABLE_ENTRIES)        return OS_FS_ERR_DEVICE_NOT_FREE;    /* now enter the info in the table */    OS_VolumeTable[i].FreeFlag = FALSE;    strcpy(OS_VolumeTable[i].VolumeName, volname);    OS_VolumeTable[i].BlockSize = blocksize;        /* note we don't know the mount point yet */    strcpy(local_volname,volname);    strcat(local_volname,":");    /* make a disk if it is RAM based */    /*------------------------------- */    if (OS_VolumeTable[i].VolumeType == RAM_DISK)    {        pBlkDev = ramDevCreate (address,  blocksize,  numblocks,  numblocks,  0);            if (pBlkDev == NULL)            ReturnCode = OS_FS_ERR_DRIVE_NOT_CREATED;            pVolDesc = dosFsMkfs (local_volname, pBlkDev);            if (pVolDesc == NULL)            ReturnCode =  OS_FS_ERR_DRIVE_NOT_CREATED;            ReturnCode = OS_FS_SUCCESS;    }        /* Initialize a Flash disk if thats what we have */    /* --------------------------------------------- */    else if (OS_VolumeTable[i].VolumeType == FLASH_DISK_FORMAT)    {        /*         ** Create the Flash disk device         */           if( (ataBlkDev = ataDevCreate( 0, 0, 0, 0)) == NULL)        {            printf("Error Flash Disk Device in BSP!\n");        }        else        {            printf("FLASH device initialized.\n");        }        /*         **  Attach to the Flash file system         */        if ( ( ataVolDesc = dosFsMkfs(local_volname, ataBlkDev)) == NULL )         {            printf("Error Creating DOS File system on FLASH disk in BSP!\n");            ReturnCode = OS_FS_ERR_DRIVE_NOT_CREATED;        }        else        {            printf("DOS volume on FLASH device created.\n");            ReturnCode = OS_FS_SUCCESS;        }            }/* if FLASH_DISK_FORMAT */        else if (OS_VolumeTable[i].VolumeType == FLASH_DISK_INIT)    {        /*         ** Create the Flash disk device         */           if( (ataBlkDev = ataDevCreate( 0, 0, 0, 0)) == NULL)        {            printf("Error Flash Disk Device in BSP!\n");        }        else        {            printf("FLASH device initialized.\n");        }        /*         **  Attach to the Flash file system         */        if( (ataVolDesc = dosFsDevInit (local_volname, ataBlkDev, 0)) == NULL)         {            printf("Error Creating DOS File system on FLASH disk in BSP!\n");            ReturnCode = OS_FS_ERR_DRIVE_NOT_CREATED;        }        else        {            printf("DOS volume on FLASH device created.\n");            ReturnCode = OS_FS_SUCCESS;        }            }/* if FLASH_DISK_INIT */    /* VolumeType is something else that is not supported right now */    else        ReturnCode = OS_FS_ERROR;        return ReturnCode;} /* end OS_mkfs *//*--------------------------------------------------------------------------------------    Name: OS_mount        Purpose: mounts a drive.---------------------------------------------------------------------------------------*/int32 OS_mount (const char *devname, char* mountpoint){   int i;   /* find the device in the table */    for (i = 0; i < NUM_TABLE_ENTRIES; i++)    {        if (OS_VolumeTable[i].FreeFlag == FALSE && OS_VolumeTable[i].IsMounted == FALSE             && strcmp(OS_VolumeTable[i].DeviceName, devname) == 0)            break;    }    /* make sure we found the device */    if (i >= NUM_TABLE_ENTRIES)        return OS_FS_ERR_DRIVE_NOT_CREATED;    /* attach the mountpoint */    strcpy(OS_VolumeTable[i].MountPoint, mountpoint);    OS_VolumeTable[i].IsMounted = TRUE;    return OS_FS_SUCCESS;      }/* end OS_mount *//*--------------------------------------------------------------------------------------    Name: OS_unmount        Purpose: unmounts a drive. and therefore makes all file descriptors pointing into             the drive obsolete.    Returns: OS_FS_ERR_INVALID_POINTER if name is NULL             OS_FS_ERR_PATH_TOO_LONG if the absolute path given is too long             OS_FS_ERROR if the OS calls failed             OS_FS_SUCCESS if success---------------------------------------------------------------------------------------*/int32 OS_umount (const char* mountpoint){    int fd;    STATUS ret_status;    char local_path [OS_MAX_PATH_LEN];    int i;    if (mountpoint == NULL)        return OS_FS_ERR_INVALID_POINTER;    if (strlen(mountpoint) > OS_MAX_PATH_LEN)        return OS_FS_ERR_PATH_TOO_LONG;       /* make a local copy of the path */    strcpy(local_path, mountpoint);    OS_NameChange(local_path);         /* find the device in the table */    for (i = 0; i < NUM_TABLE_ENTRIES; i++)    {        if (OS_VolumeTable[i].FreeFlag == FALSE && OS_VolumeTable[i].IsMounted == TRUE             && strcmp(OS_VolumeTable[i].MountPoint, mountpoint) == 0)            break;    }    /* make sure we found the device */    if (i >= NUM_TABLE_ENTRIES)        return OS_FS_ERROR;    fd = open(local_path,O_RDONLY,0);        if (fd == ERROR)        return OS_FS_ERROR;    ret_status = ioctl( fd, FIOUNMOUNT,0);    /* no need to close the fd, because ioctl is doing that for us */        if (ret_status == OK)        return OS_FS_SUCCESS;    else        return OS_FS_ERROR;    }/* end OS_umount *//*--------------------------------------------------------------------------------------    Name: OS_fsBlocksFree    Purpose: Returns the number of free blocks in a volume     Returns: OS_FS_INVALID_POINTER if name is NULL             OS_FS_ERROR if the OS call failed             The number of bytes free in a volume if success---------------------------------------------------------------------------------------*/int32 OS_fsBlocksFree (const char *name){    int fd;    int i;    STATUS status;    uint32 free_bytes;    char local_path [OS_MAX_PATH_LEN];    if (name == NULL)        return OS_FS_ERR_INVALID_POINTER;    for (i = 0; i < NUM_TABLE_ENTRIES; i++)    {        if (OS_VolumeTable[i].FreeFlag == FALSE && OS_VolumeTable[i].IsMounted == TRUE             && strcmp(OS_VolumeTable[i].MountPoint, name) == 0)            break;    }    /* make sure we found the device */    if (i >= NUM_TABLE_ENTRIES)        return OS_FS_ERROR;        strcpy(local_path,name);    OS_NameChange(local_path);    fd = open (local_path, O_RDONLY, 0);    if (fd == ERROR)        return OS_FS_ERROR;    status = ioctl(fd,FIONFREE, (unsigned long) &free_bytes);    if (status == ERROR)        return OS_FS_ERROR;        return (free_bytes / OS_VolumeTable[i].BlockSize);}/* end OS_fsBlocksFree *//*--------------------------------------------------------------------------------------    Name: OS_chkfs        Purpose: Checks the drives for inconsisenties and either repairs it or not    Returns: OS_FS_ERR_INVALID_POINTER if name is NULL             OS_FS_SUCCESS if success             OS_FS_ERROR if the OS calls fail---------------------------------------------------------------------------------------*/os_fshealth_t OS_chkfs (const char *name, boolean repair){      STATUS chk_status;    int fd;    char local_path [OS_MAX_PATH_LEN];    if (name == NULL)        return OS_FS_ERR_INVALID_POINTER;    strcpy(local_path,name);    OS_NameChange(local_path);    fd = open (local_path, O_RDONLY, 0);    if (fd == ERROR)        return OS_FS_ERROR;        /* Fix the disk if there are errors */    if (repair == 1)    {        chk_status = ioctl(fd, FIOCHKDSK, DOS_CHK_REPAIR | DOS_CHK_VERB_SILENT);        close(fd);        if (chk_status == OK)            return OS_FS_SUCCESS;        else            return OS_FS_ERROR;    }        /* only check the disk, don't fix anything */    else    {                chk_status = ioctl(fd, FIOCHKDSK, DOS_CHK_ONLY | DOS_CHK_VERB_SILENT);        close(fd);        if (chk_status == OK)           return OS_FS_SUCCESS;        else            return OS_FS_ERROR;    }    /* code should never get here */    return OS_FS_ERROR;}/* end OS_chkfs *//*------------------------------------------------------------------------------------- * Name: OS_NameChange *  * Purpose: Because of the abstraction of the filesystem across OSes, we have to change *          the name of the {file, directory, drive} to be what the OS can actually  *          accept---------------------------------------------------------------------------------------*/int32 OS_NameChange( char* name){       char LocalName [OS_MAX_PATH_LEN];    char newname [OS_MAX_PATH_LEN];    char devname [OS_MAX_PATH_LEN];    char filename[OS_MAX_PATH_LEN];    int NumChars;    int i=0;       /* copy the name locally for good measure */    strcpy(LocalName,name);    /*printf("Local_path: $%s\n",LocalName);*/        /* we want to find the number of chars in to LocalName the second "/" is.     * Since we know the first one is in spot 0, we star looking at 1, and go until     * we find it.*/        NumChars=1;        while (strncmp (&LocalName[NumChars],"/",1) != 0)        NumChars++;        /* don't let it overflow to cause a segfault when trying to get the highest level     * directory */        if (NumChars >= strlen(LocalName))            NumChars = strlen(LocalName);      /* copy over only the part that is the device name */    strncpy(devname,LocalName,NumChars);        strncpy(filename,(LocalName + NumChars*sizeof(char)), strlen(LocalName) - NumChars+1);        /* look for the dev name we found in the VolumeTable */    for (i = 0; i < NUM_TABLE_ENTRIES; i++)    {        if (OS_VolumeTable[i].FreeFlag == FALSE &&             strncmp(OS_VolumeTable[i].MountPoint, devname,NumChars) == 0)        {            break;        }    }    /* Make sure we found a valid drive */    if (i >= NUM_TABLE_ENTRIES)        return OS_FS_ERR_DRIVE_NOT_CREATED;       /* THIS IS THE PART THAT CHANGES FOR VXWORKS */    /* copy over the physical first part of the drive */    strcpy(newname,OS_VolumeTable[i].VolumeName);    /* concat that with the file name */    strcat(newname,":");    strcat(newname, filename);    /* push it back to the caller */    strcpy(name,newname);        /*printf("new_path  : $%s\n",newname);*/    return OS_FS_SUCCESS;    } /* end OS_NameChange*/

⌨️ 快捷键说明

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