if (aProfile == nsnull)
rv = m_reg->GetSubtree(nsIRegistry::Common, "Profiles", &key);  if (NS_SUCCEEDED(rv)) { nsIRegistry::Key newKey;   rv = m_reg->GetSubtree(key, profileName, &newKey);  if (NS_SUCCEEDED(rv)) { char* encodedProfileDir = nsnull;   rv = m_reg->GetString( newKey, "directory", &encodedProfileDir );  if (NS_SUCCEEDED(rv)) { char* isMigrated = nsnull; nsXPIDLCString orgProfileDir;   nsInputStringStream stream(encodedProfileDir); nsPersistentFileDescriptor descriptor; stream >> descriptor; *profileDir = descriptor;  PR_FREEIF(encodedProfileDir);  orgProfileDir.Copy(profileDir->GetCString());   m_reg->GetString( newKey, "migrated", &isMigrated);   if (PL_strcmp(isMigrated, "yes") == 0) { rv = m_reg->SetString(key, "CurrentProfile", profileName);  if (NS_FAILED(rv)) { #if defined(DEBUG_profile) printf("Profiles : Couldn't set CurrentProfile Name.\n"); #endif }  nsFileSpec tmpFileSpec(orgProfileDir); if (!tmpFileSpec.Exists()) {   NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv);  if (NS_FAILED(rv) || !locator) return NS_ERROR_FAILURE;  nsIFileSpec* profDefaultsDir; rv = locator->GetFileLocation(nsSpecialFileSpec::App_ProfileDefaultsFolder50, &profDefaultsDir);  if (NS_FAILED(rv) || !profDefaultsDir) return NS_ERROR_FAILURE;  nsFileSpec defaultsDirSpec;  profDefaultsDir->GetFileSpec(&defaultsDirSpec); NS_RELEASE(profDefaultsDir);   if (defaultsDirSpec.Exists()) { defaultsDirSpec.RecursiveCopy(tmpFileSpec); }  }  }  } else { #if defined(DEBUG_profile) printf("Profiles : Couldn't get Profile directory name.\n"); #endif } } else { #if defined(DEBUG_profile) printf("Profiles : Couldn't get profileName subtree.\n"); #endif } } else { #if defined(DEBUG_profile) printf("Registry : Couldn't get Profiles subtree.\n"); #endif } m_reg->Close(); } else { #if defined(DEBUG_profile) printf("Couldn't open registry.\n"); #endif }  } else { #if defined(DEBUG_profile) printf("Registry Object is NULL.\n"); #endif
NS_IMETHODIMP nsProfile::GetProfileDir(const PRUnichar *profileName, nsFileSpec* profileDir){    NS_ENSURE_ARG_POINTER(profileName);       NS_ENSURE_ARG_POINTER(profileDir);    nsresult rv = NS_OK;#if defined(DEBUG_profile)    printf("ProfileManager : GetProfileDir\n");#endif    ProfileStruct    *aProfile;    rv = gProfileDataAccess->GetValue(profileName, &aProfile);    if (NS_FAILED(rv)) return rv;	if (aProfile == nsnull)		return NS_ERROR_FAILURE;    nsCOMPtr<nsIFileSpec>spec;    rv = NS_NewFileSpec(getter_AddRefs(spec));    if (NS_FAILED(rv)) return rv;    nsCAutoString profileLocation;    profileLocation.AssignWithConversion(aProfile->profileLocation);    rv = spec->SetPersistentDescriptorString(profileLocation.GetBuffer());    if (NS_FAILED(rv)) return rv;    rv = spec->GetFileSpec(profileDir);    if (NS_FAILED(rv)) return rv;                               // Set this to be a current profile only if it is a 5.0 profile    if (aProfile->isMigrated.EqualsWithConversion(REGISTRY_YES_STRING))    {        gProfileDataAccess->SetCurrentProfile(profileName);                                          nsFileSpec tmpFileSpec(*profileDir);        if (!tmpFileSpec.Exists())         {            // Get profile defaults folder..            NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv);                                                   if (NS_FAILED(rv) || !locator)                return NS_ERROR_FAILURE;                                                    nsCOMPtr <nsIFileSpec> profDefaultsDir;                        rv = locator->GetFileLocation(                             nsSpecialFileSpec::App_ProfileDefaultsFolder50,                              getter_AddRefs(profDefaultsDir));            if (NS_FAILED(rv) || !profDefaultsDir)                return NS_ERROR_FAILURE;                                                    nsFileSpec defaultsDirSpec;            profDefaultsDir->GetFileSpec(&defaultsDirSpec);            // Need a separate hack for Mac. For now app folder is the fall back on Mac.            nsFilePath(tmpFileSpec.GetNativePathCString(), PR_TRUE);                                                    // Copy contents from defaults folder.            if (defaultsDirSpec.Exists())                defaultsDirSpec.RecursiveCopy(tmpFileSpec);        }    }	delete aProfile;    return rv;}