Recent

Author Topic: Problem reading out the messages of fdisk -l  (Read 3808 times)

Amnon82

  • New Member
  • *
  • Posts: 37
Problem reading out the messages of fdisk -l
« on: March 12, 2007, 07:53:07 pm »
I did a button with followed code:

Code: [Select]
procedure TForm1.Button3Click(Sender: TObject);
var
  S: TStringList;
  M: TMemoryStream;
  P: TProcess;
  n: LongInt;
  BytesRead: LongInt;
  LZumZerlegen: String;
  LErgebnis: TStringlist;
  I, i1, i2 : integer ;
begin

    combobox1.clear;
    combobox2.clear;

    listbox1.clear;
    listbox2.clear;
   // n := 0;
   // listbox1.clear;
   // listbox2.clear;
   
    combobox1.text := 'updating ... ';
    combobox2.text :='updating ... ';

  memo2.lines.add(DateToStr(Date) + '-' +TimeToStr(Time) + ' - get HDD table') ;
 
  // We cannot use poWaitOnExit here since we don't
  // know the size of the output. On Linux the size of the
  // output pipe is 2 kB. If the output data is more, we
  // need to read the data. This isn't possible since we are
  // waiting. So we get a deadlock here.
  //
  // A temp Memorystream is used to buffer the output
  M := TMemoryStream.Create;
  BytesRead := 0;
 
    form1.memo2.lines.add('-- create process --');

  P := TProcess.Create(nil);
  P.CommandLine := 'fdisk -l';
  P.Options := [poUsePipes];
  form1.memo2.lines.add('-- executing --');
  P.Execute;
  while P.Running= true do
  begin
    sleep (250);
    Application.ProcessMessages;
     // make sure we have room
     form1.memo2.lines.add('-- make sure we have room --');
    M.SetSize(BytesRead + READ_BYTES);

    // try reading it
   form1.memo2.lines.add('-- try reading it --');
    n := P.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
    if n > 0
    then begin
       form1.memo2.lines.add('-- n > 0 --');
       Inc(BytesRead, n);
      Write('.')
    end
    else begin
      // no data, wait 100 ms
      form1.memo2.lines.add('-- no data --');
      Sleep(100);
    end;
  end;
 
  // read last part
  repeat
    // make sure we have room
    M.SetSize(BytesRead + READ_BYTES);
    // try reading it
     form1.memo2.lines.add('-- try reading it 2 --');
    n := P.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
    if n > 0
    then begin
           form1.memo2.lines.add('-- n > 0 (2) --');
      Inc(BytesRead, n);
      Write('.');
    end;
  until n <= 0;
 
  if BytesRead > 0 then WriteLn;
  M.SetSize(BytesRead);
  form1.memo2.lines.add('-- executed --');

  S := TStringList.Create;
  S.LoadFromStream(M);
  form1.memo2.lines.add('-- linecount = ' + inttostr(S.Count) + ' --');
  for n := 0 to S.Count - 1 do
  begin
   form1. listbox2.items.add(S[n]);
  end;
 
  form1.memo2.lines.add('-- end --');
 

  S.clear;
  M.clear;
 
  S.Free;
  P.Free;
  M.Free;
 
  memo2.lines.add(DateToStr(Date) + '-' +TimeToStr(Time) + ' - readout HDD table') ;
  for i := 0 to listbox2.items.count -1 do
  begin
    if (Pos('dev/', listbox2.items[i]) > 0) then
    begin
    if (Pos(':', listbox2.items[i]) > 0) or (Pos('xtended', listbox2.items[i]) > 0)
    then
    begin
    // nothing
    end
    else
    begin
    listbox1.items.add(listbox2.items[i]);
    end;
    end;
    end;
   
    for i1 := 0 to listbox1.items.count -1 do
    begin
      LZumZerlegen := listbox1.items[i1];
      LErgebnis := TStringlist.Create;
      splitString(LZumZerlegen, ' ', LErgebnis);
       if LErgebnis[LErgebnis.count -1] = 'swap' then
       begin
       combobox2.items.add(LErgebnis[0] + ' - ' + LErgebnis[LErgebnis.count -1]);
       end
       else
       begin
        combobox1.items.add(LErgebnis[0] + ' - ' + LErgebnis[LErgebnis.count -1]);
        end;
       
       // listbox3.items.add (LErgebnis[0] + ' - ' + LErgebnis[LErgebnis.count -1]);
      LErgebnis.Free;
    end;
   
    if combobox1.itemindex = -1 then combobox1.itemindex := 0;
    if combobox2.itemindex = -1 then combobox2.itemindex := 0;
   
    memo2.lines.add(DateToStr(Date) + '-' +TimeToStr(Time) + ' - get HDD table - done') ;
    button1.enabled := true;
end;      



When I click it the first time I get this in the log:

Code: [Select]
12-3-07-19:47 - Installer started
12-3-07-19:47 - get HDD table
-- create process --
-- executing --
-- make sure we have room --
-- try reading it --
-- n > 0 --
-- make sure we have room --
-- try reading it --
-- no data --
-- try reading it 2 --
-- executed --
-- linecount = 21 --
-- end --
12-3-07-19:47 - readout HDD table
12-3-07-19:47 - get HDD table - done


But when I click the button the second time I only get this in the log:

Code: [Select]
12-3-07-19:47 - get HDD table
12-3-07-19:47 - readout HDD table
12-3-07-19:47 - get HDD table - done


My question is now: why ???

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Problem reading out the messages of fdisk -l
« Reply #1 on: March 12, 2007, 08:42:18 pm »
Quote from: "Amnon82"
My question is now: why ???


Lazarus has modern debugging. Use it to find out. On the text editor you can click on the grey area on the left to set break points. Set a break point on that function and run your application.

When the app stops executing at your breakpoint, you can use the Run and Step buttons to control it´s execution. You can also move the mouse over your variables on the text editor, so a hint with their value will appear.

This way you can debug your app, and find out why things happen the way they do.

 

TinyPortal © 2005-2018