【列表5.19】枚举一个进程的所有文件。
type
// Process file enumeration callback.
TProcFileEnumCallback = procedure (pid: __pid_t;
fd: Integer) of object;
{
ProcEnumFiles - Enumerate the files used by the process.
If the calling process does not have permissions to
access the fd directory for this process, no FDs will
be enumerated.
}
procedure ProcEnumFiles (pid: __pid_t; cb: TProcFileEnumCallback);
var
sr : TSearchRec;
findRslt : Integer;
sFilename : String;
fd : Integer;
begin
sFilename := Format ('/proc/%d/fd/*,, [pid]);
findRslt := FindFirst (sFilename, faAnyFile, sr);
try
while findRslt = 0 do
begin
try
fd := StrToInt (sr. Name);
except
fd := -1;
end;
if fd -1 then
cb (pid, fd);
findRslt := FindNext (sr);
end;
finally
FindClose (sr);
end;
end;
// ProcGetFD - return the contents of the link for file fd.
function ProcGetFD (pid: __pid_t; fd: Integer): String;
begin
Result := ProcReadLink (pid, Format ('fd/%d,[fd]));
end;