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; char* orgProfileDir = nsnull;   nsInputStringStream stream(encodedProfileDir); nsPersistentFileDescriptor descriptor; stream >> descriptor; *profileDir = descriptor;  PR_FREEIF(encodedProfileDir);  orgProfileDir = PL_strdup(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()) {   nsIFileLocator* locator = nsnull;  rv = nsServiceManager::GetService(kFileLocatorCID, nsIFileLocator::GetIID(), (nsISupports**)&locator);  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); }  nsServiceManager::ReleaseService(kFileLocatorCID, locator); }  }  } 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, nsIFile **profileDir){    NS_ENSURE_ARG(profileName);       NS_ENSURE_ARG_POINTER(profileDir);    *profileDir = nsnull;    nsresult rv = NS_OK;#if defined(DEBUG_profile_verbose)    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<nsILocalFile>aProfileDir;    rv = aProfile->GetResolvedProfileDir(getter_AddRefs(aProfileDir));    if (NS_SUCCEEDED(rv) && aProfileDir)    {#ifdef XP_MAC        PRBool exists;        rv = aProfileDir->Exists(&exists);        if (NS_FAILED(rv)) return rv;        if (exists) {            PRBool inTrash;            nsCOMPtr<nsIFile> trashFolder;                                        rv = NS_GetSpecialDirectory(NS_MAC_TRASH_DIR, getter_AddRefs(trashFolder));            if (NS_FAILED(rv)) return rv;            rv = trashFolder->Contains(aProfileDir, PR_TRUE, &inTrash);            if (NS_FAILED(rv)) return rv;            if (inTrash) {                aProfileDir = nsnull;                rv = NS_ERROR_FILE_NOT_FOUND;            }          }#endif        *profileDir = aProfileDir;        NS_IF_ADDREF(*profileDir);    }    	delete aProfile;    return rv;}