File: XADTarSparseHandle.m

package info (click to toggle)
unar 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,664 kB
  • sloc: ansic: 52,939; objc: 39,563; cpp: 4,074; makefile: 99; perl: 10
file content (272 lines) | stat: -rw-r--r-- 8,259 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#import "XADTarSparseHandle.h"
#import "Realloc.h"

@implementation XADTarSparseHandle

// Make a new sparse handle by wrapping around another CSHandle
-(id)initWithHandle:(CSHandle *)handle size:(off_t)size
{
	if( (self = [super initWithName:[handle name]]) )
	{
		parent = [handle retain];
		regions = malloc( sizeof( XADTarSparseRegion ) );
		regions[ 0 ].nextRegion = -1;
		regions[ 0 ].size = [parent fileSize];
		regions[ 0 ].offset = 0;
		regions[ 0 ].hasData = YES;
		regions[ 0 ].dataOffset = 0;
		
		numRegions = 1;
		currentOffset = 0;
		currentRegion = 0;
		realFileSize = size;
		
	}
	return( self );
}

// Copy constructor
-(id)initAsCopyOf:(XADTarSparseHandle *)other
{
	if( (self = [super initAsCopyOf:other]) )
	{
		parent = [other->parent copy];
		numRegions = other->numRegions;
		regions = malloc( sizeof( XADTarSparseRegion ) * numRegions );
		memcpy( regions, other->regions, sizeof( XADTarSparseRegion ) * numRegions );
		currentOffset = other->currentOffset;
		currentRegion = other->currentRegion;
		realFileSize = other->realFileSize;
	}
	return( self );
}

// Free all regions, allow parent release.
-(void)dealloc
{
	free( regions );
	[parent release];
	[super dealloc];
}

// Find which region some offset is in.
-(int)regionIndexForOffset:(off_t)offset
{
	// That is not a valid offset!
	if( offset >= [self fileSize] ) {
		[self _raiseEOF];
	}

	// Go through all regions until one fits.
	int index = 0;
	while(
		index < numRegions &&
		(offset < regions[ index ].offset ||
		offset >= regions[ index ].offset + regions[ index ].size)
	)
	{
		index++;
	}

	return( index );
}

// Add a new sparse region.
// You can only ever add a region entirely inside of another region.
-(void)addSparseRegionFrom:(off_t)start length:(off_t)length
{
	for( int i = 0; i < numRegions; i++ ) {
	//	fprintf( stderr, "%d: %d->%d (%s - %d) => %d\n", i, regions[ i ].offset, regions[ i ].size, (regions[ i ].hasData ? "has data" : "no data"), regions[ i ].dataOffset, regions[ i ].nextRegion );
	}

	int inRegion = [self regionIndexForOffset:start];
	//fprintf( stderr, "In region %d\n", inRegion );
	if( start + length >= regions[ inRegion ].offset + regions[ inRegion ].size )
	{
		//fprintf( stderr, "s: %d; l: %d; iro:%d; irs: %d; s1: %d, s2: %d (i: %d)\n", start, length, regions[ inRegion ].offset, regions[ inRegion ].size, start + length, regions[ inRegion ].offset + regions[ inRegion ].size, [self regionIndexForOffset:start] );
		
		[NSException raise:NSInvalidArgumentException format:@"Attempted to add sparse region over region boundary."];
	}

	// Make two new regions.
	regions = Realloc( regions, sizeof( XADTarSparseRegion ) * ( numRegions + 2 ) );

	// Start processing at the end.
	regions[ numRegions + 1 ].offset = start + length;
	regions[ numRegions + 1 ].dataOffset = start - regions[ inRegion ].offset;
	regions[ numRegions + 1 ].size = regions[ inRegion ].size - regions[ numRegions + 1 ].dataOffset;
	regions[ numRegions + 1 ].nextRegion = regions[ inRegion ].nextRegion;
	regions[ numRegions + 1 ].hasData = YES;

	// Sparse region being added.
	regions[ numRegions ].offset = start;
	regions[ numRegions ].size = length;
	regions[ numRegions ].hasData = NO;
	regions[ numRegions ].nextRegion = numRegions + 1;

	// Refumble old region.
	regions[ inRegion ].size = regions[ inRegion ].size - regions[ numRegions + 1 ].size;
	regions[ inRegion ].nextRegion = numRegions;

	// Current region might have changed by this.
	currentRegion = [self regionIndexForOffset:currentOffset];

	numRegions += 2;

	//fprintf( stderr, "Adding section worked.\n" );
}

// Add a new sparse region as last region of a file.
-(void)addFinalSparseRegionEndingAt:(off_t)regionEndsAt
{
	for( int i = 0; i < numRegions; i++ ) {
// 		fprintf( stderr, "%d: %d->%d (%s - %d) => %d\n", i, regions[ i ].offset, regions[ i ].size, (regions[ i ].hasData ? "has data" : "no data"), regions[ i ].dataOffset, regions[ i ].nextRegion );
	}

	XADTarSparseRegion inRegion = regions[ [self regionIndexForOffset:([self fileSize] - 1)] ];
	
	// Make a new region.
	regions = Realloc( regions, sizeof( XADTarSparseRegion ) * ( numRegions + 1 ) );

	// Figure out the current size.
	off_t sizeBeforeAdding = 0;
	for( int i = 0; i < numRegions; i++ )
	{
		sizeBeforeAdding += regions[ i ].size;
	}

	// Sparse region being added.
	regions[ numRegions ].offset = sizeBeforeAdding;
	regions[ numRegions ].size = regionEndsAt - sizeBeforeAdding;
	regions[ numRegions ].hasData = NO;
	regions[ numRegions ].nextRegion = -1;

	// Refumble old region.
	inRegion.nextRegion = numRegions;

	// Current region might have changed by this.
	currentRegion = [self regionIndexForOffset:currentOffset];

	numRegions++;

	for( int i = 0; i < numRegions; i++ ) {
// 		fprintf( stderr, "%d: %d->%d (%s - %d) => %d\n", i, regions[ i ].offset, regions[ i ].size, (regions[ i ].hasData ? "has data" : "no data"), regions[ i ].dataOffset, regions[ i ].nextRegion );
	}

// 	fprintf( stderr, "Adding final section worked.\n" );
}

// Set the only region to "empty".
-(void)setSingleEmptySparseRegion
{
	regions[ numRegions ].offset = 0;
	regions[ numRegions ].size = [self fileSize];
	regions[ numRegions ].hasData = NO;
	regions[ numRegions ].nextRegion = -1;

	currentRegion = [self regionIndexForOffset:currentOffset];

	for( int i = 0; i < numRegions; i++ ) {
// 		fprintf( stderr, "%d: %d->%d (%s - %d) => %d\n", i, regions[ i ].offset, regions[ i ].size, (regions[ i ].hasData ? "has data" : "no data"), regions[ i ].dataOffset, regions[ i ].nextRegion );
	}

// 	fprintf( stderr, "Setting single section worked.\n" );
}

// Return real file size.
-(off_t)fileSize
{
	return( realFileSize );
}

// Just return our saved offset.
-(off_t)offsetInFile
{
	return( currentOffset );
}

-(BOOL)atEndOfFile
{
	return( currentOffset == [self fileSize] );
}

// Just set the internal offset.
// Seeking in parent handle is done on demand.
-(void)seekToFileOffset:(off_t)offs
{
	currentOffset = offs;
}

// The same as for seekToFileOffset applies.
-(void)seekToEndOfFile
{
	currentOffset = [self fileSize];
}

// Return data from parent handle or \0 in sparse regions.
// I'd heartily recommend not adding more sparse regions after you started
// reading because if you do things will break.
-(int)readAtMost:(int)num toBuffer:(void *)buffer
{
// 	fprintf( stderr, "Readatmost %d\n", num );
	// Do not read further than allowed.
	if( currentOffset + num > [self fileSize] )
	{
//		fprintf( stderr, "Oops: %d plus %d = %d > %d\n", currentOffset, num, currentOffset + num, [self fileSize] );
//		[self _raiseEOF];
	}

	// Seek if we have to.
	if( regions[ currentRegion ].hasData && regions[ currentRegion ].dataOffset != [parent offsetInFile] )
	{
	//	fprintf( stderr, "Seeking: %d.\n", regions[ currentRegion ].dataOffset );
		[parent seekToFileOffset:regions[ currentRegion ].dataOffset];
	}
	
	// Fill the buffer with data.
	memset( buffer, 0, num );
	long positionInBuffer = 0;
	long stopAtSize = [self fileSize];
	off_t positionInRegion = regions[ currentRegion ].offset - currentOffset;
	off_t dataLeftInRegion = regions[ currentRegion ].size - positionInRegion;
	while( positionInBuffer + dataLeftInRegion < num && currentOffset < stopAtSize )
	{
// 		fprintf( stderr, "Reading: %d really %d.\n", positionInBuffer, currentOffset );
		if( regions[ currentOffset ].hasData )
		{
			[parent readAtMost:dataLeftInRegion toBuffer:buffer];
		}
		currentRegion = regions[ currentRegion ].nextRegion;
		positionInRegion = 0;
		dataLeftInRegion = regions[ currentRegion ].size;
		currentOffset += dataLeftInRegion;
	}

	// Read the last segment of data, if required.
	if( regions[ currentRegion ].hasData )
	{
		[parent readAtMost:(num - positionInBuffer) toBuffer:buffer];
		currentOffset += num - positionInBuffer;
	}

	// If we in a sparse region now, push the file offset up.
	if( currentOffset < [self fileSize] ) {
		long remaining = [self fileSize] - currentOffset;
		if( positionInBuffer + remaining <= num )
		{
			positionInBuffer += remaining;
			currentOffset += remaining;
		}
		else
		{
			currentOffset += num - positionInBuffer;
			positionInBuffer = num;
		}
	}
	
// 	fprintf( stderr, "Readatmost okay, read %d.\n", positionInBuffer );

	return( positionInBuffer );
}

@end