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

📄 fdi_file.c

📁 Flash file system
💻 C
📖 第 1 页 / 共 5 页
字号:
   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*/
         fdi_sleep(TASK_DELAY);           /* put the task to sleep... */
         /*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*/
         fdi_sleep(TASK_DELAY);           /* put the task to sleep... */
         /*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*/
         fdi_sleep(TASK_DELAY);           /* put the task to sleep... */
         /*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_fread
 *###
 *### DESCRIPTION:
 *###    The function FDI_fread reads up to count elements of the indicated
 *###    size from the input stream into the specified buffer.  The actual
 *###    number of items read is returned by FDI_fread; it may be less than
 *###    count if end-of-file is encountered.
 *###
 *### USAGE:
 *###    actual_read = FDI_fread(&new_struct, sizeof(NEW_STRUCT), 1, fp);
 *###
 *### PARAMETERS:
 *###
 *### INPUTS:
 *###  element_size      size of element referenced by buffer_ptr
 *###  count             number of buffer_ptr elements to be read
 *###  stream            file stream identifier
 *###
 *### OUTPUTS:
 *###  buffer_ptr        void pointer to a buffer of data to place data read
 *###
 *### RETURNS:
 *###  Returns the number of elements successfully read; it may be less than
 *###  count if EOF is encountered.  If an error is encountered, zero is
 *###  returned.
 *###*/
size_t
FDI_fread(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 */

   /* 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 pointer to reduce array math */
   stream_info_ptr = &(StreamInfoTable[stream]);

   /* 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 write-only mode */
   if ((stream_info_ptr->mode_flag & READ_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 read permissions */
   if (((getuid() == stream_info_ptr->open_file_info.owner_id) &&
        ((stream_info_ptr->open_file_info.permissions & S_IRUSR) !=
         S_IRUSR)) ||
       ((getuid() != stream_info_ptr->open_file_info.owner_id) &&
        (getgid() == stream_info_ptr->open_file_info.group_id) &&
        ((stream_info_ptr->open_file_info.permissions & S_IRGRP) !=
         S_IRGRP)) ||
       ((getuid() != stream_info_ptr->open_file_info.owner_id) &&
        (getgid() != stream_info_ptr->open_file_info.group_id) &&
        ((stream_info_ptr->open_file_info.permissions & S_IRWLD) !=
        S_IRWLD)))
   {
      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;
   }

   /* test if reading beyond end of existing file size */
   if ((stream_info_ptr->file_pointer + (count * element_size)) >
       stream_info_ptr->open_file_info.size)
   {
      cmd_cntrl.count = stream_info_ptr->open_file_info.size -
                        stream_info_ptr->file_pointer;
      stream_info_ptr->read_eof = EOF;      /* log error for feof */
   }
   else
   {
      cmd_cntrl.count = element_size * count;
   }

   /* prepare to read from the file data identifier */
   cmd_cntrl.buffer_ptr = (BYTE_PTR)buffer_ptr;
   cmd_cntrl.offset = stream_info_ptr->file_pointer;
   cmd_cntrl.identifier = stream_info_ptr->open_file_info.data_id;
   cmd_cntrl.type = FILE_SUPPORT_DATA_TYPE;
   cmd_cntrl.priority = FDI_MIN_PRIORITY;

   /* call FDI_Read to read the actual file data from flash */
   status = FDI_Read(&cmd_cntrl);

   if (status == ERR_NONE)
   {
      /* increment the seek file pointer by the number of bytes read */
      stream_info_ptr->file_pointer += cmd_cntrl.actual;

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

      /* successful, return the actual number of bytes read */
      return (cmd_cntrl.actual / element_size);
   }
   else
   {
      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;
   }
}                                      /* ENDOF FDI_fread */


/*############################################################################
 *### FDI_fclose
 *###

⌨️ 快捷键说明

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