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

📄 fdi_file.c

📁 FDI Intel开发的FLASH文件系统,功能很强大
💻 C
📖 第 1 页 / 共 5 页
字号:
 *### PARAMETERS:
 *###
 *### INPUTS:
 *###  buffer_ptr           const void pointer to a buffer of data to be
 *###                       written
 *###  element_size         size of element referenced by buffer_ptr
 *###  count                number of buffer_ptr elements to be written
 *###  stream               file stream identifier
 *###
 *### OUTPUTS:
 *###
 *### RETURNS:
 *###  Returns the number of elements successfully queued, which will be
 *###  equal to the parameter count unless an error occurred.
 *###*/
size_t
FDI_fwrite(const void *buffer_ptr,
           size_t element_size,
           size_t count,
           FILE_ID stream)
{
   COMMAND_CONTROL cmd_cntrl;          /* command control structure for APIS */
   ERR_CODE        status;             /* define ERROR_CODE variable */
   STREAM_INFO     *stream_info_ptr;   /* pointer to table entry */
   FILE_INFO       *file_info_ptr;     /* pointer to open file info */
   BYTE            data_sub_command;   /* sub_command used for data param */

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

   /* make sure stream is valid */
   if ((stream == 0) || (stream > NUM_OPEN_FILES))
   {
      FDI_errno = ERR_PARAM;

      return FILE_NULL;
   }

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

   /* make sure stream is open */
   if (StreamInfoTable[stream].mode_flag == MODE_INVALID)
   {
      FDI_errno = ERR_NOTOPEN;

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

      return FILE_NULL;
   }

   /* initialize STREAM_INFO and FILE_INFO pointers to reduce array math */
   stream_info_ptr = &(StreamInfoTable[stream]);
   file_info_ptr = &(StreamInfoTable[stream].open_file_info);

   /* make sure count and element_size parameters aren't zero */
   if ((count == 0) || (element_size == 0))
   {
      stream_info_ptr->ferrno = ERR_PARAM;   /* log error for use by ferror */
      FDI_errno = ERR_PARAM;

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

      return FILE_NULL;
   }

   /* test if stream is not opened with read-only mode */
   if ((stream_info_ptr->mode_flag & WRITE_PERMITTED) == 0)
   {
      stream_info_ptr->ferrno = ERR_OPENMODE; /* log error for use by ferror */
      FDI_errno = ERR_OPENMODE;

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

      return FILE_NULL;
   }

   /* check file for write permissions */
   /* E5.1.722 START */
   if (HasPermissionToWrite(stream_info_ptr, getuid(), getgid()) == FALSE)
   /* E5.1.722 END */
   {
      stream_info_ptr->ferrno = ERR_ACCESS; /* log error for use by ferror */
      FDI_errno = ERR_ACCESS;

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

      return FILE_NULL;
   }

   /* if file opened for append, set file_pointer to end of file */
   if (stream_info_ptr->mode_flag & WRITE_END_ONLY)
   {
      stream_info_ptr->file_pointer = file_info_ptr->size;
   }

   /* test if seek is less than the current file size */
   if (stream_info_ptr->file_pointer < file_info_ptr->size)
   {
      /* test if writing beyond end of existing file size */
      if ((stream_info_ptr->file_pointer + (element_size * count)) >
          file_info_ptr->size)
      {
         stream_info_ptr->ferrno = ERR_PARAM; /* log error for use by ferror */
         FDI_errno = ERR_PARAM;

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

         return FILE_NULL;
      }

      /* setup the sub_command for replacement */
      data_sub_command = WRITE_REPLACE;

      /* set size for power loss recovery */
      file_info_ptr->plr_size = file_info_ptr->size;
   }
   /* else if equal setup the sub_command for appending */
   else if (stream_info_ptr->file_pointer == file_info_ptr->size)
   {
      data_sub_command = WRITE_APPEND;

      /* set size for power loss recovery */
      file_info_ptr->plr_size = file_info_ptr->size + (element_size * count);
   }
   /* else seeked beyond the end of the file, return an error */
   else
   {
      stream_info_ptr->ferrno = ERR_PARAM;   /* log error for use by ferror */
      FDI_errno = ERR_PARAM;

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

      return FILE_NULL;
   }

   /* set time and date for power loss recovery */
   file_info_ptr->plr_time = gettime();
   file_info_ptr->plr_date = getdate();

   /* only need to write new file info for PLR if appending */
   if (data_sub_command == WRITE_APPEND)
   {
      /* command control structure setup for FDI_Write */
      /* ------------------------------------------------------- */
      cmd_cntrl.buffer_ptr  = (BYTE_PTR)file_info_ptr;
      cmd_cntrl.count       = sizeof(FILE_INFO);
      cmd_cntrl.offset      = 0;
      /* cmd_cntrl.aux         = unused; */
      /* cmd_cntrl.actual      = unused; */
      cmd_cntrl.identifier  = stream_info_ptr->file_info_id;
      cmd_cntrl.type        = FILE_SUPPORT_INFO_TYPE;
      cmd_cntrl.priority    = FDI_MIN_PRIORITY;
      cmd_cntrl.sub_command = WRITE_REPLACE;
      /* cmd_cntrl.reserved    = unused; */
      /* ------------------------------------------------------- */

      /* call FDI_Write to write the new file info structure */
      while ((status = FDI_Write(&cmd_cntrl)) == ERR_Q_FULL)
      {
         /*E.5.0.604.START*/
         /*E.5.0.702 START */
         /* change TASK_DELAY to FILE_TASK_DELAY_TIME to avoid confusion */
         fdi_sleep(FILE_TASK_DELAY_TIME);     /* put the task to sleep... */
         /* E.5.0.702 END */
         /*E.5.0.604.END*/
      }

      /* command control structure after FDI_Write
      * -------------------------------------------------------
      * ALL fields            = unchanged
      * ------------------------------------------------------- */

      /* check if the call failed */
      if (status != ERR_NONE)
      {
         stream_info_ptr->ferrno = status;  /* log error for use by ferror */
         FDI_errno = status;

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

         return FILE_NULL;
      }

      /* the following macro is for FDI Development Testing only */
      mDEBUG_FM_PLR_RETURN_VALUE(FILE_NULL)
   }

   /* command control structure setup for FDI_Write */
   /* ------------------------------------------------------- */
   cmd_cntrl.buffer_ptr  = (BYTE_PTR)buffer_ptr;
   cmd_cntrl.count       = element_size * count;
   cmd_cntrl.offset      = stream_info_ptr->file_pointer;
   /* cmd_cntrl.aux         = unused; */
   /* cmd_cntrl.actual      = unused; */
   cmd_cntrl.identifier  = stream_info_ptr->open_file_info.data_id;
   cmd_cntrl.type        = FILE_SUPPORT_DATA_TYPE;
   cmd_cntrl.priority    = FDI_MIN_PRIORITY;
   cmd_cntrl.sub_command = data_sub_command;
   /* cmd_cntrl.reserved    = unused; */
   /* ------------------------------------------------------- */

   /* call FDI_Write to write the actual file data to flash */
   while ((status = FDI_Write(&cmd_cntrl)) == ERR_Q_FULL)
   {
         /*E.5.0.604.START*/
         /*E.5.0.702 START */
         /* change TASK_DELAY to FILE_TASK_DELAY_TIME to avoid confusion */
         fdi_sleep(FILE_TASK_DELAY_TIME);     /* put the task to sleep... */
         /* E.5.0.702 END */
         /*E.5.0.604.END*/
   }

   /* command control structure after FDI_Write
   * -------------------------------------------------------
   * ALL fields            = unchanged
   * ------------------------------------------------------- */

   /* check if the call failed */
   if (status != ERR_NONE)
   {
      stream_info_ptr->ferrno = status;      /* log error for use by ferror */
      FDI_errno = status;

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

      return FILE_NULL;
   }

   /* the following macro is for FDI Development Testing only */
   mDEBUG_FM_PLR_RETURN_VALUE(FILE_NULL)

   /* update size, date and time file was modified */
   file_info_ptr->size = file_info_ptr->plr_size;
   file_info_ptr->date = file_info_ptr->plr_date;
   file_info_ptr->time = file_info_ptr->plr_time;

   /* command control structure setup for FDI_Write */
   /* ------------------------------------------------------- */
   cmd_cntrl.buffer_ptr  = (BYTE_PTR)file_info_ptr;
   cmd_cntrl.count       = sizeof(FILE_INFO);
   cmd_cntrl.offset      = 0;
   /* cmd_cntrl.aux         = unused; */
   /* cmd_cntrl.actual      = unused; */
   cmd_cntrl.identifier  = stream_info_ptr->file_info_id;
   cmd_cntrl.type        = FILE_SUPPORT_INFO_TYPE;
   cmd_cntrl.priority    = FDI_MIN_PRIORITY;
   cmd_cntrl.sub_command = WRITE_REPLACE;
   /* cmd_cntrl.reserved    = unused; */
   /* ------------------------------------------------------- */

   /* call FDI_Write to write the file info structure to flash */
   while ((status = FDI_Write(&cmd_cntrl)) == ERR_Q_FULL)
   {
         /*E.5.0.604.START*/
         /*E.5.0.702 START */
         /* change TASK_DELAY to FILE_TASK_DELAY_TIME to avoid confusion */
         fdi_sleep(FILE_TASK_DELAY_TIME);     /* put the task to sleep... */
         /* E.5.0.702 END */
         /*E.5.0.604.END*/
   }

   /* command control structure after FDI_Write
   * -------------------------------------------------------
   * ALL fields            = unchanged
   * ------------------------------------------------------- */

   /* check if the call failed */
   if (status != ERR_NONE)
   {
      stream_info_ptr->ferrno = status;      /* log error for use by ferror */
      FDI_errno = status;

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

      return FILE_NULL;
   }

   /* the following macro is for FDI Development Testing only */
   mDEBUG_FM_PLR_RETURN_VALUE(FILE_NULL)

   /* increment the seek file pointer by the number of bytes written */
   stream_info_ptr->file_pointer += (element_size * count);

   /* if the data in search info matches the information
    * in stream info then update the search info structure */
   if (SearchInfoPtr->file_info_id == stream_info_ptr->file_info_id)
   {
      SearchInfoPtr->open_file_info = stream_info_ptr->open_file_info;
   }

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

   /* if successful return the size of the file data written */
   return count;
}                                      /* ENDOF FDI_fwrite */

/*############################################################################
 *### FDI_fsetsize
 *###
 *### DESCRIPTION:
 *###    The function FDI_fsetsize sets the file to new size, implies truncating
 *###    some data on the old file. The truncated offset is at the current
 *###    file pointer location
 *###
 *### USAGE:
 *###    int FDI_fsetsize(FILE_ID stream);
 *###
 *### PARAMETERS:
 *###
 *###  INPUTS:
 *###  stream               file stream identifier
 *###
 *### OUTPUTS:
 *###
 *### RETURNS:
 *###*/
int
FDI_fsetsize(FILE_ID stream)
{
   COMMAND_CONTROL cmd_cntrl;          /* command control structure for APIS */
   DWORD trunc_count;
   ERR_CODE        status;             /* define ERROR_CODE variable */
   STREAM_INFO     *stream_info_ptr;   /* pointer to table entry */
   FILE_INFO       *file_info_ptr;     /* pointer to open file info */

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

   /* make sure stream is valid */
   if ((stream == 0) || (stream > NUM_OPEN_FILES))
   {
      FDI_errno = ERR_PARAM;

      return FILE_NULL;
   }

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

   /* make sure stream is open */
   if (StreamInfoTable[stream].mode_flag == MODE_INVALID)
   {
      FDI_errno = ERR_NOTOPEN;

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

      return FILE_NULL;
   }

   /* initialize STREAM_INFO and FILE_INFO pointers to reduce array math */
   stream_info_ptr = &(StreamInfoTable[stream]);
   file_info_ptr = &(StreamInfoTable[stream].open_file_info);

   /* test if stream is not opened with read-only mode */
   if ((stream_info_ptr->mode_flag & WRITE_PERMITTED) == 0)
   {

⌨️ 快捷键说明

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