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
)) {
106 #if defined(__WIN32__)
107 fd
= open(path
, O_RDONLY
|CF_OPENFLGS
, 0666|_S_IREAD
);
109 fd
= open(path
, O_RDONLY
|CF_OPENFLGS
, 0666);
114 if (fstat(fd
, &statBuf
) < 0) {
115 int saveerr
= thread_errno();
117 thread_set_errno(saveerr
);
120 if ((statBuf
.st_mode
& S_IFMT
) != S_IFREG
) {
122 thread_set_errno(EACCES
);
125 if (statBuf
.st_size
== 0) {
126 *bytes
= CFAllocatorAllocate(alloc
, 4, 0); // don't return constant string -- it's freed!
127 if (__CFOASafe
) __CFSetLastAllocationEventName(*bytes
, "CFUtilities (file-bytes)");
130 CFIndex desiredLength
;
131 if ((maxLength
>= statBuf
.st_size
) || (maxLength
== 0)) {
132 desiredLength
= statBuf
.st_size
;
134 desiredLength
= maxLength
;
136 *bytes
= CFAllocatorAllocate(alloc
, desiredLength
, 0);
137 if (__CFOASafe
) __CFSetLastAllocationEventName(*bytes
, "CFUtilities (file-bytes)");
138 // fcntl(fd, F_NOCACHE, 1);
139 if (read(fd
, *bytes
, desiredLength
) < 0) {
140 CFAllocatorDeallocate(alloc
, *bytes
);
144 *length
= desiredLength
;
150 __private_extern__ Boolean
_CFWriteBytesToFile(CFURLRef url
, const void *bytes
, CFIndex length
) {
154 char path
[CFMaxPathSize
];
155 if (!CFURLGetFileSystemRepresentation(url
, true, path
, CFMaxPathSize
)) {
159 #if defined(__WIN32__)
166 if (0 == stat(path
, &statBuf
)) {
167 mode
= statBuf
.st_mode
;
168 } else if (thread_errno() != ENOENT
) {
171 #if defined(__WIN32__)
172 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|CF_OPENFLGS
, 0666|_S_IWRITE
);
174 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|CF_OPENFLGS
, 0666);
179 if (length
&& write(fd
, bytes
, length
) != length
) {
180 int saveerr
= thread_errno();
182 thread_set_errno(saveerr
);
185 #if defined(__WIN32__)
186 FlushFileBuffers((HANDLE
)_get_osfhandle(fd
));
195 /* 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
196 If both are present, they are assumed to be in-synch; that is, they both refer to the same directory. */
197 __private_extern__ CFMutableArrayRef
_CFContentsOfDirectory(CFAllocatorRef alloc
, char *dirPath
, void *dirSpec
, CFURLRef dirURL
, CFStringRef matchingAbstractType
) {
198 CFMutableArrayRef files
= NULL
;
199 Boolean releaseBase
= false;
200 CFIndex pathLength
= dirPath
? strlen(dirPath
) : 0;
201 // MF:!!! Need to use four-letter type codes where appropriate.
202 CFStringRef extension
= (matchingAbstractType
? _CFCopyExtensionForAbstractType(matchingAbstractType
) : NULL
);
203 CFIndex extLen
= (extension
? CFStringGetLength(extension
) : 0);
204 uint8_t extBuff
[CFMaxPathSize
];
207 CFStringGetBytes(extension
, CFRangeMake(0, extLen
), CFStringFileSystemEncoding(), 0, false, extBuff
, CFMaxPathSize
, &extLen
);
208 extBuff
[extLen
] = '\0';
211 uint8_t pathBuf
[CFMaxPathSize
];
214 if (!CFURLGetFileSystemRepresentation(dirURL
, true, pathBuf
, CFMaxPathLength
)) {
215 if (extension
) CFRelease(extension
);
219 pathLength
= strlen(dirPath
);
223 #if defined(__WIN32__)
224 WIN32_FIND_DATA file
;
227 if (pathLength
+ 2 >= CFMaxPathLength
) {
229 CFRelease(extension
);
234 dirPath
[pathLength
] = '\\';
235 dirPath
[pathLength
+ 1] = '*';
236 dirPath
[pathLength
+ 2] = '\0';
237 handle
= FindFirstFileA(dirPath
, &file
);
238 if (INVALID_HANDLE_VALUE
== handle
) {
239 dirPath
[pathLength
] = '\0';
241 CFRelease(extension
);
246 files
= CFArrayCreateMutable(alloc
, 0, &kCFTypeArrayCallBacks
);
250 CFIndex namelen
= strlen(file
.cFileName
);
251 if (file
.cFileName
[0] == '.' && (namelen
== 1 || (namelen
== 2 && file
.cFileName
[1] == '.'))) {
255 // Check to see if it matches the extension we're looking for.
256 if (_stricmp(&(file
.cFileName
[namelen
- extLen
]), extBuff
) != 0) {
260 if (dirURL
== NULL
) {
261 dirURL
= CFURLCreateFromFileSystemRepresentation(alloc
, dirPath
, pathLength
, true);
264 // MF:!!! What about the trailing slash?
265 fileURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc
, file
.cFileName
, namelen
, false, dirURL
);
266 CFArrayAppendValue(files
, fileURL
);
268 } while (FindNextFileA(handle
, &file
));
270 dirPath
[pathLength
] = '\0';
272 #elif defined(__svr4__) || defined(__hpux__) || defined(__LINUX__) || defined(__FREEBSD__)
273 /* Solaris and HPUX Implementation */
274 /* The Solaris and HPUX code has not been updated for:
275 base has been renamed dirURL
276 dirPath may be NULL (in which case dirURL is not)
277 if dirPath is NULL, pathLength is 0
283 dirp
= opendir(dirPath
);
286 CFRelease(extension
);
289 // raiseErrno("opendir", path);
291 files
= CFArrayCreateMutable(alloc
, 0, & kCFTypeArrayCallBacks
);
293 while((dp
= readdir(dirp
)) != NULL
) {
295 unsigned namelen
= strlen(dp
->d_name
);
297 // skip . & ..; they cause descenders to go berserk
298 if (dp
->d_name
[0] == '.' && (namelen
== 1 || (namelen
== 2 && dp
->d_name
[1] == '.'))) {
303 // Check to see if it matches the extension we're looking for.
304 if (strncmp(&(dp
->d_name
[namelen
- extLen
]), extBuff
, extLen
) != 0) {
308 if (dirURL
== NULL
) {
309 dirURL
= CFURLCreateFromFileSystemRepresentation(alloc
, dirPath
, pathLength
, true);
312 // MF:!!! What about the trailing slash?
313 fileURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc
, dp
->d_name
, namelen
, false, dirURL
);
314 CFArrayAppendValue(files
, fileURL
);
317 err
= closedir(dirp
);
324 CFRelease(extension
);
327 // raiseErrno("closedir", path);
330 #elif defined(__MACH__)
335 fd
= open(dirPath
, O_RDONLY
, 0777);
338 CFRelease(extension
);
342 files
= CFArrayCreateMutable(alloc
, 0, &kCFTypeArrayCallBacks
);
344 while ((numread
= getdirentries(fd
, dirge
, sizeof(dirge
), &basep
)) > 0) {
346 for (dent
= (struct dirent
*)dirge
; dent
< (struct dirent
*)(dirge
+ numread
); dent
= (struct dirent
*)((char *)dent
+ dent
->d_reclen
)) {
350 nameLen
= dent
->d_namlen
;
351 // skip . & ..; they cause descenders to go berserk
352 if (0 == dent
->d_fileno
|| (dent
->d_name
[0] == '.' && (nameLen
== 1 || (nameLen
== 2 && dent
->d_name
[1] == '.')))) {
356 // Check to see if it matches the extension we're looking for.
357 if (strncmp(&(dent
->d_name
[nameLen
- extLen
]), extBuff
, extLen
) != 0) {
361 if (dirURL
== NULL
) {
362 dirURL
= CFURLCreateFromFileSystemRepresentation(alloc
, dirPath
, pathLength
, true);
366 if (dent
->d_type
== DT_DIR
|| dent
->d_type
== DT_UNKNOWN
) {
367 Boolean isDir
= (dent
->d_type
== DT_DIR
);
370 char subdirPath
[CFMaxPathLength
];
372 strncpy(subdirPath
, dirPath
, pathLength
);
373 subdirPath
[pathLength
] = '/';
374 strncpy(subdirPath
+ pathLength
+ 1, dent
->d_name
, nameLen
);
375 subdirPath
[pathLength
+ nameLen
+ 1] = '\0';
376 if (stat(subdirPath
, &statBuf
) == 0) {
377 isDir
= ((statBuf
.st_mode
& S_IFMT
) == S_IFDIR
);
380 fileURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc
, dent
->d_name
, nameLen
, isDir
, dirURL
);
382 fileURL
= CFURLCreateFromFileSystemRepresentationRelativeToBase (alloc
, dent
->d_name
, nameLen
, false, dirURL
);
384 CFArrayAppendValue(files
, fileURL
);
395 CFRelease(extension
);
401 #error _CFContentsOfDirectory() unknown architechture, not implemented
406 CFRelease(extension
);
414 __private_extern__ SInt32
_CFGetFileProperties(CFAllocatorRef alloc
, CFURLRef pathURL
, Boolean
*exists
, SInt32
*posixMode
, int64_t *size
, CFDateRef
*modTime
, SInt32
*ownerID
, CFArrayRef
*dirContents
) {
416 Boolean isDirectory
= false;
419 char path
[CFMaxPathLength
];
421 if ((exists
== NULL
) && (posixMode
== NULL
) && (size
== NULL
) && (modTime
== NULL
) && (ownerID
== NULL
) && (dirContents
== NULL
)) {
426 if (!CFURLGetFileSystemRepresentation(pathURL
, true, path
, CFMaxPathLength
)) {
430 if (stat(path
, &statBuf
) != 0) {
431 // stat failed, but why?
432 if (thread_errno() == ENOENT
) {
435 return thread_errno();
439 isDirectory
= ((statBuf
.st_mode
& S_IFMT
) == S_IFDIR
);
443 if (exists
!= NULL
) {
444 *exists
= fileExists
;
447 if (posixMode
!= NULL
) {
450 *posixMode
= statBuf
.st_mode
;
460 *size
= statBuf
.st_size
;
467 if (modTime
!= NULL
) {
469 CFTimeInterval theTime
;
471 theTime
= kCFAbsoluteTimeIntervalSince1970
+ statBuf
.st_mtime
;
473 *modTime
= CFDateCreate(alloc
, theTime
);
479 if (ownerID
!= NULL
) {
482 *ownerID
= statBuf
.st_uid
;
489 if (dirContents
!= NULL
) {
490 if (fileExists
&& isDirectory
) {
492 CFMutableArrayRef contents
= _CFContentsOfDirectory(alloc
, path
, NULL
, pathURL
, NULL
);
495 *dirContents
= contents
;
507 // MF:!!! Should pull in the rest of the UniChar based path utils from Foundation.
508 #if defined(__MACH__) || defined(__svr4__) || defined(__hpux__) || defined(__LINUX__) || defined(__FREEBSD__)
509 #define UNIX_PATH_SEMANTICS
510 #elif defined(__WIN32__)
511 #define WINDOWS_PATH_SEMANTICS
513 #error Unknown platform
516 #if defined(WINDOWS_PATH_SEMANTICS)
517 #define CFPreferredSlash ((UniChar)'\\')
518 #elif defined(UNIX_PATH_SEMANTICS)
519 #define CFPreferredSlash ((UniChar)'/')
520 #elif defined(HFS_PATH_SEMANTICS)
521 #define CFPreferredSlash ((UniChar)':')
523 #error Cannot define NSPreferredSlash on this platform
526 #if defined(HFS_PATH_SEMANTICS)
527 #define HAS_DRIVE(S) (false)
528 #define HAS_NET(S) (false)
530 #define HAS_DRIVE(S) ((S)[1] == ':' && (('A' <= (S)[0] && (S)[0] <= 'Z') || ('a' <= (S)[0] && (S)[0] <= 'z')))
531 #define HAS_NET(S) ((S)[0] == '\\' && (S)[1] == '\\')
534 #if defined(WINDOWS_PATH_SEMANTICS)
535 #define IS_SLASH(C) ((C) == '\\' || (C) == '/')
536 #elif defined(UNIX_PATH_SEMANTICS)
537 #define IS_SLASH(C) ((C) == '/')
538 #elif defined(HFS_PATH_SEMANTICS)
539 #define IS_SLASH(C) ((C) == ':')
542 __private_extern__ Boolean
_CFIsAbsolutePath(UniChar
*unichars
, CFIndex length
) {
546 #if defined(WINDOWS_PATH_SEMANTICS)
547 if (unichars
[0] == '~') {
553 if (HAS_NET(unichars
)) {
559 if (IS_SLASH(unichars
[2]) && HAS_DRIVE(unichars
)) {
562 #elif defined(HFS_PATH_SEMANTICS)
563 return !IS_SLASH(unichars
[0]);
565 if (unichars
[0] == '~') {
568 if (IS_SLASH(unichars
[0])) {
575 __private_extern__ Boolean
_CFStripTrailingPathSlashes(UniChar
*unichars
, CFIndex
*length
) {
576 Boolean destHasDrive
= (1 < *length
) && HAS_DRIVE(unichars
);
577 CFIndex oldLength
= *length
;
578 while (((destHasDrive
&& 3 < *length
) || (!destHasDrive
&& 1 < *length
)) && IS_SLASH(unichars
[*length
- 1])) {
581 return (oldLength
!= *length
);
584 __private_extern__ Boolean
_CFAppendPathComponent(UniChar
*unichars
, CFIndex
*length
, CFIndex maxLength
, UniChar
*component
, CFIndex componentLength
) {
585 if (0 == componentLength
) {
588 if (maxLength
< *length
+ 1 + componentLength
) {
595 if (!IS_SLASH(unichars
[0])) {
596 unichars
[(*length
)++] = CFPreferredSlash
;
600 if (!HAS_DRIVE(unichars
) && !HAS_NET(unichars
)) {
601 unichars
[(*length
)++] = CFPreferredSlash
;
605 unichars
[(*length
)++] = CFPreferredSlash
;
608 memmove(unichars
+ *length
, component
, componentLength
* sizeof(UniChar
));
609 *length
+= componentLength
;
613 __private_extern__ Boolean
_CFAppendPathExtension(UniChar
*unichars
, CFIndex
*length
, CFIndex maxLength
, UniChar
*extension
, CFIndex extensionLength
) {
614 if (maxLength
< *length
+ 1 + extensionLength
) {
617 if ((0 < extensionLength
&& IS_SLASH(extension
[0])) || (1 < extensionLength
&& HAS_DRIVE(extension
))) {
620 _CFStripTrailingPathSlashes(unichars
, length
);
625 if (IS_SLASH(unichars
[0]) || unichars
[0] == '~') {
630 if (HAS_DRIVE(unichars
) || HAS_NET(unichars
)) {
635 if (IS_SLASH(unichars
[2]) && HAS_DRIVE(unichars
)) {
640 if (0 < *length
&& unichars
[0] == '~') {
642 Boolean hasSlash
= false;
643 for (idx
= 1; idx
< *length
; idx
++) {
644 if (IS_SLASH(unichars
[idx
])) {
653 unichars
[(*length
)++] = '.';
654 memmove(unichars
+ *length
, extension
, extensionLength
* sizeof(UniChar
));
655 *length
+= extensionLength
;
659 __private_extern__ Boolean
_CFTransmutePathSlashes(UniChar
*unichars
, CFIndex
*length
, UniChar replSlash
) {
660 CFIndex didx
, sidx
, scnt
= *length
;
661 sidx
= (1 < *length
&& HAS_NET(unichars
)) ? 2 : 0;
663 while (sidx
< scnt
) {
664 if (IS_SLASH(unichars
[sidx
])) {
665 unichars
[didx
++] = replSlash
;
666 for (sidx
++; sidx
< scnt
&& IS_SLASH(unichars
[sidx
]); sidx
++);
668 unichars
[didx
++] = unichars
[sidx
++];
672 return (scnt
!= didx
);
675 __private_extern__ CFIndex
_CFStartOfLastPathComponent(UniChar
*unichars
, CFIndex length
) {
680 for (idx
= length
- 1; idx
; idx
--) {
681 if (IS_SLASH(unichars
[idx
- 1])) {
685 if ((2 < length
) && HAS_DRIVE(unichars
)) {
691 __private_extern__ CFIndex
_CFLengthAfterDeletingLastPathComponent(UniChar
*unichars
, CFIndex length
) {
696 for (idx
= length
- 1; idx
; idx
--) {
697 if (IS_SLASH(unichars
[idx
- 1])) {
698 if ((idx
!= 1) && (!HAS_DRIVE(unichars
) || idx
!= 3)) {
704 if ((2 < length
) && HAS_DRIVE(unichars
)) {
710 __private_extern__ CFIndex
_CFStartOfPathExtension(UniChar
*unichars
, CFIndex length
) {
715 for (idx
= length
- 1; idx
; idx
--) {
716 if (IS_SLASH(unichars
[idx
- 1])) {
719 if (unichars
[idx
] != '.') {
722 if (idx
== 2 && HAS_DRIVE(unichars
)) {
730 __private_extern__ CFIndex
_CFLengthAfterDeletingPathExtension(UniChar
*unichars
, CFIndex length
) {
731 CFIndex start
= _CFStartOfPathExtension(unichars
, length
);
732 return ((0 < start
) ? start
: length
);
736 #undef UNIX_PATH_SEMANTICS
737 #undef WINDOWS_PATH_SEMANTICS
738 #undef HFS_PATH_SEMANTICS
739 #undef CFPreferredSlash