forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFBundle.c
1770 lines (1551 loc) · 77.9 KB
/
CFBundle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* CFBundle.c
Copyright (c) 1999-2018, Apple Inc. and the Swift project authors
Portions Copyright (c) 2014-2018, Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
Responsibility: Tony Parker
*/
#include "CFBundle_Internal.h"
#include <CoreFoundation/CFPropertyList.h>
#include <CoreFoundation/CFNumber.h>
#include <CoreFoundation/CFSet.h>
#include <CoreFoundation/CFURLAccess.h>
#include <CoreFoundation/CFError.h>
#include <CoreFoundation/CFError_Private.h>
#include <string.h>
#include <CoreFoundation/CFPriv.h>
#include "CFInternal.h"
#include "CFRuntime_Internal.h"
#include <CoreFoundation/CFByteOrder.h>
#include "CFBundle_BinaryTypes.h"
#include <ctype.h>
#include <sys/stat.h>
#include <stdlib.h>
#if defined(BINARY_SUPPORT_DYLD)
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <crt_externs.h>
#endif /* BINARY_SUPPORT_DYLD */
#if defined(BINARY_SUPPORT_DLFCN)
#include <dlfcn.h>
#ifndef RTLD_FIRST
#define RTLD_FIRST 0
#endif
#endif /* BINARY_SUPPORT_DLFCN */
#if TARGET_OS_MAC
#include <fcntl.h>
#elif TARGET_OS_WIN32
#include <fcntl.h>
#include <io.h>
#endif
static void _CFBundleFlushBundleCachesAlreadyLocked(CFBundleRef bundle, Boolean alreadyLocked);
static void _CFBundleUnloadScheduledBundles(void);
#define LOG_BUNDLE_LOAD 0
// Public CFBundle Info plist keys
CONST_STRING_DECL(kCFBundleInfoDictionaryVersionKey, "CFBundleInfoDictionaryVersion")
CONST_STRING_DECL(kCFBundleExecutableKey, "CFBundleExecutable")
CONST_STRING_DECL(kCFBundleIdentifierKey, "CFBundleIdentifier")
CONST_STRING_DECL(kCFBundleVersionKey, "CFBundleVersion")
CONST_STRING_DECL(kCFBundleDevelopmentRegionKey, "CFBundleDevelopmentRegion")
CONST_STRING_DECL(kCFBundleLocalizationsKey, "CFBundleLocalizations")
// Private CFBundle Info plist keys, possible candidates for public constants
CONST_STRING_DECL(_kCFBundleAllowMixedLocalizationsKey, "CFBundleAllowMixedLocalizations")
CONST_STRING_DECL(_kCFBundleSupportedPlatformsKey, "CFBundleSupportedPlatforms")
CONST_STRING_DECL(_kCFBundleResourceSpecificationKey, "CFBundleResourceSpecification")
// Finder stuff
CONST_STRING_DECL(_kCFBundlePackageTypeKey, "CFBundlePackageType")
CONST_STRING_DECL(_kCFBundleSignatureKey, "CFBundleSignature")
CONST_STRING_DECL(_kCFBundleIconFileKey, "CFBundleIconFile")
CONST_STRING_DECL(_kCFBundleDocumentTypesKey, "CFBundleDocumentTypes")
CONST_STRING_DECL(_kCFBundleURLTypesKey, "CFBundleURLTypes")
// Keys that are usually localized in InfoPlist.strings
CONST_STRING_DECL(kCFBundleNameKey, "CFBundleName")
CONST_STRING_DECL(_kCFBundleDisplayNameKey, "CFBundleDisplayName")
CONST_STRING_DECL(_kCFBundleShortVersionStringKey, "CFBundleShortVersionString")
CONST_STRING_DECL(_kCFBundleGetInfoStringKey, "CFBundleGetInfoString")
CONST_STRING_DECL(_kCFBundleGetInfoHTMLKey, "CFBundleGetInfoHTML")
// Sub-keys for CFBundleDocumentTypes dictionaries
CONST_STRING_DECL(_kCFBundleTypeNameKey, "CFBundleTypeName")
CONST_STRING_DECL(_kCFBundleTypeRoleKey, "CFBundleTypeRole")
CONST_STRING_DECL(_kCFBundleTypeIconFileKey, "CFBundleTypeIconFile")
CONST_STRING_DECL(_kCFBundleTypeOSTypesKey, "CFBundleTypeOSTypes")
CONST_STRING_DECL(_kCFBundleTypeExtensionsKey, "CFBundleTypeExtensions")
CONST_STRING_DECL(_kCFBundleTypeMIMETypesKey, "CFBundleTypeMIMETypes")
// Sub-keys for CFBundleURLTypes dictionaries
CONST_STRING_DECL(_kCFBundleURLNameKey, "CFBundleURLName")
CONST_STRING_DECL(_kCFBundleURLIconFileKey, "CFBundleURLIconFile")
CONST_STRING_DECL(_kCFBundleURLSchemesKey, "CFBundleURLSchemes")
// Compatibility key names
CONST_STRING_DECL(_kCFBundleOldExecutableKey, "NSExecutable")
CONST_STRING_DECL(_kCFBundleOldInfoDictionaryVersionKey, "NSInfoPlistVersion")
CONST_STRING_DECL(_kCFBundleOldNameKey, "NSHumanReadableName")
CONST_STRING_DECL(_kCFBundleOldIconFileKey, "NSIcon")
CONST_STRING_DECL(_kCFBundleOldDocumentTypesKey, "NSTypes")
CONST_STRING_DECL(_kCFBundleOldShortVersionStringKey, "NSAppVersion")
// Compatibility CFBundleDocumentTypes key names
CONST_STRING_DECL(_kCFBundleOldTypeNameKey, "NSName")
CONST_STRING_DECL(_kCFBundleOldTypeRoleKey, "NSRole")
CONST_STRING_DECL(_kCFBundleOldTypeIconFileKey, "NSIcon")
CONST_STRING_DECL(_kCFBundleOldTypeExtensions1Key, "NSUnixExtensions")
CONST_STRING_DECL(_kCFBundleOldTypeExtensions2Key, "NSDOSExtensions")
CONST_STRING_DECL(_kCFBundleOldTypeOSTypesKey, "NSMacOSType")
// Internally used keys for loaded Info plists.
CONST_STRING_DECL(_kCFBundleInfoPlistURLKey, "CFBundleInfoPlistURL")
CONST_STRING_DECL(_kCFBundleRawInfoPlistURLKey, "CFBundleRawInfoPlistURL")
CONST_STRING_DECL(_kCFBundleNumericVersionKey, "CFBundleNumericVersion")
CONST_STRING_DECL(_kCFBundleExecutablePathKey, "CFBundleExecutablePath")
CONST_STRING_DECL(_kCFBundleResourcesFileMappedKey, "CSResourcesFileMapped")
CONST_STRING_DECL(_kCFBundleCFMLoadAsBundleKey, "CFBundleCFMLoadAsBundle")
// Keys used by NSBundle for loaded Info plists.
CONST_STRING_DECL(_kCFBundlePrincipalClassKey, "NSPrincipalClass")
static _CFMutex CFBundleGlobalDataLock = _CF_MUTEX_STATIC_INITIALIZER;
static CFMutableDictionaryRef _bundlesByIdentifier = NULL;
static CFMutableDictionaryRef _bundlesByURL = NULL;
static CFMutableArrayRef _allBundles = NULL;
static CFMutableSetRef _bundlesToUnload = NULL;
static Boolean _scheduledBundlesAreUnloading = false;
static CFBundleRef _CFBundleCreate(CFAllocatorRef allocator, CFURLRef bundleURL, Boolean doFinalProcessing, Boolean unique, Boolean addToTables);
static void _CFBundleEnsureBundlesUpToDateWithHint(CFStringRef hint);
static void _CFBundleEnsureAllBundlesUpToDate(void);
static void _CFBundleEnsureBundleExistsForImagePath(CFStringRef imagePath, Boolean permissive);
static void _CFBundleEnsureBundlesExistForImagePaths(CFArrayRef imagePaths);
#pragma mark -
#if !DEPLOYMENT_RUNTIME_OBJC && !TARGET_OS_WIN32 && !TARGET_OS_ANDROID
// Functions and constants for FHS bundles:
#define _CFBundleFHSDirectory_share CFSTR("share")
static Boolean _CFBundleURLIsForFHSInstalledBundle(CFURLRef bundleURL) {
// Paths of this form are FHS installed bundles:
// <anywhere>/share/<name>.resources
CFStringRef extension = CFURLCopyPathExtension(bundleURL);
CFURLRef parentURL = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorSystemDefault, bundleURL);
CFStringRef containingDirectoryName = parentURL ? CFURLCopyLastPathComponent(parentURL) : NULL;
Boolean isFHSBundle =
extension &&
containingDirectoryName &&
CFEqual(extension, _CFBundleSiblingResourceDirectoryExtension) &&
CFEqual(containingDirectoryName, _CFBundleFHSDirectory_share);
if (extension) CFRelease(extension);
if (parentURL) CFRelease(parentURL);
if (containingDirectoryName) CFRelease(containingDirectoryName);
return isFHSBundle;
}
#endif // !DEPLOYMENT_RUNTIME_OBJC && !TARGET_OS_WIN32 && !TARGET_OS_ANDROID
CF_CROSS_PLATFORM_EXPORT Boolean _CFBundleSupportsFHSBundles() {
#if !DEPLOYMENT_RUNTIME_OBJC && !TARGET_OS_WIN32 && !TARGET_OS_ANDROID
return true;
#else
return false;
#endif
}
CF_CROSS_PLATFORM_EXPORT Boolean _CFBundleSupportsFreestandingBundles() {
#if !DEPLOYMENT_RUNTIME_OBJC
return true;
#else
return false;
#endif
}
#pragma mark -
CF_PRIVATE os_log_t _CFBundleResourceLogger(void) {
static os_log_t _log;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_log = os_log_create("com.apple.CFBundle", "resources");
});
return _log;
}
CF_PRIVATE os_log_t _CFBundleLocalizedStringLogger(void) {
static os_log_t _log;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_log = os_log_create("com.apple.CFBundle", "strings");
});
return _log;
}
#pragma mark -
#if TARGET_OS_OSX
// Some apps may rely on the fact that CFBundle used to allow bundle objects to be deallocated (despite handing out unretained pointers via CFBundleGetBundleWithIdentifier or CFBundleGetAllBundles). To remain compatible even in the face of unsafe behavior, we can optionally use unsafe-unretained memory management for holding on to bundles.
static Boolean _useUnsafeUnretainedTables(void) {
return false;
}
#endif
#pragma mark -
#pragma mark Bundle Tables
static void _CFBundleAddToTables(CFBundleRef bundle) {
if (bundle->_isUnique) return;
CFStringRef bundleID = CFBundleGetIdentifier(bundle);
_CFMutexLock(&CFBundleGlobalDataLock);
// Add to the _allBundles list
if (!_allBundles) {
CFArrayCallBacks callbacks = kCFTypeArrayCallBacks;
#if TARGET_OS_OSX
if (_useUnsafeUnretainedTables()) {
callbacks.retain = NULL;
callbacks.release = NULL;
}
#endif
// The _allBundles array holds a strong reference on the bundle.
// It does this to prevent a race on bundle deallocation / creation. See: <rdar://problem/6606482> CFBundle isn't thread-safe in RR mode
// Also, the existence of the CFBundleGetBundleWithIdentifier / CFBundleGetAllBundles API means that any bundle we hand out from there must be permanently retained, or callers will potentially have an object that can be deallocated out from underneath them.
_allBundles = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &callbacks);
}
CFArrayAppendValue(_allBundles, bundle);
// Add to the table that maps urls to bundles
if (!_bundlesByURL) {
CFDictionaryValueCallBacks nonRetainingDictionaryValueCallbacks = kCFTypeDictionaryValueCallBacks;
nonRetainingDictionaryValueCallbacks.retain = NULL;
nonRetainingDictionaryValueCallbacks.release = NULL;
_bundlesByURL = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &nonRetainingDictionaryValueCallbacks);
}
CFDictionarySetValue(_bundlesByURL, bundle->_url, bundle);
// Add to the table that maps identifiers to bundles
if (bundleID) {
CFMutableArrayRef bundlesWithThisID = NULL;
CFBundleRef existingBundle = NULL;
if (!_bundlesByIdentifier) {
_bundlesByIdentifier = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
bundlesWithThisID = (CFMutableArrayRef)CFDictionaryGetValue(_bundlesByIdentifier, bundleID);
if (bundlesWithThisID) {
CFIndex i, count = CFArrayGetCount(bundlesWithThisID);
UInt32 existingVersion, newVersion = CFBundleGetVersionNumber(bundle);
for (i = 0; i < count; i++) {
existingBundle = (CFBundleRef)CFArrayGetValueAtIndex(bundlesWithThisID, i);
existingVersion = CFBundleGetVersionNumber(existingBundle);
// If you load two bundles with the same identifier and the same version, the last one wins.
if (newVersion >= existingVersion) break;
}
CFArrayInsertValueAtIndex(bundlesWithThisID, i, bundle);
// We've encountered a bundle with this ID already.
// Output some additional info here. It may not be an error (adding a newer version of a bundle is supported), so use os_log_debug.
os_log_debug(_CFBundleResourceLogger(), "More than one bundle with the same identifier has been added: %{public}@", bundlesWithThisID);
} else {
CFArrayCallBacks nonRetainingArrayCallbacks = kCFTypeArrayCallBacks;
nonRetainingArrayCallbacks.retain = NULL;
nonRetainingArrayCallbacks.release = NULL;
bundlesWithThisID = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &nonRetainingArrayCallbacks);
CFArrayAppendValue(bundlesWithThisID, bundle);
CFDictionarySetValue(_bundlesByIdentifier, bundleID, bundlesWithThisID);
CFRelease(bundlesWithThisID);
}
}
_CFMutexUnlock(&CFBundleGlobalDataLock);
}
static void _CFBundleRemoveFromTables(CFBundleRef bundle, CFURLRef bundleURL, CFStringRef bundleID) {
// Since we no longer allow bundles to be removed from tables, this method does nothing. Modifying the tables during deallocation is risky because if the caller has over-released the bundle object then we will deadlock on the global lock.
#if TARGET_OS_OSX
if (_useUnsafeUnretainedTables()) {
// Except for special cases of unsafe-unretained, where we must clean up the table or risk handing out a zombie object. There may still be outstanding pointers to these bundes (e.g. the result of CFBundleGetBundleWithIdentifier) but there is nothing we can do about that after this point.
// Unique bundles aren't in the tables anyway
if (bundle->_isUnique) return;
_CFMutexLock(&CFBundleGlobalDataLock);
// Remove from the table of all bundles
if (_allBundles) {
CFIndex i = CFArrayGetFirstIndexOfValue(_allBundles, CFRangeMake(0, CFArrayGetCount(_allBundles)), bundle);
if (i >= 0) CFArrayRemoveValueAtIndex(_allBundles, i);
}
// Remove from the table that maps urls to bundles
if (bundleURL && _bundlesByURL) {
CFBundleRef bundleForURL = (CFBundleRef)CFDictionaryGetValue(_bundlesByURL, bundleURL);
if (bundleForURL == bundle) CFDictionaryRemoveValue(_bundlesByURL, bundleURL);
}
// Remove from the table that maps identifiers to bundles
if (bundleID && _bundlesByIdentifier) {
CFMutableArrayRef bundlesWithThisID = (CFMutableArrayRef)CFDictionaryGetValue(_bundlesByIdentifier, bundleID);
if (bundlesWithThisID) {
CFIndex count = CFArrayGetCount(bundlesWithThisID);
while (count-- > 0) if (bundle == (CFBundleRef)CFArrayGetValueAtIndex(bundlesWithThisID, count)) CFArrayRemoveValueAtIndex(bundlesWithThisID, count);
if (0 == CFArrayGetCount(bundlesWithThisID)) CFDictionaryRemoveValue(_bundlesByIdentifier, bundleID);
}
}
_CFMutexUnlock(&CFBundleGlobalDataLock);
}
#endif
}
static CFBundleRef _CFBundleGetFromTables(CFStringRef bundleID) {
CFBundleRef result = NULL, bundle;
_CFMutexLock(&CFBundleGlobalDataLock);
if (_bundlesByIdentifier && bundleID) {
// Note that this array is maintained in descending order by version number
CFArrayRef bundlesWithThisID = (CFArrayRef)CFDictionaryGetValue(_bundlesByIdentifier, bundleID);
if (bundlesWithThisID) {
CFIndex i, count = CFArrayGetCount(bundlesWithThisID);
if (count > 0) {
// First check for loaded bundles so we will always prefer a loaded to an unloaded bundle
for (i = 0; !result && i < count; i++) {
bundle = (CFBundleRef)CFArrayGetValueAtIndex(bundlesWithThisID, i);
if (CFBundleIsExecutableLoaded(bundle)) result = bundle;
}
// If no loaded bundle, simply take the first item in the array, i.e. the one with the latest version number
if (!result) result = (CFBundleRef)CFArrayGetValueAtIndex(bundlesWithThisID, 0);
}
}
}
_CFMutexUnlock(&CFBundleGlobalDataLock);
return result;
}
static CFBundleRef _CFBundleCopyFromTablesForURL(CFURLRef url) {
/*
If you're curious why this doesn't consult the main bundle URL, consider the case where you have a directory structure like this:
/S/L/F/Foo.framework/Foo
/S/L/F/Foo.framework/food (a daemon for the Foo framework)
And the main executable is 'food'.
This flat structure can happen on iOS, with its more common version 3 bundles. In this scenario, there are theoretically two different bundles that could be returned: one for the framework, one for the daemon. They have the same URL but different bundle identifiers.
Since the main bundle is not part of the bundle tables, we can support this scenario by having the _bundlesByURL data structure hold the bundle for URL "/S/L/F/Foo.framework/Foo" and _mainBundle (in CFBundle_Main.c) hold the bundle for URL "/S/L/F/Foo.framework/food".
*/
CFBundleRef result = NULL;
_CFMutexLock(&CFBundleGlobalDataLock);
if (_bundlesByURL) result = (CFBundleRef)CFDictionaryGetValue(_bundlesByURL, url);
if (result && !result->_url) {
result = NULL;
CFDictionaryRemoveValue(_bundlesByURL, url);
}
if (result) CFRetain(result);
_CFMutexUnlock(&CFBundleGlobalDataLock);
return result;
}
#pragma mark -
CF_PRIVATE uint8_t _CFBundleEffectiveLayoutVersion(CFBundleRef bundle) {
uint8_t localVersion = bundle->_version;
// exclude type 0 bundles with no binary (or CFM binary) and no Info.plist, since they give too many false positives
if (0 == localVersion) {
CFDictionaryRef infoDict = CFBundleGetInfoDictionary(bundle);
if (!infoDict || 0 == CFDictionaryGetCount(infoDict)) {
#if defined(BINARY_SUPPORT_DYLD)
CFURLRef executableURL = CFBundleCopyExecutableURL(bundle);
if (executableURL) {
if (bundle->_binaryType == __CFBundleUnknownBinary) bundle->_binaryType = _CFBundleGrokBinaryType(executableURL);
if (bundle->_binaryType == __CFBundleCFMBinary || bundle->_binaryType == __CFBundleUnreadableBinary) {
localVersion = 4;
} else {
bundle->_resourceData._executableLacksResourceFork = true;
}
CFRelease(executableURL);
} else {
localVersion = 4;
}
#else
CFURLRef executableURL = CFBundleCopyExecutableURL(bundle);
if (executableURL) {
CFRelease(executableURL);
} else {
localVersion = 4;
}
#endif /* BINARY_SUPPORT_DYLD */
}
}
return localVersion;
}
CFBundleRef _CFBundleCreateIfLooksLikeBundle(CFAllocatorRef allocator, CFURLRef url) {
// It is assumed that users of this SPI do not want this bundle to persist forever.
CFBundleRef bundle = _CFBundleCreateUnique(allocator, url);
if (bundle) {
uint8_t localVersion = _CFBundleEffectiveLayoutVersion(bundle);
if (3 == localVersion || 4 == localVersion) {
CFRelease(bundle);
bundle = NULL;
}
}
return bundle;
}
CF_EXPORT Boolean _CFBundleURLLooksLikeBundle(CFURLRef url) {
Boolean result = false;
CFBundleRef bundle = _CFBundleCreateIfLooksLikeBundle(kCFAllocatorSystemDefault, url);
if (bundle) {
result = true;
CFRelease(bundle);
}
return result;
}
CFBundleRef _CFBundleGetMainBundleIfLooksLikeBundle(void) {
CFBundleRef mainBundle = CFBundleGetMainBundle();
if (mainBundle && (3 == mainBundle->_version || 4 == mainBundle->_version)) mainBundle = NULL;
return mainBundle;
}
Boolean _CFBundleMainBundleInfoDictionaryComesFromResourceFork(void) {
CFBundleRef mainBundle = CFBundleGetMainBundle();
return (mainBundle && mainBundle->_resourceData._infoDictionaryFromResourceFork);
}
CF_EXPORT CFBundleRef _CFBundleCreateIfMightBeBundle(CFAllocatorRef allocator, CFURLRef url) {
// This function is obsolete
CFBundleRef bundle = CFBundleCreate(allocator, url);
return bundle;
}
static void _CFBundleFlushBundleCachesAlreadyLocked(CFBundleRef bundle, Boolean alreadyLocked) {
CFDictionaryRef oldInfoDict = bundle->_infoDict;
CFTypeRef val;
bundle->_infoDict = NULL;
if (bundle->_localInfoDict) {
CFRelease(bundle->_localInfoDict);
bundle->_localInfoDict = NULL;
}
if (bundle->_infoPlistUrl) {
CFRelease(bundle->_infoPlistUrl);
bundle->_infoPlistUrl = NULL;
}
if (bundle->_developmentRegion) {
CFRelease(bundle->_developmentRegion);
bundle->_developmentRegion = NULL;
}
if (bundle->_executablePath) {
CFRelease(bundle->_executablePath);
bundle->_executablePath = NULL;
}
if (bundle->_searchLanguages) {
CFRelease(bundle->_searchLanguages);
bundle->_searchLanguages = NULL;
}
if (bundle->_stringTable) {
CFRelease(bundle->_stringTable);
bundle->_stringTable = NULL;
}
CFBundleGetInfoDictionary(bundle);
if (oldInfoDict) {
if (!bundle->_infoDict) bundle->_infoDict = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
val = CFDictionaryGetValue(oldInfoDict, _kCFBundlePrincipalClassKey);
if (val) CFDictionarySetValue((CFMutableDictionaryRef)bundle->_infoDict, _kCFBundlePrincipalClassKey, val);
CFRelease(oldInfoDict);
}
_CFBundleFlushQueryTableCache(bundle);
}
CF_EXPORT void _CFBundleFlushBundleCaches(CFBundleRef bundle) {
_CFBundleFlushBundleCachesAlreadyLocked(bundle, false);
}
#if !(__OBJC__ || __OBJC2__)
static void _CFBundleArrayApplyFlushBundleCaches(const void *value, void *unusedContext) {
_CFBundleFlushBundleCachesAlreadyLocked((CFBundleRef)value, true);
}
#endif
CF_PRIVATE void _CFBundleFlushAllBundleCaches(void) {
_CFMutexLock(&CFBundleGlobalDataLock);
#if __OBJC__ || __OBJC2__
for (id value in (id)_allBundles) {
_CFBundleFlushBundleCachesAlreadyLocked((CFBundleRef)value, true);
}
#else
CFArrayApplyFunction(_allBundles, CFRangeMake(0, CFArrayGetCount(_allBundles)), &_CFBundleArrayApplyFlushBundleCaches, NULL);
#endif
_CFMutexUnlock(&CFBundleGlobalDataLock);
}
CFBundleRef CFBundleGetBundleWithIdentifier(CFStringRef bundleID) {
CFBundleRef result = NULL;
if (bundleID) {
CFBundleRef main = CFBundleGetMainBundle();
if (main) {
CFDictionaryRef infoDict = CFBundleGetInfoDictionary(main);
if (infoDict) {
CFStringRef mainBundleID = CFDictionaryGetValue(infoDict, kCFBundleIdentifierKey);
if (mainBundleID && CFGetTypeID(mainBundleID) == CFStringGetTypeID() && CFEqual(mainBundleID, bundleID)) {
return main;
}
}
}
result = _CFBundleGetFromTables(bundleID);
#if TARGET_OS_MAC
if (!result) {
// Try to create the bundle for the caller and try again
void *p = __builtin_return_address(0);
if (p) {
CFStringRef imagePath = _CFBundleCopyLoadedImagePathForPointer(p);
// If the pointer is in Foundation, we were called by NSBundle and we should look one more frame up the stack for a hint
if (imagePath && CFStringHasSuffix(imagePath, CFSTR("/Foundation"))) {
CFRelease(imagePath);
// Reset to NULL in case p is null below, that will make us fall back through the right path
imagePath = NULL;
p = __builtin_return_address(1);
if (p) {
imagePath = _CFBundleCopyLoadedImagePathForPointer(p);
}
}
if (imagePath) {
// As this is a fast-path check, we don't want to be aggressive about assuming that the executable URL that we may have received from DYLD via _CFBundleCopyLoadedImagePathForPointer should be turned into a framework URL. If we do, then it is possible that an executable located inside a framework bundle which does not normally link that framework will cause us to load it unintentionally (31165928).
// For example:
// Foo.framework/
// Resources/
// HelperTool
//
// With permissive set to 'true', this would make the 'Foo.framework' bundle exist, but there is no reason why HelperTool is required to have loaded Foo.framework.
_CFBundleEnsureBundleExistsForImagePath(imagePath, false);
CFRelease(imagePath);
}
// Now try again
result = _CFBundleGetFromTables(bundleID);
}
}
#endif
if (!result) {
// Try to guess the bundle from the identifier and try again
_CFBundleEnsureBundlesUpToDateWithHint(bundleID);
// Now try again
result = _CFBundleGetFromTables(bundleID);
}
}
if (!result) {
// Make sure all bundles have been created and try again.
_CFBundleEnsureAllBundlesUpToDate();
// Now try again
result = _CFBundleGetFromTables(bundleID);
}
return result;
}
static CFStringRef __CFBundleCopyDescription(CFTypeRef cf) {
char buff[CFMaxPathSize];
CFStringRef path = NULL, binaryType = NULL, retval = NULL;
if (((CFBundleRef)cf)->_url && CFURLGetFileSystemRepresentation(((CFBundleRef)cf)->_url, true, (uint8_t *)buff, CFMaxPathSize)) path = CFStringCreateWithFileSystemRepresentation(kCFAllocatorSystemDefault, buff);
switch (((CFBundleRef)cf)->_binaryType) {
case __CFBundleCFMBinary:
binaryType = CFSTR("");
break;
case __CFBundleDYLDExecutableBinary:
binaryType = CFSTR("executable, ");
break;
case __CFBundleDYLDBundleBinary:
binaryType = CFSTR("bundle, ");
break;
case __CFBundleDYLDFrameworkBinary:
binaryType = CFSTR("framework, ");
break;
case __CFBundleDLLBinary:
binaryType = CFSTR("DLL, ");
break;
case __CFBundleUnreadableBinary:
binaryType = CFSTR("");
break;
default:
binaryType = CFSTR("");
break;
}
if (((CFBundleRef)cf)->_plugInData._isPlugIn) {
retval = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("CFBundle/CFPlugIn %p <%@> (%@%@loaded)"), cf, path, binaryType, ((CFBundleRef)cf)->_isLoaded ? CFSTR("") : CFSTR("not "));
} else {
retval = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("CFBundle %p <%@> (%@%@loaded)"), cf, path, binaryType, ((CFBundleRef)cf)->_isLoaded ? CFSTR("") : CFSTR("not "));
}
if (path) CFRelease(path);
return retval;
}
static void __CFBundleDeallocate(CFTypeRef cf) {
CFBundleRef bundle = (CFBundleRef)cf;
CFURLRef bundleURL;
CFStringRef bundleID = NULL;
__CFGenericValidateType(cf, CFBundleGetTypeID());
bundleURL = bundle->_url;
bundle->_url = NULL;
if (bundle->_infoDict) bundleID = (CFStringRef)CFDictionaryGetValue(bundle->_infoDict, kCFBundleIdentifierKey);
_CFBundleRemoveFromTables(bundle, bundleURL, bundleID);
CFBundleUnloadExecutable(bundle);
_CFBundleDeallocatePlugIn(bundle);
if (bundleURL) {
CFRelease(bundleURL);
}
if (bundle->_infoDict) CFRelease(bundle->_infoDict);
if (bundle->_localInfoDict) CFRelease(bundle->_localInfoDict);
if (bundle->_searchLanguages) CFRelease(bundle->_searchLanguages);
if (bundle->_executablePath) CFRelease(bundle->_executablePath);
if (bundle->_developmentRegion) CFRelease(bundle->_developmentRegion);
if (bundle->_infoPlistUrl) CFRelease(bundle->_infoPlistUrl);
if (bundle->_stringTable) CFRelease(bundle->_stringTable);
if (bundle->_bundleBasePath) CFRelease(bundle->_bundleBasePath);
if (bundle->_queryTable) CFRelease(bundle->_queryTable);
if (bundle->_localizations) CFRelease(bundle->_localizations);
if (bundle->_resourceDirectoryContents) CFRelease(bundle->_resourceDirectoryContents);
if (bundle->_additionalResourceBundles) CFRelease(bundle->_additionalResourceBundles);
_CFMutexDestroy(&(bundle->_bundleLoadingLock));
}
const CFRuntimeClass __CFBundleClass = {
_kCFRuntimeScannedObject,
"CFBundle",
NULL, // init
NULL, // copy
__CFBundleDeallocate,
NULL, // equal
NULL, // hash
NULL, //
__CFBundleCopyDescription
};
// From CFBundle_Resources.c
CF_PRIVATE void _CFBundleResourcesInitialize(void);
CFTypeID CFBundleGetTypeID(void) {
return _kCFRuntimeIDCFBundle;
}
// TODO: Remove this SPI, it appears no one is using it
// <rdar://problem/30925651> Remove _CFBundleGetExistingBundleWithBundleURL
CFBundleRef _CFBundleGetExistingBundleWithBundleURL(CFURLRef bundleURL) {
CFBundleRef bundle = NULL;
char buff[CFMaxPathSize];
CFURLRef newURL = NULL;
if (!CFURLGetFileSystemRepresentation(bundleURL, true, (uint8_t *)buff, CFMaxPathSize)) return NULL;
newURL = CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault, (uint8_t *)buff, strlen(buff), true);
if (!newURL) newURL = (CFURLRef)CFRetain(bundleURL);
// First check the main bundle; otherwise fallback to the other tables
CFBundleRef main = CFBundleGetMainBundle();
if (main->_url && newURL && CFEqual(main->_url, newURL)) {
return main;
}
bundle = _CFBundleCopyFromTablesForURL(newURL);
if (bundle) CFRelease(bundle);
CFRelease(newURL);
return bundle;
}
static CFBundleRef _CFBundleCreate(CFAllocatorRef allocator, CFURLRef bundleURL, Boolean doFinalProcessing, Boolean unique, Boolean addToTables) {
CFBundleRef bundle = NULL;
char buff[CFMaxPathSize];
Boolean exists = false;
SInt32 mode = 0;
CFURLRef newURL = NULL;
uint8_t localVersion = 0;
if (!CFURLGetFileSystemRepresentation(bundleURL, true, (uint8_t *)buff, CFMaxPathSize)) return NULL;
newURL = CFURLCreateFromFileSystemRepresentation(allocator, (uint8_t *)buff, strlen(buff), true);
if (!newURL) newURL = (CFURLRef)CFRetain(bundleURL);
// Don't go searching for the URL in the tables if the bundle is unique or the main bundle (addToTables == false)
if (!unique && addToTables) {
bundle = _CFBundleCopyFromTablesForURL(newURL);
if (bundle) {
CFRelease(newURL);
return bundle;
}
}
localVersion = _CFBundleGetBundleVersionForURL(newURL);
if (localVersion == 3) {
SInt32 res = _CFGetPathProperties(allocator, (char *)buff, &exists, &mode, NULL, NULL, NULL, NULL);
#if TARGET_OS_WIN32
if (!(res == 0 && exists && ((mode & S_IFMT) == S_IFDIR))) {
// 2nd chance at finding a bundle path - remove the last path component (e.g., mybundle.resources) and try again
CFURLRef shorterPath = CFURLCreateCopyDeletingLastPathComponent(allocator, newURL);
CFRelease(newURL);
newURL = shorterPath;
res = _CFGetFileProperties(allocator, newURL, &exists, &mode, NULL, NULL, NULL, NULL);
}
#endif
if (res == 0) {
if (!exists || ((mode & S_IFMT) != S_IFDIR)) {
CFRelease(newURL);
return NULL;
}
} else {
CFRelease(newURL);
return NULL;
}
}
bundle = (CFBundleRef)_CFRuntimeCreateInstance(allocator, CFBundleGetTypeID(), sizeof(struct __CFBundle) - sizeof(CFRuntimeBase), NULL);
if (!bundle) {
CFRelease(newURL);
return NULL;
}
bundle->_url = newURL;
#if !DEPLOYMENT_RUNTIME_OBJC && !TARGET_OS_WIN32 && !TARGET_OS_ANDROID
bundle->_isFHSInstalledBundle = _CFBundleURLIsForFHSInstalledBundle(newURL);
#endif
bundle->_version = localVersion;
bundle->_infoDict = NULL;
bundle->_localInfoDict = NULL;
bundle->_searchLanguages = NULL;
bundle->_executablePath = NULL;
bundle->_developmentRegion = NULL;
bundle->_infoPlistUrl = NULL;
bundle->_developmentRegionCalculated = 0;
#if defined(BINARY_SUPPORT_DYLD)
/* We'll have to figure it out later */
bundle->_binaryType = __CFBundleUnknownBinary;
#elif defined(BINARY_SUPPORT_DLL)
/* We support DLL only */
bundle->_binaryType = __CFBundleDLLBinary;
bundle->_hModule = NULL;
#else
/* We'll have to figure it out later */
bundle->_binaryType = __CFBundleUnknownBinary;
#endif /* BINARY_SUPPORT_DYLD */
bundle->_isLoaded = false;
bundle->_sharesStringsFiles = false;
bundle->_isUnique = unique;
#if TARGET_OS_MAC
if (!__CFgetenv("CFBundleDisableStringsSharing") &&
(strncmp(buff, "/System/Library/Frameworks", 26) == 0) &&
(strncmp(buff + strlen(buff) - 10, ".framework", 10) == 0)) bundle->_sharesStringsFiles = true;
#endif
bundle->_connectionCookie = NULL;
bundle->_handleCookie = NULL;
bundle->_imageCookie = NULL;
bundle->_moduleCookie = NULL;
bundle->_resourceData._executableLacksResourceFork = false;
bundle->_resourceData._infoDictionaryFromResourceFork = false;
bundle->_stringTable = NULL;
bundle->_plugInData._isPlugIn = false;
bundle->_plugInData._loadOnDemand = false;
bundle->_plugInData._isDoingDynamicRegistration = false;
bundle->_plugInData._instanceCount = 0;
bundle->_plugInData._registeredFactory = false;
bundle->_plugInData._factories = NULL;
_CFMutexCreate(&(bundle->_bundleLoadingLock));
bundle->_lock = CFLockInit;
bundle->_resourceDirectoryContents = NULL;
bundle->_localizations = NULL;
bundle->_lookedForLocalizations = false;
bundle->_queryLock = CFLockInit;
bundle->_queryTable = NULL;
CFURLRef absoURL = CFURLCopyAbsoluteURL(bundle->_url);
bundle->_bundleBasePath = CFURLCopyFileSystemPath(absoURL, PLATFORM_PATH_STYLE);
CFRelease(absoURL);
bundle->_additionalResourceLock = CFLockInit;
bundle->_additionalResourceBundles = NULL;
CFBundleGetInfoDictionary(bundle);
// Do this so that we can use the dispatch_once on the ivar of this bundle safely
OSMemoryBarrier();
if (addToTables) {
_CFBundleAddToTables(bundle);
}
if (doFinalProcessing) {
_CFBundleInitPlugIn(bundle);
}
return bundle;
}
CFBundleRef CFBundleCreate(CFAllocatorRef allocator, CFURLRef bundleURL) {
if (NULL == bundleURL) return NULL;
// _CFBundleCreate doesn't know about the main bundle, so we have to check that first. If the URL passed in is the same as the main bundle, then we'll need to return that bundle first.
// Result will be nil if the bundleURL passed in happened to have been the main bundle.
// As a fallback, check now to see if the main bundle URL is equal to bundleURL. If so, return that bundle instead of nil (32988858).
CFBundleRef main = CFBundleGetMainBundle();
if (main && main->_url && CFEqual(main->_url, bundleURL)) {
CFRetain(main);
return main;
}
return _CFBundleCreate(allocator, bundleURL, true, false, true);
}
CFBundleRef _CFBundleCreateUnique(CFAllocatorRef allocator, CFURLRef bundleURL) {
// This function can never return an existing CFBundleRef object.
return _CFBundleCreate(allocator, bundleURL, true, true, false);
}
CF_PRIVATE CFBundleRef _CFBundleCreateMain(CFAllocatorRef allocator, CFURLRef mainBundleURL) {
// Do not add the main bundle to tables
return _CFBundleCreate(allocator, mainBundleURL, false, false, false);
}
CFArrayRef CFBundleCreateBundlesFromDirectory(CFAllocatorRef alloc, CFURLRef directoryURL, CFStringRef bundleType) {
CFMutableArrayRef bundles = CFArrayCreateMutable(alloc, 0, &kCFTypeArrayCallBacks);
CFArrayRef URLs = _CFCreateContentsOfDirectory(alloc, NULL, NULL, directoryURL, bundleType);
if (URLs) {
CFIndex i, c = CFArrayGetCount(URLs);
CFURLRef curURL;
CFBundleRef curBundle;
for (i = 0; i < c; i++) {
curURL = (CFURLRef)CFArrayGetValueAtIndex(URLs, i);
curBundle = CFBundleCreate(alloc, curURL);
if (curBundle) CFArrayAppendValue(bundles, curBundle);
}
CFRelease(URLs);
}
return bundles;
}
CFURLRef CFBundleCopyBundleURL(CFBundleRef bundle) {
if (bundle->_url) CFRetain(bundle->_url);
return bundle->_url;
}
UInt32 CFBundleGetVersionNumber(CFBundleRef bundle) {
CFDictionaryRef infoDict = CFBundleGetInfoDictionary(bundle);
CFNumberRef versionValue = (CFNumberRef)CFDictionaryGetValue(infoDict, _kCFBundleNumericVersionKey);
if (!versionValue || CFGetTypeID(versionValue) != CFNumberGetTypeID()) return 0;
UInt32 vers = 0;
CFNumberGetValue(versionValue, kCFNumberSInt32Type, &vers);
return vers;
}
CFStringRef CFBundleGetDevelopmentRegion(CFBundleRef bundle) {
dispatch_once(&bundle->_developmentRegionCalculated, ^{
CFStringRef devRegion = NULL;
CFDictionaryRef infoDict = CFBundleGetInfoDictionary(bundle);
if (infoDict) {
devRegion = (CFStringRef)CFDictionaryGetValue(infoDict, kCFBundleDevelopmentRegionKey);
if (devRegion && (CFGetTypeID(devRegion) != CFStringGetTypeID() || CFStringGetLength(devRegion) == 0)) {
devRegion = NULL;
}
}
if (devRegion) bundle->_developmentRegion = (CFStringRef)CFRetain(devRegion);
});
return bundle->_developmentRegion;
}
Boolean _CFBundleGetHasChanged(CFBundleRef bundle) {
// This SPI isn't very useful, so now we just return true (30211007)
return true;
}
void _CFBundleSetStringsFilesShared(CFBundleRef bundle, Boolean flag) {
bundle->_sharesStringsFiles = flag;
}
Boolean _CFBundleGetStringsFilesShared(CFBundleRef bundle) {
return bundle->_sharesStringsFiles;
}
CF_EXPORT CFURLRef CFBundleCopySupportFilesDirectoryURL(CFBundleRef bundle) {
CFURLRef bundleURL = bundle->_url;
uint8_t version = bundle->_version;
CFURLRef result = NULL;
if (bundleURL) {
if (1 == version) {
result = CFURLCreateWithString(kCFAllocatorSystemDefault, _CFBundleSupportFilesURLFromBase1, bundleURL);
} else if (2 == version) {
result = CFURLCreateWithString(kCFAllocatorSystemDefault, _CFBundleSupportFilesURLFromBase2, bundleURL);
} else {
result = (CFURLRef)CFRetain(bundleURL);
}
}
return result;
}
CF_PRIVATE CFURLRef _CFBundleCopyResourcesDirectoryURLInDirectory(CFURLRef bundleURL, uint8_t version) {
CFURLRef result = NULL;
if (bundleURL) {
if (0 == version) {
result = CFURLCreateWithString(kCFAllocatorSystemDefault, _CFBundleResourcesURLFromBase0, bundleURL);
} else if (1 == version) {
result = CFURLCreateWithString(kCFAllocatorSystemDefault, _CFBundleResourcesURLFromBase1, bundleURL);
} else if (2 == version) {
result = CFURLCreateWithString(kCFAllocatorSystemDefault, _CFBundleResourcesURLFromBase2, bundleURL);
} else {
result = (CFURLRef)CFRetain(bundleURL);
}
}
return result;
}
CF_EXPORT CFURLRef CFBundleCopyResourcesDirectoryURL(CFBundleRef bundle) {
return _CFBundleCopyResourcesDirectoryURLInDirectory(bundle->_url, bundle->_version);
}
CF_PRIVATE CFURLRef _CFBundleCopyAppStoreReceiptURLInDirectory(CFURLRef bundleURL, uint8_t version) {
CFURLRef result = NULL;
if (bundleURL) {
if (0 == version) {
result = CFURLCreateWithString(kCFAllocatorSystemDefault, _CFBundleAppStoreReceiptURLFromBase0, bundleURL);
} else if (1 == version) {
result = CFURLCreateWithString(kCFAllocatorSystemDefault, _CFBundleAppStoreReceiptURLFromBase1, bundleURL);
} else if (2 == version) {
result = CFURLCreateWithString(kCFAllocatorSystemDefault, _CFBundleAppStoreReceiptURLFromBase2, bundleURL);
}
}
return result;
}
CFURLRef _CFBundleCopyAppStoreReceiptURL(CFBundleRef bundle) {
return _CFBundleCopyAppStoreReceiptURLInDirectory(bundle->_url, bundle->_version);
}
CF_CROSS_PLATFORM_EXPORT CFStringRef _CFBundleCopyExecutablePath(CFBundleRef bundle) {
return _CFBundleCopyExecutableName(bundle, NULL, NULL);
}
CF_PRIVATE CFStringRef _CFBundleCopyExecutableName(CFBundleRef bundle, CFURLRef url, CFDictionaryRef infoDict) {
CFStringRef executableName = NULL;
if (!infoDict && bundle) infoDict = CFBundleGetInfoDictionary(bundle);
if (!url && bundle) url = bundle->_url;
if (infoDict) {
// Figure out the name of the executable.
// First try for the new key in the plist.
executableName = (CFStringRef)CFDictionaryGetValue(infoDict, kCFBundleExecutableKey);
// Second try for the old key in the plist.
if (!executableName) executableName = (CFStringRef)CFDictionaryGetValue(infoDict, _kCFBundleOldExecutableKey);
if (executableName && CFGetTypeID(executableName) == CFStringGetTypeID() && CFStringGetLength(executableName) > 0) {
CFRetain(executableName);
} else {
executableName = NULL;
}
}
if (!executableName && url) {
// Third, take the name of the bundle itself (with path extension stripped)
CFURLRef absoluteURL = CFURLCopyAbsoluteURL(url);
CFStringRef bundlePath = CFURLCopyFileSystemPath(absoluteURL, PLATFORM_PATH_STYLE);
CFRelease(absoluteURL);
if (bundlePath) {
CFIndex len = CFStringGetLength(bundlePath);
CFIndex startOfBundleName = _CFStartOfLastPathComponent2(bundlePath);
CFIndex endOfBundleName = _CFLengthAfterDeletingPathExtension2(bundlePath);
if (startOfBundleName <= len && endOfBundleName <= len && startOfBundleName < endOfBundleName) {
executableName = CFStringCreateWithSubstring(kCFAllocatorSystemDefault, bundlePath, CFRangeMake(startOfBundleName, endOfBundleName - startOfBundleName));
}
CFRelease(bundlePath);
}