ObjFW  Check-in [b4ebcc74b1]

Overview
Comment:OFFile: Check arguments for nil.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b4ebcc74b12ad205cf14970856e7ecaf9ce9e592a4563938e57bb51a3a19ad4c
User & Date: js on 2013-07-08 22:20:26
Other Links: manifest | tags
Context
2013-07-09
00:20
Add forwardingTargetForSelector: for ARM/ELF. check-in: 27daad1290 user: js tags: trunk
2013-07-08
22:20
OFFile: Check arguments for nil. check-in: b4ebcc74b1 user: js tags: trunk
21:34
OFNumber: Correctly handle NAN. check-in: bc2f7c2124 user: js tags: trunk
Changes

Modified src/OFFile.m from [0c8ee7ced6] to [cbc105e33a].

103
104
105
106
107
108
109

110
111
112
113
114
115
116
117
#define DEFAULT_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
#define DIR_MODE DEFAULT_MODE | S_IXUSR | S_IXGRP | S_IXOTH

#if defined(OF_HAVE_CHOWN) && defined(OF_HAVE_THREADS)
static of_mutex_t mutex;
#endif


static int parse_mode(const char *mode)
{
	if (!strcmp(mode, "r"))
		return O_RDONLY;
	if (!strcmp(mode, "rb"))
		return O_RDONLY | O_BINARY;
	if (!strcmp(mode, "r+"))
		return O_RDWR;







>
|







103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#define DEFAULT_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
#define DIR_MODE DEFAULT_MODE | S_IXUSR | S_IXGRP | S_IXOTH

#if defined(OF_HAVE_CHOWN) && defined(OF_HAVE_THREADS)
static of_mutex_t mutex;
#endif

static int
parse_mode(const char *mode)
{
	if (!strcmp(mode, "r"))
		return O_RDONLY;
	if (!strcmp(mode, "rb"))
		return O_RDONLY | O_BINARY;
	if (!strcmp(mode, "r+"))
		return O_RDWR;
190
191
192
193
194
195
196



197
198
199
200
201
202
203
	}

	return ret;
}

+ (bool)fileExistsAtPath: (OFString*)path
{



#ifndef _WIN32
	struct stat s;

	if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)
		return false;
#else







>
>
>







191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
	}

	return ret;
}

+ (bool)fileExistsAtPath: (OFString*)path
{
	if (path == nil)
		@throw [OFInvalidArgumentException exception];

#ifndef _WIN32
	struct stat s;

	if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)
		return false;
#else
211
212
213
214
215
216
217



218
219
220
221
222
223
224
		return true;

	return false;
}

+ (bool)directoryExistsAtPath: (OFString*)path
{



#ifndef _WIN32
	struct stat s;

	if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)
		return false;
#else







>
>
>







215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
		return true;

	return false;
}

+ (bool)directoryExistsAtPath: (OFString*)path
{
	if (path == nil)
		@throw [OFInvalidArgumentException exception];

#ifndef _WIN32
	struct stat s;

	if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)
		return false;
#else
232
233
234
235
236
237
238



239
240
241
242
243
244
245
		return true;

	return false;
}

+ (void)createDirectoryAtPath: (OFString*)path
{



#ifndef _WIN32
	if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    DIR_MODE))
#else
	if (_wmkdir([path UTF16String]))
#endif
		@throw [OFCreateDirectoryFailedException







>
>
>







239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
		return true;

	return false;
}

+ (void)createDirectoryAtPath: (OFString*)path
{
	if (path == nil)
		@throw [OFInvalidArgumentException exception];

#ifndef _WIN32
	if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    DIR_MODE))
#else
	if (_wmkdir([path UTF16String]))
#endif
		@throw [OFCreateDirectoryFailedException
255
256
257
258
259
260
261



262
263
264
265
266
267
268
	OFEnumerator *enumerator;

	if (!createParents) {
		[OFFile createDirectoryAtPath: path];
		return;
	}




	pool = objc_autoreleasePoolPush();

	pathComponents = [path pathComponents];
	enumerator = [pathComponents objectEnumerator];
	while ((component = [enumerator nextObject]) != nil) {
		void *pool2 = objc_autoreleasePoolPush();








>
>
>







265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
	OFEnumerator *enumerator;

	if (!createParents) {
		[OFFile createDirectoryAtPath: path];
		return;
	}

	if (path == nil)
		@throw [OFInvalidArgumentException exception];

	pool = objc_autoreleasePoolPush();

	pathComponents = [path pathComponents];
	enumerator = [pathComponents objectEnumerator];
	while ((component = [enumerator nextObject]) != nil) {
		void *pool2 = objc_autoreleasePoolPush();

284
285
286
287
288
289
290
291





292
293
294
295
296
297
298
	}

	objc_autoreleasePoolPop(pool);
}

+ (OFArray*)contentsOfDirectoryAtPath: (OFString*)path
{
	OFMutableArray *files = [OFMutableArray array];






#ifndef _WIN32
	DIR *dir;
	struct dirent *dirent;

	if ((dir = opendir([path cStringWithEncoding:
	    OF_STRING_ENCODING_NATIVE])) == NULL)







|
>
>
>
>
>







297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
	}

	objc_autoreleasePoolPop(pool);
}

+ (OFArray*)contentsOfDirectoryAtPath: (OFString*)path
{
	OFMutableArray *files;

	if (path == nil)
		@throw [OFInvalidArgumentException exception];

	files = [OFMutableArray array];

#ifndef _WIN32
	DIR *dir;
	struct dirent *dirent;

	if ((dir = opendir([path cStringWithEncoding:
	    OF_STRING_ENCODING_NATIVE])) == NULL)
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
380
381
382
383
384
385
386
387
388
389
390



391
392
393
394
395
396
397
	[files makeImmutable];

	return files;
}

+ (void)changeCurrentDirectoryPath: (OFString*)path
{



#ifndef _WIN32
	if (chdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#else
	if (_wchdir([path UTF16String]))
#endif
		@throw [OFChangeCurrentDirectoryPathFailedException
		    exceptionWithDirectoryPath: path];
}

+ (off_t)sizeOfFileAtPath: (OFString*)path
{



#ifndef _WIN32
	struct stat s;

	if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)
#else
	struct _stat s;

	if (_wstat([path UTF16String], &s) == -1)
#endif
		/* FIXME: Maybe use another exception? */
		@throw [OFOpenFileFailedException exceptionWithPath: path
							       mode: @"r"];

	return s.st_size;
}

+ (OFDate*)modificationDateOfFileAtPath: (OFString*)path
{



#ifndef _WIN32
	struct stat s;

	if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)
#else
	struct _stat s;







>
>
>











>
>
>



















>
>
>







372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
	[files makeImmutable];

	return files;
}

+ (void)changeCurrentDirectoryPath: (OFString*)path
{
	if (path == nil)
		@throw [OFInvalidArgumentException exception];

#ifndef _WIN32
	if (chdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#else
	if (_wchdir([path UTF16String]))
#endif
		@throw [OFChangeCurrentDirectoryPathFailedException
		    exceptionWithDirectoryPath: path];
}

+ (off_t)sizeOfFileAtPath: (OFString*)path
{
	if (path == nil)
		@throw [OFInvalidArgumentException exception];

#ifndef _WIN32
	struct stat s;

	if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)
#else
	struct _stat s;

	if (_wstat([path UTF16String], &s) == -1)
#endif
		/* FIXME: Maybe use another exception? */
		@throw [OFOpenFileFailedException exceptionWithPath: path
							       mode: @"r"];

	return s.st_size;
}

+ (OFDate*)modificationDateOfFileAtPath: (OFString*)path
{
	if (path == nil)
		@throw [OFInvalidArgumentException exception];

#ifndef _WIN32
	struct stat s;

	if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)
#else
	struct _stat s;
406
407
408
409
410
411
412



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
	return [OFDate dateWithTimeIntervalSince1970: s.st_mtime];
}

#ifdef OF_HAVE_CHMOD
+ (void)changePermissionsOfItemAtPath: (OFString*)path
			  permissions: (mode_t)permissions
{



# ifndef _WIN32
	if (chmod([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    permissions))
# else
	if (_wchmod([path UTF16String], permissions))
# endif
		@throw [OFChangePermissionsFailedException
		    exceptionWithPath: path
			  permissions: permissions];
}
#endif

#ifdef OF_HAVE_CHOWN
+ (void)changeOwnerOfItemAtPath: (OFString*)path
			  owner: (OFString*)owner
			  group: (OFString*)group
{
	uid_t uid = -1;
	gid_t gid = -1;

	if (owner == nil && group == nil)
		@throw [OFInvalidArgumentException exception];

# ifdef OF_HAVE_THREADS
	if (!of_mutex_lock(&mutex))
		@throw [OFLockFailedException exception];

	@try {







>
>
>




















|







433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
	return [OFDate dateWithTimeIntervalSince1970: s.st_mtime];
}

#ifdef OF_HAVE_CHMOD
+ (void)changePermissionsOfItemAtPath: (OFString*)path
			  permissions: (mode_t)permissions
{
	if (path == nil)
		@throw [OFInvalidArgumentException exception];

# ifndef _WIN32
	if (chmod([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    permissions))
# else
	if (_wchmod([path UTF16String], permissions))
# endif
		@throw [OFChangePermissionsFailedException
		    exceptionWithPath: path
			  permissions: permissions];
}
#endif

#ifdef OF_HAVE_CHOWN
+ (void)changeOwnerOfItemAtPath: (OFString*)path
			  owner: (OFString*)owner
			  group: (OFString*)group
{
	uid_t uid = -1;
	gid_t gid = -1;

	if (path == nil || (owner == nil && group == nil))
		@throw [OFInvalidArgumentException exception];

# ifdef OF_HAVE_THREADS
	if (!of_mutex_lock(&mutex))
		@throw [OFLockFailedException exception];

	@try {
478
479
480
481
482
483
484
485
486
487
488
489
490





491
492
493
494
495
496
497
								 group: group];
}
#endif

+ (void)copyFileAtPath: (OFString*)source
		toPath: (OFString*)destination
{
	void *pool = objc_autoreleasePoolPush();
	bool override;
	OFFile *sourceFile = nil;
	OFFile *destinationFile = nil;
	char *buffer;
	size_t pageSize;






	if ([self directoryExistsAtPath: destination]) {
		OFString *filename = [source lastPathComponent];
		destination = [OFString stringWithPath: destination, filename,
							nil];
	}








|





>
>
>
>
>







508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
								 group: group];
}
#endif

+ (void)copyFileAtPath: (OFString*)source
		toPath: (OFString*)destination
{
	void *pool;
	bool override;
	OFFile *sourceFile = nil;
	OFFile *destinationFile = nil;
	char *buffer;
	size_t pageSize;

	if (source == nil || destination == nil)
		@throw [OFInvalidArgumentException exception];

	pool = objc_autoreleasePoolPush();

	if ([self directoryExistsAtPath: destination]) {
		OFString *filename = [source lastPathComponent];
		destination = [OFString stringWithPath: destination, filename,
							nil];
	}

536
537
538
539
540
541
542





543
544
545
546
547
548
549
550

	objc_autoreleasePoolPop(pool);
}

+ (void)renameItemAtPath: (OFString*)source
		  toPath: (OFString*)destination
{





	void *pool = objc_autoreleasePoolPush();

	if ([self directoryExistsAtPath: destination]) {
		OFString *filename = [source lastPathComponent];
		destination = [OFString stringWithPath: destination, filename,
							nil];
	}








>
>
>
>
>
|







571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590

	objc_autoreleasePoolPop(pool);
}

+ (void)renameItemAtPath: (OFString*)source
		  toPath: (OFString*)destination
{
	void *pool;

	if (source == nil || destination == nil)
		@throw [OFInvalidArgumentException exception];

	pool = objc_autoreleasePoolPush();

	if ([self directoryExistsAtPath: destination]) {
		OFString *filename = [source lastPathComponent];
		destination = [OFString stringWithPath: destination, filename,
							nil];
	}

559
560
561
562
563
564
565



566
567
568
569
570
571
572
573
574
575
576
577
578





579
580
581
582
583
584
585
586
			    destinationPath: destination];

	objc_autoreleasePoolPop(pool);
}

+ (void)removeItemAtPath: (OFString*)path
{



#ifndef _WIN32
	if (remove([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#else
	if (_wremove([path UTF16String]))
#endif
		@throw [OFRemoveItemFailedException
		    exceptionWithItemPath: path];
}

#ifdef OF_HAVE_LINK
+ (void)linkItemAtPath: (OFString*)source
		toPath: (OFString*)destination
{





	void *pool = objc_autoreleasePoolPush();

	if ([self directoryExistsAtPath: destination]) {
		OFString *filename = [source lastPathComponent];
		destination = [OFString stringWithPath: destination, filename,
							nil];
	}








>
>
>













>
>
>
>
>
|







599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
			    destinationPath: destination];

	objc_autoreleasePoolPop(pool);
}

+ (void)removeItemAtPath: (OFString*)path
{
	if (path == nil)
		@throw [OFInvalidArgumentException exception];

#ifndef _WIN32
	if (remove([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#else
	if (_wremove([path UTF16String]))
#endif
		@throw [OFRemoveItemFailedException
		    exceptionWithItemPath: path];
}

#ifdef OF_HAVE_LINK
+ (void)linkItemAtPath: (OFString*)source
		toPath: (OFString*)destination
{
	void *pool;

	if (source == nil || destination == nil)
		@throw [OFInvalidArgumentException exception];

	pool = objc_autoreleasePoolPush();

	if ([self directoryExistsAtPath: destination]) {
		OFString *filename = [source lastPathComponent];
		destination = [OFString stringWithPath: destination, filename,
							nil];
	}

594
595
596
597
598
599
600





601
602
603
604
605
606
607
608
}
#endif

#ifdef OF_HAVE_SYMLINK
+ (void)createSymbolicLinkAtPath: (OFString*)source
		 destinationPath: (OFString*)destination
{





	void *pool = objc_autoreleasePoolPush();

	if ([self directoryExistsAtPath: destination]) {
		OFString *filename = [source lastPathComponent];
		destination = [OFString stringWithPath: destination, filename,
							nil];
	}








>
>
>
>
>
|







642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
}
#endif

#ifdef OF_HAVE_SYMLINK
+ (void)createSymbolicLinkAtPath: (OFString*)source
		 destinationPath: (OFString*)destination
{
	void *pool;

	if (source == nil || destination == nil)
		@throw [OFInvalidArgumentException exception];

	pool = objc_autoreleasePoolPush();

	if ([self directoryExistsAtPath: destination]) {
		OFString *filename = [source lastPathComponent];
		destination = [OFString stringWithPath: destination, filename,
							nil];
	}

616
617
618
619
620
621
622



623
624
625
626
627
628
629
}

+ (OFString*)destinationOfSymbolicLinkAtPath: (OFString*)path
{
	char destination[PATH_MAX];
	ssize_t length;




	length = readlink([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    destination, PATH_MAX);

	if (length < 0)
		@throw [OFOpenFileFailedException exceptionWithPath: path
							       mode: @"r"];








>
>
>







669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
}

+ (OFString*)destinationOfSymbolicLinkAtPath: (OFString*)path
{
	char destination[PATH_MAX];
	ssize_t length;

	if (path == nil)
		@throw [OFInvalidArgumentException exception];

	length = readlink([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    destination, PATH_MAX);

	if (length < 0)
		@throw [OFOpenFileFailedException exceptionWithPath: path
							       mode: @"r"];