Skip to content

Commit

Permalink
Move old process name domain to new bundle identifier domain automati…
Browse files Browse the repository at this point in the history
…cally
  • Loading branch information
rfm committed Dec 28, 2024
1 parent e7716e6 commit 950e2a5
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions Source/NSUserDefaults.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ @interface NSUserDefaultsWin32 : NSUserDefaults
static NSUserDefaults *sharedDefaults = nil;
static NSDictionary *argumentsDictionary = nil;
static NSString *bundleIdentifier = nil;
static NSString *processName = nil;
static NSRecursiveLock *classLock = nil;
static NSLock *syncLock = nil;
static BOOL initializing = NO;
Expand Down Expand Up @@ -662,6 +663,7 @@ + (void) atExit
{
DESTROY(sharedDefaults);
DESTROY(bundleIdentifier);
DESTROY(processName);
DESTROY(argumentsDictionary);
DESTROY(classLock);
DESTROY(syncLock);
Expand Down Expand Up @@ -713,16 +715,17 @@ + (void) initialize
argumentsDictionary = [NSDictionary new];
[self registerAtExit];

processName = [[[NSProcessInfo processInfo] processName] copy];
key = [GSPrivateInfoDictionary(nil) objectForKey: @"CFBundleIdentifier"];
if (NO == [key isKindOfClass: [NSString class]])
{
/* No bundle identifier found: use the process name instead.
*/
key = [[NSProcessInfo processInfo] processName];
key = processName;
if (NO == [key isKindOfClass: [NSString class]])
{
fprintf(stderr, "+[NSUserDefaults initialize] unable to"
" determine bundle identfier or process name.\n");
" determine bundle identifier or process name.\n");
key = nil;
}
}
Expand Down Expand Up @@ -2629,7 +2632,7 @@ - (BOOL) _readDefaults
}
domainName = [domainName stringByDeletingPathExtension];

/* We may what to know what domains are being loaded.
/* We may want to know what domains are being loaded.
*/
NSDebugMLog(@"domain name: %@", domainName);

Expand Down Expand Up @@ -2872,10 +2875,32 @@ - (BOOL) synchronize
{
NSFileManager *mgr;
NSMutableDictionary *disk;
BOOL found;

mgr = [NSFileManager defaultManager];
disk = nil;
if (YES == [mgr isReadableFileAtPath: path])
found = [mgr isReadableFileAtPath: path];

/* The default domain name for a program changed from being the name
* of the executable to being the bundle identifier (if available).
* If the domain file does not exist for the new name but does exist
* for the old name, we move the file to the modern location.
*/
if (NO == found
&& [name isEqual: bundleIdentifier]
&& NO == [name isEqual: processName])
{
NSString *pp = [owner _directory];

pp = [pp stringByAppendingPathComponent: processName];
pp = [pp stringByAppendingPathExtension: @"plist"];
if ([mgr isReadableFileAtPath: pp])
{
found = [mgr movePath: pp toPath: path handler: nil];
}
}

if (found)
{
NSData *data;

Expand Down

1 comment on commit 950e2a5

@triplef
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this! One thing less to worry about in our own code.

Please sign in to comment.