📄 fdi_file.c
字号:
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->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;
}
if (stream_info_ptr->file_pointer == file_info_ptr->size)
{
stream_info_ptr->ferrno = ERR_NONE;
FDI_errno = ERR_NONE;
/* unlock the File Manager API semaphore */
SEM_MTX_POST(FileAPIMutexSemaphore);
return FILE_NULL;
}
/* set size for power loss recovery */
file_info_ptr->plr_size = stream_info_ptr->file_pointer;
/* set time and date for power loss recovery */
file_info_ptr->plr_time = gettime();
file_info_ptr->plr_date = getdate();
/* 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 = NULL;
cmd_cntrl.count = file_info_ptr->size - stream_info_ptr->file_pointer;
trunc_count = cmd_cntrl.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 = WRITE_TRUNCATE;
/* 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 */
/* E.5.2.872 Begin */
if ((status != ERR_NONE) && (status != ERR_TRUNCATE))
/* E.5.2.872 End */
{
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)
/* 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 trunc_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 */
/* E5.1.722 START */
if (HasPermissionToRead(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;
}
/* 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
*###
*### DESCRIPTION:
*### The function FDI_fclose closes an open stream in an appropriate and
*### orderly fashion, including any necessary emptying of internal data
*### buffers.
*###
*### USAGE:
*### close_status = FDI_fclose(stream);
*###
*### PARAMETERS:
*###
*### INPUTS:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -