Skip to content

Commit c42d1d1

Browse files
committed
ADD: add the retry mechanism for TDarwinIOVolumns.createVolumns()
1 parent 5e29809 commit c42d1d1

1 file changed

Lines changed: 56 additions & 27 deletions

File tree

src/platform/unix/darwin/udarwinio.pas

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface
1717
uses
1818
Classes, SysUtils,
1919
MacOSAll, CocoaAll, CocoaUtils,
20-
uMyDarwin;
20+
uMyDarwin, uLog;
2121

2222
type
2323
natural_t = UInt32;
@@ -114,32 +114,24 @@ function TDarwinIOVolumns.createVolumns: NSArray;
114114
ioServiceObject: io_object_t;
115115
ioVolumnObject: io_object_t;
116116
ret: integer;
117-
volumnProperties: NSMutableDictionary;
118117
volumns: NSMutableArray;
119-
120-
bsdName: CFTypeRef;
121-
groupUUID: CFTypeRef;
122-
roleValue: CFTypeRef;
123-
removable: CFTypeRef;
124-
begin
125-
Result:= nil;
126-
127-
ret:= IOServiceGetMatchingServices(
128-
kIOMasterPortDefault,
129-
IOServiceMatching( 'IOMediaBSDClient' ),
130-
@ioIterator );
131-
if ret <> 0 then
132-
Exit;
133-
134-
volumns:= NSMutableArray.new;
135-
136-
repeat
118+
hasMore: Boolean;
119+
120+
function addOneVolumn: Boolean;
121+
var
122+
volumnProperties: NSMutableDictionary;
123+
bsdName: CFTypeRef;
124+
groupUUID: CFTypeRef;
125+
roleValue: CFTypeRef;
126+
removable: CFTypeRef;
127+
begin
128+
Result:= False;
137129
ioServiceObject:= IOIteratorNext( ioIterator );
138130
if ioServiceObject = 0 then
139-
break;
131+
Exit;
140132
ret:= IORegistryEntryGetParentEntry( ioServiceObject, kIOServicePlane, @ioVolumnObject );
141133
if ret <> 0 then
142-
break;
134+
Exit;
143135
volumnProperties:= NSMutableDictionary.new;
144136
bsdName:= IORegistryEntryCreateCFProperty( ioVolumnObject, BsdName_KEY, kCFAllocatorDefault, 0 );
145137
volumnProperties.setValue_forKey( bsdName , BsdName_KEY );
@@ -161,12 +153,36 @@ function TDarwinIOVolumns.createVolumns: NSArray;
161153
if Assigned( removable ) then
162154
CFRelease( removable );
163155

164-
IOObjectRelease( ioVolumnObject );
165-
IOObjectRelease( ioServiceObject );
166-
until False;
156+
Result:= True;
157+
end;
167158

168-
IOObjectRelease( ioIterator );
159+
begin
160+
volumns:= NSMutableArray.new;
169161
Result:= volumns;
162+
163+
ret:= IOServiceGetMatchingServices(
164+
kIOMasterPortDefault,
165+
IOServiceMatching( 'IOMediaBSDClient' ),
166+
@ioIterator );
167+
if ret <> 0 then
168+
Exit;
169+
170+
repeat
171+
ioServiceObject:= 0;
172+
ioVolumnObject:= 0;
173+
ret:= 0;
174+
175+
hasMore:= addOneVolumn();
176+
if ret <> 0 then
177+
volumns.removeAllObjects;
178+
179+
if ioVolumnObject <> 0 then
180+
IOObjectRelease( ioVolumnObject );
181+
if ioServiceObject <> 0 then
182+
IOObjectRelease( ioServiceObject );
183+
until NOT hasMore;
184+
185+
IOObjectRelease( ioIterator );
170186
end;
171187

172188
function TDarwinIOVolumns.getDeviceID(const fs: PDarwinStatfs): NSString;
@@ -246,8 +262,21 @@ function TDarwinIOVolumns.getStatfsByDeviceID(const deviceID: NSString
246262

247263
constructor TDarwinIOVolumns.Create(const pStatfs: PDarwinStatfs;
248264
const statfsCount: Integer);
265+
var
266+
i: Integer;
249267
begin
250-
_volumns:= createVolumns;
268+
for i:= 1 to 10 do begin
269+
_volumns:= createVolumns;
270+
if _volumns.count > 0 then
271+
break;
272+
LogWrite( 'error in TDarwinIOVolumns.createVolumns(), times=' + IntToStr(i), lmtError );
273+
if i < 10 then begin
274+
_volumns.release;
275+
_volumns:= nil;
276+
sleep( 3000 );
277+
end;
278+
end;
279+
LogWrite( 'TDarwinIOVolumns.createVolumns() Result:'#13 + _volumns.description.utf8String );
251280
_pStatfs:= pStatfs;
252281
_statfsCount:= statfsCount;
253282
end;

0 commit comments

Comments
 (0)