Changeset 28 for trunk/UPhysDrive.pas


Ignore:
Timestamp:
Apr 7, 2016, 7:01:24 PM (8 years ago)
Author:
chronos
Message:
  • Modified: Dynamically calculate sector count from drive size and sector size.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UPhysDrive.pas

    r21 r28  
    66
    77uses
    8   Classes, SysUtils, Contnrs, UFindFile, UCommon
     8  Classes, SysUtils, Contnrs, UFindFile, UCommon, Math
    99  {$IFDEF Windows},ActiveX,ComObj,Variants
    1010  {$ENDIF};
     
    1616
    1717  TDriveInfo = class
     18  private
     19    FSectorSize: Integer;
     20    FSectorCount: Int64;
     21    FSize: Int64;
     22    function GetSectorCount: Int64;
     23    procedure SetSectorSize(AValue: Integer);
     24    procedure SetSize(AValue: Int64);
     25  public
    1826    Kind: TDriveKind;
    1927    Model: string;
    20     Size: Int64;
    21     SectorCount: Int64;
    22     SectorSize: Integer;
    2328    Path: string;
     29    constructor Create;
    2430    procedure Assign(Source: TDriveInfo);
     31    property SectorSize: Integer read FSectorSize write SetSectorSize;
     32    property SectorCount: Int64 read GetSectorCount;
     33    property Size: Int64 read FSize write SetSize;
    2534  end;
    2635
     
    4251  UPrefixMultiplier;
    4352
     53resourcestring
     54  SMinSectorSize = 'Minimum sector size is 1';
     55
    4456{ TDriveInfo }
     57
     58procedure TDriveInfo.SetSectorSize(AValue: Integer);
     59begin
     60  if FSectorSize = AValue then Exit;
     61  if Avalue < 1 then
     62    raise Exception.Create(SMinSectorSize);
     63  FSectorSize := AValue;
     64  FSectorCount := Ceil(FSize / FSectorSize);
     65end;
     66
     67function TDriveInfo.GetSectorCount: Int64;
     68begin
     69  Result := Ceil(FSize / FSectorSize);
     70end;
     71
     72procedure TDriveInfo.SetSize(AValue: Int64);
     73begin
     74  if FSize=AValue then Exit;
     75  FSize:=AValue;
     76end;
     77
     78constructor TDriveInfo.Create;
     79begin
     80  FSectorSize := 1;
     81end;
    4582
    4683procedure TDriveInfo.Assign(Source: TDriveInfo);
     
    4986  Model := Source.Model;
    5087  Size := Source.Size;
    51   SectorCount := Source.SectorCount;
     88  FSectorCount := Source.FSectorCount;
    5289  SectorSize := Source.SectorSize;
    5390end;
     
    108145      NewDriveInfo.Model := Trim(LoadFileToStr(List[I] + '/device/model'));
    109146      NewDriveInfo.SectorSize := StrToInt(Trim(LoadFileToStr(List[I] + '/queue/physical_block_size')));
    110       NewDriveInfo.SectorCount := StrToInt(Trim(LoadFileToStr(List[I] + '/size'))) * 512 div NewDriveInfo.SectorSize;
    111       NewDriveInfo.Size := NewDriveInfo.SectorCount * NewDriveInfo.SectorSize;
     147      NewDriveInfo.Size := StrToInt(Trim(LoadFileToStr(List[I] + '/size'))) * 512;
    112148      NewDriveInfo.Path := '/dev/' + ExtractFileName(List[I]);
    113149      Add(NewDriveInfo);
     
    123159  NewDriveInfo.Model := 'File';
    124160  NewDriveInfo.SectorSize := 4096;
    125   NewDriveInfo.SectorCount := 0;
    126161  NewDriveInfo.Size := 0;
    127162  NewDriveInfo.Path := '';
Note: See TracChangeset for help on using the changeset viewer.