@@ -1428,102 +1428,75 @@ int fat16_rmdir(fat16_fs_t* fs, uint16_t dir_cluster)
14281428 return FAT_OK ;
14291429}
14301430
1431- int fat16_mv (fat16_fs_t * fs , const char * src , const char * dst )
1432- {
1433- if (!fs || !src || !dst )
1434- return FAT_ERR_NOT_FOUND ;
1435-
1436- uint16_t src_parent , dst_parent ;
1437- char src_name [13 ], dst_name [13 ];
1438-
1439- /* ---------- resolve parents ---------- */
1440- if (fat16_find_parent (fs , src , & src_parent , src_name ) != FAT_OK )
1441- return FAT_ERR_NOT_FOUND ;
1442-
1443- if (fat16_find_parent (fs , dst , & dst_parent , dst_name ) != FAT_OK )
1444- return FAT_ERR_NOT_FOUND ;
1445-
1446- /* ---------- find source entry ---------- */
1431+ int fat16_mv (
1432+ fat16_fs_t * fs ,
1433+ uint16_t src_parent ,
1434+ const char * src_name ,
1435+ uint16_t dst_parent ,
1436+ const char * dst_name
1437+ ) {
14471438 fat16_dir_entry_t src_entry ;
1448- if (fat16_find_in_dir (fs , src_parent , src_name , & src_entry ) != FAT_OK ) {
1449- printf ("mv: cannot stat '%s'\n" , src );
1450- return FAT_ERR_NOT_FOUND ;
1451- }
14521439
1453- /* ---------- block "." and ".." ---------- */
1454- if (fat16_is_reserved_name ( src_name ) || fat16_is_reserved_name ( dst_name ) )
1440+ // 1. Find source
1441+ if (fat16_find_in_dir ( fs , src_parent , src_name , & src_entry ) != FAT_OK )
14551442 return FAT_ERR_NOT_FOUND ;
14561443
1457- /* ---------- destination must not exist ---------- */
1444+ // 2. Ensure destination does NOT exist
14581445 fat16_dir_entry_t tmp ;
1459- if (fat16_find_in_dir (fs , dst_parent , dst_name , & tmp ) == FAT_OK ) {
1460- printf ("mv: destination '%s' already exists\n" , dst );
1461- return FAT_ERR_NOT_FOUND ;
1462- }
1446+ if (fat16_find_in_dir (fs , dst_parent , dst_name , & tmp ) == FAT_OK )
1447+ return FAT_ERR_EXISTS ;
14631448
1464- /* ---------- prepare new entry ---------- */
1449+ // 3. Prepare new entry
14651450 fat16_dir_entry_t new_entry = src_entry ;
14661451 fat16_format_name (dst_name , new_entry .name );
14671452
1468- /* ---------- insert into destination ---------- */
1469- uint8_t buf [512 ];
1453+ // 4. Insert into destination directory
14701454 uint32_t lba , idx ;
1471-
14721455 if (dst_parent == 0 ) {
1473- /* ROOT DIRECTORY */
1456+ uint8_t buf [ 512 ];
14741457 for (uint32_t i = 0 ; i < fs -> root_dir_sectors ; i ++ ) {
14751458 ahci_read_sector (fs -> portno , fs -> root_dir_start + i , buf , 1 );
1476- fat16_dir_entry_t * e = (fat16_dir_entry_t * )buf ;
1477-
1459+ fat16_dir_entry_t * d = (fat16_dir_entry_t * )buf ;
14781460 for (int j = 0 ; j < DIR_ENTRIES_PER_SECTOR ; j ++ ) {
1479- if (e [j ].name [0 ] == 0x00 || e [j ].name [0 ] == 0xE5 ) {
1480- e [j ] = new_entry ;
1481- ahci_write_sector (
1482- fs -> portno ,
1483- fs -> root_dir_start + i ,
1484- buf ,
1485- 1
1486- );
1487- goto delete_old ;
1461+ if (d [j ].name [0 ] == 0x00 || d [j ].name [0 ] == 0xE5 ) {
1462+ d [j ] = new_entry ;
1463+ ahci_write_sector (fs -> portno , fs -> root_dir_start + i , buf , 1 );
1464+ goto inserted ;
14881465 }
14891466 }
14901467 }
14911468 return FAT_ERR_NOT_FOUND ;
1492- }
1493-
1494- /* SUBDIRECTORY */
1495- if (fat16_find_free_dir_entry (fs , dst_parent , & lba , & idx ) != FAT_OK )
1496- return FAT_ERR_NOT_FOUND ;
1469+ } else {
1470+ if (fat16_find_free_dir_entry (fs , dst_parent , & lba , & idx ) != FAT_OK )
1471+ return FAT_ERR_NOT_FOUND ;
14971472
1498- ahci_read_sector (fs -> portno , lba , buf , 1 );
1499- ((fat16_dir_entry_t * )buf )[idx ] = new_entry ;
1500- ahci_write_sector (fs -> portno , lba , buf , 1 );
1473+ uint8_t buf [512 ];
1474+ ahci_read_sector (fs -> portno , lba , buf , 1 );
1475+ ((fat16_dir_entry_t * )buf )[idx ] = new_entry ;
1476+ ahci_write_sector (fs -> portno , lba , buf , 1 );
1477+ }
15011478
1502- delete_old :
1503- /* ---------- delete old entry ---------- */
1479+ inserted :
1480+ // 5. Delete source entry (DO NOT FREE CLUSTERS)
15041481 char fatname [11 ];
15051482 fat16_format_name (src_name , fatname );
15061483
15071484 if (src_parent == 0 ) {
1485+ uint8_t buf [512 ];
15081486 for (uint32_t i = 0 ; i < fs -> root_dir_sectors ; i ++ ) {
15091487 ahci_read_sector (fs -> portno , fs -> root_dir_start + i , buf , 1 );
1510- fat16_dir_entry_t * e = (fat16_dir_entry_t * )buf ;
1511-
1488+ fat16_dir_entry_t * d = (fat16_dir_entry_t * )buf ;
15121489 for (int j = 0 ; j < DIR_ENTRIES_PER_SECTOR ; j ++ ) {
1513- if (memcmp (e [j ].name , fatname , 11 ) == 0 ) {
1514- e [j ].name [0 ] = 0xE5 ;
1515- ahci_write_sector (
1516- fs -> portno ,
1517- fs -> root_dir_start + i ,
1518- buf ,
1519- 1
1520- );
1490+ if (memcmp (d [j ].name , fatname , 11 ) == 0 ) {
1491+ d [j ].name [0 ] = 0xE5 ;
1492+ ahci_write_sector (fs -> portno , fs -> root_dir_start + i , buf , 1 );
15211493 return FAT_OK ;
15221494 }
15231495 }
15241496 }
1525- return FAT_ERR_NOT_FOUND ;
1497+ } else {
1498+ return fat16_delete_entry_in_cluster (fs , src_parent , fatname );
15261499 }
15271500
1528- return fat16_delete_entry_in_cluster ( fs , src_parent , fatname ) ;
1501+ return FAT_OK ;
15291502}
0 commit comments