⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fat32.lst

📁 fat32文件C语言的实现13
💻 LST
📖 第 1 页 / 共 5 页
字号:
 983          *----------------------------------------------------------------------------
 984          * PURPOSE:
 985          *   Fetch the first directory info in cache
 986          *----------------------------------------------------------------------------
 987          * EXAMPLE:
 988          *----------------------------------------------------------------------------
 989          * NOTE:
 990          *----------------------------------------------------------------------------
 991          * REQUIREMENTS:
 992          *****************************************************************************/ 
 993          bit fat_seek_first (void)
 994          {
 995   1        fat_clear_dir_info(); 
 996   1        fat_dir_current_sect = fat_dir_start_sect;
 997   1        fat_current_start_entry_position = 0;
 998   1        fat_current_end_entry_position = 0;
 999   1      
1000   1        if (dir_is_root)
1001   1        { /* root diretory */
1002   2          fat_dir_list_index = 1;                         
1003   2          fat_fetch_file_info(&fat_cache.current, FETCH_NEXT);  /* fetch first root entry */
1004   2          return OK;
1005   2        }
1006   1        else
1007   1        { /* not root dir */
1008   2          fat_fetch_file_info(&fat_cache.parent, FETCH_NEXT);   /* dot entry          */
1009   2          fat_fetch_file_info(&fat_cache.parent, FETCH_NEXT);   /* dotdot entry       */
1010   2          fat_dir_list_index = 2;                               /* update entry index */
1011   2          return fat_goto_next();                               /* update first file info */
1012   2        }
1013   1      }
1014          
1015          
1016          /*F**************************************************************************
1017          * NAME: fat_goto_subdir
1018          *----------------------------------------------------------------------------
1019          * PARAMS:
1020          *   id: file extension to select
1021          *
1022          * return:
1023          *   - OK: subdir selected
1024          *   - KO: current entry not a directory
1025          *   - KO: low level error
1026          *----------------------------------------------------------------------------
1027          * PURPOSE:
1028          *   Go to the subdir if current is a directory
1029          *----------------------------------------------------------------------------
1030          * EXAMPLE:
1031          *----------------------------------------------------------------------------
1032          * NOTE:
1033          *   Also called by goto_parentdir() with current info from parent info
1034          *----------------------------------------------------------------------------
1035          * REQUIREMENTS:
1036          *****************************************************************************/ 
1037          bit fat_goto_subdir (Byte id)
1038          {                        
C51 COMPILER V6.20c  FAT32                                                                 10/19/2004 12:22:16 PAGE 18  

1039   1        /* check if current file is a directory */
1040   1        if ((fat_cache.current.attributes & ATTR_DIRECTORY) == ATTR_DIRECTORY)
1041   1        {
1042   2          /* computes the sector address (RELATIVE) */
1043   2          if (fat_cache.current.start_cluster == fat_rootclus_fat32)
1044   2          {
1045   3            return fat_get_root_directory(id);                    /* go to root dir */
1046   3          }
1047   2      
1048   2          /* go to not root dir */ 
1049   2          fat_get_clusters(&dclusters, MAX_DIR_FRAGMENT_NUMBER);  /* get directory allocation table */          
             -                               
1050   2          fat_last_dclust_index = fat_last_clust_index;           /* save last index position for chain cluster 
             -*/
1051   2          fat_dir_current_sect = (((Uint32)(dclusters[0].cluster)) * fat_cluster_size)    /* computes sector add
             -ress from allocation table */
1052   2                                 + fat_ptr_data;
1053   2      
1054   2          fat_get_dir_file_list(id);                              /* create list of entries */
1055   2          fat_dir_list_index = 1;                                 /* point on first root entry */
1056   2          fat_current_start_entry_position = 0;
1057   2          fat_current_end_entry_position = 0;    
1058   2          
1059   2          fat_fetch_file_info(&fat_cache.current, FETCH_NEXT);    /* dot entry    */   
1060   2          fat_fetch_file_info(&fat_cache.parent, FETCH_NEXT);     /* dotdot entry */
1061   2          fat_dir_list_index = 2;                                 /* update index position entry */
1062   2          if(fat_cache.parent.start_cluster == 0x00)              /* if parent dir is root */
1063   2          {
1064   3            fat_cache.parent.start_cluster = fat_rootclus_fat32;  /* then update start cluster value */
1065   3          }
1066   2          dir_is_root = FALSE;
1067   2          return fat_goto_next();
1068   2        }
1069   1        else
1070   1          return KO;                                /* current entry is not a dir */
1071   1      }
1072            
1073          
1074          /*F**************************************************************************
1075          * NAME: fat_goto_parentdir
1076          *----------------------------------------------------------------------------
1077          * PARAMS: 
1078          *   id: file extension to select
1079          *
1080          * return:
1081          *----------------------------------------------------------------------------
1082          * PURPOSE:
1083          *   Go to the parent directory
1084          *----------------------------------------------------------------------------
1085          * EXAMPLE:
1086          *----------------------------------------------------------------------------
1087          * NOTE:
1088          *----------------------------------------------------------------------------
1089          * REQUIREMENTS:
1090          *****************************************************************************/ 
1091          bit fat_goto_parentdir (Byte id)
1092          { 
1093   1      Uint32 temp_cluster;
1094   1        temp_cluster = dclusters[0].cluster + 2;        /* save cluster info */
1095   1        if (temp_cluster != fat_rootclus_fat32)
1096   1        {
1097   2          fat_cache.current = fat_cache.parent;         /* goto the parent directory */
C51 COMPILER V6.20c  FAT32                                                                 10/19/2004 12:22:16 PAGE 19  

1098   2        
1099   2          /* issue the equivalent to a cd .. DOS command */
1100   2          if (fat_goto_subdir(id))
1101   2          {
1102   3            while (temp_cluster != fat_cache.current.start_cluster)
1103   3            {
1104   4              if (fat_goto_next() == KO)
1105   4                break;
1106   4            }
1107   3            if (temp_cluster == fat_cache.current.start_cluster)
1108   3              return OK;
1109   3            else
1110   3              return KO;
1111   3        
1112   3          }
1113   2          else
1114   2          {
1115   3            return KO;
1116   3          }
1117   2        }
1118   1        else
1119   1        {
1120   2          return OK; /* Nothing to do because we are in the root directory */
1121   2        }
1122   1      }
1123          
1124          
1125          
1126          /*F**************************************************************************
1127          * NAME: fat_update_fat_sector
1128          *----------------------------------------------------------------------------
1129          * PARAMS: 
1130          *   sector_number : fat sector position
1131          *
1132          * return:
1133          *----------------------------------------------------------------------------
1134          * PURPOSE:
1135          *   update a sector of fat
1136          *----------------------------------------------------------------------------
1137          * EXAMPLE:
1138          *----------------------------------------------------------------------------
1139          * NOTE:
1140          *   This function check if there is 2 fats to be updated
1141          *----------------------------------------------------------------------------
1142          * REQUIREMENTS:
1143          *****************************************************************************/  
1144          void fat_update_fat_sector (Uint16 sector_number)
1145          {
1146   1        fat_up_down_load_sector(fat_ptr_fats + sector_number, DOWNLOAD);                   /* FAT 1 update */
1147   1        if (fat_2_is_present == TRUE)
1148   1        {
1149   2          fat_up_down_load_sector(fat_ptr_fats + sector_number + fat_fat_size, DOWNLOAD);  /* FAT 2 update */
1150   2        }      
1151   1      }
1152          
1153          
1154          /*F**************************************************************************
1155          * NAME: fat_update_entry_fat
1156          *----------------------------------------------------------------------------
1157          * PARAMS:
1158          *
1159          * return:
C51 COMPILER V6.20c  FAT32                                                                 10/19/2004 12:22:16 PAGE 20  

1160          *----------------------------------------------------------------------------
1161          * PURPOSE:
1162          *   Update root entry and FAT after a writing file session (create or re-write)
1163          *----------------------------------------------------------------------------
1164          * EXAMPLE:
1165          *----------------------------------------------------------------------------
1166          * NOTE:
1167          *----------------------------------------------------------------------------
1168          * REQUIREMENTS:
1169          *   
1170          *****************************************************************************/
1171          void fat_update_entry_fat (void)
1172          {
1173   1      Byte index;
1174   1      Uint16 chain_index;
1175   1      Uint16 sector_number;
1176   1      Byte id;
1177   1      Uint16 i;
1178   1      Uint16 j;
1179   1      Uint32 cluster;
1180   1      Uint16 temp;
1181   1      Uint32 nb_cluster;
1182   1      
1183   1      /*********************/
1184   1      /* Update directory entry */
1185   1      /*********************/
1186   1        fat_clear_dir_info();
1187   1        fat_dir_current_offs = (fat_current_end_entry_position - 1) * 32;
1188   1        fat_calc_cluster();
1189   1        fat_up_down_load_sector(fat_dir_current_sect, UPLOAD);
1190   1        j = ((fat_current_end_entry_position - 1) % 16) * 32 ;                /* Position of entry in the sector
             - */
1191   1      
1192   1        /* Update file size */
1193   1        if (fat_cache.current.size.l <= fat_file_size.l)
1194   1          fat_cache.current.size.l = fat_file_size.l;
1195   1        fat_buf_sector[j + 28] = fat_cache.current.size.b[3];
1196   1        fat_buf_sector[j + 29] = fat_cache.current.size.b[2];
1197   1        fat_buf_sector[j + 30] = fat_cache.current.size.b[1];
1198   1        fat_buf_sector[j + 31] = fat_cache.current.size.b[0];
1199   1      
1200   1        ext[0] = fat_buf_sector[j + 8];
1201   1        ext[1] = fat_buf_sector[j + 9];
1202   1        ext[2] = fat_buf_sector[j + 10];
1203   1        id = fat_check_ext();
1204   1      
1205   1        fat_up_down_load_sector(fat_dir_current_sect, DOWNLOAD);
1206   1      
1207   1      /********************/
1208   1      /* Update fat 1 & 2 */
1209   1      /********************/
1210   1        /* Calculate file size cluster */
1211   1        nb_cluster = (fat_cache.current.size.l / SECTOR_SIZE) / fat_cluster_size;
1212   1        if ((fat_cache.current.size.l % (fat_cluster_size * SECTOR_SIZE)))
1213   1        {
1214   2          nb_cluster++;
1215   2        }
1216   1        j = 1;
1217   1        index = 0;
1218   1      
1219   1      /********************/
1220   1      /* FAT32 management */
C51 COMPILER V6.20c  FAT32                                                                 10/19/2004 12:22:16 PAGE 21  

1221   1      /********************/
1222   1      
1223   1        /* init the starting cluster value */
1224   1        cluster = fclusters[0].cluster + 2;
1225   1        /* Start at first chain cluster */
1226   1        sector_number = cluster / 128;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -