📄 osfileapi.c
字号:
/*** File : osfilesys.c** Author : *//**************************************************************************************** INCLUDE FILES****************************************************************************************/#include "vxWorks.h"#include "stdio.h"#include "stdlib.h"#include "string.h"#include "ioLib.h"#include "ramDrv.h"#include "dosFsLib.h"#include "dirent.h"#include "stat.h"#include "cbioLib.h"#include "ramDiskCbio.h"#include "common_types.h"#include "osapi.h"/**************************************************************************************** DEFINES****************************************************************************************//**************************************************************************************** GLOBAL DATA****************************************************************************************//**************************************************************************************** INITIALIZATION FUNCTION****************************************************************************************/int32 OS_check_name_length(const char *path);int32 OS_NameChange( char* name);/**************************************************************************************** Filesys API****************************************************************************************//*** Standard File system API*//*-------------------------------------------------------------------------------------- Name: OS_creat Purpose: creates a file specified by const char *path, with read/write permissions by access. The file is also automatically opened by the create call. Returns: OS_FS_INVALID_POINTER if path is NULL OS_FS_PATH_TOO_LONG if path exceeds the maximum number of chars OS_FS_ERR_NAME_TOO_LONG if the name of the file is too long OS_FS_ERROR if permissions are unknown or OS call fails OS_FS_SUCCESS if success ---------------------------------------------------------------------------------------*/int32 OS_creat (const char *path, uint32 access){ int status; char local_path[OS_MAX_PATH_LEN]; if (path == NULL) return OS_FS_ERR_INVALID_POINTER; if (strlen(path) > OS_MAX_PATH_LEN) return OS_FS_ERR_PATH_TOO_LONG; /* make a local copy of the path */ strcpy(local_path, path); OS_NameChange(local_path); /* check if the name of the file is too long */ if (OS_check_name_length(local_path) == OS_FS_ERROR) return OS_FS_ERR_NAME_TOO_LONG; status = creat(local_path, (int) access); if (status != ERROR) return status; else return OS_FS_ERROR; } /* end OS_creat *//*-------------------------------------------------------------------------------------- Name: OS_open Purpose: Opens a file. flags parameters are OS_READ_ONLY,OS_WRITE_ONLY, or OS_READ_WRITE Returns: OS_FS_INVALID_POINTER if path is NULL OS_FS_PATH_TOO_LONG if path exceeds the maximum number of chars OS_FS_ERR_NAME_TOO_LONG if the name of the file is too long OS_FS_ERROR if permissions are unknown or OS call fails a file descriptor if success---------------------------------------------------------------------------------------*/int32 OS_open (const char *path, uint32 flags, uint32 mode){ int status; char local_path[OS_MAX_PATH_LEN]; if(path == NULL) return OS_FS_ERR_INVALID_POINTER; if (strlen(path) > OS_MAX_PATH_LEN) return OS_FS_ERR_PATH_TOO_LONG; /* make a local copy of the path */ strcpy(local_path, path); OS_NameChange(local_path); /* check if the name of the file is too long */ if (OS_check_name_length(local_path) == OS_FS_ERROR) return OS_FS_ERR_NAME_TOO_LONG; /* mode is not used in dosFs, just in NFS drives */ status = open(local_path, (int) flags, (int) mode); if (status != ERROR) return status; else return OS_FS_ERROR; } /* end OS_open *//*-------------------------------------------------------------------------------------- Name: OS_close Purpose: Closes a file. Returns: OS_FS_ERROR if file descriptor could not be closed OS_FS_SUCCESS if success---------------------------------------------------------------------------------------*/int32 OS_close (uint32 filedes){ int status; status = close ((int) filedes); if (status == ERROR) return OS_FS_ERROR; else return OS_FS_SUCCESS; }/* end OS_close *//*-------------------------------------------------------------------------------------- Name: OS_read Purpose: reads up to nbytes from a file, and puts them into buffer. Returns: OS_FS_ERR_INVALID_POINTER if buffer is a null pointer OS_FS_ERROR if OS call failed number of bytes read if success---------------------------------------------------------------------------------------*/int32 OS_read (uint32 filedes, void *buffer, uint32 nbytes){ int status; if (buffer == NULL) return OS_FS_ERR_INVALID_POINTER; status = read (filedes, (char*) buffer, (size_t) nbytes); if (status == ERROR) return OS_FS_ERROR; return status; }/* end OS_read *//*-------------------------------------------------------------------------------------- Name: OS_write Purpose: writes to a file. copies up to a maximum of nbtyes of buffer to the file described in filedes Returns: OS_FS_INVALID_POINTER if buffer is NULL OS_FS_ERROR if OS call failed number of bytes written if success---------------------------------------------------------------------------------------*/int32 OS_write (uint32 filedes, void *buffer, uint32 nbytes){ int status; if (buffer == NULL) return OS_FS_ERR_INVALID_POINTER; status = write( (int) filedes, (char*) buffer, (size_t) nbytes ); if (status != ERROR) return (int32) status; else return (int32) OS_FS_ERROR; }/* end OS_write *//*-------------------------------------------------------------------------------------- Name: OS_chmod Notes: This is not going to be implemented because there is no use for this function.---------------------------------------------------------------------------------------*/int32 OS_chmod (const char *path, uint32 access){ return OS_FS_UNIMPLEMENTED;} /* end OS_chmod *//*-------------------------------------------------------------------------------------- Name: OS_stat Purpose: returns information about a file or directory in a os_fs_stat structure Returns: OS_FS_ERR_INVALID_POINTER if path or filestats is NULL OS_FS_ERR_PATH_TOO_LONG if the path is too long to be stored locally OS_FS_ERR_NAME_TOO_LONG if the name of the file is too long to be stored OS_FS_ERROR id the OS call failed OS_FS_SUCCESS if success Note: The information returned is in the structure pointed to by filestats ---------------------------------------------------------------------------------------*/int32 OS_stat (const char *path, os_fstat_t *filestats){ int ret_val; char local_path[OS_MAX_PATH_LEN]; if (path == NULL || filestats == NULL) return OS_FS_ERR_INVALID_POINTER; if (strlen(path) > OS_MAX_PATH_LEN) return OS_FS_ERR_PATH_TOO_LONG; /* make a local copy of the path */ strcpy(local_path, path); OS_NameChange(local_path); /* check if the name of the file is too long */ if (OS_check_name_length(local_path) == OS_FS_ERROR) return OS_FS_ERR_NAME_TOO_LONG; ret_val = stat( (char*) local_path, filestats); if (ret_val == ERROR) return OS_FS_ERROR; else return OS_FS_SUCCESS; } /* end OS_stat *//*-------------------------------------------------------------------------------------- Name: OS_lseek Purpose: sets the read/write pointer to a specific offset in a specific file. Whence is either OS_SEEK_SET,OS_SEEK_CUR, or OS_SEEK_END Returns: the new offset from the beginning of the file OS_FS_ERROR if OS call failed---------------------------------------------------------------------------------------*/int32 OS_lseek (uint32 filedes, int32 offset, uint32 whence){ int status; status = lseek( (int) filedes, (long) offset, (int) whence ); if (status != ERROR) return status; else return OS_FS_ERROR; }/* end OS_lseek *//*-------------------------------------------------------------------------------------- Name: OS_remove Purpose: removes a given filename from the drive Returns: OS_FS_SUCCESS if the driver returns OK OS_FS_ERROR if there is no device or the driver returns error OS_FS_ERR_INVALID_POINTER if path is NULL OS_FS_ERR_PATH_TOO_LONG if path is too long to be stored locally OS_FS_ERR_NAME_TOO_LONG if the name of the file to remove is too long to be stored locally---------------------------------------------------------------------------------------*/int32 OS_remove (const char *path){ int status; char local_path[OS_MAX_PATH_LEN]; if (path == NULL) return OS_FS_ERR_INVALID_POINTER; if (strlen(path) > OS_MAX_PATH_LEN) return OS_FS_ERR_PATH_TOO_LONG; /* make a local copy of the path */ strcpy(local_path, path);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -