ObjFW  Check-in [ce99d76d79]

Overview
Comment:OFFile: Clean up the stat mess.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ce99d76d79f8c5fddcc16ea4d70204132674fd0f4d5247b4d01d1abf7b7eb4df
User & Date: js on 2013-12-15 14:57:39
Other Links: manifest | tags
Context
2013-12-15
16:31
Fix ObjFW.h. check-in: f03942db89 user: js tags: trunk
14:57
OFFile: Clean up the stat mess. check-in: ce99d76d79 user: js tags: trunk
14:35
configure.ac: Move host-specific flags. check-in: 87b56ba86f user: js tags: trunk
Changes

Modified configure.ac from [264690facc] to [b35cbc2251].

580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
AC_MSG_CHECKING(for atomic operations)
AS_IF([test x"$atomic_ops" != x"none"], [
	AC_DEFINE(OF_HAVE_ATOMIC_OPS, 1, [Whether we have atomic operations])
	AC_SUBST(ATOMIC_H, "atomic.h")
])
AC_MSG_RESULT($atomic_ops)

AC_CHECK_FUNCS([sysconf gmtime_r localtime_r nanosleep])

AC_CHECK_HEADERS([pwd.h grp.h])
AC_CHECK_FUNC(chmod, [
	AC_DEFINE(OF_HAVE_CHMOD, 1, [Whether we have chmod()])
])
AC_CHECK_FUNC(chown, [
	AC_DEFINE(OF_HAVE_CHOWN, 1, [Whether we have chown()])







|







580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
AC_MSG_CHECKING(for atomic operations)
AS_IF([test x"$atomic_ops" != x"none"], [
	AC_DEFINE(OF_HAVE_ATOMIC_OPS, 1, [Whether we have atomic operations])
	AC_SUBST(ATOMIC_H, "atomic.h")
])
AC_MSG_RESULT($atomic_ops)

AC_CHECK_FUNCS([sysconf gmtime_r localtime_r nanosleep lstat])

AC_CHECK_HEADERS([pwd.h grp.h])
AC_CHECK_FUNC(chmod, [
	AC_DEFINE(OF_HAVE_CHMOD, 1, [Whether we have chmod()])
])
AC_CHECK_FUNC(chown, [
	AC_DEFINE(OF_HAVE_CHOWN, 1, [Whether we have chown()])

Modified src/OFFile.h from [00385e02ec] to [e01ad20745].

18
19
20
21
22
23
24

25
26
27
28
29






30
31
32
33
34
35
36
# define __STDC_LIMIT_MACROS
#endif
#ifndef __STDC_CONSTANT_MACROS
# define __STDC_CONSTANT_MACROS
#endif

#include <sys/types.h>


#import "OFSeekableStream.h"

@class OFArray;
@class OFDate;







/*!
 * @brief A class which provides functions to read, write and manipulate files.
 */
@interface OFFile: OFSeekableStream
{
	int  _fd;







>





>
>
>
>
>
>







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
# define __STDC_LIMIT_MACROS
#endif
#ifndef __STDC_CONSTANT_MACROS
# define __STDC_CONSTANT_MACROS
#endif

#include <sys/types.h>
#include <sys/stat.h>

#import "OFSeekableStream.h"

@class OFArray;
@class OFDate;

#ifndef _WIN32
typedef struct stat of_stat_t;
#else
typedef struct _stat of_stat_t;
#endif

/*!
 * @brief A class which provides functions to read, write and manipulate files.
 */
@interface OFFile: OFSeekableStream
{
	int  _fd;
283
284
285
286
287
288
289









 * @brief Initializes an already allocated OFFile.
 *
 * @param fd A file descriptor, returned from for example open().
 *	     It is not closed when the OFFile object is deallocated!
 */
- initWithFileDescriptor: (int)fd;
@end
















>
>
>
>
>
>
>
>
>
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
 * @brief Initializes an already allocated OFFile.
 *
 * @param fd A file descriptor, returned from for example open().
 *	     It is not closed when the OFFile object is deallocated!
 */
- initWithFileDescriptor: (int)fd;
@end

#ifdef __cplusplus
extern "C" {
#endif
extern int of_stat(OFString *path, of_stat_t *buffer);
extern int of_lstat(OFString *path, of_stat_t *buffer);
#ifdef __cplusplus
}
#endif

Modified src/OFFile.m from [95c6f11fb5] to [17aa806995].

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>

#ifdef HAVE_PWD_H
# include <pwd.h>
#endif
#ifdef HAVE_GRP_H







<
<







26
27
28
29
30
31
32


33
34
35
36
37
38
39
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include <unistd.h>



#include <fcntl.h>
#include <dirent.h>

#ifdef HAVE_PWD_H
# include <pwd.h>
#endif
#ifdef HAVE_GRP_H
103
104
105
106
107
108
109

























110
111
112
113
114
115
116

#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
parseMode(const char *mode)
{
	if (!strcmp(mode, "r"))
		return O_RDONLY;
	if (!strcmp(mode, "rb"))







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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

#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

int
of_stat(OFString *path, of_stat_t *buffer)
{
#ifdef _WIN32
	return _wstat([path UTF16String], buffer);
#else
	return stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    buffer);
#endif
}

int
of_lstat(OFString *path, of_stat_t *buffer)
{
#if defined(_WIN32)
	return _wstat([path UTF16String], buffer);
#elif defined(HAVE_LSTAT)
	return lstat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    buffer);
#else
	return stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
	    buffer);
#endif
}

static int
parseMode(const char *mode)
{
	if (!strcmp(mode, "r"))
		return O_RDONLY;
	if (!strcmp(mode, "rb"))
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
	}

	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
	struct _stat s;

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

	if (S_ISREG(s.st_mode))
		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
	struct _stat s;

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

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

	return false;
}

#ifdef OF_HAVE_SYMLINK
+ (bool)symbolicLinkExistsAtPath: (OFString*)path
{
	struct stat s;

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

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

	if (S_ISLNK(s.st_mode))
		return true;

	return false;
}







>
>



<
<
<
<
|

<
<
<
<
<
<









>
>



<
<
<
<
|

<
<
<
<
<
<










|




<
|







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
	}

	return ret;
}

+ (bool)fileExistsAtPath: (OFString*)path
{
	of_stat_t s;

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





	if (of_stat(path, &s) == -1)
		return false;







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

	return false;
}

+ (bool)directoryExistsAtPath: (OFString*)path
{
	of_stat_t s;

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





	if (of_stat(path, &s) == -1)
		return false;







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

	return false;
}

#ifdef OF_HAVE_SYMLINK
+ (bool)symbolicLinkExistsAtPath: (OFString*)path
{
	of_stat_t s;

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


	if (of_lstat(path, &s) == -1)
		return false;

	if (S_ISLNK(s.st_mode))
		return true;

	return false;
}
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
#endif
		@throw [OFChangeCurrentDirectoryPathFailedException
		    exceptionWithPath: 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"];

	/* On Android, off_t is 32 bit, but st_size is long long there */
	return (off_t)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;

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

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







>
>



<
<
<
<
|
<
<
<
<
<




|





>
>



<
<
<
<
|
<
<
<
<
<







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
#endif
		@throw [OFChangeCurrentDirectoryPathFailedException
		    exceptionWithPath: path];
}

+ (off_t)sizeOfFileAtPath: (OFString*)path
{
	of_stat_t s;

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





	if (of_stat(path, &s) == -1)





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

	/* FIXME: On Android, off_t is 32 bit, but st_size is long long there */
	return (off_t)s.st_size;
}

+ (OFDate*)modificationDateOfFileAtPath: (OFString*)path
{
	of_stat_t s;

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





	if (of_stat(path, &s) == -1)





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

	/* FIXME: We could be more precise on some OSes */
	return [OFDate dateWithTimeIntervalSince1970: s.st_mtime];
}
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
567
568
569
570
571
}
#endif

+ (void)copyItemAtPath: (OFString*)source
		toPath: (OFString*)destination
{
	void *pool;
#ifndef _WIN32
	struct stat s;
#else
	struct _stat s;
#endif

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

	pool = objc_autoreleasePoolPush();

#ifndef _WIN32
	if (lstat([destination
		cStringWithEncoding: OF_STRING_ENCODING_NATIVE], &s) == 0) {
#else
	if (_wstat([destination UTF16String], &s) == 0) {
#endif
		errno = EEXIST;
		@throw [OFCopyItemFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination];
	}

#ifndef _WIN32
	if (lstat([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE], &s))
#else
	if (_wstat([source UTF16String], &s))
#endif
		@throw [OFCopyItemFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination];

	if (S_ISDIR(s.st_mode)) {
		OFArray *contents;
		OFEnumerator *enumerator;







<
<
<
|
<






<
|
<
<
<
<






<
<
<
|
<







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
}
#endif

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



	of_stat_t s;


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

	pool = objc_autoreleasePoolPush();


	if (of_lstat(destination, &s) == 0) {




		errno = EEXIST;
		@throw [OFCopyItemFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination];
	}




	if (of_lstat(source, &s))

		@throw [OFCopyItemFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination];

	if (S_ISDIR(s.st_mode)) {
		OFArray *contents;
		OFEnumerator *enumerator;
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
	objc_autoreleasePoolPop(pool);
}

+ (void)moveItemAtPath: (OFString*)source
		toPath: (OFString*)destination
{
	void *pool;
#ifndef _WIN32
	struct stat s;
#else
	struct _stat s;
#endif

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

	pool = objc_autoreleasePoolPush();

#ifndef _WIN32
	if (lstat([destination
		cStringWithEncoding: OF_STRING_ENCODING_NATIVE], &s) == 0) {
#else
	if (_wstat([destination UTF16String], &s) == 0) {
#endif
		errno = EEXIST;
		@throw [OFCopyItemFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination];
	}

#ifndef _WIN32







<
<
<
|
<






<
|
<
<
<
<







644
645
646
647
648
649
650



651

652
653
654
655
656
657

658




659
660
661
662
663
664
665
	objc_autoreleasePoolPop(pool);
}

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



	of_stat_t s;


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

	pool = objc_autoreleasePoolPush();


	if (of_lstat(destination, &s) == 0) {




		errno = EEXIST;
		@throw [OFCopyItemFailedException
		    exceptionWithSourcePath: source
			    destinationPath: destination];
	}

#ifndef _WIN32
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753

	objc_autoreleasePoolPop(pool);
}

+ (void)removeItemAtPath: (OFString*)path
{
	void *pool;
#ifndef _WIN32
	struct stat s;
#else
	struct _stat s;
#endif

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

	pool = objc_autoreleasePoolPush();

#ifndef _WIN32
	if (lstat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], &s))
#else
	if (_wstat([path UTF16String], &s))
#endif
		@throw [OFRemoveItemFailedException exceptionWithPath: path];

	if (S_ISDIR(s.st_mode)) {
		OFArray *contents;
		OFEnumerator *enumerator;
		OFString *item;








<
<
<
|
<






<
<
<
|
<







694
695
696
697
698
699
700



701

702
703
704
705
706
707



708

709
710
711
712
713
714
715

	objc_autoreleasePoolPop(pool);
}

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



	of_stat_t s;


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

	pool = objc_autoreleasePoolPush();




	if (of_lstat(path, &s))

		@throw [OFRemoveItemFailedException exceptionWithPath: path];

	if (S_ISDIR(s.st_mode)) {
		OFArray *contents;
		OFEnumerator *enumerator;
		OFString *item;