ObjFW  Diff

Differences From Artifact [20426bd041]:

To Artifact [281aa87c5c]:


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
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
273







-
+
+











-
+
+












-
+
+











-
+
+

-
+














-
+
+














+
-
+
+








+ (OFString*)currentDirectoryPath
{
	OFString *ret;
	char *buffer = getcwd(NULL, 0);

	@try {
		ret = [OFString stringWithCString: buffer];
		ret = [OFString stringWithCString: buffer
					 encoding: OF_STRING_ENCODING_NATIVE];
	} @finally {
		free(buffer);
	}

	return ret;
}

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

	if (stat([path cString], &s) == -1)
	if (stat([path cStringWithEncoding: 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 cString], &s) == -1)
	if (stat([path cStringWithEncoding: 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 cString], DIR_MODE))
	if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    DIR_MODE))
#else
	if (mkdir([path cString]))
	if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#endif
		@throw [OFCreateDirectoryFailedException newWithClass: self
								 path: path];
}

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

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

	if ((dir = opendir([path cString])) == NULL)
	if ((dir = opendir([path cStringWithEncoding:
	    OF_STRING_ENCODING_NATIVE])) == NULL)
		@throw [OFOpenFileFailedException newWithClass: self
							  path: path
							  mode: @"r"];

	@try {
		pool = [[OFAutoreleasePool alloc] init];

		while ((dirent = readdir(dir)) != NULL) {
			OFString *file;

			if (!strcmp(dirent->d_name, ".") ||
			    !strcmp(dirent->d_name, ".."))
				continue;

			file = [OFString
			file = [OFString stringWithCString: dirent->d_name];
			    stringWithCString: dirent->d_name
				     encoding: OF_STRING_ENCODING_NATIVE];
			[files addObject: file];

			[pool releaseObjects];
		}

		[pool release];
	} @finally {
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
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







-
+









-
+







	[files makeImmutable];

	return files;
}

+ (void)changeToDirectory: (OFString*)path
{
	if (chdir([path cString]))
	if (chdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
		@throw [OFChangeDirectoryFailedException newWithClass: self
								 path: path];
}

#ifndef _PSP
+ (void)changeModeOfFile: (OFString*)path
		  toMode: (mode_t)mode
{
# ifndef _WIN32
	if (chmod([path cString], mode))
	if (chmod([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], mode))
		@throw [OFChangeFileModeFailedException newWithClass: self
								path: path
								mode: mode];
# else
	DWORD attributes = GetFileAttributes([path cString]);

	if (attributes == INVALID_FILE_ATTRIBUTES)
345
346
347
348
349
350
351
352


353
354
355
356
357
358
359
352
353
354
355
356
357
358

359
360
361
362
363
364
365
366
367







-
+
+







}
#endif

+ (OFDate*)modificationDateOfFile: (OFString*)path
{
	struct stat s;

	if (stat([path cString], &s) == -1)
	if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    &s) == -1)
		/* FIXME: Maybe use another exception? */
		@throw [OFOpenFileFailedException newWithClass: self
							  path: path
							  mode: @"r"];

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







-
+
+












-
+
+














-
+
+







	[mutex lock];

	@try {
# endif
		if (owner != nil) {
			struct passwd *passwd;

			if ((passwd = getpwnam([owner cString])) == NULL)
			if ((passwd = getpwnam([owner cStringWithEncoding:
			    OF_STRING_ENCODING_NATIVE])) == NULL)
				@throw [OFChangeFileOwnerFailedException
				    newWithClass: self
					    path: path
					   owner: owner
					   group: group];

			uid = passwd->pw_uid;
		}

		if (group != nil) {
			struct group *group_;

			if ((group_ = getgrnam([group cString])) == NULL)
			if ((group_ = getgrnam([group cStringWithEncoding:
			    OF_STRING_ENCODING_NATIVE])) == NULL)
				@throw [OFChangeFileOwnerFailedException
				    newWithClass: self
					    path: path
					   owner: owner
					   group: group];

			gid = group_->gr_gid;
		}
# ifdef OF_THREADS
	} @finally {
		[mutex unlock];
	}
# endif

	if (chown([path cString], uid, gid))
	if (chown([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    uid, gid))
		@throw [OFChangeFileOwnerFailedException newWithClass: self
								 path: path
								owner: owner
								group: group];
}
#endif

475
476
477
478
479
480
481
482


483
484


485
486
487
488
489
490
491
492
493
494
495
496

497
498

499
500
501
502
503
504
505
506

507
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
533
534
535
536
537
538
539
540
541
542
543
544


545
546
547
548
549
550
551
486
487
488
489
490
491
492

493
494
495

496
497
498
499
500
501
502
503
504
505
506
507
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
533
534
535

536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557

558
559
560
561
562
563
564
565
566







-
+
+

-
+
+











-
+

-
+







-
+
















-
+
+




















-
+
+







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

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

	[pool release];
}

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

+ (void)deleteDirectoryAtPath: (OFString*)path
{
	if (rmdir([path cString]))
	if (rmdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
		@throw [OFDeleteDirectoryFailedException newWithClass: self
								 path: path];
}

#ifndef _WIN32
+ (void)linkFileAtPath: (OFString*)source
		toPath: (OFString*)destination
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

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

	if (link([source cString], [destination cString]) != 0)
	if (link([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    [destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]) != 0)
		@throw [OFLinkFailedException newWithClass: self
						sourcePath: source
					   destinationPath: destination];

	[pool release];
}
#endif

#if !defined(_WIN32) && !defined(_PSP)
+ (void)symlinkFileAtPath: (OFString*)source
		   toPath: (OFString*)destination
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

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

	if (symlink([source cString], [destination cString]) != 0)
	if (symlink([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    [destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]) != 0)
		@throw [OFSymlinkFailedException newWithClass: self
						   sourcePath: source
					      destinationPath: destination];

	[pool release];
}
#endif
562
563
564
565
566
567
568
569


570
571
572
573
574


575
576
577
578
579
580
581
577
578
579
580
581
582
583

584
585
586
587
588


589
590
591
592
593
594
595
596
597







-
+
+



-
-
+
+







	  mode: (OFString*)mode
{
	self = [super init];

	@try {
		int flags;

		if ((flags = parse_mode([mode cString])) == -1)
		if ((flags = parse_mode([mode cStringWithEncoding:
		    OF_STRING_ENCODING_NATIVE])) == -1)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		if ((fileDescriptor = open([path cString], flags,
		    DEFAULT_MODE)) == -1)
		if ((fileDescriptor = open([path cStringWithEncoding:
		    OF_STRING_ENCODING_NATIVE], flags, DEFAULT_MODE)) == -1)
			@throw [OFOpenFileFailedException newWithClass: isa
								  path: path
								  mode: mode];

		closable = YES;
	} @catch (id e) {
		[self release];