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

📄 fdi_file.c

📁 FDI Intel开发的FLASH文件系统,功能很强大
💻 C
📖 第 1 页 / 共 5 页
字号:

      return FILE_NULL;
   }

   if (open_mode[2] == '+')
   {
      stream_info_ptr->mode_flag |= (READ_PERMITTED | WRITE_PERMITTED);
   }
   else if (open_mode[2] != FILE_EOS)      /* error unknown mode */
   {
      INIT_STREAM_INFO(stream_info_ptr);
      FDI_errno = ERR_PARAM;

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

      return FILE_NULL;
   }


   /* if the file name matches the search info file name */
   if (!FM_CompareStrings(filename_ptr,
                          SearchInfoPtr->open_file_info.file_name,
                          (FILE_NAME_SIZE + 1)))
   {
      /* copy the search info file information */
      stream_info_ptr->open_file_info = SearchInfoPtr->open_file_info;

      /* set matching files flag true */
      stream_info_ptr->file_info_id = SearchInfoPtr->file_info_id;
   }
   else /* else there is no file info in search info or there was no match */
   {
      /* look for an existing file matching STREAM_INFO field file_name */
      status = GetFileInfo(filename_ptr,
                           &(stream_info_ptr->file_info_id),
                           &(stream_info_ptr->open_file_info));

      /* return an error if the file did not exist and FILE_MUST_EXIST */
      if ((status == ERR_NOTEXISTS) &&
          (stream_info_ptr->mode_flag & FILE_MUST_EXIST))
      {
         /* clear STREAM_INFO of all the file's information */
         INIT_STREAM_INFO(stream_info_ptr);
         FDI_errno = status;

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

         return FILE_NULL;
      }
   }

   /* E5.1.722 START */
   /* if the file exists, use the permissions found on the file */
   if (status == ERR_NONE)
   {
      stream_info_ptr->permissions =
         stream_info_ptr->open_file_info.permissions;
   }
   /* otherwise, use the new, default permissions */
   else
   {
      stream_info_ptr->permissions = FILE_DEFAULT_PERMISSIONS;
   }

   user_id  = getuid();
   group_id = getgid();
   /* E5.1.722 END */

   /*
    * if the file exists and was opened with write mode, then check
    * the file permissions for write access.
    */
   if ((status == ERR_NONE) &&
       (stream_info_ptr->mode_flag & WRITE_PERMITTED) != 0)
   {
      /* check file for correct permissions */
      /* E5.1.722 START */
      if (HasPermissionToWrite(stream_info_ptr, user_id, group_id) == FALSE)
      /* E5.1.722 END */
      {
         /* clear STREAM_INFO of all the file's information */
         INIT_STREAM_INFO(stream_info_ptr);
         FDI_errno = ERR_ACCESS;

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

         return FILE_NULL;
      }
   }
   /*
    * if the file exists and was opened with read mode, then check
    * the file permissions for read access.
    */
   if ((status == ERR_NONE) &&
       (stream_info_ptr->mode_flag & READ_PERMITTED) != 0)
   {
      /* check file for correct permissions */
      /* E5.1.722 START */
      if (HasPermissionToRead(stream_info_ptr, user_id, group_id) == FALSE)
      /* E5.1.722 END */
      {
         /* clear STREAM_INFO of all the file's information */
         INIT_STREAM_INFO(stream_info_ptr);
         FDI_errno = ERR_ACCESS;

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

         return FILE_NULL;
      }
   }
   /*
    * if the file exists and was opened with a 'w' mode, then the current
    * file contents is lost and the file is deleted.
    */
   if ((status == ERR_NONE) &&
       (stream_info_ptr->mode_flag & FILE_CONTENT_LOST))
   {
      /* E5.1.722 START */
      /* save permissions for the newly created file */
      /* permissions = stream_info_ptr->open_file_info.permissions; */
      /* E5.1.722 END */

      status = FileDelete(stream_info_ptr->file_info_id,
                          stream_info_ptr->open_file_info.data_id, TRUE);
      if (status != ERR_NONE)
      {
         /* clear STREAM_INFO of all the file's information */
         INIT_STREAM_INFO(stream_info_ptr);
         FDI_errno = status;

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

         return FILE_NULL;
      }
      /* wait until the queue is empty */
      do
      {
          /*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.START*/
         FDI_Status(&fdi_status);         /* get status of queue */
      } while (fdi_status & FDI_Q_PEND);

      /* Check whether there is a failed reclaim */
      if (status == ERR_NONE)
      {
         mDEBUG_FM_PLR_CHECK_RECL_ERROR()
      }

      /*
       * set status for creating a new file info structure parameter and
       * stream
       */
      status = ERR_NOTEXISTS;
   }
   /*
    * if there are no matching files and mode_flag is not FILE_MUST_EXIST
    * then create the file in flash
    */
   if (status == ERR_NOTEXISTS)
   {
      /* try using FLT index from previous match
       * makes sure we use the old FLT index if truncating in wb mode */
      if (FLT_GetNumEntries() >= NUM_FILES)
      {
         /* clear STREAM_INFO of all the file's information */
         INIT_STREAM_INFO(stream_info_ptr);
         FDI_errno = ERR_MAX_EXISTS;

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

         return FILE_NULL;
      }
      /* setup command control structure for call to FDI_Open */
      cmd_cntrl.sub_command = OPEN_CREATE;
      cmd_cntrl.aux = EnableDataStreaming;
      cmd_cntrl.identifier = NEW_DATA_STREAM_ID;
      cmd_cntrl.type = FILE_SUPPORT_DATA_TYPE;
      cmd_cntrl.priority = FDI_MIN_PRIORITY;

      /* call FDI_Open for the file support data's identifier */
      status = FDI_Open(&cmd_cntrl);
      if (status != ERR_NONE)
      {
         /* clear STREAM_INFO of all the file's information */
         INIT_STREAM_INFO(stream_info_ptr);
         FDI_errno = status;

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

         return FILE_NULL;
      }

      /*
       * assign data_id to file support data's identifier from the
       * returned identifier value in cmd_cntrl after the FDI_Open call
       */
      stream_info_ptr->open_file_info.data_id = cmd_cntrl.identifier;

      /*
       * create a new file info structure
       * copy the filename to stream info's file info field
       */
      FM_CopyString(stream_info_ptr->open_file_info.file_name,
                    filename_ptr, (FILE_NAME_SIZE + 1));

      /* assign time returned from the system call to gettime() */
      stream_info_ptr->open_file_info.time = gettime();

      /* assign date returned from the system call to getdate() */
      stream_info_ptr->open_file_info.date = getdate();

      /* assign size to zero (0) */
      stream_info_ptr->open_file_info.size = 0;

      /* assign plr fields to zero (0) */
      stream_info_ptr->open_file_info.plr_size = 0;
      stream_info_ptr->open_file_info.plr_time = 0;
      stream_info_ptr->open_file_info.plr_date = 0;

      /* initialize plr id to invalid */
      stream_info_ptr->open_file_info.plr_id = FILE_INVALID;

      /* initialize file permission access */
      /* E5.1.722 START */
      stream_info_ptr->open_file_info.permissions =
         stream_info_ptr->permissions;
      /* E5.1.722 END */

      /* initialize to full permission access */
      stream_info_ptr->open_file_info.owner_id = getuid();

      /* initialize to full permission access */
      stream_info_ptr->open_file_info.group_id = getgid();

      /*
       * setup command control structure to create the file info
       * structure
       */
      cmd_cntrl.buffer_ptr = (BYTE_PTR)&(stream_info_ptr->open_file_info);
      cmd_cntrl.count = sizeof(FILE_INFO);
      cmd_cntrl.offset = 0;
      cmd_cntrl.sub_command = WRITE_APPEND;
      cmd_cntrl.identifier = NEW_DATA_STREAM_ID;
      cmd_cntrl.type = FILE_SUPPORT_INFO_TYPE;
      cmd_cntrl.priority = FDI_MIN_PRIORITY;

      /* call FDI_Write for the file support info's identifier */
      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*/
      }

      /* for non-q_full error */
      if (status != ERR_NONE)
      {
         /* clear STREAM_INFO of all the file's information */
         INIT_STREAM_INFO(stream_info_ptr);
         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)

      /*
       * save file info structure's identifier into stream info field
       * file_info_id from the return identifier value in cmd_cntrl after
       * the FDI_Write call
       */
      stream_info_ptr->file_info_id = cmd_cntrl.identifier;

      /* initialize the file_pointer to the beginning of the file */
      stream_info_ptr->file_pointer = 0;

      /* initialize an available entry in the FLT for this file */
      if (FLT_AddEntry(
             stream_info_ptr->open_file_info.file_name,
             stream_info_ptr->file_info_id
             ) != FLT_ErrorNone)
      {
         FDI_errno = ERR_MAX_EXISTS;
         return FILE_NULL;
      }
   }
   else /* else there is an existing file and a match occurred */
   {
      /* setup to open the existing file */
      cmd_cntrl.identifier = stream_info_ptr->open_file_info.data_id;

      /* set FDI_Open sub_command according to the mode_flag */
      if (stream_info_ptr->mode_flag & WRITE_PERMITTED)
      {
         cmd_cntrl.sub_command = OPEN_MODIFY;
      }
      else
      {
         cmd_cntrl.sub_command = OPEN_READ;
      }
      cmd_cntrl.aux = EnableDataStreaming;
      cmd_cntrl.type = FILE_SUPPORT_DATA_TYPE;
      cmd_cntrl.priority = FDI_MIN_PRIORITY;

      /* call FDI_Open for the existing file data */
      status = FDI_Open(&cmd_cntrl);

      if (status != ERR_NONE)
      {
         /* clear STREAM_INFO of all the file's information */
         INIT_STREAM_INFO(stream_info_ptr);
         FDI_errno = status;

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

         return FILE_NULL;
      }

      /* check if open for write permitted only at the end of the stream */
      if (stream_info_ptr->mode_flag & WRITE_END_ONLY)
      {
         cmd_cntrl.offset = stream_info_ptr->open_file_info.size;
         cmd_cntrl.count = 0;

         /* call FDI_Read to setup the internal structures at the offset */
         status = FDI_Read(&cmd_cntrl);

         /* initialize the file_pointer to the end of the file */
         stream_info_ptr->file_pointer = cmd_cntrl.offset;
      }
      else
      {
         /* initialize the file_pointer to the beginning of the file */
         stream_info_ptr->file_pointer = 0;
      }
   }

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

   /* if successful return identifier */
   return return_index;
}                                      /* ENDOF FDI_fopen */


/*############################################################################
 *### FDI_fwrite
 *###
 *### DESCRIPTION:
 *###    The function FDI_fwrite writes count elements of size element_size
 *###    from the specified array.  The actual number of items written is
 *###    returned by FDI_fwrite; it will be the same as count unless an error
 *###    occurs.
 *###
 *### USAGE:
 *###    actual_written = FDI_fwrite(&new_struct, sizeof(NEW_STRUCT), 1, fp);
 *###

⌨️ 快捷键说明

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