2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 Copyright 1999-2002, Apple, Inc. All rights reserved.
25 Responsibility: Christopher Kane
28 #include "CFInternal.h"
30 #if defined(__WIN32__)
35 #define timeval xxx_timeval
36 #define BOOLEAN xxx_BOOLEAN
51 #include <sys/types.h>
58 #if defined(__WIN32__)
59 #define CF_OPENFLGS (_O_BINARY|_O_NOINHERIT)
61 #define CF_OPENFLGS (0)
65 __private_extern__ CFStringRef
_CFCopyExtensionForAbstractType(CFStringRef abstractType
) {
66 return (abstractType
? CFRetain(abstractType
) : NULL
);
70 __private_extern__ Boolean
_CFCreateDirectory(const char *path
) {
71 #if defined(__WIN32__)
72 return CreateDirectoryA(path
, (LPSECURITY_ATTRIBUTES
)NULL
);
74 return ((mkdir(path
, 0777) == 0) ? true : false);
78 __private_extern__ Boolean
_CFRemoveDirectory(const char *path
) {
79 #if defined(__WIN32__)
80 return RemoveDirectoryA(path
);
82 return ((rmdir(path
) == 0) ? true : false);
86 __private_extern__ Boolean
_CFDeleteFile(const char *path
) {
87 #if defined(__WIN32__)
88 return DeleteFileA(path
);
90 return unlink(path
) == 0;
94 __private_extern__ Boolean
_CFReadBytesFromFile(CFAllocatorRef alloc
, CFURLRef url
, void **bytes
, CFIndex
*length
, CFIndex maxLength
) {
95 // maxLength is the number of bytes desired, or 0 if the whole file is desired regardless of length.
98 char path
[CFMaxPathSize
];
99 if (!CFURLGetFileSystemRepresentation(url
, true, path
, CFMaxPathSize
)) {
105 #if defined(__WIN32__)
106 fd
= open(path
, O_RDONLY
|CF_OPENFLGS
, 0666|_S_IREAD
);
108 fd
= open(path
, O_RDONLY
|CF_OPENFLGS
, 0666);
113 if (fstat(fd
, &statBuf
) < 0) {
114 int saveerr
= thread_errno();
116 thread_set_errno(saveerr
);
119 if ((statBuf
.st_mode
& S_IFMT
) != S_IFREG
) {
121 thread_set_errno(EACCES
);
124 if (statBuf
.st_size
== 0) {
125 *bytes
= CFAllocatorAllocate(alloc
, 4, 0); // don't return constant string -- it's freed!
126 if (__CFOASafe
) __CFSetLastAllocationEventName(*bytes
, "CFUtilities (file-bytes)");
129 CFIndex desiredLength
;
130 if ((maxLength
>= statBuf
.st_size
) || (maxLength
== 0)) {
131 desiredLength
= statBuf
.st_size
;
133 desiredLength
= maxLength
;
135 *bytes
= CFAllocatorAllocate(alloc
, desiredLength
, 0);
136 if (__CFOASafe
) __CFSetLastAllocationEventName(*bytes
, "CFUtilities (file-bytes)");
137 // fcntl(fd, F_NOCACHE, 1);
138 if (read(fd
, *bytes
, desiredLength
) < 0) {
139 CFAllocatorDeallocate(alloc
, *bytes
);
143 *length
= desiredLength
;
149 __private_extern__ Boolean
_CFWriteBytesToFile(CFURLRef url
, const void *bytes
, CFIndex length
) {
153 char path
[CFMaxPathSize
];
154 if (!CFURLGetFileSystemRepresentation(url
, true, path
, CFMaxPathSize
)) {
158 #if defined(__WIN32__)
165 if (0 == stat(path
, &statBuf
)) {
166 mode
= statBuf
.st_mode
;
167 } else if (thread_errno() != ENOENT
) {
170 #if defined(__WIN32__)
171 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|CF_OPENFLGS
, 0666|_S_IWRITE
);
173 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|CF_OPENFLGS
, 0666);
178 if (length
&& write(fd
, bytes
, length
) != length
) {
179 int saveerr
= thread_errno();
181 thread_set_errno(saveerr
);
184 #if defined(__WIN32__)
185 FlushFileBuffers((HANDLE
)_get_osfhandle(fd
));
194 /* On Mac OS 8/9, one of dirSpec and dirURL must be non-NULL. On all other platforms, one of path and dirURL must be non-NULL
195 If both are present, they are assumed to be in-synch; that is, they both refer to the same directory. */
196 __private_extern__ CFMutableArrayRef
_CFContentsOfDirectory(CFAllocatorRef alloc
, char *dirPath
, void *dirSpec
, CFURLRef dirURL
, CFStringRef matchingAbstractType
) {
197 CFMutableArrayRef files
= NULL
;
198 Boolean releaseBase
= false;
199 CFIndex pathLength
= dirPath
? strlen(dirPath
) : 0;
200 // MF:!!! Need to use four-letter type codes where appropriate.
201 CFStringRef extension
= (matchingAbstractType
? _CFCopyExtensionForAbstractType(matchingAbstractType
) : NULL
);
202 CFIndex extLen
= (extension
? CFStringGetLength(extension
) : 0);
203 uint8_t extBuff
[CFMaxPathSize
];
206 CFStringGetBytes(extension
, CFRangeMake(0, extLen
), CFStringFileSystemEncoding(), 0, false, extBuff
, CFMaxPathSize
, &extLen
);
207 extBuff
[extLen
] = '\0';
210 uint8_t pathBuf
[CFMaxPathSize
];
213 if (!CFURLGetFileSystemRepresentation(dirURL
, true, pathBuf
, CFMaxPathLength
)) {
214 if (extension
) CFRelease(extension
);
218 pathLength
= strlen(dirPath
);
222 #if defined(__WIN32__)
223 WIN32_FIND_DATA file
;
226 if (pathLength
+ 2 >= CFMaxPathLength
) {
228 CFRelease(extension
);
233 dirPath
[pathLength
] = '\\';
234 dirPath
[pathLength
+ 1] = '*';
235 dirPath
[pathLength
+ 2] = '\0';
236 handle
= FindFirstFileA(dirPath
, &file
);
237 if (INVALID_HANDLE_VALUE
== handle
) {
238 dirPath
[pathLength
] = '\0';
240 CFRelease(extension
);
245 files
= CFArrayCreateMutable(alloc
, 0, &kCFTypeArrayCallBacks
);
249 CFIndex namelen
= strlen(file
.cFileName
);
250 if (file
.cFileName
[0] == '.' && (namelen
== 1 || (namelen
== 2 && file
.cFileName
[1] == '.'))) {
254 // Check to see if it matches the extension we're looking for.
255 if (_stricmp(&(file
.cFileName
[namelen
- extLen
]), extBuff
) != 0) {
259 if (dirURL
== NULL
) {
260 dirURL
= CFURLCreateFromFileSystemRepresentation(alloc
, dirPath
, pathLength
, true);
263 // MF:!!! What about the trailing slash?
264 fileURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc
, file
.cFileName
, namelen
, false, dirURL
);
265 CFArrayAppendValue(files
, fileURL
);
267 } while (FindNextFileA(handle
, &file
));
269 dirPath
[pathLength
] = '\0';
271 #elif defined(__svr4__) || defined(__hpux__) || defined(__LINUX__) || defined(__FREEBSD__)
272 /* Solaris and HPUX Implementation */
273 /* The Solaris and HPUX code has not been updated for:
274 base has been renamed dirURL
275 dirPath may be NULL (in which case dirURL is not)
276 if dirPath is NULL, pathLength is 0
282 dirp
= opendir(dirPath
);
285 CFRelease(extension
);
288 // raiseErrno("opendir", path);
290 files
= CFArrayCreateMutable(alloc
, 0, & kCFTypeArrayCallBacks
);
292 while((dp
= readdir(dirp
)) != NULL
) {
294 unsigned namelen
= strlen(dp
->d_name
);
296 // skip . & ..; they cause descenders to go berserk
297 if (dp
->d_name
[0] == '.' && (namelen
== 1 || (namelen
== 2 && dp
->d_name
[1] == '.'))) {
302 // Check to see if it matches the extension we're looking for.
303 if (strncmp(&(dp
->d_name
[namelen
- extLen
]), extBuff
, extLen
) != 0) {
307 if (dirURL
== NULL
) {
308 dirURL
= CFURLCreateFromFileSystemRepresentation(alloc
, dirPath
, pathLength
, true);
311 // MF:!!! What about the trailing slash?
312 fileURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc
, dp
->d_name
, namelen
, false, dirURL
);
313 CFArrayAppendValue(files
, fileURL
);
316 err
= closedir(dirp
);
323 CFRelease(extension
);
326 // raiseErrno("closedir", path);
329 #elif defined(__MACH__)
334 fd
= open(dirPath
, O_RDONLY
, 0777);
337 CFRelease(extension
);
341 files
= CFArrayCreateMutable(alloc
, 0, &kCFTypeArrayCallBacks
);
343 while ((numread
= getdirentries(fd
, dirge
, sizeof(dirge
), &basep
)) > 0) {
345 for (dent
= (struct dirent
*)dirge
; dent
< (struct dirent
*)(dirge
+ numread
); dent
= (struct dirent
*)((char *)dent
+ dent
->d_reclen
)) {
349 nameLen
= dent
->d_namlen
;
350 // skip . & ..; they cause descenders to go berserk
351 if (0 == dent
->d_fileno
|| (dent
->d_name
[0] == '.' && (nameLen
== 1 || (nameLen
== 2 && dent
->d_name
[1] == '.')))) {
355 // Check to see if it matches the extension we're looking for.
356 if (strncmp(&(dent
->d_name
[nameLen
- extLen
]), extBuff
, extLen
) != 0) {
360 if (dirURL
== NULL
) {
361 dirURL
= CFURLCreateFromFileSystemRepresentation(alloc
, dirPath
, pathLength
, true);
365 if (dent
->d_type
== DT_DIR
|| dent
->d_type
== DT_UNKNOWN
) {
366 Boolean isDir
= (dent
->d_type
== DT_DIR
);
369 char subdirPath
[CFMaxPathLength
];
371 strncpy(subdirPath
, dirPath
, pathLength
);
372 subdirPath
[pathLength
] = '/';
373 strncpy(subdirPath
+ pathLength
+ 1, dent
->d_name
, nameLen
);
374 subdirPath
[pathLength
+ nameLen
+ 1] = '\0';
375 if (stat(subdirPath
, &statBuf
) == 0) {
376 isDir
= ((statBuf
.st_mode
& S_IFMT
) == S_IFDIR
);
379 fileURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc
, dent
->d_name
, nameLen
, isDir
, dirURL
);
381 fileURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase (alloc
, dent
->d_name
, nameLen
, false, dirURL
);
383 CFArrayAppendValue(files
, fileURL
);
394 CFRelease(extension
);
400 #error _CFContentsOfDirectory() unknown architechture, not implemented
405 CFRelease(extension
);
413 __private_extern__ SInt32
_CFGetFileProperties(CFAllocatorRef alloc
, CFURLRef pathURL
, Boolean
*exists
, SInt32
*posixMode
, int64_t *size
, CFDateRef
*modTime
, SInt32
*ownerID
, CFArrayRef
*dirContents
) {
415 Boolean isDirectory
= false;
418 char path
[CFMaxPathLength
];
420 if ((exists
== NULL
) && (posixMode
== NULL
) && (size
== NULL
) && (modTime
== NULL
) && (ownerID
== NULL
) && (dirContents
== NULL
)) {
425 if (!CFURLGetFileSystemRepresentation(pathURL
, true, path
, CFMaxPathLength
)) {
429 if (stat(path
, &statBuf
) != 0) {
430 // stat failed, but why?
431 if (thread_errno() == ENOENT
) {
434 return thread_errno();
438 isDirectory
= ((statBuf
.st_mode
& S_IFMT
) == S_IFDIR
);
442 if (exists
!= NULL
) {
443 *exists
= fileExists
;
446 if (posixMode
!= NULL
) {
449 *posixMode
= statBuf
.st_mode
;
459 *size
= statBuf
.st_size
;
466 if (modTime
!= NULL
) {
468 CFTimeInterval theTime
;
470 theTime
= kCFAbsoluteTimeIntervalSince1970
+ statBuf
.st_mtime
;
472 *modTime
= CFDateCreate(alloc
, theTime
);
478 if (ownerID
!= NULL
) {
481 *ownerID
= statBuf
.st_uid
;
488 if (dirContents
!= NULL
) {
489 if (fileExists
&& isDirectory
) {
491 CFMutableArrayRef contents
= _CFContentsOfDirectory(alloc
, path
, NULL
, pathURL
, NULL
);
494 *dirContents
= contents
;
506 // MF:!!! Should pull in the rest of the UniChar based path utils from Foundation.
507 #if defined(__MACH__) || defined(__svr4__) || defined(__hpux__) || defined(__LINUX__) || defined(__FREEBSD__)
508 #define UNIX_PATH_SEMANTICS
509 #elif defined(__WIN32__)
510 #define WINDOWS_PATH_SEMANTICS
512 #error Unknown platform
515 #if defined(WINDOWS_PATH_SEMANTICS)
516 #define CFPreferredSlash ((UniChar)'\\')
517 #elif defined(UNIX_PATH_SEMANTICS)
518 #define CFPreferredSlash ((UniChar)'/')
519 #elif defined(HFS_PATH_SEMANTICS)
520 #define CFPreferredSlash ((UniChar)':')
522 #error Cannot define NSPreferredSlash on this platform
525 #if defined(HFS_PATH_SEMANTICS)
526 #define HAS_DRIVE(S) (false)
527 #define HAS_NET(S) (false)
529 #define HAS_DRIVE(S) ((S)[1] == ':' && (('A' <= (S)[0] && (S)[0] <= 'Z') || ('a' <= (S)[0] && (S)[0] <= 'z')))
530 #define HAS_NET(S) ((S)[0] == '\\' && (S)[1] == '\\')
533 #if defined(WINDOWS_PATH_SEMANTICS)
534 #define IS_SLASH(C) ((C) == '\\' || (C) == '/')
535 #elif defined(UNIX_PATH_SEMANTICS)
536 #define IS_SLASH(C) ((C) == '/')
537 #elif defined(HFS_PATH_SEMANTICS)
538 #define IS_SLASH(C) ((C) == ':')
541 __private_extern__ Boolean
_CFIsAbsolutePath(UniChar
*unichars
, CFIndex length
) {
545 #if defined(WINDOWS_PATH_SEMANTICS)
546 if (unichars
[0] == '~') {
552 if (HAS_NET(unichars
)) {
558 if (IS_SLASH(unichars
[2]) && HAS_DRIVE(unichars
)) {
561 #elif defined(HFS_PATH_SEMANTICS)
562 return !IS_SLASH(unichars
[0]);
564 if (unichars
[0] == '~') {
567 if (IS_SLASH(unichars
[0])) {
574 __private_extern__ Boolean
_CFStripTrailingPathSlashes(UniChar
*unichars
, CFIndex
*length
) {
575 Boolean destHasDrive
= (1 < *length
) && HAS_DRIVE(unichars
);
576 CFIndex oldLength
= *length
;
577 while (((destHasDrive
&& 3 < *length
) || (!destHasDrive
&& 1 < *length
)) && IS_SLASH(unichars
[*length
- 1])) {
580 return (oldLength
!= *length
);
583 __private_extern__ Boolean
_CFAppendPathComponent(UniChar
*unichars
, CFIndex
*length
, CFIndex maxLength
, UniChar
*component
, CFIndex componentLength
) {
584 if (0 == componentLength
) {
587 if (maxLength
< *length
+ 1 + componentLength
) {
594 if (!IS_SLASH(unichars
[0])) {
595 unichars
[(*length
)++] = CFPreferredSlash
;
599 if (!HAS_DRIVE(unichars
) && !HAS_NET(unichars
)) {
600 unichars
[(*length
)++] = CFPreferredSlash
;
604 unichars
[(*length
)++] = CFPreferredSlash
;
607 memmove(unichars
+ *length
, component
, componentLength
* sizeof(UniChar
));
608 *length
+= componentLength
;
612 __private_extern__ Boolean
_CFAppendPathExtension(UniChar
*unichars
, CFIndex
*length
, CFIndex maxLength
, UniChar
*extension
, CFIndex extensionLength
) {
613 if (maxLength
< *length
+ 1 + extensionLength
) {
616 if ((0 < extensionLength
&& IS_SLASH(extension
[0])) || (1 < extensionLength
&& HAS_DRIVE(extension
))) {
619 _CFStripTrailingPathSlashes(unichars
, length
);
624 if (IS_SLASH(unichars
[0]) || unichars
[0] == '~') {
629 if (HAS_DRIVE(unichars
) || HAS_NET(unichars
)) {
634 if (IS_SLASH(unichars
[2]) && HAS_DRIVE(unichars
)) {
639 if (0 < *length
&& unichars
[0] == '~') {
641 Boolean hasSlash
= false;
642 for (idx
= 1; idx
< *length
; idx
++) {
643 if (IS_SLASH(unichars
[idx
])) {
652 unichars
[(*length
)++] = '.';
653 memmove(unichars
+ *length
, extension
, extensionLength
* sizeof(UniChar
));
654 *length
+= extensionLength
;
658 __private_extern__ Boolean
_CFTransmutePathSlashes(UniChar
*unichars
, CFIndex
*length
, UniChar replSlash
) {
659 CFIndex didx
, sidx
, scnt
= *length
;
660 sidx
= (1 < *length
&& HAS_NET(unichars
)) ? 2 : 0;
662 while (sidx
< scnt
) {
663 if (IS_SLASH(unichars
[sidx
])) {
664 unichars
[didx
++] = replSlash
;
665 for (sidx
++; sidx
< scnt
&& IS_SLASH(unichars
[sidx
]); sidx
++);
667 unichars
[didx
++] = unichars
[sidx
++];
671 return (scnt
!= didx
);
674 __private_extern__ CFIndex
_CFStartOfLastPathComponent(UniChar
*unichars
, CFIndex length
) {
679 for (idx
= length
- 1; idx
; idx
--) {
680 if (IS_SLASH(unichars
[idx
- 1])) {
684 if ((2 < length
) && HAS_DRIVE(unichars
)) {
690 __private_extern__ CFIndex
_CFLengthAfterDeletingLastPathComponent(UniChar
*unichars
, CFIndex length
) {
695 for (idx
= length
- 1; idx
; idx
--) {
696 if (IS_SLASH(unichars
[idx
- 1])) {
697 if ((idx
!= 1) && (!HAS_DRIVE(unichars
) || idx
!= 3)) {
703 if ((2 < length
) && HAS_DRIVE(unichars
)) {
709 __private_extern__ CFIndex
_CFStartOfPathExtension(UniChar
*unichars
, CFIndex length
) {
714 for (idx
= length
- 1; idx
; idx
--) {
715 if (IS_SLASH(unichars
[idx
- 1])) {
718 if (unichars
[idx
] != '.') {
721 if (idx
== 2 && HAS_DRIVE(unichars
)) {
729 __private_extern__ CFIndex
_CFLengthAfterDeletingPathExtension(UniChar
*unichars
, CFIndex length
) {
730 CFIndex start
= _CFStartOfPathExtension(unichars
, length
);
731 return ((0 < start
) ? start
: length
);
735 #undef UNIX_PATH_SEMANTICS
736 #undef WINDOWS_PATH_SEMANTICS
737 #undef HFS_PATH_SEMANTICS
738 #undef CFPreferredSlash