FX_DS.C
上传用户:super_houu
上传日期:2008-09-21
资源大小:4099k
文件大小:17k
- /**************************************************************************/
- /* */
- /* Copyright (c) 1996-2002 by Express Logic Inc. */
- /* */
- /* This software is copyrighted by and is the sole property of Express */
- /* Logic, Inc. All rights, title, ownership, or other interests */
- /* in the software remain the property of Express Logic, Inc. This */
- /* software may only be used in accordance with the corresponding */
- /* license agreement. Any unauthorized use, duplication, transmission, */
- /* distribution, or disclosure of this software is expressly forbidden. */
- /* */
- /* This Copyright notice may not be removed or modified without prior */
- /* written consent of Express Logic, Inc. */
- /* */
- /* Express Logic, Inc. reserves the right to modify this software */
- /* without notice. */
- /* */
- /* Express Logic, Inc. info@expresslogic.com */
- /* 11423 West Bernardo Court http://www.expresslogic.com */
- /* San Diego, CA 92127 */
- /* */
- /**************************************************************************/
- /**************************************************************************/
- /**************************************************************************/
- /** */
- /** FileX Component */
- /** */
- /** Directory (DIR) */
- /** */
- /**************************************************************************/
- /**************************************************************************/
- #include "Config.h" // Global Configuration - do not remove!
- #ifdef ENABLE_FILEX
- #ifdef _DEBUG
- #undef IFTRACE
- #define IFTRACE if (gTraceFileSys)
- #include "DebugDbgMain.h"
- #endif //_DEBUG
- #define FX_SOURCE_CODE
- /* Include necessary system files. */
- #include "PlaycoreFileSysFileXfx_api.h"
- #include "PlaycoreFileSysFileXfx_sys.h"
- #include "PlaycoreFileSysFileXfx_dir.h"
- #include "PlaycoreFileSysFileXfx_uti.h"
- #ifndef FX_NO_LOCAL_PATH
- extern TX_THREAD *_tx_thread_current_ptr;
- #endif
- /**************************************************************************/
- /* */
- /* FUNCTION RELEASE */
- /* */
- /* _fx_directory_search PORTABLE C */
- /* 3.0 */
- /* AUTHOR */
- /* */
- /* William E. Lamie, Express Logic, Inc. */
- /* */
- /* DESCRIPTION */
- /* */
- /* This function searches the media for the supplied name. The search */
- /* routine will find files as well as directory names. */
- /* */
- /* INPUT */
- /* */
- /* media_ptr Media control block pointer */
- /* name_ptr Name pointer (8 chars + ext) */
- /* entry_ptr Pointer to directory entry */
- /* record */
- /* last_dir_ptr Pointer to destination for */
- /* the last directory entry */
- /* last_name_ptr Pointer to the last name */
- /* token that was searched for */
- /* */
- /* OUTPUT */
- /* */
- /* return status */
- /* */
- /* CALLS */
- /* */
- /* _fx_directory_name_extract Extract directory name from */
- /* input string */
- /* _fx_directory_entry_read Read entries from root dir */
- /* _fx_utlity_FAT_entry_read Read FAT entries to calculate */
- /* the sub-directory size */
- /* */
- /* CALLED BY */
- /* */
- /* FileX System Functions */
- /* */
- /* RELEASE HISTORY */
- /* */
- /* DATE NAME DESCRIPTION */
- /* */
- /* 01-01-1999 William E. Lamie Initial Version 1.0 */
- /* 03-01-2000 William E. Lamie Modified comment(s), and */
- /* added logic to properly */
- /* handle ".." searches from */
- /* sub-directories off of the */
- /* root directory, resulting */
- /* in version 1.0b. */
- /* 01-28-2001 William E. Lamie Modified comment(s), added */
- /* local path logic, and */
- /* deleted old directory cache */
- /* invalidate code, resulting */
- /* in version 2.0. */
- /* 03-01-2002 Mohammad N. Minhaz Modified comment(s), */
- /* changed to 32 bit variables,*/
- /* changed FAT32 root directory*/
- /* logic, and added logic for */
- /* long file names, resulting */
- /* in version 3.0. */
- /* */
- /**************************************************************************/
- UINT _fx_directory_search(FX_MEDIA *media_ptr, CHAR *name_ptr, FX_DIR_ENTRY *entry_ptr,
- FX_DIR_ENTRY *last_dir_ptr, CHAR **last_name_ptr)
- {
- ULONG i;
- UINT found;
- UINT status;
- ULONG cluster, next_cluster;
- ULONG directory_size;
- CHAR *dir_name_ptr;
- CHAR *work_ptr;
- FX_DIR_ENTRY search_dir;
- FX_DIR_ENTRY *search_dir_ptr;
- CHAR name[FX_MAX_LONG_NAME_LEN];
- ULONG fat_reserved;
- /* Determine the type of FAT and setup variables accordingly. */
- if (media_ptr -> fx_media_32_bit_FAT) fat_reserved = FX_RESERVED_1_32;
- else fat_reserved = FX_RESERVED_1;
- /* Setup the last directory, if required. */
- if (last_dir_ptr)
- {
- /* Set the first character of the directory entry to NULL to
- indicate root or no directory. */
- last_dir_ptr -> fx_dir_entry_name[0] = 0;
- }
- /* Determine if the file name has a full directory path. */
- if (*name_ptr == '\')
- {
-
- /* Directory name has full path, set the search pointer to NULL. */
- search_dir_ptr = FX_NULL;
- }
- else
- {
- /* Set the initial search directory to the current working
- directory - if there is one. */
- /* First check for a local path pointer stored in the thread control block. This
- is only available in ThreadX Version 4 and above. */
- #ifndef FX_NO_LOCAL_PATH
- if (_tx_thread_current_ptr -> tx_filex_ptr)
- {
- /* Determine if the local directory is not the root directory. */
- if ( ((FX_PATH *) _tx_thread_current_ptr -> tx_filex_ptr) -> fx_path_directory.fx_dir_entry_name[0])
- {
- /* Start at the current working directory of the media. */
- search_dir = ((FX_PATH *) _tx_thread_current_ptr -> tx_filex_ptr) -> fx_path_directory;
- /* Set the internal pointer to the search directory as well. */
- search_dir_ptr = &search_dir;
- }
- else
- {
- /* We are searching in the root directory. */
- search_dir_ptr = FX_NULL;
- }
- } else
- #endif
- if (media_ptr -> fx_media_default_path.fx_path_directory.fx_dir_entry_name[0])
- {
-
- /* Start at the current working directory of the media. */
- search_dir = media_ptr -> fx_media_default_path.fx_path_directory;
-
- /* Set the internal pointer to the search directory as well. */
- search_dir_ptr = &search_dir;
- }
- else
- {
- /* The current default directory is the root so just set the
- search directory pointer to NULL. */
- search_dir_ptr = FX_NULL;
- }
- }
- /* Loop to traverse the directory paths to find the specified file. */
- do
- {
- /* Remember the last name pointer, if required. */
- if (last_name_ptr)
- {
- /* Just set the last name to initial name string. */
- *last_name_ptr = name_ptr;
- }
- /* Extract file name. */
- name_ptr = _fx_directory_name_extract(name_ptr, name);
- /* Calculate the directory size. */
- if (search_dir_ptr)
- {
- /* Calculate the directory size by counting the allocated
- clusters for it. */
- i = 0;
- cluster = search_dir_ptr -> fx_dir_entry_cluster;
- while ((cluster >= FX_FAT_ENTRY_START) &&
- (cluster < fat_reserved))
- {
- /* Increment the cluster count. */
- i++;
- /* Read the next FAT entry. */
- status = _fx_utility_FAT_entry_read(media_ptr, cluster, &next_cluster);
- /* Check the return status. */
- if (status != FX_SUCCESS)
- {
- /* Return the bad status. */
- return(status);
- }
- if ((cluster == next_cluster) || (i > media_ptr -> fx_media_total_clusters))
- {
- /* Return the bad status. */
- return(FX_FAT_READ_ERROR);
- }
- cluster = next_cluster;
- }
- /* Now we can calculate the directory size. */
- directory_size = (((ULONG) media_ptr -> fx_media_bytes_per_sector) *
- ((ULONG) media_ptr -> fx_media_sectors_per_cluster) * i)
- / (ULONG) FX_DIR_ENTRY_SIZE;
- /* Also save this in the directory entry so we don't have to
- calculate it later. */
- search_dir_ptr -> fx_dir_entry_file_size = directory_size;
- /* If required, copy the last search directory entry into the
- destination. */
- if ((last_dir_ptr) && (search_dir_ptr))
- {
- /* Copy the last search directory into the destination. */
- *last_dir_ptr = *search_dir_ptr;
- }
- }
- else
- {
- /* Directory size is the number of entries in the root directory. */
- directory_size = (ULONG) media_ptr -> fx_media_root_directory_entries;
- }
- /* Loop through entries in the directory. Yes, this is a
- linear search! */
- i = 0;
- found = FX_FALSE;
- do
- {
- /* Read an entry from the directory. */
- status = _fx_directory_entry_read(media_ptr, search_dir_ptr, &i, entry_ptr);
- i++;
- /* Check for error status. */
- if (status != FX_SUCCESS)
- return(status);
- /* Determine if this is an empty entry. */
- if (((BYTE)entry_ptr->fx_dir_entry_name[0]) == FX_DIR_ENTRY_FREE)
- continue;
- /* Determine if this is the last directory entry. */
- //JERRYC_2004AUG25 if (entry_ptr -> fx_dir_entry_name[0] == FX_DIR_ENTRY_DONE)
- if (entry_ptr -> fx_dir_entry_name[0] == FX_DIR_ENTRY_DONE && i > 1)
- break;
- /* Compare the input name and extension with the directory
- entry. */
- work_ptr = name;
- dir_name_ptr = entry_ptr -> fx_dir_entry_name;
-
- do
- {
- /* Compare name with directory name. */
- if (*work_ptr != *dir_name_ptr)
- break;
- /* Otherwise, move the name pointers and increment the
- count. */
- work_ptr++;
- dir_name_ptr++;
- } while (*dir_name_ptr);
- /* Determine if the requested name has been found. If so,
- return success to the caller. */
- if ((*dir_name_ptr == 0) && (*work_ptr == *dir_name_ptr))
- {
- /* Yes, the name was located. All pertinent directory
- information is in the directory entry field. */
- found = FX_TRUE;
- }
- } while ((i < directory_size) && (!found));
- /* Now determine if we have a match. */
- if (!found)
- {
- /* Return a "not found" status to the caller. */
- return(FX_NOT_FOUND);
- }
- /* Determine if the found entry is indeed a sub-directory. */
- if (entry_ptr -> fx_dir_entry_attributes & FX_DIRECTORY)
- {
- /* Move the directory search pointer to this entry. */
- search_dir = *entry_ptr;
- search_dir_ptr = &search_dir;
- /* Now determine if the new search directory is the root
- directory. */
- if (!search_dir_ptr -> fx_dir_entry_cluster)
- {
- /* This is a backward link to the root directory. Make
- sure this is indicated in the search directory
- information. */
- search_dir_ptr -> fx_dir_entry_name[0] = 0;
- /* Determine if we need to remember this in the last
- directory searched return area. */
- if (last_dir_ptr)
- {
- /* Yes, return this value to the caller. */
- *last_dir_ptr = *search_dir_ptr;
- }
- /* Set the search directory pointer to NULL to indicate
- we are at the root directory. */
- search_dir_ptr = FX_NULL;
- }
- }
- else
- {
- /* This is not a directory, we better return not found
- since we can't continue the search. */
- if (name_ptr)
- {
- /* Return not-found status to caller. */
- return(FX_NOT_FOUND);
- }
- }
- } while(name_ptr);
- /* Return status to the caller. */
- if (found)
- return(FX_SUCCESS);
- else
- return(FX_NOT_FOUND);
- }
- #endif