WKern
Loading...
Searching...
No Matches
rmfile.c
Go to the documentation of this file.
1/*
2WKern - A Bare Metal OS / Kernel I am maKing (For Fun)
3Copyright (C) 2025 Wdboyes13
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#include <fileio/fileio.h>
20#include <io/kio.h>
21#include <mem/kmem.h>
22#include <types/nums.h>
23
38void Fat16RemoveFile(const char *filename, const char *ext) {
39 u32 entries_per_sector = fat16.bytes_per_sector / 32;
40 u32 root_dir_sectors =
41 ((fat16.root_entry_count * 32) + fat16.bytes_per_sector - 1) /
42 fat16.bytes_per_sector;
43 u8 sector[512];
44
45 for (u32 i = 0; i < root_dir_sectors; i++) {
46 AtaReadSector(fat16.root_dir_start_lba + i, sector);
47 for (u32 j = 0; j < entries_per_sector; j++) {
48 u8 *entry = &sector[j * 32];
49
50 if (entry[0] == 0x00) {
51 return; // no more entries
52 }
53
54 if (entry[0] == 0xE5) {
55 continue; // already deleted
56 }
57
58 char name_pad[8];
59 char ext_pad[3];
60 Padname(filename, name_pad, 8);
61 Padname(ext, ext_pad, 3);
62
63 // ChecK filename and extension match
64 if (Kmemcmp(entry, name_pad, 8) == 0 &&
65 Kmemcmp(entry + 8, ext_pad, 3) == 0) {
66 // MarK entry as deleted
67 entry[0] = 0xE5;
68
69 // Write bacK directory sector
70 AtaWriteSector(fat16.root_dir_start_lba + i, sector);
71
72 // Free FAT clusters
73 u16 cluster = entry[26] | (entry[27] << 8);
74 while (cluster >= 0x0002 && cluster <= 0xFFEF) {
75 u32 fat_offset = cluster * 2;
76 u32 fat_sector = fat16.fat_start_lba + (fat_offset / 512);
77 u32 fat_index = fat_offset % 512;
78
79 u8 fatbuf[512];
80 AtaReadSector(fat_sector, fatbuf);
81
82 // Read next cluster in chain
83 u16 next_cluster =
84 fatbuf[fat_index] | (fatbuf[fat_index + 1] << 8);
85
86 // Clear current cluster in FAT
87 fatbuf[fat_index] = 0x00;
88 fatbuf[fat_index + 1] = 0x00;
89 AtaWriteSector(fat_sector, fatbuf);
90
91 cluster = next_cluster;
92 }
93 return; // Done deleting file
94 }
95 }
96 }
97}
void AtaWriteSector(u32 lba, const u8 *buffer)
Write a single 512-byte sector to the ATA device using LBA addressing.
Definition ATA.c:100
void AtaReadSector(u32 lba, u8 *buffer)
Read a single 512-byte sector from the ATA device using LBA addressing.
Definition ATA.c:65
u8 sector[SECTOR_SIZE]
Buffer for reading sectors.
Definition MBR.c:57
struct FaT16Info fat16
Holds FAT16 filesystem info and a sector buffer.
Definition fat16_mnt.c:25
Definitions and declarations for FAT16 filesystem and ATA I/O.
int Kmemcmp(const void *a, const void *b, size_t n)
Compare memory.
Definition memutil.c:59
unsigned int u32
32-Bit Unsigned Int
Definition nums.h:30
unsigned short u16
16-Bit Unsigned Int
Definition nums.h:36
unsigned char u8
8-Bit Unsigned Int
Definition nums.h:32
void Fat16RemoveFile(const char *filename, const char *ext)
Remove a file from the FAT16 filesystem.
Definition rmfile.c:38