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

📄 fdi_file.c

📁 FDI Intel开发的FLASH文件系统,功能很强大
💻 C
📖 第 1 页 / 共 5 页
字号:
/* ###########################################################################
###  Intel Confidential
###  Copyright (c) Intel Corporation 1995-2002
###  All Rights Reserved.
###  -------------------------------------------------------------------------
###  Project: Flash Data Integrator
###
###  Module: FDI_FILE.C - The file support API shall exist as a software layer
###                       that utilizes the standard FDI API functions.  The
###                       file API shall be implemented with an ANSI stdio
###                       interface.  The file API provides file information
###                       associated with each file.  The application shall be
###                       responsible for sorting the file info.
###  Functions:
###    FDI_fopen           opens the file whose name is the string pointed to
###                           by filename and associates a stream with it
###    FDI_fwrite          writes from the array buffer, up to count elements
###                           whose size is element_size, to a specified stream
###    FDI_fread           reads into the array buffer, up to count elements of
###                           size element_size, from a specified stream
###    FDI_fclose          flushes a stream specified by stream and closes file
###    FDI_fseek           sets the file position indicator for a stream
###                           specified
###    FDI_findfirst       begins search for files specified by wildcards
###    FDI_findnext        fetches subsequent files that match the filename
###                           given in FDI_findfirst
###    FDI_remove          deletes the file specified by filename
###    FDI_rename          changes the name of a file from oldname to newname
###    FDI_fileinit        initial files after successful FDI_Init
###    FDI_feof            EOF detected while reading from input stream
###    FDI_ferror          error has occurred while reading from or writing to
###                           the stream
###    FDI_clearerr        resets error and end-of-file indicators on stream
###    FDI_chmod           change file permissions
###    FDI_fchmod          change open stream permissions
###    FDI_stat            get file permissions
###    FDI_fstat           get open stream permissions
###    FDI_ftell           get the file pointer
###    FDI_fileterminate   deallocates resources
###
###    FileNameSearch      searches the file info structures for matching file
###    FileNameMatch       compares one name to another including wildcards
###    FileDelete          deletes file info and data info for a file
###    FileNameValid       determines if filename's size and chars are valid
###    FilesOnFlashValid   determines if files on flash are valid
###    DeletePLRFile       deletes file indicated by plr_id field of file info
###    RestoreFile         restores file to plr_size in file info
###    GetNextFreeFLTIndex returns the next free index into FLT
###
###    FileGetTime         returns a default time value
###    FileGetDate         returns a default date value
###    FileGetGID          returns a default group id
###    FileGetUID          returns a default user id
###
###  $Archive: /FDI/SRC/FM_ENH/fdi_file.c $
###  $Revision: 64 $
###  $Date: 10/14/04 2:01p $
###  $Author: Wtiso $
###  $NoKeywords $
########################################################################### */

/*                                                               
 *****************************************************************
 * NOTICE OF LICENSE AGREEMENT                                    
 *                                                                
 * This code is provided by Intel Corp., and the use is governed  
 * under the terms of a license agreement. See license agreement  
 * for complete terms of license.                                 
 *                                                                
 * YOU MAY ONLY USE THE SOFTWARE WITH INTEL FLASH PRODUCTS.  YOUR 
 * USE OF THE SOFTWARE WITH ANY OTHER FLASH PRODUCTS IS EXPRESSLY 
 * PROHIBITED UNLESS AND UNTIL YOU APPLY FOR, AND ARE GRANTED IN  
 * INTEL'S SOLE DISCRETION, A SEPARATE WRITTEN SOFTWARE LICENSE   
 * FROM INTEL LICENSING ANY SUCH USE.                             
 *****************************************************************
 */
 

#define CURRENT_FILE_BEING_COMPILED FDI_FM

/* ### Include Files
 * ################################# */

#include "fdi_ext.h"
#include "fdi_mutx.h"
#include "fdi_file.h"

#if (FILE_MANAGER == TRUE)
#include "fm_flt.h"


/* ### Local Declarations
 * ################################# */

/* mode_flag bit masks */
#define FILE_MUST_EXIST                0x01
#define FILE_CONTENT_LOST              0x02
#define READ_PERMITTED                 0x04
#define WRITE_PERMITTED                0x08
#define WRITE_END_ONLY                 0x10
#define MODE_INVALID                   0x00

#define FILE_INVALID                   0xFFFF /* invalid identifier value */
#define FILE_NULL                      0x00   /* File Manager NULL value */
#define FILE_NOERR                     0x00   /* API no error return value */
#define FILE_NOEOF                     0x00   /* indicates no EOF */
#define INIT_BYTE                      0x00   /* MemorySet init byte value */

/* define macros to initialize structures */
#define INIT_FILE_INFO(fi_ptr) \
          MemorySet((BYTE_PTR)(fi_ptr), INIT_BYTE, sizeof(FILE_INFO)); \
          ((FILE_INFO *)(fi_ptr))->data_id = FILE_INVALID; \
          ((FILE_INFO *)(fi_ptr))->plr_id = FILE_INVALID

#define INIT_STREAM_INFO(si_ptr) \
          MemorySet((BYTE_PTR)(si_ptr), INIT_BYTE, sizeof(STREAM_INFO)); \
          ((STREAM_INFO *)(si_ptr))->open_file_info.data_id = FILE_INVALID; \
          ((STREAM_INFO *)(si_ptr))->open_file_info.plr_id = FILE_INVALID; \
          ((STREAM_INFO *)(si_ptr))->file_info_id = FILE_INVALID

/*
 * local structure contains the open stream information much like an
 * operating system's FILE pointer structure
 */
typedef struct tag_stream_info
{
   DWORD      file_pointer;            /* offset into the open file */
   FILE_INFO  open_file_info;          /* file information */
   FILE_ID    file_info_id;            /* file info parameter's identifier */
   /* E5.1.722 START */
   WORD       permissions;             /* permissions of the stream at open */
   /* E5.1.722 END */
   int        read_eof;                /* track read accesses past EOF */
   ERR_CODE   ferrno;                  /* track errors found in this stream */
   char       mode_flag;               /* bit 0 named file must already exist
                                        * bit 1 existing file's contents lost
                                        * bit 2 read from stream permitted
                                        * bit 3 write to stream permitted
                                        * bit 4 write permitted only at end
                                        *       of stream */
}  STREAM_INFO;

/* E5.1.722 START */
/* specifies the default permissions a file should have when created */
#define FILE_DEFAULT_PERMISSIONS       (S_IRWXU | S_IRWXG | S_IRWXW)
/* E5.1.722 END */

/* StreamInfoTable is the STREAM_INFO structure array used
 * by API's to keep track of opened files
 * index 0 is reserved for search info
 */
static STREAM_INFO StreamInfoTable[NUM_OPEN_FILES + 1];

/* the search info structure is at index 0 in StreamInfoTable so
 * FDI_fopen can't return 0 since it is reserved for error
 */
static STREAM_INFO *const SearchInfoPtr = StreamInfoTable;

/* search name plus end of string character */
static FDI_TCHAR FLTSearchName[FILE_NAME_SIZE + 1];

static WORD FileInitComplete = FALSE; /* flag to confirm file init
                                       * function was called */

/* invalid filename characters array
 * must end with end of string character as array end marker
 */
static const FDI_TCHAR InvalidFilenameChars[] = {FILE_SCWILDCARD,
                                                 FILE_MCWILDCARD,
                                                 FILE_EOS};

/* the following aren't defined as static since they are used for testing
 * purposes.
 */
SEM_MTX_ID FileAPIMutexSemaphore = SEM_NULL;  /* File Manager API semaphore */

WORD EnableDataStreaming;  /* set during initialization for data streaming */

/* ### Local Function Declarations
 * ################################# */

static ERR_CODE FileNameSearch(const FDI_TCHAR *, FILE_ID *, FILE_INFO *);
static ERR_CODE FileDelete(FILE_ID, FILE_ID, int);
static ERR_CODE DeletePLRFile(FILE_ID, FILE_INFO *);
static ERR_CODE RestoreFile(FILE_ID, FILE_INFO *);
static int FileNameMatch(const FDI_TCHAR *, const FDI_TCHAR *);
static int FileNameValid(const FDI_TCHAR *);
static int FilesOnFlashValid(void);

/* please replace these functions with something appropriate for your OS */
static int FileGetTime(void);
static int FileGetDate(void);
static int FileGetGID(void);
static int FileGetUID(void);


/* These are string functions to manipulate single or multi-byte characters */
int FM_CompareStrings(const FDI_TCHAR *string1, 
                      const FDI_TCHAR *string2, 
                      const size_t count);
FDI_TCHAR *FM_CopyString(FDI_TCHAR *dest_string, 
                         const FDI_TCHAR *source_string, 
                         const size_t count);
size_t FM_GetStringLength(const FDI_TCHAR *string);
/* E 5.1.849 START */
const FDI_TCHAR *FM_SearchCharacter(const FDI_TCHAR *string, 
                              const FDI_TCHAR character, 
                              const size_t count);
/* E 5.1.849 END */                              
char FM_ConvertToLowerCase(char character);
static ERR_CODE GetFileInfo(const FDI_TCHAR *, FILE_ID *, FILE_INFO *);

/* E5.1.722 START */
static BOOL HasPermissionToWrite(const STREAM_INFO *, WORD, WORD);
static BOOL HasPermissionToRead(const STREAM_INFO *, WORD, WORD);
/* E5.1.722 END */


/* ### Global Declarations
 * ################################# */

ERR_CODE FDI_errno; /* track file system errors not associated
                     * with an open file identifier */
extern BYTE FDI_InitComplete;


/*############################################################################
 *### FDI_fopen
 *###
 *### DESCRIPTION:
 *###    The function FDI_fopen takes as arguments a file name and a mode;
 *###    each is specified as a character string.  The file name is used in
 *###    an implementation-specified matter to open or create a file and
 *###    associate it with a stream.  Returns the StreamInfoTable index used
 *###    for the opened file.
 *###
 *### USAGE:
 *###    file_identifier = FDI_fopen(filename_ptr, wb);
 *###
 *### PARAMETERS:
 *###
 *### INPUTS:
 *###  filename_ptr         const character string for file name
 *###  mode                 const character string for type specification;
 *###                       modes supported: rb, wb, ab, rb+, wb+, ab+
 *### OUTPUTS:
 *###
 *### RETURNS:
 *###  Returns the StreamInfoTable index used for the opened file.
 *###  If an error is detected, FDI_fopen returns 0.
 *###*/
FILE_ID
FDI_fopen(const FDI_TCHAR *filename_ptr, const char *mode)
{
   COMMAND_CONTROL cmd_cntrl;          /* define command control structure for
                                        * FDI API's */
   char            open_mode[8];       /* copy of input parameter lower case */
   WORD            index_src = 0;      /* character array index */
   WORD            index_dest = 0;     /* character array index */
   ERR_CODE        status = ERR_NONE;  /* define ERROR_CODE variable */
   BYTE            fdi_status;         /* returned by FDI_Status */
   DWORD           index1;             /* loop control variable */
   FILE_ID         return_index;       /* local to hold the table index used */
   STREAM_INFO     *stream_info_ptr;   /* pointer to table entry used */

   /* E5.1.722 START */
   WORD  user_id;
   WORD  group_id;
   /* E5.1.722 END */

   /* make sure file init function has been called */
   if (FileInitComplete == FALSE)
   {
      FDI_errno = ERR_INIT;
      return FILE_NULL;
   }

   /* make sure filename_ptr is valid */
   if (FileNameValid(filename_ptr) == FALSE)
   {
      FDI_errno = ERR_PARAM;
      return FILE_NULL;
   }

   /* lock the File Manager API semaphore */
   SEM_MTX_WAIT(FileAPIMutexSemaphore);

   /* test for an open file and return error if true
    * look for an existing file already in StreamInfoTable
    */
   for (index1 = 1; index1 <= NUM_OPEN_FILES; index1++)
   {
      if (FileNameMatch(filename_ptr,
                        StreamInfoTable[index1].open_file_info.file_name))
      {
         FDI_errno = ERR_OPEN;

         /* unlock the File Manager API semaphore */
         SEM_MTX_POST(FileAPIMutexSemaphore);

         return FILE_NULL;
      }
   }

   /* assign a StreamInfoTable entry to utilize for the file stream */
   for (index1 = 1; index1 <= NUM_OPEN_FILES; index1++)
   {
      if (StreamInfoTable[index1].mode_flag == MODE_INVALID)
      {
         break;
      }
   }
   if (index1 > NUM_OPEN_FILES)
   {
      FDI_errno = ERR_MAX_OPEN;

      /* unlock the File Manager API semaphore */
      SEM_MTX_POST(FileAPIMutexSemaphore);

      return FILE_NULL;
   }

   /* set assigned table entry to a pointer to STREAM_INFO */
   stream_info_ptr = &(StreamInfoTable[index1]);

   /* save index used to return to user */
   return_index = index1;

   *open_mode = FILE_EOS;
   /*
    * copy parameter mode to a local copy open_mode converting uppercase
    * to lower case and ignoring white spaces
    */
   while ((mode[index_src] != FILE_EOS) &&
          (index_src < (int)(sizeof(open_mode) - 1)))
   {
      /* ignoring white spaces */    
      if (mode[index_src] != '\t' && mode[index_src] != ' ')
      {
         /* convert uppercase characters to lower case */
         open_mode[index_dest] = FM_ConvertToLowerCase(mode[index_src]);
      }
      index_src++;
      index_dest++;
   }
   open_mode[index_dest] = FILE_EOS;       /* close the string */

   /*
    * set stream info field mode_flag based on input parameter mode for
    *    supported invoking modes
    */
   switch (open_mode[0])
   {
      case 'r':
         stream_info_ptr->mode_flag |= (FILE_MUST_EXIST | READ_PERMITTED);
         break;
      case 'w':
         stream_info_ptr->mode_flag |= (FILE_CONTENT_LOST | WRITE_PERMITTED);
         break;
      case 'a':
         stream_info_ptr->mode_flag |= (WRITE_END_ONLY | WRITE_PERMITTED);
         break;
      default:
         break;
   }
   /*
    * IF mode_flag was not modified in the previous switch OR the
    * second character in mode is not a 'b'
    */
   if ((stream_info_ptr->mode_flag == MODE_INVALID) || (open_mode[1] != 'b'))
   {
      INIT_STREAM_INFO(stream_info_ptr);
      FDI_errno = ERR_PARAM;

      /* unlock the File Manager API semaphore */
      SEM_MTX_POST(FileAPIMutexSemaphore);

⌨️ 快捷键说明

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