@@ -222,20 +222,21 @@ int fat16_find_in_dir(
222222{
223223 uint8_t buf [512 ];
224224
225- debug_printf ("[fat16] find_in_dir cluster=%u\n" , dir_cluster );
226-
227- /* ROOT DIR */
228- if (dir_cluster == FAT16_ROOT_CLUSTER ) {
229- uint32_t lba = fs -> root_dir_start ;
230- uint32_t sectors = fs -> root_dir_sectors ;
231-
232- for (uint32_t s = 0 ; s < sectors ; s ++ ) {
233- ahci_read_sector (fs -> portno , lba + s , buf , 1 );
225+ /* ---------- ROOT DIR ---------- */
226+ if (dir_cluster == 0 ) {
227+ for (uint32_t s = 0 ; s < fs -> root_dir_sectors ; s ++ ) {
228+ ahci_read_sector (fs -> portno , fs -> root_dir_start + s , buf , 1 );
234229 fat16_dir_entry_t * e = (fat16_dir_entry_t * )buf ;
235230
236231 for (int i = 0 ; i < DIR_ENTRIES_PER_SECTOR ; i ++ ) {
237- if (e [i ].name [0 ] == 0x00 ) return FAT_ERR_NOT_FOUND ;
238- if (e [i ].name [0 ] == 0xE5 ) continue ;
232+ if (e [i ].name [0 ] == 0x00 )
233+ return FAT_ERR_NOT_FOUND ;
234+ if (e [i ].name [0 ] == 0xE5 )
235+ continue ;
236+ if ((e [i ].attr & 0x0F ) == 0x0F )
237+ continue ;
238+ if (e [i ].attr & 0x08 )
239+ continue ;
239240
240241 if (fat16_name_eq (e [i ].name , name )) {
241242 * out = e [i ];
@@ -246,7 +247,7 @@ int fat16_find_in_dir(
246247 return FAT_ERR_NOT_FOUND ;
247248 }
248249
249- /* NORMAL DIRECTORY */
250+ /* ---------- SUBDIRECTORY ---------- */
250251 uint16_t cluster = dir_cluster ;
251252
252253 while (cluster >= 2 && cluster < FAT16_EOC ) {
@@ -257,8 +258,14 @@ int fat16_find_in_dir(
257258 fat16_dir_entry_t * e = (fat16_dir_entry_t * )buf ;
258259
259260 for (int i = 0 ; i < DIR_ENTRIES_PER_SECTOR ; i ++ ) {
260- if (e [i ].name [0 ] == 0x00 ) return FAT_ERR_NOT_FOUND ;
261- if (e [i ].name [0 ] == 0xE5 ) continue ;
261+ if (e [i ].name [0 ] == 0x00 )
262+ return FAT_ERR_NOT_FOUND ;
263+ if (e [i ].name [0 ] == 0xE5 )
264+ continue ;
265+ if ((e [i ].attr & 0x0F ) == 0x0F )
266+ continue ;
267+ if (e [i ].attr & 0x08 )
268+ continue ;
262269
263270 if (fat16_name_eq (e [i ].name , name )) {
264271 * out = e [i ];
@@ -267,14 +274,13 @@ int fat16_find_in_dir(
267274 }
268275 }
269276
270- uint16_t next = fat16_read_fat_fs (fs , cluster );
271- if (next == cluster ) break ;
272- cluster = next ;
277+ cluster = fat16_read_fat_fs (fs , cluster );
273278 }
274279
275280 return FAT_ERR_NOT_FOUND ;
276281}
277282
283+
278284void fat16_list_dir_cluster (fat16_fs_t * fs , uint16_t start_cluster ) {
279285 uint8_t buf [512 ];
280286 uint16_t cluster = start_cluster ;
@@ -943,10 +949,11 @@ int fat16_mkdir(fat16_fs_t* fs, uint16_t parent_cluster, const char* name) {
943949 return FAT_OK ;
944950}
945951
946- static int fat16_dir_is_empty (fat16_fs_t * fs , uint16_t cluster ) {
952+ static int fat16_dir_is_empty (fat16_fs_t * fs , uint16_t cluster )
953+ {
947954 uint8_t buf [512 ];
948955
949- while (cluster < FAT16_EOC ) {
956+ while (cluster >= 2 && cluster < FAT16_EOC ) {
950957 uint32_t lba = fat16_cluster_lba (fs , cluster );
951958
952959 for (uint32_t s = 0 ; s < fs -> bs .sectors_per_cluster ; s ++ ) {
@@ -955,22 +962,23 @@ static int fat16_dir_is_empty(fat16_fs_t* fs, uint16_t cluster) {
955962
956963 for (int i = 0 ; i < DIR_ENTRIES_PER_SECTOR ; i ++ ) {
957964 if (e [i ].name [0 ] == 0x00 )
958- return FAT_ERR_NOT_EMPTY ;
965+ return FAT_OK ; // end → empty
959966
960967 if (e [i ].name [0 ] == 0xE5 )
961968 continue ;
962969
963- if (e [i ].name [0 ] == '.' && (e [i ].name [1 ] == ' ' || e [i ].name [1 ] == '.' ))
970+ if (fat16_name_eq (e [i ].name , "." ) ||
971+ fat16_name_eq (e [i ].name , ".." ))
964972 continue ;
965-
966- return FAT_OK ; // found real entry
973+
974+ return FAT_ERR_NOT_EMPTY ;
967975 }
968976 }
969977
970978 cluster = fat16_read_fat_fs (fs , cluster );
971979 }
972980
973- return FAT_ERR_NOT_EMPTY ;
981+ return FAT_OK ;
974982}
975983
976984int fat16_create_path (fat16_fs_t * fs ,
@@ -1161,6 +1169,9 @@ int fat16_ls(fat16_fs_t* fs, const char* path) {
11611169 for (int i = 0 ; i < DIR_ENTRIES_PER_SECTOR ; i ++ ) {
11621170 if (e [i ].name [0 ] == 0x00 ) return FAT_OK ;
11631171 if (e [i ].name [0 ] == 0xE5 ) continue ;
1172+ if ((e [i ].attr & 0x0F ) == 0x0F ) continue ;
1173+ if (e [i ].attr & 0x08 ) continue ;
1174+
11641175
11651176 char name [13 ];
11661177 fat16_unformat_name (& e [i ], name );
@@ -1190,6 +1201,9 @@ int fat16_ls(fat16_fs_t* fs, const char* path) {
11901201 for (int i = 0 ; i < DIR_ENTRIES_PER_SECTOR ; i ++ ) {
11911202 if (e [i ].name [0 ] == 0x00 ) return FAT_OK ;
11921203 if (e [i ].name [0 ] == 0xE5 ) continue ;
1204+ if ((e [i ].attr & 0x0F ) == 0x0F ) continue ;
1205+ if (e [i ].attr & 0x08 ) continue ;
1206+
11931207
11941208 char name [13 ];
11951209 fat16_unformat_name (& e [i ], name );
@@ -1217,63 +1231,56 @@ int fat16_cd(fat16_fs_t* fs, const char* path, uint16_t* pwd_cluster) {
12171231 return FAT_OK ;
12181232}
12191233
1220-
12211234int fat16_resolve_path (
12221235 fat16_fs_t * fs ,
12231236 const char * path ,
1224- uint16_t pwd_cluster , // current working directory cluster
1225- uint16_t * out_cluster // result cluster
1237+ uint16_t pwd_cluster ,
1238+ uint16_t * out_cluster
12261239) {
12271240 char part [13 ];
1228- uint16_t current_cluster ;
1229-
1241+ uint16_t current = (path [0 ] == '/' ) ? 0 : pwd_cluster ;
12301242 const char * p = path ;
1231- while (* p == '/' ) p ++ ;
12321243
1233- // absolute path starts from root
1234- if (path [0 ] == '/' ) {
1235- current_cluster = 0 ;
1236- } else {
1237- // relative path starts from pwd
1238- current_cluster = pwd_cluster ;
1239- }
1244+ while (* p == '/' ) p ++ ;
12401245
12411246 while (* p ) {
12421247 int len = 0 ;
12431248 while (p [len ] && p [len ] != '/' ) len ++ ;
12441249
1245- if (len >= 13 ) len = 12 ;
12461250 memcpy (part , p , len );
12471251 part [len ] = 0 ;
12481252
1249- // handle special cases
12501253 if (strcmp (part , "." ) == 0 ) {
1251- // stay in current cluster
1252- } else if ( strcmp ( part , ".." ) == 0 ) {
1253- fat16_dir_entry_t entry ;
1254- if (fat16_find_in_dir ( fs , current_cluster , ".." , & entry ) ! = 0 ) {
1255- current_cluster = 0 ; // fallback to root if parent not found
1254+ /* no-op */
1255+ }
1256+ else if ( strcmp ( part , ".." ) == 0 ) {
1257+ if (current = = 0 ) {
1258+ /* stay at root */
12561259 } else {
1257- current_cluster = entry .first_cluster ;
1260+ fat16_dir_entry_t e ;
1261+ if (fat16_find_in_dir (fs , current , ".." , & e ) == 0 )
1262+ current = e .first_cluster ;
1263+ else
1264+ current = 0 ;
12581265 }
1259- } else {
1260- fat16_dir_entry_t entry ;
1261- if (fat16_find_in_dir (fs , current_cluster , part , & entry ) != 0 )
1262- return FAT_ERR_NOT_FOUND ; // path component not found
1263- if (!(entry .attr & 0x10 ))
1264- return FAT_ERR_NOT_FOUND ; // must be directory
1265- current_cluster = entry .first_cluster ;
1266+ }
1267+ else {
1268+ fat16_dir_entry_t e ;
1269+ if (fat16_find_in_dir (fs , current , part , & e ) != 0 )
1270+ return FAT_ERR_NOT_FOUND ;
1271+ if (!(e .attr & 0x10 ))
1272+ return FAT_ERR_NOT_FOUND ;
1273+ current = e .first_cluster ;
12661274 }
12671275
12681276 p += len ;
12691277 while (* p == '/' ) p ++ ;
12701278 }
12711279
1272- * out_cluster = current_cluster ;
1280+ * out_cluster = current ;
12731281 return FAT_OK ;
12741282}
12751283
1276-
12771284int fat16_ls_cwd (fat16_fs_t * fs , const char * path ) {
12781285 if (!path || path [0 ] == '\0' ) {
12791286 path = fs -> cwd_path ;
0 commit comments