ObjFW  Diff

Differences From Artifact [61775ac9b0]:

To Artifact [a86cf89293]:


69
70
71
72
73
74
75
76

77
78
79
80
81
82
83
#import "OFUnlockFailedException.h"
#import "OFWriteFailedException.h"

#import "autorelease.h"
#import "macros.h"

#ifdef _WIN32
# import <windows.h>

#endif

#ifndef O_BINARY
# define O_BINARY 0
#endif

#ifndef S_IRGRP







|
>







69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#import "OFUnlockFailedException.h"
#import "OFWriteFailedException.h"

#import "autorelease.h"
#import "macros.h"

#ifdef _WIN32
# include <windows.h>
# include <wchar.h>
#endif

#ifndef O_BINARY
# define O_BINARY 0
#endif

#ifndef S_IRGRP
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
	return [[[self alloc]
	    initWithFileDescriptor: filedescriptor] autorelease];
}

+ (OFString*)currentDirectoryPath
{
	OFString *ret;

	char *buffer = getcwd(NULL, 0);




	@try {

		ret = [OFString stringWithCString: buffer
					 encoding: OF_STRING_ENCODING_NATIVE];



	} @finally {
		free(buffer);
	}

	return ret;
}

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

	struct stat s;

	if (stat([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)
		return NO;







	if (S_ISREG(s.st_mode))
		return YES;

	return NO;
}

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

	struct stat s;

	if (stat([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)
		return NO;







	if (S_ISDIR(s.st_mode))
		return YES;

	return NO;
}

+ (void)createDirectoryAtPath: (OFString*)path
{
#ifndef _WIN32
	if (mkdir([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE],
	    DIR_MODE))
#else
	if (mkdir([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE]))
#endif
		@throw [OFCreateDirectoryFailedException
		    exceptionWithClass: self
				  path: path];
}

+ (void)createDirectoryAtPath: (OFString*)path







>

>
>
>


>


>
>
>









>





>
>
>
>
>
>









>





>
>
>
>
>
>













|







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
	return [[[self alloc]
	    initWithFileDescriptor: filedescriptor] autorelease];
}

+ (OFString*)currentDirectoryPath
{
	OFString *ret;
#ifndef _WIN32
	char *buffer = getcwd(NULL, 0);
#else
	wchar_t *buffer = _wgetcwd(NULL, 0);
#endif

	@try {
#ifndef _WIN32
		ret = [OFString stringWithCString: buffer
					 encoding: OF_STRING_ENCODING_NATIVE];
#else
		ret = [OFString stringWithUTF16String: buffer];
#endif
	} @finally {
		free(buffer);
	}

	return ret;
}

+ (BOOL)fileExistsAtPath: (OFString*)path
{
#ifndef _WIN32
	struct stat s;

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

	if (_wstat([path UTF16String], &s) == -1)
		return NO;
#endif

	if (S_ISREG(s.st_mode))
		return YES;

	return NO;
}

+ (BOOL)directoryExistsAtPath: (OFString*)path
{
#ifndef _WIN32
	struct stat s;

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

	if (_wstat([path UTF16String], &s) == -1)
		return NO;
#endif

	if (S_ISDIR(s.st_mode))
		return YES;

	return NO;
}

+ (void)createDirectoryAtPath: (OFString*)path
{
#ifndef _WIN32
	if (mkdir([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE],
	    DIR_MODE))
#else
	if (_wmkdir([path UTF16String]))
#endif
		@throw [OFCreateDirectoryFailedException
		    exceptionWithClass: self
				  path: path];
}

+ (void)createDirectoryAtPath: (OFString*)path
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
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
425
426
427
428
		}
	} @finally {
		closedir(dir);
	}
#else
	void *pool = objc_autoreleasePoolPush();
	HANDLE handle;
	WIN32_FIND_DATA fd;

	path = [path stringByAppendingString: @"\\*"];

	if ((handle = FindFirstFile([path cStringUsingEncoding:
	    OF_STRING_ENCODING_NATIVE], &fd)) == INVALID_HANDLE_VALUE)
		@throw [OFOpenFileFailedException exceptionWithClass: self
								path: path
								mode: @"r"];

	@try {
		do {
			void *pool2 = objc_autoreleasePoolPush();
			OFString *file;

			if (!strcmp(fd.cFileName, ".") ||
			    !strcmp(fd.cFileName, ".."))
				continue;

			file = [OFString
			    stringWithCString: fd.cFileName
				     encoding: OF_STRING_ENCODING_NATIVE];
			[files addObject: file];

			objc_autoreleasePoolPop(pool2);
		} while (FindNextFile(handle, &fd));
	} @finally {
		FindClose(handle);
	}

	objc_autoreleasePoolPop(pool);
#endif

	[files makeImmutable];

	return files;
}

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

	if (chdir([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE]))



		@throw [OFChangeDirectoryFailedException
		    exceptionWithClass: self
				  path: path];
}

#ifndef _PSP
+ (void)changeModeOfFileAtPath: (OFString*)path
			  mode: (mode_t)mode
{
# ifndef _WIN32
	if (chmod([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE], mode))
		@throw [OFChangeFileModeFailedException
		    exceptionWithClass: self
				  path: path
				  mode: mode];
# else
	DWORD attributes = GetFileAttributes(
	    [path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE]);

	if (attributes == INVALID_FILE_ATTRIBUTES)
		@throw [OFChangeFileModeFailedException
		    exceptionWithClass: self
				  path: path
				  mode: mode];

	if ((mode / 100) & 2)
		attributes &= ~FILE_ATTRIBUTE_READONLY;
	else
		attributes |= FILE_ATTRIBUTE_READONLY;

	if (!SetFileAttributes([path cStringUsingEncoding:
	    OF_STRING_ENCODING_NATIVE], attributes))
		@throw [OFChangeFileModeFailedException
		    exceptionWithClass: self
				  path: path
				  mode: mode];
# endif
}
#endif

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

	struct stat s;

	if (stat([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)





		/* FIXME: Maybe use another exception? */
		@throw [OFOpenFileFailedException exceptionWithClass: self
								path: path
								mode: @"r"];

	return s.st_size;
}

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

	struct stat s;

	if (stat([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)





		/* FIXME: Maybe use another exception? */
		@throw [OFOpenFileFailedException exceptionWithClass: self
								path: path
								mode: @"r"];

	/* FIXME: We could be more precise on some OSes */
	return [OFDate dateWithTimeIntervalSince1970: s.st_mtime];







|



|
|









|
|


|
<
<



|














>

>
>
>
















|
<












|
<










>




>
>
>
>
>










>




>
>
>
>
>







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
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
425
426
427
428
429
430
431
432
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
		}
	} @finally {
		closedir(dir);
	}
#else
	void *pool = objc_autoreleasePoolPush();
	HANDLE handle;
	WIN32_FIND_DATAW fd;

	path = [path stringByAppendingString: @"\\*"];

	if ((handle = FindFirstFileW([path UTF16String],
	    &fd)) == INVALID_HANDLE_VALUE)
		@throw [OFOpenFileFailedException exceptionWithClass: self
								path: path
								mode: @"r"];

	@try {
		do {
			void *pool2 = objc_autoreleasePoolPush();
			OFString *file;

			if (!wcscmp(fd.cFileName, L".") ||
			    !wcscmp(fd.cFileName, L".."))
				continue;

			file = [OFString stringWithUTF16String: fd.cFileName];


			[files addObject: file];

			objc_autoreleasePoolPop(pool2);
		} while (FindNextFileW(handle, &fd));
	} @finally {
		FindClose(handle);
	}

	objc_autoreleasePoolPop(pool);
#endif

	[files makeImmutable];

	return files;
}

+ (void)changeToDirectoryAtPath: (OFString*)path
{
#ifndef _WIN32
	if (chdir([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE]))
#else
	if (_wchdir([path UTF16String]))
#endif
		@throw [OFChangeDirectoryFailedException
		    exceptionWithClass: self
				  path: path];
}

#ifndef _PSP
+ (void)changeModeOfFileAtPath: (OFString*)path
			  mode: (mode_t)mode
{
# ifndef _WIN32
	if (chmod([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE], mode))
		@throw [OFChangeFileModeFailedException
		    exceptionWithClass: self
				  path: path
				  mode: mode];
# else
	DWORD attributes = GetFileAttributesW([path UTF16String]);


	if (attributes == INVALID_FILE_ATTRIBUTES)
		@throw [OFChangeFileModeFailedException
		    exceptionWithClass: self
				  path: path
				  mode: mode];

	if ((mode / 100) & 2)
		attributes &= ~FILE_ATTRIBUTE_READONLY;
	else
		attributes |= FILE_ATTRIBUTE_READONLY;

	if (!SetFileAttributesW([path UTF16String], attributes))

		@throw [OFChangeFileModeFailedException
		    exceptionWithClass: self
				  path: path
				  mode: mode];
# endif
}
#endif

+ (off_t)sizeOfFileAtPath: (OFString*)path
{
#ifndef _WIN32
	struct stat s;

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

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

	return s.st_size;
}

+ (OFDate*)modificationDateOfFileAtPath: (OFString*)path
{
#ifndef _WIN32
	struct stat s;

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

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

	/* FIXME: We could be more precise on some OSes */
	return [OFDate dateWithTimeIntervalSince1970: s.st_mtime];
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
587
588
589

590



591
592
593
594
595
596
597
							nil];
	}

#ifndef _WIN32
	if (rename([source cStringUsingEncoding: OF_STRING_ENCODING_NATIVE],
	    [destination cStringUsingEncoding: OF_STRING_ENCODING_NATIVE]))
#else
	if (!MoveFile([source cStringUsingEncoding: OF_STRING_ENCODING_NATIVE],
	    [destination cStringUsingEncoding: OF_STRING_ENCODING_NATIVE]))
#endif
		@throw [OFRenameFileFailedException
		    exceptionWithClass: self
			    sourcePath: source
		       destinationPath: destination];

	objc_autoreleasePoolPop(pool);
}

+ (void)deleteFileAtPath: (OFString*)path
{
#ifndef _WIN32
	if (unlink([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE]))
#else
	if (!DeleteFile([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE]))
#endif
		@throw [OFDeleteFileFailedException exceptionWithClass: self
								  path: path];
}

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

	if (rmdir([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE]))



		@throw [OFDeleteDirectoryFailedException
		    exceptionWithClass: self
				  path: path];
}

#ifndef _WIN32
+ (void)linkFileAtPath: (OFString*)source







<
|














|







>

>
>
>







594
595
596
597
598
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
635
							nil];
	}

#ifndef _WIN32
	if (rename([source cStringUsingEncoding: OF_STRING_ENCODING_NATIVE],
	    [destination cStringUsingEncoding: OF_STRING_ENCODING_NATIVE]))
#else

	if (!MoveFileW([source UTF16String], [destination UTF16String]))
#endif
		@throw [OFRenameFileFailedException
		    exceptionWithClass: self
			    sourcePath: source
		       destinationPath: destination];

	objc_autoreleasePoolPop(pool);
}

+ (void)deleteFileAtPath: (OFString*)path
{
#ifndef _WIN32
	if (unlink([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE]))
#else
	if (_wunlink([path UTF16String]))
#endif
		@throw [OFDeleteFileFailedException exceptionWithClass: self
								  path: path];
}

+ (void)deleteDirectoryAtPath: (OFString*)path
{
#ifndef _WIN32
	if (rmdir([path cStringUsingEncoding: OF_STRING_ENCODING_NATIVE]))
#else
	if (_wrmdir([path UTF16String]))
#endif
		@throw [OFDeleteDirectoryFailedException
		    exceptionWithClass: self
				  path: path];
}

#ifndef _WIN32
+ (void)linkFileAtPath: (OFString*)source
653
654
655
656
657
658
659
660
661
662
663
664
665

666
667




668
669
670
671
672
673
674
	  mode: (OFString*)mode
{
	self = [super init];

	@try {
		int flags;

		if ((flags = parse_mode([mode cStringUsingEncoding:
		    OF_STRING_ENCODING_NATIVE])) == -1)
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];


		if ((fd = open([path cStringUsingEncoding:
		    OF_STRING_ENCODING_NATIVE], flags, DEFAULT_MODE)) == -1)




			@throw [OFOpenFileFailedException
			    exceptionWithClass: [self class]
					  path: path
					  mode: mode];

		closable = YES;
	} @catch (id e) {







|
<




>


>
>
>
>







691
692
693
694
695
696
697
698

699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
	  mode: (OFString*)mode
{
	self = [super init];

	@try {
		int flags;

		if ((flags = parse_mode([mode UTF8String])) == -1)

			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

#ifndef _WIN32
		if ((fd = open([path cStringUsingEncoding:
		    OF_STRING_ENCODING_NATIVE], flags, DEFAULT_MODE)) == -1)
#else
		if ((fd = _wopen([path UTF16String], flags,
		    DEFAULT_MODE)) == -1)
#endif
			@throw [OFOpenFileFailedException
			    exceptionWithClass: [self class]
					  path: path
					  mode: mode];

		closable = YES;
	} @catch (id e) {