Started rewrite of storage system.

git-svn-id: https://spexeah.com:8443/svn/Asuro@1299 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c
This commit is contained in:
aaron 2020-07-17 22:30:36 +00:00
parent 984b6a89c8
commit d4234225d1
3 changed files with 114 additions and 365 deletions

View File

@ -357,7 +357,7 @@ begin
for i:= 0 to LL_Size(list) - 1 do begin for i:= 0 to LL_Size(list) - 1 do begin
dir := PDirectory(LL_get(list, i)); dir := PDirectory(LL_get(list, i));
entry^.fileName:= pchar(dir^.fileName); entry^.fileName:= pchar(dir^.fileName);
entry^.extension:= pchar(dir^.fileExtension); // entry^.extension:= pchar(dir^.fileExtension);
if dir^.attributes = $10 then begin if dir^.attributes = $10 then begin
entry^.entryType:= TDirectory_Entry_Type.directoryEntry; entry^.entryType:= TDirectory_Entry_Type.directoryEntry;
@ -578,10 +578,10 @@ begin
for i:=0 to LL_size(directories) - 1 do begin for i:=0 to LL_size(directories) - 1 do begin
dir:= PDirectory(LL_get(directories, i)); dir:= PDirectory(LL_get(directories, i));
if (dir^.fileName = entry^.fileName) and (dir^.fileExtension = entry^.extension) then begin // if (dir^.fileName = entry^.fileName) and (dir^.fileExtension = entry^.extension) then begin
exists:= true; // exists:= true;
break; // break;
end; // end;
end; end;
push_trace('writefile.1'); push_trace('writefile.1');
@ -613,7 +613,7 @@ begin
end else begin end else begin
push_trace('writefile.1.2.1'); push_trace('writefile.1.2.1');
startCluster:= writeDirectory(volume, directory, entry^.fileName, 0, status, entry^.extension); // startCluster:= writeDirectory(volume, directory, entry^.fileName, 0, status, entry^.extension);
clusterDifference:= (byteCount div bootRecord^.sectorsize) div 4; //-1 is that needed? clusterDifference:= (byteCount div bootRecord^.sectorsize) div 4; //-1 is that needed?
push_trace('writefile.1.2.2'); push_trace('writefile.1.2.2');

View File

@ -1,12 +1,8 @@
{ ************************************************ {
* Asuro Driver->Storage->StorageManagment Storage Managment System
* Unit: Drivers/storage/storagemanagement
* Description: interface for storage drivers @author(Aaron Hance ah@aaronhance.me)
* }
************************************************
* Author: Aaron Hance
* Contributors:
************************************************ }
unit storagemanagement; unit storagemanagement;
interface interface
@ -61,10 +57,11 @@ type
sectorSize : uint32; sectorSize : uint32;
freeSectors : uint32; freeSectors : uint32;
filesystem : PFilesystem; filesystem : PFilesystem;
//filesystemInfo : Puint32; // type dependant on filesystem. can be null if not creating volume now { True if this drive contains the loaded OS }
//directory : PLinkedListBase; // type dependant on filesytem? isBootDrive : boolean;
end; end;
{ Generic storage device }
TStorage_Device = record TStorage_Device = record
idx : uint8; idx : uint8;
controller : TControllerType; controller : TControllerType;
@ -80,9 +77,10 @@ type
end; end;
APStorage_Device = array[0..255] of PStorage_device; APStorage_Device = array[0..255] of PStorage_device;
TDirectory_Entry = record //TODO implement dtae.time stuff { Generic directory entry }
TDirectory_Entry = record
{ Contains filename and optionally file extension, seperated by the last period.}
fileName : pchar; fileName : pchar;
extension : pchar;
entryType : TDirectory_Entry_Type; entryType : TDirectory_Entry_Type;
creationT : TDateTime; creationT : TDateTime;
modifiedT : TDateTime; modifiedT : TDateTime;
@ -101,304 +99,133 @@ function get_device_list() : PLinkedListBase;
procedure register_filesystem(filesystem : PFilesystem); procedure register_filesystem(filesystem : PFilesystem);
procedure register_volume(device : PStorage_Device; volume : PStorage_Volume); procedure register_volume(device : PStorage_Device; volume : PStorage_Volume);
function writeNewFile(fileName : pchar; extension : pchar; buffer : puint32; size : uint32) : uint32; //function writeNewFile(fileName : pchar; extension : pchar; buffer : puint32; size : uint32) : uint32;
function readFile(fileName : pchar; extension : pchar; buffer : puint32; byteCount : puint32) : puint32; //function readFile(fileName : pchar; extension : pchar; buffer : puint32; byteCount : puint32) : puint32;
//TODO write partition table //TODO write partition table
implementation implementation
function controller_type_2_string(controllerType : TControllerType) : pchar;
begin
case controllerType of
ControllerIDE : controller_type_2_string:= 'IDE';
ControllerUSB : controller_type_2_string:= 'USB';
ControllerAHCI : controller_type_2_string:= 'ACHI (SATA)';
ControllerNET : controller_type_2_string:= 'Net';
end;
end;
{ Disk subcommand for listing drives }
procedure ls_command(params : PParamList);
var
i : uint16;
begin
push_trace('storagemanagment.ls_command');
//if no storage device print none found
if LL_Size(storageDevices) < 1 then begin
console.writestringlnWnd('No storage devices found.', getTerminalHWND());
exit;
end;
//print number of storage devices
console.writeintwnd(LL_Size(storageDevices), getTerminalHWND());
console.writestringlnWnd(' devices found', getTerminalHWND());
for i:=0 to LL_Size(storageDevices)-1 do begin
//print device id and type
console.writeintwnd(i, getTerminalHWND());
console.writestringwnd(' - Device type: ', getTerminalHWND());
console.writestringwnd(controller_type_2_string(PStorage_Device(LL_Get(storageDevices, i))^.controller), getTerminalHWND());
console.writestringwnd(', ', getTerminalHWND());
//print device capcity
console.writestringwnd(' Capacity: ', getTerminalHWND());
console.writeintwnd((( PStorage_Device(LL_Get(storageDevices, i))^.maxSectorCount * PStorage_Device(LL_Get(storageDevices, i))^.sectorSize) DIV 1024) DIV 1024, getTerminalHWND());
console.writestringlnWnd('MB', getTerminalHWND());
end;
end;
{ Disk subcommand for formatting drives }
procedure format_command(params : PParamList);
var
driveIndex : uint16;
filesystemString : pchar;
filesystem : PFileSystem;
begin
// driveIndex:= stringToInt( getParam(params, 1) );
// PFilesystem(LL_Get(filesystems, 0))^.createCallback(
// (PStorage_Device(LL_Get(storageDevices, drive))), 10000, 1, spc); //todo fs
end;
{ Command for managing and getting information from disks. }
procedure disk_command(params : PParamList); procedure disk_command(params : PParamList);
var var
i : uint8; i : uint16;
spc : puint32; drive : uint16;
drive : uint32; subcmd : pchar;
begin begin
push_trace('storagemanagement.diskcommand'); push_trace('StorageManagment.disk_command');
spc:= puint32(kalloc(4));
spc^:= 4;
if stringEquals(getParam(0, params), 'ls') and (LL_Size(storageDevices) > 0) then begin subcmd:= getParam(0, params);
for i:=0 to LL_Size(storageDevices) - 1 do begin
// if PStorage_Device(LL_Get(storageDevices, i))^.maxSectorCount = 0 then break; //ls for listing storage devices
console.writeint(i); if stringEquals(subcmd, 'ls') then begin
console.writestring(') Device_Type: '); ls_command(params);
case PStorage_Device(LL_Get(storageDevices, i))^.controller of exit;
ControllerIDE : console.writestring('IDE, ');
ControllerUSB : console.writestring('USB, ');
ControllerAHCI : console.writestring('AHCI, ');
ControllerNET : console.writestring('NET, ');
end;
console.writestring('Capacity: ');
console.writeint((( PStorage_Device(LL_Get(storageDevices, i))^.maxSectorCount * PStorage_Device(LL_Get(storageDevices, i))^.sectorSize) DIV 1024) DIV 1024);
console.writestringln('MB');
end;
end else if stringEquals(getParam(0, params), 'format') then begin //disk format 0 fat32
//check param count!
drive := stringToInt(getParam(1, params));
console.writeintln(drive); // works
if stringEquals(getParam(2, params), 'fat32') then begin
PFilesystem(LL_Get(filesystems, 0))^.createCallback((PStorage_Device(LL_Get(storageDevices, drive))), 1000, 1, spc); //todo check fs
console.writestring('Drive ');
//console.writeint(drive); // page faults
console.writestringln(' formatted.');
end; end;
end else if stringEquals(getParam(0, params), 'lsfs') then begin //format command for clearing a disk and make a new volume
for i:=0 to LL_Size(filesystems)-1 do begin if stringEquals(subcmd, 'format') then begin
//print file systems
console.writestringln(PFilesystem(LL_Get(filesystems, i))^.sName);
end;
end;
pop_trace;
end; end;
procedure volume_command(params : PParamList); //lsfs command for listing filesystems
var if stringEquals(subcmd, 'lsfs') then begin
i : uint32;
ii : uint16;
device : PStorage_device;
volume : PStorage_volume;
begin
if stringEquals(getParam(0, params), 'ls') and (LL_Size(storageDevices) > 0) then begin
for i:=0 to LL_Size(storageDevices) - 1 do begin
device := PStorage_device(LL_Get(storageDevices, i));
if LL_Size(device^.volumes) > 0 then begin
for ii:= 0 to LL_Size(device^.volumes) - 1 do begin
volume:= PStorage_volume(LL_Get(device^.volumes, ii));
console.writeint(i);
console.writestring(') Filesystem: ');
console.writestring(volume^.filesystem^.sName);
console.writestring(' Free Space: ');
console.writeintln(volume^.freeSectors * volume^.sectorSize DIV 1024 DIV 1024);
end;
end; end;
end; pop_trace();
end;
end;
function padWithSpaces(str : pchar; length : uint16) : pchar;
var
i : uint32;
ii : uint32;
begin
for i:=0 to length do begin
if str[i] = ' ' then begin
for ii:=i to length do begin
padWithSpaces[i]:= ' ';
end;
end else begin
if str[i] = char(0) then exit;
padWithSpaces[i]:= str[i];
end;
end;
end;
function allButLast(str : pchar; delim : char) : pchar;
var
strings : PLinkedListBase;
i : uint32;
begin
strings:= STRLL_FromString(str, delim);
if STRLL_Size(strings) > 2 then begin
for i:=0 to STRLL_Size(strings) - 2 do begin
stringConcat(allButLast, STRLL_Get(strings, i));
end;
end else begin
if STRLL_Size(strings) = 2 then begin
allButLast:= STRLL_Get(strings, 0);
end else begin
allButLast:= str;
end;
end;
console.writestringlnWND(allButLast, getTerminalHWND());
STRLL_Clear(strings);
STRLL_Free(strings);
end;
procedure list_command(params : PParamList);
var
dirEntries : PLinkedListBase;
i : uint32;
ii : uint32;
error : puint32;
nli : uint32 = 0;
begin
error := puint32(kalloc(4));
if paramCount(params) > 1 then begin
console.writestringlnWND('', getTerminalHWND());
console.writestringlnWND('Lists the files and directories in working directory, or optionaly, the specified directory.', getTerminalHWND());
console.writestringlnWND('', getTerminalHWND());
console.writestringlnWND('Usage:', getTerminalHWND());
console.writestringlnWND('ls <directory>', getTerminalHWND());
end else begin
if paramCount(params) = 1 then begin
dirEntries:= rootVolume^.filesystem^.readDirCallback(rootVolume, pchar(getParam(0, params)), error);
end else begin
dirEntries:= rootVolume^.filesystem^.readDirCallback(rootVolume, getWorkingDirectory(), error);
end;
//loop and print dirs
for i:=2 to LL_Size(dirEntries) - 1 do begin
console.writestringWND(' /', getTerminalHWND);
console.writestringWND( PDirectory_Entry(LL_Get(dirEntries, i ))^.fileName , getTerminalHWND());
if nli > 3 then begin
console.writestringlnWND('',getTerminalHWND());
nli:= 0;
end else begin
nli+=1;
end;
end;
console.writestringlnWND('',getTerminalHWND());
end;
kfree(error);
end;
procedure change_dir_command(params : PParamList);
var
targetDirectory : pchar;
dirEntries : PLinkedListBase;
lastString : pchar;
entry : PDirectory_Entry;
error : puint32;
begin
error := puint32(kalloc(4));
if paramCount(params) = 1 then begin
if (pchar(getParam(0, params))[0] = '/') then begin //search from root
targetDirectory:= pchar(getParam(0,params));
dirEntries:= rootVolume^.filesystem^.readDirCallback(rootVolume, pchar( getParam(0, params)), error);
console.writestringlnWND('from root', getTerminalHWND());
end else begin //search from working directory
//concat current dir and param
targetDirectory:= stringConcat(getWorkingDirectory, '/');
targetDirectory:= stringConcat(targetDirectory, pchar(getParam(0,params)) ); //do i need pad with space?
dirEntries:= rootVolume^.filesystem^.readDirCallback(rootVolume, targetDirectory, error); // need to be able to supply attributes for list conversion method
end;
if error^ = 0 then begin
setworkingdirectory(targetDirectory); // need to clean ..
end;
kfree(puint32(dirEntries));
end else begin
console.writestringlnWND('', getTerminalHWND());
console.writestringlnWND('Changes the current working directory, this is the context wich other commands rely on.', getTerminalHWND());
console.writestringlnWND('', getTerminalHWND());
console.writestringlnWND('Usage:', getTerminalHWND());
console.writestringlnWND('cd <directory>', getTerminalHWND());
end;
kfree(error);
end;
procedure mkdir_command(params : PParamList);
var
directories : PLinkedListBase;
targetDirectory : pchar;
temp : pchar;
target : pchar;
device : PStorage_Device;
volume : PStorage_Volume;
status : uint32;
error : puint32;
i : uint32;
isRoot : boolean = false;
begin
error:= @status;
error^ := 0;
if paramCount(params) = 1 then begin
directories:= STRLL_FromString(getParam(0, params), '/');
if LL_Size(directories) > 1 then begin
temp := allbutlast(getParam(0, params), '/');
end else begin
temp:= '/';
end;
target:= STRLL_Get(directories, LL_Size(directories)-1);
if (getParam(0, params)[0] = '/') then begin //search from root
targetDirectory:= temp;
end else begin //search from working directory
targetDirectory:= stringConcat(getWorkingDirectory, '/');
targetDirectory:= stringConcat(targetDirectory, temp ); //do i need pad with space?
end;
console.writestringlnWND(targetDirectory, getTerminalHWND());
console.writestringlnWND(target, getTerminalHWND());
device:= PStorage_Device(LL_Get(storageDevices, 0));
volume:= PStorage_Volume(LL_Get(device^.volumes, 0));
volume^.filesystem^.createDirCallback(volume, targetDirectory, target, $10, error);
if error^ <> 0 then console.writestringlnWND('ERROR', getTerminalHWND());
end; // else print out help
end;
procedure txt_command(params : PParamList);
var
buffer : puint32;
entry : TDirectory_Entry;
error : puint32;
i : uint32 = 1;
fatArray : byteArray8 = ('F','A','T','3','2',' ',' ',' ');
begin
push_trace('txt_command');
error:= puint32(kalloc(512));
buffer:= puint32(kalloc(2048));
memset(uint32(buffer), 0, 2048);
entry.fileName:= getParam(0, params);
entry.extension:= 'txt';
entry.entryType:= TDirectory_Entry_Type.fileEntry;
PByteArray8(buffer)^ := fatArray;
push_trace('txt_cmd_');
rootVolume^.filesystem^.writeCallback(rootVolume, '.', @entry, stringSize(pchar(buffer)), buffer, error); //need to change the function pointer to match and impiment it in the filesystem init.
end; end;
{ Initialise the storage manager }
procedure init(); procedure init();
begin begin
push_trace('storagemanagement.init'); push_trace('StorageManagment.init');
setworkingdirectory('.'); setworkingdirectory('.');
storageDevices:= ll_New(sizeof(TStorage_Device)); storageDevices:= ll_new(sizeof(TStorage_Device));
fileSystems:= ll_New(sizeof(TFilesystem)); fileSystems:= ll_New(sizeof(TFilesystem));
terminal.registerCommand('DISK', @disk_command, 'Disk utility'); terminal.registerCommand('DISK', @disk_command, 'Disk utility');
terminal.registerCommand('VOLUME', @volume_command, 'Volume utility');
terminal.registerCommandEx('mkdir', @mkdir_command, 'Make Directory', true); pop_trace();
terminal.registerCommandEx('ls', @list_command, 'List contents of directory', true);
terminal.registerCommandEx('cd', @change_dir_command, 'Change the working directory', true);
terminal.registerCommandEx('txt', @txt_command, 'testing file write', false);
end; end;
{ Register a new drive with the storage manager }
procedure register_device(device : PStorage_device); procedure register_device(device : PStorage_device);
var var
i : uint8; i : uint8;
elm : void; elm : void;
begin begin
push_trace('storagemanagement.register_device()'); push_trace('StorageManagment.register_device');
//add the drive to the list of storage devices.
elm:= LL_Add(storageDevices); elm:= LL_Add(storageDevices);
PStorage_device(elm)^ := device^; PStorage_device(elm)^ := device^;
//create empty volume list for drive
PStorage_Device(LL_Get(storageDevices, LL_Size(storageDevices) - 1))^.volumes := LL_New(sizeof(TStorage_Volume)); PStorage_Device(LL_Get(storageDevices, LL_Size(storageDevices) - 1))^.volumes := LL_New(sizeof(TStorage_Volume));
//check for volumes //check for volumes
for i:=0 to ll_size(filesystems) - 1 do begin for i:=0 to ll_size(filesystems) - 1 do begin
PFileSystem(LL_Get(filesystems, i))^.detectCallback(PStorage_Device(LL_Get(storageDevices, LL_Size(storageDevices) - 1))); PFileSystem(LL_Get(filesystems, i))^.detectCallback(PStorage_Device(LL_Get(storageDevices, LL_Size(storageDevices) - 1)));
end; end;
pop_trace();
end; end;
function get_device_list() : PLinkedListBase; function get_device_list() : PLinkedListBase;
@ -406,7 +233,6 @@ begin
get_device_list:= storageDevices; get_device_list:= storageDevices;
end; end;
procedure register_filesystem(filesystem : PFilesystem); procedure register_filesystem(filesystem : PFilesystem);
var var
elm : void; elm : void;
@ -426,81 +252,4 @@ begin
if rootVolume = PStorage_Volume(0) then rootVolume:= volume; if rootVolume = PStorage_Volume(0) then rootVolume:= volume;
end; end;
function writeNewFile(fileName : pchar; extension : pchar; buffer : puint32; size : uint32) : uint32;
var
entry : TDirectory_Entry;
error : puint32;
begin
push_trace('storagemanagment.writenewfile');
error:= puint32(kalloc(512));
entry.fileName:= fileName;
entry.extension:= extension;
entry.entryType:= TDirectory_Entry_Type.fileEntry;
rootVolume^.filesystem^.writeCallback(rootVolume, getWorkingDirectory(), @entry, size, buffer, error);
writeNewFile:= error^; //memory leak
end;
function cleanString(str : pchar) : pchar;
var
i : uint32;
ii: uint32;
begin
cleanString:= pchar(kalloc(10));
memset(uint32(cleanstring), 0, 10);
for i:=0 to 7 do begin
if str[i] = char(0) then begin
for ii:=i to 7 do begin
cleanString[ii]:= ' ';
end;
break;
end else begin
cleanString[i]:= str[i];
end;
end;
end;
function readFile(fileName : pchar; extension : pchar; buffer : puint32; byteCount : puint32) : puint32;
var
error : puint32;
dirs : PLinkedListBase;
exists : boolean = false;
i : uint32;
cleanFileName : pchar;
otherCleanFileName : pchar;
begin
push_trace('storagemanagement.readfile');
//bytecount := puint32(kalloc(4));
//memset(uint32(bytecount), 0, 4);
error := puint32(kalloc(4));
readfile:= error;
// cleanFileName := cleanString(filename);
// dirs := rootVolume^.filesystem^.readdirCallback(rootVolume, getWorkingDirectory(), error);
// for i:=0 to LL_Size(dirs) -1 do begin
// otherCleanFileName := cleanString(PDirectory_Entry(LL_get(dirs, i))^.filename); //TODO clean extension
// writestringlnWND(otherCleanFileName, getTerminalHWND());
// writestringlnWND(cleanFileName, getTerminalHWND());
// if stringEquals(cleanFileName, otherCleanFileName) then begin
// writestringlnWND('match found!', getTerminalHWND());
// exists := true;
// error^ := 0;
// end;
// kfree(puint32(otherCleanFileName));
// end;
// kfree(puint32(cleanFileName));
error^ := rootVolume^.filesystem^.readCallback(rootVolume, getWorkingDirectory(), fileName, extension, buffer, bytecount);
end;
end. end.

View File

@ -75,7 +75,7 @@ begin
//writeintlnWND(info.key_code, getTerminalHWND()); //writeintlnWND(info.key_code, getTerminalHWND());
if (info.CTRL_DOWN and (info.key_code = 115)) then begin if (info.CTRL_DOWN and (info.key_code = 115)) then begin
//SAVE FILE //SAVE FILE
storagemanagement.writeNewFile(fileName, extension, puint32(chars), stringSize(chars)); // storagemanagement.writeNewFile(fileName, extension, puint32(chars), stringSize(chars));
writestringlnWND('saved', getTerminalHWND()); writestringlnWND('saved', getTerminalHWND());
//writeintlnWND(stringSize(chars), getTerminalHWND()); //writeintlnWND(stringSize(chars), getTerminalHWND());
end else begin end else begin
@ -125,7 +125,7 @@ begin
bytes := puint32(kalloc(4)); bytes := puint32(kalloc(4));
bytes^ := 0; bytes^ := 0;
buffer := kalloc(4); buffer := kalloc(4);
error := storagemanagement.readfile(fileName, extension, buffer, bytes); // error := storagemanagement.readfile(fileName, extension, buffer, bytes);
if error^ = 1 then begin if error^ = 1 then begin
//file doesn't exists, make new //file doesn't exists, make new