fix: FAT32 >1024 byte read/write truncation, VFS write notifications & dir watch
continuous-integration/drone/pr Build is failing
continuous-integration/drone/push Build is passing

- FAT32 writeFile: replace hardcoded spc=4 with bootRecord^.spc for
  cluster allocation and per-sector write loop
- FAT32 writeFile: update directory entry byteSize on disk after write
- FAT32 readFile: use dir^.byteSize instead of cluster-based estimate,
  fix off-by-one in read loop, remove spurious +sectorSize allocation
- VFS: fire VFS_OnMutation on sync/async writes (cache invalidate + watch)
- VFS: invalidate all cache entries for volume on mutation (relPath mismatch)
- VFS: add vfsParentDir helper, store VFSDir in file descriptors
- FDTable: add VFSDir field, free on close
- File browser: enable directory watch & poll timer, two-step new file
  creation (open + write), remove io.syslog import
- Linker: add --no-warn-execstack to suppress ld exit code 1 on warning
- Add core.strings.helpers unit (getFileExtension, fmtFileSize, sort)
This commit is contained in:
2026-03-11 07:39:00 +00:00
parent b20b88abfd
commit 1633a1e76c
6 changed files with 403 additions and 40 deletions
+30 -12
View File
@@ -30,7 +30,6 @@ uses
core.strings,
core.strings.helpers,
core.gfx.fileicons,
io.syslog,
debug.tracer,
core.util,
arch.x86.util;
@@ -228,7 +227,8 @@ procedure fb_rename_cb(e: Plv_event); cdecl; forward;
procedure fb_rename_ok_cb(e: Plv_event); cdecl; forward;
procedure fb_newfile_cb(e: Plv_event); cdecl; forward;
procedure fb_newfile_ok_cb(e: Plv_event); cdecl; forward;
procedure fb_newfile_done(error: TError; userdata: pointer); forward;
procedure fb_newfile_opened(error: TError; userdata: pointer); forward;
procedure fb_newfile_written(error: TError; userdata: pointer); forward;
procedure fb_newfile_done_timer(tmr: Plv_timer); cdecl; forward;
{ Phase 7 — context menu }
procedure fb_row_long_press_cb(e: Plv_event); cdecl; forward;
@@ -557,10 +557,10 @@ end;
procedure navigate_to(state: PFileBrowserState; newDir: pchar);
begin
{ Unwatch old directory }
{if state^.watch_id <> 0 then begin
if state^.watch_id <> 0 then begin
driver.storage.vfs.UnwatchDirectory(state^.watch_id);
state^.watch_id := 0;
end;}
end;
push_history(state, newDir);
if state^.cur_path <> nil then kfree(void(state^.cur_path));
state^.cur_path := stringCopy(newDir);
@@ -568,8 +568,8 @@ begin
build_breadcrumbs(state);
if state^.tab_bar <> nil then rebuild_tab_bar(state);
{ Watch new directory }
{state^.watch_dirty := false;
state^.watch_id := driver.storage.vfs.WatchDirectory(newDir, @fb_watch_cb, state);}
state^.watch_dirty := false;
state^.watch_id := driver.storage.vfs.WatchDirectory(newDir, @fb_watch_cb, state);
schedule_refresh(state);
end;
@@ -1843,20 +1843,38 @@ begin
nfctx^.err := eNone;
nfctx^.handle := 0;
driver.storage.vfs.OpenFileAsync(fp, omCreate, nfctx^.handle,
@nfctx^.err, @fb_newfile_done, nfctx);
@nfctx^.err, @fb_newfile_opened, nfctx);
kfree(void(fp));
end;
{ Async completion for file creation }
procedure fb_newfile_done(error: TError; userdata: pointer);
{ Async step 1: file descriptor opened — now write 0 bytes to create dir entry }
procedure fb_newfile_opened(error: TError; userdata: pointer);
var
nfctx : PNewFileCtx;
begin
nfctx := PNewFileCtx(userdata);
if nfctx = nil then exit;
nfctx^.err := error;
{ Close the handle if it was opened successfully }
if (error = eNone) and (nfctx^.handle <> 0) then
if (error = eNone) and (nfctx^.handle <> 0) then begin
{ WriteFileAsync needs a non-nil buffer; Length=0 so it is never read }
driver.storage.vfs.WriteFileAsync(nfctx^.handle, 0,
puint8(@nfctx^.err), 0, @fb_newfile_written, nfctx);
end else begin
if nfctx^.handle <> 0 then
driver.storage.vfs.CloseFile(nfctx^.handle);
lv_timer_create(@fb_newfile_done_timer, 1, nfctx);
end;
end;
{ Async step 2: write complete — close handle and schedule UI refresh }
procedure fb_newfile_written(error: TError; userdata: pointer);
var
nfctx : PNewFileCtx;
begin
nfctx := PNewFileCtx(userdata);
if nfctx = nil then exit;
if error <> eNone then nfctx^.err := error;
if nfctx^.handle <> 0 then
driver.storage.vfs.CloseFile(nfctx^.handle);
lv_timer_create(@fb_newfile_done_timer, 1, nfctx);
end;
@@ -3205,7 +3223,7 @@ begin
rebuild_tab_bar(state);
{ Create 500ms poll timer for directory watch notifications }
{state^.watch_timer := lv_timer_create(@fb_watch_poll_timer, 500, state);}
state^.watch_timer := lv_timer_create(@fb_watch_poll_timer, 500, state);
{ Default bookmarks }
add_bookmark_entry(state, '/');
File diff suppressed because it is too large Load Diff
@@ -40,6 +40,7 @@ type
Volume : PStorage_Volume;
Directory : pchar; { directory path within the volume }
FileName : pchar; { filename within that directory }
VFSDir : pchar; { full VFS parent directory path, e.g. '/sys' }
OpenMode : uint8; { TOpenMode ordinal — avoids VFS type dependency }
DataBuffer : puint32; { pre-loaded data for small files, nil otherwise }
DataSize : uint32; { size of pre-loaded data in bytes }
@@ -109,6 +110,10 @@ begin
kfree(void(entry^.FileName));
entry^.FileName := nil;
end;
if entry^.VFSDir <> nil then begin
kfree(void(entry^.VFSDir));
entry^.VFSDir := nil;
end;
entry^.InUse := false;
entry^.Loaded := false;
+46 -5
View File
@@ -448,14 +448,42 @@ end;
calling FireWatchEvent directly. }
procedure VFS_OnMutation(event : TVFSWatchEvent; dirPath : pchar; itemPath : pchar; vol : PStorage_Volume; relPath : pchar);
begin
{ Invalidate cache for the volume + relative path }
DirCache_Invalidate(vol, relPath);
{ Invalidate ALL cache entries for the volume. relPath is a file-level
path (e.g. 'test.txt') whose format differs from the directory-level
keys stored in the cache (e.g. '/'), so targeted matching fails.
Passing nil evicts every entry for the volume — safe and correct. }
DirCache_Invalidate(vol, nil);
{ Fire watch callbacks }
FireWatchEvent(event, dirPath, itemPath);
end;
{ =========================================================================== }
{ Return a heap-allocated copy of the parent directory portion of an
absolute VFS path. '/sys/hello.txt' -> '/sys', '/hello.txt' -> '/'. }
function vfsParentDir(path : pchar) : pchar;
var
abs : pchar;
len : uint32;
last : uint32;
i : uint32;
begin
abs := MakeAbsolutePath(path);
len := stringSize(abs);
last := 0;
if len > 0 then
for i := 0 to len - 1 do
if abs[i] = '/' then last := i;
if last = 0 then begin
vfsParentDir := stringNew(1);
vfsParentDir[0] := '/';
end else begin
vfsParentDir := stringNew(last);
core.util.memcpy(uint32(abs), uint32(vfsParentDir), last);
end;
kfree(void(abs));
end;
function makeRelative(Path : pchar; From : pchar) : pchar;
var
Result : pchar;
@@ -822,6 +850,7 @@ begin
newObj^.Parent := Parent;
newObj^.Reference := nil;
newObj^.FileSize := entry^.fileSize;
case entry^.entryType of
directoryEntry: newObj^.ObjectType := otDIRECTORY;
fileEntry: newObj^.ObjectType := otFILE;
@@ -1124,6 +1153,7 @@ begin
fd^.Volume := nil;
fd^.Directory := nil;
fd^.FileName := stringCopy(vfsObj^.ObjectName);
fd^.VFSDir := nil; { devices have no parent directory }
fd^.OpenMode := uint8(ord(OpenMode));
fd^.DataBuffer := nil;
fd^.DataSize := 0;
@@ -1159,6 +1189,7 @@ begin
fd^.Volume := vol;
fd^.Directory := dir;
fd^.FileName := fname;
fd^.VFSDir := vfsParentDir(Filename);
fd^.OpenMode := uint8(ord(OpenMode));
fd^.DataBuffer := nil;
fd^.DataSize := 0;
@@ -1287,9 +1318,12 @@ begin
status := puint32(kalloc(4));
status^ := 0;
vol^.filesystem^.writeCallback(vol, fd^.Directory, @dirEntry, Length, padBuf, status);
if status^ = 0 then
WriteFile := Length
else
if status^ = 0 then begin
WriteFile := Length;
{ Invalidate cache + fire watch notification so open file
browsers see the updated size / new entry. }
VFS_OnMutation(weModified, fd^.VFSDir, fd^.VFSDir, vol, fd^.Directory);
end else
WriteFile := 0;
kfree(puint32(status));
kfree(padBuf);
@@ -1458,6 +1492,12 @@ begin
exit;
end;
{ Invalidate directory cache before dispatching the write. The file
browser (and any other caller) only re-reads directory listings after
the completion callback fires, by which time the disk write has
finished and the fresh read will return the updated listing. }
VFS_OnMutation(weModified, fd^.VFSDir, fd^.VFSDir, vol, fd^.Directory);
{ Prefer async hook; fall back to sync if not available }
if vol^.filesystem^.writeAsyncCallback <> nil then begin
dirEntry.fileName := fd^.FileName;
@@ -1556,6 +1596,7 @@ begin
fd^.Volume := vol;
fd^.Directory := dir;
fd^.FileName := fname;
fd^.VFSDir := vfsParentDir(Filename);
fd^.OpenMode := uint8(ord(OpenMode));
fd^.DataBuffer:= nil;
fd^.DataSize := 0;
@@ -1362,7 +1362,6 @@ var
directories : PLinkedListBase;
clusters : PLinkedListBase;
startCluster: uint32;
device : PStorage_Device;
dir : PDirectory;
exists : boolean = false;
sectorCount : uint32;
@@ -1371,12 +1370,19 @@ var
dataStart : uint32;
iterations : uint32;
bufferPointer : puint32;
dataPosition : uint32;
i : uint32;
status : puint32;
namePart : pchar;
extPart : pchar;
{ Variables for updating directory entry byteSize }
entryIndex : uint32;
parentCluster : uint32;
dirSectorBuf : puint32;
dirSectorLoc : uint32;
entriesPerSec : uint32;
entryOffset : uint32;
begin
push_trace('driver.storage.fs.fat32.writeFile.enter');
io.syslog.logln('FAT32', 'writeFile: enter');
@@ -1392,7 +1398,6 @@ begin
end;
bootRecord:= readBootRecord(volume);
device:= volume^.device;
push_trace('driver.storage.fs.fat32.writeFile.readDir');
directories:= readDirectory(volume, directory, status);
@@ -1417,6 +1422,7 @@ begin
dir:= PDirectory(LL_get(directories, i));
if compareByteArray8(dir^.fileName, cleanString(namePart, status)) and matchExtension(dir^.fileExtension, extPart) then begin
exists:= true;
entryIndex:= i;
break;
end;
end;
@@ -1467,6 +1473,7 @@ begin
push_trace('driver.storage.fs.fat32.writeFile.newFile');
io.syslog.logln('FAT32', 'writeFile: creating new file');
entryIndex:= LL_size(directories); { New entry appended at end }
startCluster:= writeDirectory(volume, directory, entry^.fileName, 0, status);
{ If writeDirectory failed (disk full, dir full, invalid name, etc.), propagate error }
@@ -1479,7 +1486,7 @@ begin
exit;
end;
clusterDifference:= (byteCount div bootRecord^.sectorsize) div 4;
clusterDifference:= (byteCount div bootRecord^.sectorsize) div bootRecord^.spc;
push_trace('driver.storage.fs.fat32.writeFile.newFile.setupFat');
for i:= startcluster to startCluster + clusterDifference - 1 do begin
@@ -1493,18 +1500,33 @@ begin
push_trace('driver.storage.fs.fat32.writeFile.writeSectors');
io.syslog.logln('FAT32', 'writeFile: writing sectors');
iterations:= (bytecount div bootRecord^.sectorSize) div bootRecord^.spc; //no of clusters
{ Calculate total sectors needed and write each sector individually.
Assumes contiguous clusters starting at startCluster. }
iterations:= (bytecount + bootRecord^.sectorSize - 1) div bootRecord^.sectorSize;
if iterations > 0 then begin
for i:=0 to iterations - 1 do begin
bufferPointer:= puint32(uint32(buffer) + uint32(i * bootRecord^.sectorSize));
driver.storage.mgr.storage_write(volume^.device, dataStart + (startCluster * bootRecord^.spc) + i, 1, bufferPointer);
end;
end;
for i:=0 to iterations do begin
dataPosition:= i * uint32(bootRecord^.sectorsize * 4); //needs to be bytes / 4
bufferPointer:= @buffer[dataPosition div 4]; //todo change to puint8
driver.storage.mgr.storage_write(volume^.device, dataStart + (startCluster * bootRecord^.spc) + (i * 4), 1, bufferPointer); //i * 4 needs to be changed, TODO fix fucking driver.storage.ctl.ide driver, it suks
driver.storage.mgr.storage_write(volume^.device, dataStart + (startCluster * bootRecord^.spc) + (i * 4) + 1, 1, @bufferPointer[512 div 4]);
driver.storage.mgr.storage_write(volume^.device, dataStart + (startCluster * bootRecord^.spc) + (i * 4) + 2, 1, @bufferPointer[1024 div 4]);
driver.storage.mgr.storage_write(volume^.device, dataStart + (startCluster * bootRecord^.spc) + (i * 4) + 3, 1, @bufferPointer[1536 div 4]);
{ Write succeeded — now update the directory entry's byteSize on disk }
push_trace('driver.storage.fs.fat32.writeFile.updateByteSize');
if LL_size(directories) > 0 then begin
parentCluster:= uint32(PDirectory(LL_get(directories, 0))^.clusterLow)
or uint32(PDirectory(LL_get(directories, 0))^.clusterHigh shl 16);
entriesPerSec:= bootRecord^.sectorSize div uint32(sizeof(TDirectory));
dirSectorLoc:= (entryIndex * uint32(sizeof(TDirectory))) div bootRecord^.sectorSize;
dirSectorLoc:= dirSectorLoc + (parentCluster * bootRecord^.spc);
entryOffset:= entryIndex mod entriesPerSec;
dirSectorBuf:= puint32(kalloc(bootRecord^.sectorSize));
driver.storage.mgr.storage_read(volume^.device, dataStart + dirSectorLoc, 1, dirSectorBuf);
PDirectory(dirSectorBuf)[entryOffset].byteSize:= byteCount;
driver.storage.mgr.storage_write(volume^.device, dataStart + dirSectorLoc, 1, dirSectorBuf);
kfree(dirSectorBuf);
end;
{ Write succeeded }
io.syslog.logln('FAT32', 'writeFile: done');
if statusOut <> nil then statusOut^:= ord(eNone);
@@ -1579,28 +1601,33 @@ begin
clusters := getFatChain(volume, cluster, bootRecord);
noClusters := LL_size(clusters);
data := puint32(kalloc(noClusters * bootRecord^.spc * bootRecord^.sectorSize + bootRecord^.sectorSize));
data := puint32(kalloc(noClusters * bootRecord^.spc * bootRecord^.sectorSize));
if data = puint32(0) then begin
push_trace('UNABLE TO ALLOCATE MEMORY');
io.syslog.logln('FAT32', 'readFile: OOM allocating data buffer');
end;
memset(uint32(data), 0, noClusters * bootRecord^.spc * bootRecord^.sectorSize + bootRecord^.sectorSize);
memset(uint32(data), 0, noClusters * bootRecord^.spc * bootRecord^.sectorSize);
readbuffer := puint32(kalloc(bootRecord^.sectorSize * 2));
readbuffer := puint32(kalloc(bootRecord^.sectorSize));
if readbuffer = puint32(0) then begin
push_trace('UNABLE TO ALLOCATE MEMORY');
io.syslog.logln('FAT32', 'readFile: OOM allocating read buffer');
end;
memset(uint32(readbuffer), 0, bootRecord^.sectorSize * 2);
for i:=0 to noClusters * bootRecord^.spc do begin
driver.storage.mgr.storage_read(volume^.device, dataStart + ((cluster * bootRecord^.spc)+ i), 1, readbuffer);
memcpy(uint32(readbuffer), uint32(@data[i*bootRecord^.sectorSize div 4]), bootRecord^.sectorSize);
if noClusters * bootRecord^.spc > 0 then begin
for i:=0 to (noClusters * bootRecord^.spc) - 1 do begin
driver.storage.mgr.storage_read(volume^.device, dataStart + ((cluster * bootRecord^.spc) + i), 1, readbuffer);
memcpy(uint32(readbuffer), uint32(@data[i * bootRecord^.sectorSize div 4]), bootRecord^.sectorSize);
end;
end;
kfree(readbuffer);
buffer^ := uint32(data);
bytecount^ := noClusters * bootRecord^.spc * bootRecord^.sectorSize + bootRecord^.sectorSize;//maybe need to spc
{ Report actual file size from directory entry, not cluster-based estimate }
if dir^.byteSize > 0 then
bytecount^ := dir^.byteSize
else
bytecount^ := noClusters * bootRecord^.spc * bootRecord^.sectorSize;
readFile:= statusOut^;
io.syslog.logln('FAT32', 'readFile: done');
LL_Free(dirs);
+1 -1
View File
@@ -19,4 +19,4 @@ echo " "
LIBGCC=$(gcc -m32 -print-libgcc-file-name)
echo "libgcc: ${LIBGCC}"
ld -m elf_i386 -s --gc-sections -Ttoolchain/linker.script -o bin/kernel.bin $objstring --start-group lib/liblvgl.a ${LIBGCC} --end-group
ld -m elf_i386 -s --gc-sections --no-warn-execstack -Ttoolchain/linker.script -o bin/kernel.bin $objstring --start-group lib/liblvgl.a ${LIBGCC} --end-group