@@ -31,8 +31,8 @@ public class GitPack : IDisposable
31
31
private readonly Func < FileStream > packStream ;
32
32
private readonly Lazy < FileStream > indexStream ;
33
33
private readonly GitPackCache cache ;
34
- private MemoryMappedFile packFile ;
35
- private MemoryMappedViewAccessor accessor ;
34
+ private MemoryMappedFile ? packFile = null ;
35
+ private MemoryMappedViewAccessor ? accessor = null ;
36
36
37
37
// Maps GitObjectIds to offets in the git pack.
38
38
private readonly Dictionary < GitObjectId , long > offsets = new Dictionary < GitObjectId , long > ( ) ;
@@ -98,8 +98,11 @@ public GitPack(GetObjectFromRepositoryDelegate getObjectFromRepositoryDelegate,
98
98
this . indexStream = indexStream ?? throw new ArgumentNullException ( nameof ( indexStream ) ) ;
99
99
this . cache = cache ?? new GitPackMemoryCache ( ) ;
100
100
101
- this . packFile = MemoryMappedFile . CreateFromFile ( this . packStream ( ) , mapName : null , 0 , MemoryMappedFileAccess . Read , HandleInheritability . None , leaveOpen : false ) ;
102
- this . accessor = this . packFile . CreateViewAccessor ( 0 , 0 , MemoryMappedFileAccess . Read ) ;
101
+ if ( IntPtr . Size > 4 )
102
+ {
103
+ this . packFile = MemoryMappedFile . CreateFromFile ( this . packStream ( ) , mapName : null , 0 , MemoryMappedFileAccess . Read , HandleInheritability . None , leaveOpen : false ) ;
104
+ this . accessor = this . packFile . CreateViewAccessor ( 0 , 0 , MemoryMappedFileAccess . Read ) ;
105
+ }
103
106
}
104
107
105
108
/// <summary>
@@ -202,7 +205,17 @@ public Stream GetObject(long offset, string objectType)
202
205
}
203
206
204
207
var packStream = this . GetPackStream ( ) ;
205
- Stream objectStream = GitPackReader . GetObject ( this , packStream , offset , objectType , packObjectType ) ;
208
+ Stream objectStream ;
209
+
210
+ try
211
+ {
212
+ objectStream = GitPackReader . GetObject ( this , packStream , offset , objectType , packObjectType ) ;
213
+ }
214
+ catch
215
+ {
216
+ packStream . Dispose ( ) ;
217
+ throw ;
218
+ }
206
219
207
220
return this . cache . Add ( offset , objectStream ) ;
208
221
}
@@ -240,8 +253,8 @@ public void Dispose()
240
253
this . indexReader . Value . Dispose ( ) ;
241
254
}
242
255
243
- this . accessor . Dispose ( ) ;
244
- this . packFile . Dispose ( ) ;
256
+ this . accessor ? . Dispose ( ) ;
257
+ this . packFile ? . Dispose ( ) ;
245
258
this . cache . Dispose ( ) ;
246
259
}
247
260
@@ -265,7 +278,17 @@ public void Dispose()
265
278
266
279
private Stream GetPackStream ( )
267
280
{
268
- return new MemoryMappedStream ( this . accessor ) ;
281
+ // On 64-bit processes, we can use Memory Mapped Streams (the address space
282
+ // will be large enough to map the entire packfile). On 32-bit processes,
283
+ // we directly access the underlying stream.
284
+ if ( IntPtr . Size > 4 )
285
+ {
286
+ return new MemoryMappedStream ( this . accessor ) ;
287
+ }
288
+ else
289
+ {
290
+ return this . packStream ( ) ;
291
+ }
269
292
}
270
293
271
294
private GitPackIndexReader OpenIndex ( )
0 commit comments