ObjFW  Diff

Differences From Artifact [ea0a6a975e]:

To Artifact [87a6169ed1]:


11
12
13
14
15
16
17


18
19
20
21
22
23
24
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26







+
+







 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#define _LARGEFILE64_SOURCE

#include <assert.h>
#include <errno.h>

#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#include "unistd_wrapper.h"
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
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







-
-
+
+





-
+


+
+









+
+
-
+




-
+







#ifndef O_EXLOCK
# define O_EXLOCK 0
#endif

#ifndef OF_AMIGAOS
# define closeHandle(h) close(h)
#else
static struct of_file_handle {
	of_file_handle_t previous, next;
static struct _OFFileHandle {
	struct _OFFileHandle *previous, *next;
	BPTR handle;
	bool append;
} *firstHandle = NULL;

static void
closeHandle(of_file_handle_t handle)
closeHandle(OFFileHandle handle)
{
	Close(handle->handle);

	Forbid();

	if (handle->previous != NULL)
		handle->previous->next = handle->next;
	if (handle->next != NULL)
		handle->next->previous = handle->previous;

	if (firstHandle == handle)
		firstHandle = handle->next;

	Permit();

	free(handle);
	OFFreeMemory(handle);
}

OF_DESTRUCTOR()
{
	for (of_file_handle_t iter = firstHandle; iter != NULL;
	for (OFFileHandle iter = firstHandle; iter != NULL;
	    iter = iter->next)
		Close(iter->handle);
}
#endif

#ifndef OF_AMIGAOS
static int
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
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







-
+
-

-
+
-


-
+
-

-
+
-


-
+









-
+
-

-
+







#ifdef OF_NINTENDO_DS
	if (!nitroFSInit(NULL))
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
#endif
}

+ (instancetype)fileWithPath: (OFString *)path
+ (instancetype)fileWithPath: (OFString *)path mode: (OFString *)mode
			mode: (OFString *)mode
{
	return [[[self alloc] initWithPath: path
	return [[[self alloc] initWithPath: path mode: mode] autorelease];
				      mode: mode] autorelease];
}

+ (instancetype)fileWithURL: (OFURL *)URL
+ (instancetype)fileWithURL: (OFURL *)URL mode: (OFString *)mode
		       mode: (OFString *)mode
{
	return [[[self alloc] initWithURL: URL
	return [[[self alloc] initWithURL: URL mode: mode] autorelease];
				     mode: mode] autorelease];
}

+ (instancetype)fileWithHandle: (of_file_handle_t)handle
+ (instancetype)fileWithHandle: (OFFileHandle)handle
{
	return [[[self alloc] initWithHandle: handle] autorelease];
}

- (instancetype)init
{
	OF_INVALID_INIT_METHOD
}

- (instancetype)initWithPath: (OFString *)path
- (instancetype)initWithPath: (OFString *)path mode: (OFString *)mode
			mode: (OFString *)mode
{
	of_file_handle_t handle;
	OFFileHandle handle;

	@try {
		void *pool = objc_autoreleasePoolPush();
		int flags;

#ifndef OF_AMIGAOS
		if ((flags = parseMode(mode.UTF8String)) == -1)
237
238
239
240
241
242
243
244

245
246
247
248
249
250
251
238
239
240
241
242
243
244

245
246
247
248
249
250
251
252







-
+








		if (handle == -1)
			@throw [OFOpenItemFailedException
			    exceptionWithPath: path
					 mode: mode
					errNo: errno];
#else
		handle = of_alloc(1, sizeof(*handle));
		handle = OFAllocMemory(1, sizeof(*handle));
		@try {
			if ((flags = parseMode(mode.UTF8String,
			    &handle->append)) == -1)
				@throw [OFInvalidArgumentException exception];

			if ((handle->handle = Open([path cStringWithEncoding:
			    [OFLocale encoding]], flags)) == 0) {
290
291
292
293
294
295
296


297
298
299
300
301
302
303
304


305
306

307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327

328
329
330
331
332
333
334
335
336
337
338
339
340

341
342
343
344
345
346
347
348

349
350
351
352
353
354
355
356
357
358
359

360
361
362
363
364
365

366
367
368
369
370

371
372
373
374
375
376
377
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310

311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331

332

333
334
335
336
337
338
339
340
341
342
343

344

345
346
347
348
349
350

351
352
353
354
355
356
357
358
359
360
361

362
363
364
365
366
367

368

369
370
371

372
373
374
375
376
377
378
379







+
+








+
+

-
+




















-
+
-











-
+
-






-
+










-
+





-
+
-



-
+







					Close(handle->handle);
					@throw [OFOpenItemFailedException
					    exceptionWithPath: path
							 mode: mode
							errNo: EIO];
				}
			}

			Forbid();

			handle->previous = NULL;
			handle->next = firstHandle;

			if (firstHandle != NULL)
				firstHandle->previous = handle;

			firstHandle = handle;

			Permit();
		} @catch (id e) {
			free(handle);
			OFFreeMemory(handle);
			@throw e;
		}
#endif

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	@try {
		self = [self initWithHandle: handle];
	} @catch (id e) {
		closeHandle(handle);
		@throw e;
	}

	return self;
}

- (instancetype)initWithURL: (OFURL *)URL
- (instancetype)initWithURL: (OFURL *)URL mode: (OFString *)mode
		       mode: (OFString *)mode
{
	void *pool = objc_autoreleasePoolPush();
	OFString *fileSystemRepresentation;

	@try {
		fileSystemRepresentation = URL.fileSystemRepresentation;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithPath: fileSystemRepresentation
	self = [self initWithPath: fileSystemRepresentation mode: mode];
			     mode: mode];

	objc_autoreleasePoolPop(pool);

	return self;
}

- (instancetype)initWithHandle: (of_file_handle_t)handle
- (instancetype)initWithHandle: (OFFileHandle)handle
{
	self = [super init];

	_handle = handle;

	return self;
}

- (bool)lowlevelIsAtEndOfStream
{
	if (_handle == OF_INVALID_FILE_HANDLE)
	if (_handle == OFInvalidFileHandle)
		@throw [OFNotOpenException exceptionWithObject: self];

	return _atEndOfStream;
}

- (size_t)lowlevelReadIntoBuffer: (void *)buffer
- (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length
			  length: (size_t)length
{
	ssize_t ret;

	if (_handle == OF_INVALID_FILE_HANDLE)
	if (_handle == OFInvalidFileHandle)
		@throw [OFNotOpenException exceptionWithObject: self];

#if defined(OF_WINDOWS)
	if (length > UINT_MAX)
		@throw [OFOutOfRangeException exception];

	if ((ret = read(_handle, buffer, (unsigned int)length)) < 0)
395
396
397
398
399
400
401
402

403
404
405

406
407
408
409
410
411
412
397
398
399
400
401
402
403

404

405

406
407
408
409
410
411
412
413







-
+
-

-
+








	if (ret == 0)
		_atEndOfStream = true;

	return ret;
}

- (size_t)lowlevelWriteBuffer: (const void *)buffer
- (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length
		       length: (size_t)length
{
	if (_handle == OF_INVALID_FILE_HANDLE)
	if (_handle == OFInvalidFileHandle)
		@throw [OFNotOpenException exceptionWithObject: self];

#if defined(OF_WINDOWS)
	int bytesWritten;

	if (length > INT_MAX)
		@throw [OFOutOfRangeException exception];
454
455
456
457
458
459
460
461
462

463
464

465
466

467
468
469
470
471
472
473
455
456
457
458
459
460
461


462
463

464
465

466
467
468
469
470
471
472
473







-
-
+

-
+

-
+







						      bytesWritten: 0
							     errNo: errno];
#endif

	return (size_t)bytesWritten;
}

- (of_offset_t)lowlevelSeekToOffset: (of_offset_t)offset
			     whence: (int)whence
- (OFFileOffset)lowlevelSeekToOffset: (OFFileOffset)offset whence: (int)whence
{
	of_offset_t ret;
	OFFileOffset ret;

	if (_handle == OF_INVALID_FILE_HANDLE)
	if (_handle == OFInvalidFileHandle)
		@throw [OFNotOpenException exceptionWithObject: self];

#ifndef OF_AMIGAOS
# if defined(OF_WINDOWS)
	ret = _lseeki64(_handle, offset, whence);
# elif defined(HAVE_LSEEK64)
	ret = lseek64(_handle, offset, whence);
529
530
531
532
533
534
535
536

537
538
539
540

541
542
543
544
545
546
547

548
549
550
551
552
529
530
531
532
533
534
535

536
537
538
539

540
541
542
543
544
545
546

547
548
549
550
551
552







-
+



-
+






-
+





{
	return _handle;
}
#endif

- (void)close
{
	if (_handle == OF_INVALID_FILE_HANDLE)
	if (_handle == OFInvalidFileHandle)
		@throw [OFNotOpenException exceptionWithObject: self];

	closeHandle(_handle);
	_handle = OF_INVALID_FILE_HANDLE;
	_handle = OFInvalidFileHandle;

	[super close];
}

- (void)dealloc
{
	if (_handle != OF_INVALID_FILE_HANDLE)
	if (_handle != OFInvalidFileHandle)
		[self close];

	[super dealloc];
}
@end