Progress on flatfs
This commit is contained in:
parent
ff4c597ff3
commit
f88a282893
@ -47,7 +47,8 @@ type
|
||||
|
||||
TFile_Entry = bitpacked record
|
||||
attribues : uint8; // 0x00 = does not exsist, 0x01 = file, 0x02 = hidden, 0x04 = system/read only, 0x10 = directory, 0x20 = archive
|
||||
name: array[0..54] of char; //max file name length is 55
|
||||
name: array[0..53] of char; //max file name length is 54
|
||||
rsv : uint8;
|
||||
size: uint32;
|
||||
start : uint32;
|
||||
end;
|
||||
@ -61,6 +62,8 @@ procedure init;
|
||||
procedure create_volume(volume : PStorage_Volume; sectors : uint32; start : uint32; config : puint32);
|
||||
procedure detect_volumes(disk : PStorage_Device);
|
||||
function read_directory(volume : PStorage_Volume; directory : pchar; status : PuInt32) : PLinkedListBase;
|
||||
procedure write_directory(volume : PStorage_Volume; directory : pchar; status : PuInt32);
|
||||
procedure write_file(volume : PStorage_Volume; fileName : pchar; data : PuInt32; size : uint32; status : PuInt32);
|
||||
|
||||
implementation
|
||||
|
||||
@ -73,11 +76,15 @@ var
|
||||
directories : PLinkedListBase;
|
||||
status : PuInt32;
|
||||
|
||||
data : pchar;
|
||||
|
||||
const
|
||||
asuroArray : array[0..7] of char = 'ASURO';
|
||||
systemArray : array[0..6] of char = '/system';
|
||||
programsArray : array[0..8] of char = '/programs';
|
||||
userArray : array[0..4] of char = '/user';
|
||||
|
||||
testArray : array[0..23] of char = 'ASURO TEST DATA ARRAY';
|
||||
begin
|
||||
|
||||
info := PDisk_Info(Kalloc(512));
|
||||
@ -117,12 +124,21 @@ begin
|
||||
entryTable[2].start := 0;
|
||||
|
||||
//write file table
|
||||
// volume^.device^.writeCallback(volume^.device, start + 1, ((sizeof(TFile_Entry) * info^.fileCount) div 512), puint32(entryTable));
|
||||
volume^.device^.writeCallback(volume^.device, start + 1, 1, puint32(entryTable));
|
||||
volume^.device^.writeCallback(volume^.device, start + 1, ((sizeof(TFile_Entry) * info^.fileCount) div 512), puint32(entryTable));
|
||||
//volume^.device^.writeCallback(volume^.device, start + 1, 1, puint32(entryTable));
|
||||
|
||||
//temp read
|
||||
status := PuInt32(Kalloc(sizeof(uint32)));
|
||||
directories := read_directory(volume, '/', status);
|
||||
// directories := read_directory(volume, '/', status);
|
||||
|
||||
data := stringNew(1024);
|
||||
memset(uint32(data), 0, 513);
|
||||
memcpy(uint32(@testArray), uint32(data), 23);
|
||||
|
||||
write_directory(volume, '/logs', status);
|
||||
write_directory(volume, '/logs/test', status);
|
||||
write_file(volume, '/logs/log.txt', PuInt32(data), 1, status);
|
||||
//read_directory(volume, '/logs', status);
|
||||
end;
|
||||
|
||||
procedure write_directory(volume : PStorage_Volume; directory : pchar; status : PuInt32);
|
||||
@ -130,6 +146,8 @@ var
|
||||
directories : PLinkedListBase;
|
||||
i : uint32;
|
||||
|
||||
fileEntries : PFile_Entry;
|
||||
|
||||
fileEntry : PFile_Entry;
|
||||
buffer : PuInt32;
|
||||
|
||||
@ -139,27 +157,42 @@ var
|
||||
const
|
||||
fileCount : uint32 = 1000;
|
||||
begin
|
||||
status^ = 0;
|
||||
status^ := 0;
|
||||
|
||||
directories := read_directory(volume, directory, status);
|
||||
//directories := read_directory(volume, directory, status);
|
||||
|
||||
fileEntries := PFile_Entry(Kalloc(sizeof(TFile_Entry) * fileCount + 512));
|
||||
memset(uint32(fileEntries), 0, sizeof(TFile_Entry) * fileCount + 512);
|
||||
|
||||
volume^.device^.readCallback(volume^.device, volume^.sectorStart + 1, (sizeof(TFile_Entry) * fileCount) div 512, PuInt32(fileEntries));
|
||||
|
||||
if status^ = 0 then begin
|
||||
for i := 0 to fileCount - 1 do begin
|
||||
fileEntry := PFile_Entry(LL_Get(directories, i));
|
||||
|
||||
push_trace('write_directory_2');
|
||||
|
||||
//fileEntry := PFile_Entry(STRLL_Get(directories, i));
|
||||
fileEntry := @fileEntries[i];
|
||||
|
||||
if fileEntry^.attribues = 0 then begin
|
||||
fileEntry^.attribues := $10;
|
||||
fileEntry^.size := 0;
|
||||
fileEntry^.start := 0;
|
||||
memcpy(uint32(@fileEntry^.name), uint32(@directory), strlen(directory));
|
||||
memcpy(uint32(directory), uint32(@fileEntry^.name), stringSize(directory));
|
||||
|
||||
push_trace('write_directory_3');
|
||||
|
||||
//write file table
|
||||
buffer := PuInt32(Kalloc(512));
|
||||
buffer := PuInt32(Kalloc(513));
|
||||
memset(uint32(buffer), 0, 512);
|
||||
|
||||
push_trace('write_directory.b4_read');
|
||||
|
||||
//read file table cointaining entry of interest
|
||||
position := (i * sizeof(TFile_Entry)) div 512;
|
||||
volume^.device^.readCallback(volume^.device, sectorStart + 1 + position, 1, buffer);
|
||||
volume^.device^.readCallback(volume^.device, volume^.sectorStart + 1 + position, 1, buffer);
|
||||
|
||||
push_trace('write_directory.after_read');
|
||||
|
||||
//calculate entry offset into sector
|
||||
offset := (i * sizeof(TFile_Entry)) mod 512;
|
||||
@ -169,10 +202,12 @@ begin
|
||||
PFile_Entry(buffer)[offset] := fileEntry^;
|
||||
|
||||
//write updated file table sector
|
||||
volume^.device^.writeCallback(volume^.device, sectorStart + 1 + position, 1, buffer);
|
||||
volume^.device^.writeCallback(volume^.device, volume^.sectorStart + 1 + position, 1, buffer);
|
||||
|
||||
Kfree(buffer);
|
||||
|
||||
push_trace('write_directory.after_write');
|
||||
|
||||
break;
|
||||
|
||||
end;
|
||||
@ -183,7 +218,6 @@ begin
|
||||
status^ := 1;
|
||||
end;
|
||||
|
||||
LL_Destroy(directories);
|
||||
end;
|
||||
|
||||
function read_directory(volume : PStorage_Volume; directory : pchar; status : PuInt32) : PLinkedListBase;
|
||||
@ -199,7 +233,10 @@ var
|
||||
fileCount : uint32 = 1000;
|
||||
|
||||
begin
|
||||
|
||||
|
||||
writeStringlnWnd('read_directory', getTerminalHWND());
|
||||
redrawWindows();
|
||||
|
||||
compString := pchar(kalloc(60));
|
||||
memset(uint32(compString), 0, 60);
|
||||
|
||||
@ -208,17 +245,38 @@ begin
|
||||
|
||||
directories := STRLL_New();
|
||||
|
||||
fileEntrys := PFile_Entry(Kalloc(sizeof(TFile_Entry) * fileCount));
|
||||
fileEntrys := PFile_Entry(Kalloc(sizeof(TFile_Entry) * fileCount + 512));
|
||||
push_trace('read_directory.b4_read');
|
||||
volume^.device^.readCallback(volume^.device, volume^.sectorStart + 1, sizeof(TFile_Entry) * fileCount div 512, puint32(fileEntrys));
|
||||
push_trace('read_directory.after_read');
|
||||
|
||||
writeStringlnWnd('read_directory_1', getTerminalHWND());
|
||||
redrawWindows();
|
||||
|
||||
for i := 0 to fileCount - 1 do begin
|
||||
// if fileEntrys[i].attribues and $10 = $10 then begin
|
||||
writeStringlnWnd('read_directory_loop1', getTerminalHWND());
|
||||
redrawWindows();
|
||||
|
||||
writestringlnWND(fileEntrys[i].name, getterminalhwnd());
|
||||
|
||||
push_trace('read_directory.loop1');
|
||||
|
||||
if uint32(fileEntrys[i].name[0]) = 0 then begin
|
||||
writestringlnWND('fileEntry^.name = nil', getterminalhwnd());
|
||||
break;
|
||||
end;
|
||||
|
||||
push_trace('read_directory.loop2');
|
||||
|
||||
compString := stringSub(fileEntrys[i].name, 0, stringSize(directory));
|
||||
afterString := stringSub(fileEntrys[i].name, stringSize(directory), stringSize(fileEntrys[i].name)-1);
|
||||
|
||||
writeStringlnWnd('read_directory_loop2', getTerminalHWND());
|
||||
redrawWindows();
|
||||
|
||||
push_trace('read_directory.loop3');
|
||||
|
||||
if stringEquals(compString, directory) and
|
||||
(not stringContains(afterString, '/')) then begin
|
||||
|
||||
@ -226,9 +284,12 @@ begin
|
||||
STRLL_Add(directories, afterString);
|
||||
end;
|
||||
|
||||
|
||||
// end;
|
||||
end;
|
||||
|
||||
push_trace('read_directory.after_loop');
|
||||
|
||||
Kfree(puint32(fileEntrys));
|
||||
|
||||
read_directory := directories;
|
||||
@ -244,6 +305,9 @@ var
|
||||
tempFileEntry : PFile_Entry;
|
||||
begin
|
||||
|
||||
tempFileEntry := PFile_Entry(Kalloc(sizeof(TFile_Entry)));
|
||||
memset(uint32(tempFileEntry), 0, sizeof(TFile_Entry));
|
||||
|
||||
i:=begining;
|
||||
j:=stop;
|
||||
|
||||
@ -259,9 +323,9 @@ begin
|
||||
end;
|
||||
|
||||
if i <= j then begin
|
||||
tempFileEntry := list[i];
|
||||
tempFileEntry^ := list[i];
|
||||
list[i] := list[j];
|
||||
list[j] := tempFileEntry;
|
||||
list[j] := tempFileEntry^;
|
||||
|
||||
i := i + 1;
|
||||
j := j - 1;
|
||||
@ -276,23 +340,45 @@ begin
|
||||
quicksort_start(list, i, stop);
|
||||
end;
|
||||
|
||||
Kfree(puint32(tempFileEntry));
|
||||
|
||||
end;
|
||||
|
||||
function find_free_space(volume : PStorage_Volume; size : uint32; status : PuInt32) : uint32;
|
||||
var
|
||||
fileEntrys : PFile_Entry;
|
||||
fileEntry : TFile_Entry;
|
||||
fileEntry : PFile_Entry;
|
||||
i : uint32;
|
||||
fileCount : uint32 = 1000;
|
||||
|
||||
baseAddress : uint32;
|
||||
begin
|
||||
fileEntrys := PFile_Entry(Kalloc(sizeof(TFile_Entry) * fileCount));
|
||||
fileEntrys := PFile_Entry(Kalloc(sizeof(TFile_Entry) * fileCount + 512));
|
||||
push_trace('find_free_space.before_read');
|
||||
volume^.device^.readCallback(volume^.device, volume^.sectorStart + 1, sizeof(TFile_Entry) * fileCount div 512, puint32(fileEntrys));
|
||||
push_trace('find_free_space.after_read');
|
||||
|
||||
baseAddress := volume^.sectorStart + 2 + ( (fileCount * sizeof(TFile_Entry)) div 512);
|
||||
|
||||
//sort file table by start address using quicksort
|
||||
quicksort(fileEntrys, 0, fileCount - 1);
|
||||
//quicksort_start(fileEntrys, 0, fileCount - 1);
|
||||
|
||||
//find free space big enough to fit size
|
||||
//find free space big enough to fit size //TODO: make this work
|
||||
for i := 0 to fileCount - 1 do begin
|
||||
|
||||
if fileEntrys[i+1].start = 0 then begin
|
||||
|
||||
if fileEntrys[i].size = 0 then begin
|
||||
find_free_space := baseAddress;
|
||||
break;
|
||||
end else begin
|
||||
find_free_space := baseAddress + fileEntrys[i].start;
|
||||
break;
|
||||
end;
|
||||
|
||||
break;
|
||||
end;
|
||||
|
||||
if fileEntrys[i+1].start - (fileEntrys[i].start + fileEntrys[i].size) > size+1 then begin
|
||||
find_free_space := fileEntrys[i].start + fileEntrys[i].size;
|
||||
status^ := 0;
|
||||
@ -301,15 +387,15 @@ begin
|
||||
end;
|
||||
|
||||
Kfree(puint32(fileEntrys));
|
||||
|
||||
find_free_space := freeSpace;
|
||||
end;
|
||||
|
||||
procedure write_file(volume : PStorage_Volume; fileName : pchar; data : PuInt32; size : uint32; status : PuInt32);
|
||||
var
|
||||
directories : PLinkedListBase;
|
||||
i : uint32;
|
||||
p : uint32;
|
||||
|
||||
fileEntries : PFile_Entry;
|
||||
fileEntry : PFile_Entry;
|
||||
buffer : PuInt32;
|
||||
|
||||
@ -319,81 +405,99 @@ var
|
||||
fileCount : uint32 = 1000;
|
||||
|
||||
exsists : boolean;
|
||||
|
||||
elm : puint32;
|
||||
begin
|
||||
|
||||
//load file table
|
||||
directories := read_directory(volume, '/', status);
|
||||
push_trace('flatfs.write_file');
|
||||
|
||||
//load file table
|
||||
fileEntries := PFile_Entry(Kalloc(sizeof(TFile_Entry) * fileCount + 512));
|
||||
memset(uint32(fileEntries), 0, sizeof(TFile_Entry) * fileCount);
|
||||
|
||||
volume^.device^.readCallback(volume^.device, volume^.sectorStart + 1, sizeof(TFile_Entry) * fileCount div 512, puint32(fileEntries));
|
||||
|
||||
push_trace('flatfs.write_file.after_read');
|
||||
|
||||
//check if file exists else find free entry
|
||||
for i := 0 to fileCount - 1 do begin
|
||||
fileEntry := PFile_Entry(LL_Get(directories, i));
|
||||
//fileEntry^ := fileEntries[i];
|
||||
|
||||
if stringEquals(fileEntry^.name, fileName) then begin
|
||||
if stringEquals(fileEntries[i].name, fileName) then begin
|
||||
exsists := true;
|
||||
p:= i;
|
||||
break;
|
||||
end;
|
||||
|
||||
if fileEntry^.attribues = 0 then begin
|
||||
if fileEntries[i].attribues = 0 then begin
|
||||
exsists := false;
|
||||
p:= i;
|
||||
break;
|
||||
end;
|
||||
end;
|
||||
|
||||
//if entry is directory then exit
|
||||
if fileEntry^.attribues and $10 = $10 then begin
|
||||
if fileEntries[p].attribues and $10 = $10 then begin
|
||||
status^ := 1;
|
||||
|
||||
writestringlnWND('write_file: file is directory', getTerminalHWND());
|
||||
|
||||
exit;
|
||||
end;
|
||||
|
||||
//allocate file table
|
||||
buffer := PuInt32(Kalloc(512));
|
||||
memset(uint32(buffer), 0, 512);
|
||||
push_trace('flatfs.write_file.after_loop');
|
||||
|
||||
//read file table cointaining entry of interest
|
||||
position := (i * sizeof(TFile_Entry)) div 512;
|
||||
volume^.device^.readCallback(volume^.device, sectorStart + 1 + position, 1, buffer);
|
||||
// //allocate file table
|
||||
// buffer := PuInt32(Kalloc(512));
|
||||
// memset(uint32(buffer), 0, 512);
|
||||
|
||||
// //read file table cointaining entry of interest
|
||||
position := (p * sizeof(TFile_Entry)) div 512;
|
||||
// volume^.device^.readCallback(volume^.device, volume^.sectorStart + 1 + position, 1, buffer);
|
||||
|
||||
//calculate entry offset into sector
|
||||
offset := (i * sizeof(TFile_Entry)) mod 512;
|
||||
offset := offset div sizeof(TFile_Entry);
|
||||
// offset := (p * sizeof(TFile_Entry)) mod 512;
|
||||
// offset := offset div sizeof(TFile_Entry);
|
||||
|
||||
//if file exists update entry else create new entry
|
||||
if exsists then do begin
|
||||
if exsists then begin
|
||||
//check if size is greater than current size
|
||||
if size > fileEntry^.size then begin
|
||||
if size > fileEntries[p].size then begin
|
||||
//find free space
|
||||
fileEntry^.start := find_free_space(volume, size);
|
||||
fileEntry^.size := size;
|
||||
fileEntries[p].start := find_free_space(volume, size, status);
|
||||
fileEntries[p].size := size;
|
||||
end else begin
|
||||
//update size
|
||||
fileEntry^.size := size;
|
||||
fileEntries[p].size := size;
|
||||
end;
|
||||
|
||||
end else begin
|
||||
//find free space
|
||||
fileEntry^.start := find_free_space(volume, size);
|
||||
fileEntry^.size := size;
|
||||
fileEntry^.attribues := $01;
|
||||
fileEntries[p].start := find_free_space(volume, size, status);
|
||||
fileEntries[p].size := size;
|
||||
fileEntries[p].attribues := $01;
|
||||
|
||||
//add file name
|
||||
fileEntry^.name := fileName;
|
||||
// fileEntry^.name := fileName;
|
||||
memcpy(uint32(fileName), uint32(@fileEntries[p].name), 14);
|
||||
|
||||
//add file to table
|
||||
LL_Add(directories, fileEntry);
|
||||
end;
|
||||
|
||||
|
||||
writestringlnWND('write_file: writing file', getTerminalHWND());
|
||||
|
||||
//fileEntries[i] := fileEntry^;
|
||||
|
||||
//write file table
|
||||
volume^.device^.writeCallback(volume^.device, sectorStart + 1 + position, 1, buffer);
|
||||
volume^.device^.writeCallback(volume^.device, volume^.sectorStart + 1 , 1, puint32(@fileEntries[position])); /////
|
||||
|
||||
writeIntLnWND(fileEntries[p].start, getTerminalHWND());
|
||||
|
||||
//write file
|
||||
volume^.device^.writeCallback(volume^.device, fileEntry^.start, size div 512 + 1, data);
|
||||
volume^.device^.writeCallback(volume^.device, fileEntries[p].start, size, data);
|
||||
|
||||
//free buffer
|
||||
Kfree(puint32(buffer));
|
||||
|
||||
//free directories
|
||||
LL_Free(directories);
|
||||
Kfree(puint32(fileEntries));
|
||||
end;
|
||||
|
||||
procedure detect_volumes(disk : PStorage_Device);
|
||||
|
Loading…
x
Reference in New Issue
Block a user