{ FSIUnixUtils
Copyright (C) 2025 by Bart Broersma and Flying Sheep Inc.
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
Unit FsiUnixUtils;
interface
uses
SysUtils, Classes, Process, FsiDiskUtils, fpjson;
type
TDriveType = FsiDiskUtils.TDriveType;
TDriveTypes = FsiDiskUtils.TDriveTypes;
const
NonFixedDrives = FsiDiskUtils.NonFixedDrives;
AllDriveTypes = FsiDiskUtils.AllDriveTypes;
DriveTypeStrings: array[TDriveType] of String = ('dtUnknown',
'dtNoRootDir',
'dtRemovable',
'dtFixed',
'dtRemote',
'dtCdrom',
'dtRamDisk',
'dtDisk',
'dtPart',
'dtDeviceMapper',
'dtMultipleDevices',
'dtLoop'
);
function GetBlockDeviceInfoRecs(Refresh: Boolean): TBlockDeviceInfoRecs;
function GetRemovableMountedDisks(Refresh: Boolean): TBlockDeviceInfoRecs;
implementation
type
TBlockDeviceInfoList = class(TStringList);
var
CachedBlockDeviceInfoRecs: TBlockDeviceInfoRecs = nil;
CachedBlockDeviceInfoRecsInitialized: Boolean = False;
procedure ParseBlockDeviceInfoList(Info: TBlockDeviceInfoList; out BlockDeviceInfoRecs: TBlockDeviceInfoRecs);
var
i: Integer;
jd: TJsonData;
jarr: TJsonArray;
jobj: TJsonObject;
//const
// BoolStr: Array[Boolean] of String = ('FALSE','TRUE');
begin
BlockDeviceInfoRecs := [];
if (Info.Count = 0) then
Exit;
try
jd := GetJson(Info.Text);
if (jd.FindPath('blockdevices') <> nil) and (jd.FindPath('blockdevices').JSONType = jtArray) then
begin
jarr := TJsonArray(jd.FindPath('blockdevices'));
SetLength(BlockDeviceInfoRecs, jarr.Count);
for i := 0 to jarr.Count - 1 do
begin
jobj := jarr.Objects[i];
BlockDeviceInfoRecs[i].NAME := jobj.FindPath('name').AsString;
//writeln(format('BlkInfoRecs[%d].NAME=%s',[i,BlockDeviceInfoRecs[i].NAME]));
if not jobj.FindPath('mountpoint').IsNull then
BlockDeviceInfoRecs[i].MountPoint := jobj.FindPath('mountpoint').AsString;
//writeln(format('BlkInfoRecs[%d].MountPoint=%s',[i,BlockDeviceInfoRecs[i].MOUNTPOINT]));
if not jobj.FindPath('label').IsNull then
BlockDeviceInfoRecs[i].&LABEL := jobj.FindPath('label').AsString;
//writeln(format('BlkInfoRecs[%d].&LABEL=%s',[i,BlockDeviceInfoRecs[i].&LABEL]));
if not jobj.FindPath('fstype').IsNull then
BlockDeviceInfoRecs[i].FSTYPE := jobj.FindPath('fstype').AsString;
//writeln(format('BlkInfoRecs[%d].FSTYPE=%s',[i,BlockDeviceInfoRecs[i].FSTYPE]));
if not jobj.FindPath('type').IsNull then
BlockDeviceInfoRecs[i].&TYPE := jobj.FindPath('type').AsString;
//writeln(format('BlkInfoRecs[%d].&TYPE=%s',[i,BlockDeviceInfoRecs[i].&TYPE]));
if not jobj.FindPath('parttype').IsNull then
BlockDeviceInfoRecs[i].PartTYPE := jobj.FindPath('parttype').AsString;
//writeln(format('BlkInfoRecs[%d].PARTTYPE=%s',[i,BlockDeviceInfoRecs[i].PARTTYPE]));
if not jobj.FindPath('uuid').IsNull then
BlockDeviceInfoRecs[i].UUID := jobj.FindPath('uuid').AsString;
//writeln(format('BlkInfoRecs[%d].UUID=%s',[i,BlockDeviceInfoRecs[i].UUID]));
if not jobj.GetPath('model').IsNull then
BlockDeviceInfoRecs[i].MODEL := jobj.FindPath('model').AsString;
//writeln(format('BlkInfoRecs[%d].MODEL=%s',[i,BlockDeviceInfoRecs[i].MODEL]));
if not jobj.FindPath('tran').IsNull then
BlockDeviceInfoRecs[i].TRAN := jobj.FindPath('tran').AsString;
//writeln(format('BlkInfoRecs[%d].TRAN=%s',[i,BlockDeviceInfoRecs[i].TRAN]));
BlockDeviceInfoRecs[i].RM := jobj.FindPath('rm').AsBoolean;
//writeln(format('BlkInfoRecs[%d].RM=%s',[i,BoolStr[BlockDeviceInfoRecs[i].RM]]));
BlockDeviceInfoRecs[i].SIZE := jobj.FindPath('size').AsInt64;
//writeln(format('BlkInfoRecs[%d].SIZE=%d',[i,BlockDeviceInfoRecs[i].SIZE]));
case BlockDeviceInfoRecs[i].&TYPE of
'part' : BlockDeviceInfoRecs[i].DeviceType := dtpart;
'disk' : BlockDeviceInfoRecs[i].DeviceType := dtDisk;
'rom' : BlockDeviceInfoRecs[i].DeviceType := dtCdRom;
'loop' : BlockDeviceInfoRecs[i].DeviceType := dtLoop;
'lvm', 'crypt', 'dmraid', 'mpath', 'path', 'dm' : BlockDeviceInfoRecs[i].DeviceType := dtDeviceMapper;
'md', 'linear', 'raid0', 'raid1', 'raid4', 'raid5', 'raid10', 'multipath' : BlockDeviceInfoRecs[i].DeviceType := dtMultipleDevices;
otherwise BlockDeviceInfoRecs[i].DeviceType := dtUnknown;
end;//case
//writeln;
end;
end;
finally
jd.Free;
end;
end;
procedure GetBlockDeviceInfoList(BlockDeviceInfoList: TBlockDeviceInfoList);
var
Proc: TProcess;
i: Integer;
begin
BlockDeviceInfoList.Clear;
Proc := TProcess.Create(nil);
//lsblk -l -e 7 -A -o NAME,MOUNTPOINT,LABEL,FSTYPE,TYPE,PARTTYPE,UUID,MODEL,TRAN,RM,SIZE
//lsblk -l -a -o NAME,MOUNTPOINT,LABEL,FSTYPE,TYPE,PARTTYPE,UUID,MODEL,TRAN,RM,SIZE
try
Proc.Executable := 'lsblk'; //ToDo: get this from path ??
Proc.Parameters.Add('-l'); //list
Proc.Parameters.Add('-J'); //as json
Proc.Parameters.Add('-b'); //size in bytes
//Proc.Parameters.Add('-e7'); //exclude RAM drives
Proc.Parameters.Add('-a');
Proc.Parameters.Add('-o');
Proc.Parameters.Add('NAME,MOUNTPOINT,LABEL,FSTYPE,TYPE,PARTTYPE,UUID,MODEL,TRAN,RM,SIZE');
Proc.Options := Proc.Options + [poWaitOnExit, poUsePipes];
Proc.Execute;
BlockDeviceInfoList.LoadFromStream(Proc.Output);
for i := BlockDeviceInfoList.Count - 1 downto 0 do
if (Trim(BlockDeviceInfoList[i])='') then
BlockDeviceInfoList.Delete(i);
finally
Proc.Free;
end;
end;
function GetBlockDeviceInfoRecs(Refresh: Boolean): TBlockDeviceInfoRecs;
var
BlockDeviceInfoList: TBlockDeviceInfoList;
begin
if (not Refresh) and CachedBlockDeviceInfoRecsInitialized then
Exit(CachedBlockDeviceInfoRecs);
Result := [];
BlockDeviceInfoList := TBlockDeviceInfoList.Create;
try
GetBlockDeviceInfoList(BlockDeviceInfoList);
ParseBlockDeviceInfoList(BlockDeviceInfoList, CachedBlockDeviceInfoRecs);
CachedBlockDeviceInfoRecsInitialized := True;
Result := CachedBlockDeviceInfoRecs;
finally
BlockDeviceInfoList.Free;
end;
end;
function GetRemovableMountedDisks(Refresh: Boolean): TBlockDeviceInfoRecs;
var
i: Integer;
begin
if Refresh then
GetBlockDeviceInfoRecs(True);
Result := [];
for i := 0 to Length(CachedBlockDeviceInfoRecs) - 1 do
begin
if (CachedBlockDeviceInfoRecs[i].RM) and (CachedBlockDeviceInfoRecs[i].MOUNTPOINT <> '') then
begin
SetLength(Result, Length(Result) + 1);
Result[Length(Result) - 1] := CachedBlockDeviceInfoRecs[i];
end;
end;
end;
initialization
finalization
CachedBlockDeviceInfoRecs := nil;
end.