FX_DS.C
上传用户:super_houu
上传日期:2008-09-21
资源大小:4099k
文件大小:17k
源码类别:

DVD

开发平台:

Others

  1. /**************************************************************************/ 
  2. /*                                                                        */ 
  3. /*            Copyright (c) 1996-2002 by Express Logic Inc.               */ 
  4. /*                                                                        */ 
  5. /*  This software is copyrighted by and is the sole property of Express   */ 
  6. /*  Logic, Inc.  All rights, title, ownership, or other interests         */ 
  7. /*  in the software remain the property of Express Logic, Inc.  This      */ 
  8. /*  software may only be used in accordance with the corresponding        */ 
  9. /*  license agreement.  Any unauthorized use, duplication, transmission,  */ 
  10. /*  distribution, or disclosure of this software is expressly forbidden.  */ 
  11. /*                                                                        */
  12. /*  This Copyright notice may not be removed or modified without prior    */ 
  13. /*  written consent of Express Logic, Inc.                                */ 
  14. /*                                                                        */ 
  15. /*  Express Logic, Inc. reserves the right to modify this software        */ 
  16. /*  without notice.                                                       */ 
  17. /*                                                                        */ 
  18. /*  Express Logic, Inc.                     info@expresslogic.com         */
  19. /*  11423 West Bernardo Court               http://www.expresslogic.com   */
  20. /*  San Diego, CA  92127                                                  */
  21. /*                                                                        */
  22. /**************************************************************************/
  23. /**************************************************************************/
  24. /**************************************************************************/
  25. /**                                                                       */ 
  26. /** FileX Component                                                       */ 
  27. /**                                                                       */
  28. /**   Directory (DIR)                                                     */
  29. /**                                                                       */
  30. /**************************************************************************/
  31. /**************************************************************************/
  32. #include "Config.h" // Global Configuration - do not remove!
  33. #ifdef ENABLE_FILEX
  34. #ifdef _DEBUG
  35. #undef IFTRACE
  36. #define IFTRACE if (gTraceFileSys)
  37. #include "DebugDbgMain.h"
  38. #endif //_DEBUG
  39. #define FX_SOURCE_CODE
  40. /* Include necessary system files.  */
  41. #include "PlaycoreFileSysFileXfx_api.h"
  42. #include "PlaycoreFileSysFileXfx_sys.h"
  43. #include "PlaycoreFileSysFileXfx_dir.h"
  44. #include "PlaycoreFileSysFileXfx_uti.h"
  45. #ifndef FX_NO_LOCAL_PATH
  46. extern TX_THREAD *_tx_thread_current_ptr;
  47. #endif
  48. /**************************************************************************/ 
  49. /*                                                                        */ 
  50. /*  FUNCTION                                               RELEASE        */ 
  51. /*                                                                        */ 
  52. /*    _fx_directory_search                                PORTABLE C      */ 
  53. /*                                                           3.0          */ 
  54. /*  AUTHOR                                                                */ 
  55. /*                                                                        */ 
  56. /*    William E. Lamie, Express Logic, Inc.                               */ 
  57. /*                                                                        */ 
  58. /*  DESCRIPTION                                                           */ 
  59. /*                                                                        */ 
  60. /*    This function searches the media for the supplied name.  The search */ 
  61. /*    routine will find files as well as directory names.                 */ 
  62. /*                                                                        */ 
  63. /*  INPUT                                                                 */ 
  64. /*                                                                        */ 
  65. /*    media_ptr                             Media control block pointer   */ 
  66. /*    name_ptr                              Name pointer (8 chars + ext)  */ 
  67. /*    entry_ptr                             Pointer to directory entry    */ 
  68. /*                                            record                      */ 
  69. /*    last_dir_ptr                          Pointer to destination for    */ 
  70. /*                                            the last directory entry    */ 
  71. /*    last_name_ptr                         Pointer to the last name      */ 
  72. /*                                            token that was searched for */ 
  73. /*                                                                        */ 
  74. /*  OUTPUT                                                                */ 
  75. /*                                                                        */ 
  76. /*    return status                                                       */ 
  77. /*                                                                        */ 
  78. /*  CALLS                                                                 */ 
  79. /*                                                                        */ 
  80. /*    _fx_directory_name_extract            Extract directory name from   */ 
  81. /*                                            input string                */ 
  82. /*    _fx_directory_entry_read              Read entries from root dir    */ 
  83. /*    _fx_utlity_FAT_entry_read             Read FAT entries to calculate */ 
  84. /*                                            the sub-directory size      */ 
  85. /*                                                                        */ 
  86. /*  CALLED BY                                                             */ 
  87. /*                                                                        */ 
  88. /*    FileX System Functions                                              */ 
  89. /*                                                                        */ 
  90. /*  RELEASE HISTORY                                                       */ 
  91. /*                                                                        */ 
  92. /*    DATE              NAME                      DESCRIPTION             */ 
  93. /*                                                                        */ 
  94. /*  01-01-1999     William E. Lamie         Initial Version 1.0           */ 
  95. /*  03-01-2000     William E. Lamie         Modified comment(s), and      */ 
  96. /*                                            added logic to properly     */ 
  97. /*                                            handle ".." searches from   */ 
  98. /*                                            sub-directories off of the  */ 
  99. /*                                            root directory, resulting   */ 
  100. /*                                            in version 1.0b.            */ 
  101. /*  01-28-2001     William E. Lamie         Modified comment(s), added    */ 
  102. /*                                            local path logic, and       */ 
  103. /*                                            deleted old directory cache */ 
  104. /*                                            invalidate code, resulting  */ 
  105. /*                                            in version 2.0.             */
  106. /*  03-01-2002     Mohammad N. Minhaz       Modified comment(s),          */ 
  107. /*                                            changed to 32 bit variables,*/ 
  108. /*                                            changed FAT32 root directory*/ 
  109. /*                                            logic, and added logic for  */ 
  110. /*                                            long file names, resulting  */ 
  111. /*                                            in version 3.0.             */ 
  112. /*                                                                        */ 
  113. /**************************************************************************/ 
  114. UINT  _fx_directory_search(FX_MEDIA *media_ptr, CHAR *name_ptr, FX_DIR_ENTRY *entry_ptr, 
  115.                                     FX_DIR_ENTRY *last_dir_ptr, CHAR **last_name_ptr)
  116. {
  117. ULONG           i;
  118. UINT            found;
  119. UINT            status;
  120. ULONG           cluster, next_cluster;
  121. ULONG           directory_size;
  122. CHAR            *dir_name_ptr;
  123. CHAR            *work_ptr;
  124. FX_DIR_ENTRY    search_dir;
  125. FX_DIR_ENTRY    *search_dir_ptr;
  126. CHAR            name[FX_MAX_LONG_NAME_LEN];
  127. ULONG           fat_reserved;
  128.     /* Determine the type of FAT and setup variables accordingly.  */
  129.     if (media_ptr -> fx_media_32_bit_FAT) fat_reserved = FX_RESERVED_1_32;
  130.     else fat_reserved = FX_RESERVED_1;
  131.     /* Setup the last directory, if required.  */
  132.     if (last_dir_ptr)
  133.     {
  134.         /* Set the first character of the directory entry to NULL to 
  135.            indicate root or no directory.  */
  136.         last_dir_ptr -> fx_dir_entry_name[0] =  0;
  137.     }
  138.     /* Determine if the file name has a full directory path.  */
  139.     if (*name_ptr == '\')
  140.     {
  141.         
  142.         /* Directory name has full path, set the search pointer to NULL.  */
  143.         search_dir_ptr =  FX_NULL;
  144.     }
  145.     else
  146.     {
  147.         /* Set the initial search directory to the current working 
  148.            directory - if there is one.  */
  149.         /* First check for a local path pointer stored in the thread control block.  This
  150.            is only available in ThreadX Version 4 and above.  */
  151. #ifndef FX_NO_LOCAL_PATH
  152.         if (_tx_thread_current_ptr -> tx_filex_ptr)
  153.         {
  154.             /* Determine if the local directory is not the root directory.  */
  155.             if ( ((FX_PATH *) _tx_thread_current_ptr -> tx_filex_ptr) -> fx_path_directory.fx_dir_entry_name[0])
  156.             {
  157.                 /* Start at the current working directory of the media.  */
  158.                 search_dir =   ((FX_PATH *) _tx_thread_current_ptr -> tx_filex_ptr) -> fx_path_directory;
  159.                 /* Set the internal pointer to the search directory as well.  */
  160.                 search_dir_ptr =  &search_dir;
  161.             }
  162.             else
  163.             {
  164.                 /* We are searching in the root directory.  */
  165.                 search_dir_ptr =  FX_NULL;
  166.             }
  167.         } else
  168. #endif
  169.         if (media_ptr -> fx_media_default_path.fx_path_directory.fx_dir_entry_name[0])
  170.         {
  171.         
  172.             /* Start at the current working directory of the media.  */
  173.             search_dir =  media_ptr -> fx_media_default_path.fx_path_directory;
  174.             
  175.             /* Set the internal pointer to the search directory as well.  */
  176.             search_dir_ptr =  &search_dir;
  177.         }
  178.         else
  179.         {
  180.             /* The current default directory is the root so just set the
  181.                search directory pointer to NULL.  */
  182.             search_dir_ptr =  FX_NULL;
  183.         }
  184.     }
  185.     /* Loop to traverse the directory paths to find the specified file.  */
  186.     do
  187.     {
  188.         /* Remember the last name pointer, if required.  */
  189.         if (last_name_ptr)
  190.         {
  191.             /* Just set the last name to initial name string.  */
  192.             *last_name_ptr =  name_ptr;
  193.         }
  194.         /* Extract file name.  */
  195.         name_ptr =  _fx_directory_name_extract(name_ptr, name);
  196.         /* Calculate the directory size.  */
  197.         if (search_dir_ptr)
  198.         {
  199.             /* Calculate the directory size by counting the allocated
  200.                clusters for it.  */
  201.             i =        0;
  202.             cluster =  search_dir_ptr -> fx_dir_entry_cluster;
  203.             while ((cluster >= FX_FAT_ENTRY_START) &&
  204.                     (cluster < fat_reserved))
  205.             {
  206.                 /* Increment the cluster count.  */
  207.                 i++;
  208.                 /* Read the next FAT entry.  */
  209.                 status =  _fx_utility_FAT_entry_read(media_ptr, cluster, &next_cluster);
  210.                 /* Check the return status.  */
  211.                 if (status != FX_SUCCESS)
  212.                 {
  213.                     /* Return the bad status.  */
  214.                     return(status);
  215.                 }                
  216.                 if ((cluster == next_cluster) || (i > media_ptr -> fx_media_total_clusters)) 
  217.                 {
  218.                     /* Return the bad status.  */
  219.                     return(FX_FAT_READ_ERROR);
  220.                 }
  221.                 cluster = next_cluster;
  222.             }
  223.             /* Now we can calculate the directory size.  */
  224.             directory_size =  (((ULONG) media_ptr -> fx_media_bytes_per_sector) *
  225.                                ((ULONG) media_ptr -> fx_media_sectors_per_cluster) * i)
  226.                                 / (ULONG) FX_DIR_ENTRY_SIZE;
  227.             /* Also save this in the directory entry so we don't have to 
  228.                calculate it later.  */
  229.             search_dir_ptr -> fx_dir_entry_file_size =  directory_size;
  230.             /* If required, copy the last search directory entry into the 
  231.                destination.  */
  232.             if ((last_dir_ptr) && (search_dir_ptr))
  233.             {
  234.                 /* Copy the last search directory into the destination.  */
  235.                 *last_dir_ptr =  *search_dir_ptr;
  236.             }
  237.         }
  238.         else
  239.         {
  240.             /* Directory size is the number of entries in the root directory.  */
  241.             directory_size =  (ULONG) media_ptr -> fx_media_root_directory_entries;
  242.         }
  243.         /* Loop through entries in the directory.  Yes, this is a 
  244.            linear search!  */
  245.         i =      0;
  246.         found =  FX_FALSE;
  247.         do
  248.         {
  249.             /* Read an entry from the directory.  */
  250.             status =  _fx_directory_entry_read(media_ptr, search_dir_ptr, &i, entry_ptr);
  251.             i++;
  252.             /* Check for error status.  */
  253.             if (status != FX_SUCCESS)
  254.                 return(status);
  255.             /* Determine if this is an empty entry.  */
  256.             if (((BYTE)entry_ptr->fx_dir_entry_name[0]) == FX_DIR_ENTRY_FREE)
  257.                 continue;
  258.             /* Determine if this is the last directory entry.  */
  259. //JERRYC_2004AUG25            if (entry_ptr -> fx_dir_entry_name[0] == FX_DIR_ENTRY_DONE)
  260.             if (entry_ptr -> fx_dir_entry_name[0] == FX_DIR_ENTRY_DONE && i > 1)
  261.                 break;
  262.             /* Compare the input name and extension with the directory
  263.                entry.  */
  264.             work_ptr =      name;
  265.             dir_name_ptr =  entry_ptr -> fx_dir_entry_name;
  266.            
  267.             do 
  268.             {
  269.                 /* Compare name with directory name.  */
  270.                 if (*work_ptr != *dir_name_ptr)
  271.                     break;
  272.                 /* Otherwise, move the name pointers and increment the
  273.                    count.  */
  274.                 work_ptr++;
  275.                 dir_name_ptr++;
  276.             } while (*dir_name_ptr);
  277.             /* Determine if the requested name has been found.  If so,
  278.                return success to the caller.  */
  279.             if ((*dir_name_ptr == 0) && (*work_ptr == *dir_name_ptr))
  280.             {
  281.                 /* Yes, the name was located.  All pertinent directory
  282.                    information is in the directory entry field.  */
  283.                 found =  FX_TRUE;
  284.             }
  285.         } while ((i < directory_size) && (!found));
  286.         /* Now determine if we have a match.  */
  287.         if (!found)
  288.         {
  289.             /* Return a "not found" status to the caller.  */
  290.             return(FX_NOT_FOUND);
  291.         }
  292.         /* Determine if the found entry is indeed a sub-directory.  */
  293.         if (entry_ptr -> fx_dir_entry_attributes & FX_DIRECTORY)
  294.         {
  295.             /* Move the directory search pointer to this entry.  */
  296.             search_dir =      *entry_ptr;
  297.             search_dir_ptr =  &search_dir;
  298.             /* Now determine if the new search directory is the root 
  299.                directory.  */
  300.             if (!search_dir_ptr -> fx_dir_entry_cluster)
  301.             {
  302.                 /* This is a backward link to the root directory.  Make
  303.                    sure this is indicated in the search directory 
  304.                    information.  */
  305.                 search_dir_ptr -> fx_dir_entry_name[0] =  0;
  306.                 /* Determine if we need to remember this in the last
  307.                    directory searched return area.  */
  308.                 if (last_dir_ptr)
  309.                 {
  310.                     /* Yes, return this value to the caller.  */
  311.                     *last_dir_ptr =  *search_dir_ptr;
  312.                 }
  313.                 /* Set the search directory pointer to NULL to indicate
  314.                    we are at the root directory.  */
  315.                 search_dir_ptr =  FX_NULL;
  316.             }
  317.         }
  318.         else
  319.         {
  320.             /* This is not a directory, we better return not found
  321.                since we can't continue the search.  */
  322.             if (name_ptr)
  323.             {
  324.                 /* Return not-found status to caller.  */
  325.                 return(FX_NOT_FOUND);
  326.             }
  327.         }
  328.     } while(name_ptr);
  329.     /* Return status to the caller.  */
  330.     if (found)
  331.         return(FX_SUCCESS);
  332.     else
  333.         return(FX_NOT_FOUND);
  334. }
  335. #endif