2 * Copyright (c) 2000-2004,2011,2014 Apple 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@
28 #include <security_utilities/utilities.h>
29 #include <utilities/SecCFRelease.h>
30 #include <CoreFoundation/CoreFoundation.h>
31 #include <dispatch/dispatch.h>
36 static CFMutableDictionaryRef gCacheDictionaryRef
= NULL
;
37 static dispatch_once_t gDictionaryCreated
= 0;
38 static dispatch_queue_t gSerializeQueue
;
40 char *cached_realpath(const char * file_name
, char * resolved_name
)
42 dispatch_once(&gDictionaryCreated
,
44 gCacheDictionaryRef
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
45 gSerializeQueue
= dispatch_queue_create("com.apple.SecurityLookupCacheQueue", DISPATCH_QUEUE_SERIAL
);
49 __block
char* valueToReturn
= NULL
;
51 dispatch_sync(gSerializeQueue
,
53 // Put a maximum limit on the size of this cache.
54 CFIndex entryCount
= CFDictionaryGetCount(gCacheDictionaryRef
);
56 // make the incoming name a string
57 CFStringRef input
= CFStringCreateWithCString(NULL
, file_name
, kCFStringEncodingUTF8
);
58 if (entryCount
< OPEN_MAX
&& input
!= NULL
) // is it acceptable to use the cache?
60 // see if we can find that string in our dictionary
61 CFStringRef output
= (CFStringRef
) CFDictionaryGetValue(gCacheDictionaryRef
, input
);
65 // the string is not in our cache, so use realpath
66 valueToReturn
= realpath(file_name
, resolved_name
);
67 if (valueToReturn
!= NULL
) // no error, so continue
69 // make a new entry in the dictionary for our string
70 output
= CFStringCreateWithCString(NULL
, valueToReturn
, kCFStringEncodingUTF8
);
71 CFDictionaryAddValue(gCacheDictionaryRef
, input
, output
);
77 char* valueToFree
= NULL
;
79 // we need to extract the value from the output
81 // figure out how big to make our buffer
82 CFIndex size
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(output
), kCFStringEncodingUTF8
) + 1; // account for NULL termination
84 // if the user has passed in a buffer, use it. If not, allocate our own
86 // technically, we don't know the size of the buffer that the
87 // user has passed in. It has to be large enough to hold the
88 // string, however, so we can use size as an estimator. The
89 // result will be the same, however: If the user didn't supply
90 // enough memory, he will crash. We behave exactly the same
91 // as realpath, which is the idea.
92 char *buffer
= resolved_name
;
94 // allocate a buffer if none was passed in
96 valueToFree
= buffer
= (char*) malloc(size
);
99 if (buffer
!= NULL
) // check to see if malloc failed earlier
101 if (!CFStringGetCString(output
, buffer
, size
, kCFStringEncodingUTF8
))
103 free((void*) valueToFree
);
104 valueToReturn
= NULL
;
108 valueToReturn
= buffer
;
115 valueToReturn
= realpath(file_name
, resolved_name
);
118 CFReleaseSafe(input
);
121 return valueToReturn
;