Skip to content

Commit ede4eb6

Browse files
FAT16 MV works LES GO!!!
1 parent b3e52ef commit ede4eb6

File tree

3 files changed

+91
-88
lines changed

3 files changed

+91
-88
lines changed

disk.img

0 Bytes
Binary file not shown.

source/kernel/C/filesystems/fat16.c

Lines changed: 38 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

source/kernel/C/filesystems/vfs.c

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -430,40 +430,70 @@ int vfs_unlink(const char* path)
430430

431431
int vfs_mv(const char* src, const char* dst)
432432
{
433-
if (!src || !dst) {
434-
printf("mv: invalid arguments");
433+
char src_norm[256], dst_norm[256];
434+
vfs_normalize_path(src, src_norm);
435+
vfs_normalize_path(dst, dst_norm);
436+
437+
vfs_mount_res_t src_res, dst_res;
438+
if (vfs_resolve_mount(src_norm, &src_res) != 0) return -1;
439+
if (vfs_resolve_mount(dst_norm, &dst_res) != 0) return -1;
440+
441+
if (src_res.mnt != dst_res.mnt) {
442+
printf("mv: cross-device move not supported");
435443
return -1;
436444
}
437445

438-
char src_norm[256];
439-
char dst_norm[256];
440-
441-
vfs_normalize_path(src, src_norm);
442-
vfs_normalize_path(dst, dst_norm);
446+
if (src_res.mnt->type != FS_FAT16)
447+
return -1;
443448

444-
vfs_mount_res_t src_res;
445-
vfs_mount_res_t dst_res;
449+
fat16_fs_t* fs = (fat16_fs_t*)src_res.mnt->fs;
446450

447-
if (vfs_resolve_mount(src_norm, &src_res) != 0)
448-
return -2;
451+
/* ---------- SPLIT SRC ---------- */
452+
uint16_t src_parent = FAT16_ROOT_CLUSTER;
453+
char src_name[13];
449454

450-
if (vfs_resolve_mount(dst_norm, &dst_res) != 0)
451-
return -3;
455+
char src_tmp[256];
456+
strncpy(src_tmp, src_res.rel_path, sizeof(src_tmp));
457+
src_tmp[sizeof(src_tmp)-1] = 0;
452458

453-
/* Must be same mounted filesystem */
454-
if (src_res.mnt != dst_res.mnt) {
455-
printf("mv: cross-device move not supported");
456-
return -4;
459+
char* s = strrchr(src_tmp, '/');
460+
if (s) {
461+
*s = 0;
462+
strncpy(src_name, s + 1, sizeof(src_name));
463+
if (*src_tmp) {
464+
fat16_dir_entry_t e;
465+
if (fat16_find_path(fs, src_tmp, &e) != 0)
466+
return -1;
467+
src_parent = e.first_cluster;
468+
}
469+
} else {
470+
strncpy(src_name, src_tmp, sizeof(src_name));
457471
}
458472

459-
/* FAT16 */
460-
if (src_res.mnt->type == FS_FAT16) {
461-
fat16_fs_t* fs = (fat16_fs_t*)src_res.mnt->fs;
462-
return fat16_mv(fs, src_res.rel_path, dst_res.rel_path);
473+
/* ---------- SPLIT DST ---------- */
474+
uint16_t dst_parent = FAT16_ROOT_CLUSTER;
475+
char dst_name[13];
476+
477+
char dst_tmp[256];
478+
strncpy(dst_tmp, dst_res.rel_path, sizeof(dst_tmp));
479+
dst_tmp[sizeof(dst_tmp)-1] = 0;
480+
481+
char* d = strrchr(dst_tmp, '/');
482+
if (d) {
483+
*d = 0;
484+
strncpy(dst_name, d + 1, sizeof(dst_name));
485+
if (*dst_tmp) {
486+
fat16_dir_entry_t e;
487+
if (fat16_find_path(fs, dst_tmp, &e) != 0)
488+
return -1;
489+
dst_parent = e.first_cluster;
490+
}
491+
} else {
492+
strncpy(dst_name, dst_tmp, sizeof(dst_name));
463493
}
464494

465-
printf("mv: unknown filesystem");
466-
return -5;
495+
/* ---------- REAL MOVE ---------- */
496+
return fat16_mv(fs, src_parent, src_name, dst_parent, dst_name);
467497
}
468498

469499
const char* vfs_getcwd(void) {

0 commit comments

Comments
 (0)