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

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. /**   File (FIL)                                                          */
  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_fil.h"
  45. #include "PlaycoreFileSysFileXfx_uti.h"
  46. /**************************************************************************/ 
  47. /*                                                                        */ 
  48. /*  FUNCTION                                               RELEASE        */ 
  49. /*                                                                        */ 
  50. /*    _fx_file_seek                                       PORTABLE C      */ 
  51. /*                                                           3.0          */ 
  52. /*  AUTHOR                                                                */ 
  53. /*                                                                        */ 
  54. /*    William E. Lamie, Express Logic, Inc.                               */ 
  55. /*                                                                        */ 
  56. /*  DESCRIPTION                                                           */ 
  57. /*                                                                        */ 
  58. /*    This function positions the internal file pointers to the specified */ 
  59. /*    byte offset such that the next read or write operation will be      */ 
  60. /*    performed there.  If the byte offset is greater than the size, the  */ 
  61. /*    file pointers will be positioned to the end of the file.            */ 
  62. /*                                                                        */ 
  63. /*  INPUT                                                                 */ 
  64. /*                                                                        */ 
  65. /*    file_ptr                              File control block pointer    */ 
  66. /*    byte_offset                           Byte offset into the file     */ 
  67. /*                                                                        */ 
  68. /*  OUTPUT                                                                */ 
  69. /*                                                                        */ 
  70. /*    return status                                                       */ 
  71. /*                                                                        */ 
  72. /*  CALLS                                                                 */ 
  73. /*                                                                        */ 
  74. /*    _fx_utility_FAT_entry_read            Read a FAT entry              */ 
  75. /*                                                                        */ 
  76. /*  CALLED BY                                                             */ 
  77. /*                                                                        */ 
  78. /*    Application Code                                                    */ 
  79. /*                                                                        */ 
  80. /*  RELEASE HISTORY                                                       */ 
  81. /*                                                                        */ 
  82. /*    DATE              NAME                      DESCRIPTION             */ 
  83. /*                                                                        */ 
  84. /*  01-01-1999     William E. Lamie         Initial Version 1.0           */ 
  85. /*  04-19-1999     William E. Lamie         Added logic to seek to the    */ 
  86. /*                                            end of a file whose size    */ 
  87. /*                                            is evenly divisible by      */ 
  88. /*                                            the media's cluster size,   */ 
  89. /*                                            resulting in version 1.0a.  */ 
  90. /*  03-01-2000     William E. Lamie         Modified comment(s),          */ 
  91. /*                                            resulting in version 1.0b.  */ 
  92. /*  01-28-2001     William E. Lamie         Modified comment(s),          */ 
  93. /*                                            resulting in version 2.0.   */ 
  94. /*  03-01-2002     Mohammad N. Minhaz       Modified comment(s),          */ 
  95. /*                                            changed to 32 bit variables,*/ 
  96. /*                                            fixed possible infinite loop*/ 
  97. /*                                            caused by a corrupt FAT,    */ 
  98. /*                                            and added logic to determine*/ 
  99. /*                                            consecutive clusters,       */ 
  100. /*                                            resulting in version 3.0.   */ 
  101. /*                                                                        */ 
  102. /**************************************************************************/ 
  103. UINT  _fx_file_seek(FX_FILE *file_ptr, ULONG byte_offset)
  104. {
  105. UINT        status;
  106. ULONG       cluster;
  107. ULONG       contents;
  108. ULONG       bytes_per_cluster;
  109. ULONG       last_cluster;
  110. ULONG       cluster_count;
  111. ULONG       bytes_remaining;
  112. FX_MEDIA    *media_ptr;
  113. ULONG       fat_reserved;
  114.     /* First, determine if the file is still open.  */
  115.     if (file_ptr -> fx_file_id != FX_FILE_ID)
  116.     {
  117.         /* Return the file not open error status.  */
  118.         return(FX_NOT_OPEN);
  119.     }
  120.     /* Setup pointer to associated media control block.  */
  121.     media_ptr =  file_ptr -> fx_file_media_ptr;
  122.     /* Determine the type of FAT and setup variables accordingly.  */
  123.     if (media_ptr -> fx_media_32_bit_FAT) fat_reserved = FX_RESERVED_1_32;
  124.     else fat_reserved = FX_RESERVED_1;
  125.     /* Protect against other threads accessing the media.  */
  126.     FX_PROTECT
  127.     /* Calculate the number of bytes per cluster.  */
  128.     bytes_per_cluster =  ((ULONG) media_ptr -> fx_media_bytes_per_sector) *
  129.                          ((ULONG) media_ptr -> fx_media_sectors_per_cluster);
  130.     /* See if we need to adjust the byte offset.  */
  131.     if (byte_offset > file_ptr -> fx_file_current_file_size)
  132.     {
  133.         /* Adjust the byte offset down to the file size. */
  134.         byte_offset =  file_ptr -> fx_file_current_file_size;
  135.     }
  136.     /* check if the desired position within the leading consecutive clusters */    
  137.     if (byte_offset >= file_ptr -> fx_file_consecutive_cluster * bytes_per_cluster)
  138.     {
  139.         /* At this point, we are ready to walk list of clusters to setup the
  140.            seek position of this file.  */
  141.         /* check if byte_offset is greater than where we were left off earlier */
  142.         if (file_ptr -> fx_file_current_relative_cluster * bytes_per_cluster < byte_offset)
  143.         {
  144.             cluster =    file_ptr -> fx_file_current_physical_cluster;
  145.             bytes_remaining =   byte_offset - 
  146.                                     file_ptr -> fx_file_current_relative_cluster * bytes_per_cluster;
  147.             cluster_count = file_ptr -> fx_file_current_relative_cluster;
  148.             last_cluster = 0;
  149.         }
  150.         else 
  151.         {
  152.             cluster =    file_ptr -> fx_file_first_physical_cluster +
  153.                                     (file_ptr -> fx_file_consecutive_cluster - 1);
  154.             bytes_remaining =   byte_offset - 
  155.                                 (file_ptr -> fx_file_consecutive_cluster - 1)* bytes_per_cluster;
  156.             last_cluster =      0;
  157.             cluster_count =     (file_ptr -> fx_file_consecutive_cluster - 1);
  158.         }
  159.         /* Follow the link of FAT entries.  */
  160.         while ((cluster >= FX_FAT_ENTRY_START) && (cluster < fat_reserved))
  161.         {
  162.             /* Increment the number of clusters.  */
  163.             cluster_count++;
  164.             /* Read the current cluster entry from the FAT.  */
  165.             status =  _fx_utility_FAT_entry_read(media_ptr, cluster, &contents);
  166.             /* Check the return value.  */
  167.             if (status != FX_SUCCESS)
  168.             {
  169.                 /* Release media protection.  */
  170.                 FX_UNPROTECT
  171.                 /* Return the error status.  */
  172.                 return(status);
  173.             }
  174.             /* Save the last valid cluster.  */
  175.             last_cluster =  cluster;
  176.             /* Setup for the next cluster.  */
  177.             cluster =  contents;
  178.             /* Determine if this is the last written cluster.  */
  179.             if (bytes_remaining > bytes_per_cluster)
  180.             {
  181.                 /* Still more seeking, just decrement the working byte offset.  */
  182.                 bytes_remaining =  bytes_remaining - bytes_per_cluster;
  183.             }
  184.             else
  185.             {
  186.                 /* Remember this cluster number.  */
  187.                 file_ptr -> fx_file_current_physical_cluster =  last_cluster;
  188.                 /* Remember the relative cluster.  */
  189.                 file_ptr -> fx_file_current_relative_cluster =  cluster_count-1;
  190.                 /* If the remaining bytes exactly fits the cluster size, check for 
  191.                    a possible adjustment to the next cluster.  */
  192.                 if ((bytes_remaining == bytes_per_cluster) &&
  193.                     (cluster >= FX_FAT_ENTRY_START) && (cluster < fat_reserved))
  194.                 {
  195.                     /* We need to position to next allocated cluster.  */
  196.                     file_ptr -> fx_file_current_physical_cluster =  cluster;
  197.                     file_ptr -> fx_file_current_relative_cluster++;
  198.                     /* Clear the remaining bytes.  */
  199.                     bytes_remaining =  0;
  200.                 }
  201.                 /* This is the cluster that contains the seek position.  */
  202.                 break;
  203.             }
  204.         }        
  205.     }
  206.     else
  207.     {
  208.         /* we should directly access the desired cluster */
  209.         file_ptr -> fx_file_current_relative_cluster = byte_offset/bytes_per_cluster; 
  210.         file_ptr -> fx_file_current_physical_cluster = 
  211.         file_ptr -> fx_file_first_physical_cluster + file_ptr -> fx_file_current_relative_cluster; 
  212.         bytes_remaining =  byte_offset % bytes_per_cluster;  
  213.     }
  214.     /* Determine if the remaining bytes fit exactly into the cluster size.  */
  215.     if (bytes_remaining == bytes_per_cluster)
  216.     {
  217.         /* Position to the end of the cluster.  */
  218.         file_ptr -> fx_file_current_logical_sector =    ((ULONG) media_ptr -> fx_media_data_sector_start) +
  219.                                                         (((ULONG) file_ptr -> fx_file_current_physical_cluster - FX_FAT_ENTRY_START) *
  220.                                                         ((ULONG) media_ptr -> fx_media_sectors_per_cluster)) +
  221.                                                         ((bytes_remaining-1) / (ULONG) media_ptr -> fx_media_bytes_per_sector);
  222.         file_ptr -> fx_file_current_relative_sector =   (UINT) (((bytes_remaining-1) / (ULONG) media_ptr -> fx_media_bytes_per_sector));
  223.         file_ptr -> fx_file_current_file_offset =       byte_offset;
  224.         file_ptr -> fx_file_current_logical_offset =    media_ptr -> fx_media_bytes_per_sector;
  225.     }
  226.     else
  227.     {
  228.         /* Position the pointers to the new offset.  */
  229.         file_ptr -> fx_file_current_logical_sector =    ((ULONG) media_ptr -> fx_media_data_sector_start) +
  230.                                                         (((ULONG) file_ptr -> fx_file_current_physical_cluster - FX_FAT_ENTRY_START) *
  231.                                                         ((ULONG) media_ptr -> fx_media_sectors_per_cluster)) +
  232.                                                         (bytes_remaining / (ULONG) media_ptr -> fx_media_bytes_per_sector);
  233.         file_ptr -> fx_file_current_relative_sector =   (UINT) ((bytes_remaining / (ULONG) media_ptr -> fx_media_bytes_per_sector));
  234.         file_ptr -> fx_file_current_file_offset =       byte_offset;
  235.         file_ptr -> fx_file_current_logical_offset =    bytes_remaining % ((ULONG) media_ptr -> fx_media_bytes_per_sector);
  236.     }
  237.     
  238.     /* Release media protection.  */
  239.     FX_UNPROTECT
  240.     /* Seek is complete, return successful status.  */
  241.     return(FX_SUCCESS);
  242. }
  243. #endif