📄 fdi_file.c
字号:
return FILE_NULL;
}
/* wait until the queue is empty */
do
{
/*E.5.0.604.START*/
fdi_sleep(TASK_DELAY); /* put the task to sleep... */
/*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 ((FLTIndex = GetNextFreeFLTIndex(FLTIndex)) >= NUM_FILES)
{
/* reset FLTIndex */
FLTIndex = 0;
/* 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
*/
if (strncpy(stream_info_ptr->open_file_info.file_name,
filename_ptr, (FILE_NAME_SIZE + 1)) == NULL)
{
/* 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 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 */
stream_info_ptr->open_file_info.permissions = permissions;
/* 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*/
fdi_sleep(TASK_DELAY); /* put the task to sleep... */
/*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 (strncpy(FileLookupTable[FLTIndex].file_name,
stream_info_ptr->open_file_info.file_name,
(FILE_NAME_SIZE + 1)) == NULL)
{
/* 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;
}
FileLookupTable[FLTIndex].file_info_id =
stream_info_ptr->file_info_id;
}
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);
*###
*### 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 */
if (((getuid() == file_info_ptr->owner_id) &&
((file_info_ptr->permissions & S_IWUSR) != S_IWUSR)) ||
((getuid() != file_info_ptr->owner_id) &&
(getgid() == file_info_ptr->group_id) &&
((file_info_ptr->permissions & S_IWGRP) != S_IWGRP)) ||
((getuid() != file_info_ptr->owner_id) &&
(getgid() != file_info_ptr->group_id) &&
((file_info_ptr->permissions & S_IWWLD) != S_IWWLD)))
{
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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -