tmpLocalFile->GetParent(getter_AddRefs(parent)); tmpLocalFile->GetNativeLeafName(uniqueLeafName);
tmpLocalFile->GetNativeLeafName(uniqueLeafName);
PRInt32 ReplaceFileNow(nsIFile* replacementFile, nsIFile* doomedFile ){    PRBool flagExists, flagIsEqual;    nsresult rv;    // replacement file must exist, doomed file doesn't have to    replacementFile->Exists(&flagExists);    if ( !flagExists )        return nsInstall::DOES_NOT_EXIST;    // don't have to do anything if the files are the same    replacementFile->Equals(doomedFile, &flagIsEqual);    if ( flagIsEqual )        return nsInstall::SUCCESS;    PRInt32 result = nsInstall::ACCESS_DENIED;    // first try to rename the doomed file out of the way (if it exists)    nsCOMPtr<nsIFile>      renamedDoomedFile;    nsCOMPtr<nsILocalFile> tmpLocalFile;    nsCOMPtr<nsIFile> parent;        doomedFile->Clone(getter_AddRefs(renamedDoomedFile));    renamedDoomedFile->Exists(&flagExists);    if ( flagExists )    {#ifdef XP_MACOSX        // If we clone an nsIFile, and move the clone, the FSRef of the *original*        // file is not what you would expect - it points to the moved file. This        // is despite the fact that the two FSRefs are independent objects. Until        // the OS X file impl is changed to not use FSRefs, need to do this (see        // bug 200024).        nsCOMPtr<nsILocalFile> doomedFileLocal(do_QueryInterface(doomedFile));        nsCAutoString doomedFilePath;        rv = doomedFileLocal->GetNativePath(doomedFilePath);        if (NS_FAILED(rv))            return nsInstall::UNEXPECTED_ERROR;#endif        tmpLocalFile = do_QueryInterface(renamedDoomedFile, &rv); // Convert to an nsILocalFile        //get the leafname so we can convert its extension to .old        nsAutoString leafname;        nsCAutoString uniqueLeafName;        tmpLocalFile->GetLeafName(leafname);        // do not RFind on the native charset! UTF8 or Unicode are OK        PRInt32 extpos = leafname.RFindChar('.');        if (extpos != -1)        {            // We found the extension;             leafname.Truncate(extpos + 1); //strip off the old extension        }        leafname.Append(NS_LITERAL_STRING("old"));                //Now reset the leafname        tmpLocalFile->SetLeafName(leafname);        tmpLocalFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0644);        tmpLocalFile->GetParent(getter_AddRefs(parent)); //get the parent for later use in MoveTo        tmpLocalFile->GetNativeLeafName(uniqueLeafName);//this is the new "unique" leafname        rv = doomedFile->Clone(getter_AddRefs(renamedDoomedFile));// Reset renamedDoomed file so doomedfile isn't                                                                   //   changed during the MoveTo call        if (NS_FAILED(rv)) result = nsInstall::UNEXPECTED_ERROR;        rv = renamedDoomedFile->MoveToNative(parent, uniqueLeafName);                if (NS_FAILED(rv))            return nsInstall::UNEXPECTED_ERROR;        // The implementation of MoveToNative() on some platforms (osx and win32) resets        // the object to the 'moved to' filename.  This is incorrect behavior.  This        // implementation will be fixed in the future.  We need to take into account that        // fix by setting renamedDoomedFile to the filename that it was moved to above.        // See bug 200024.        rv = renamedDoomedFile->SetNativeLeafName(uniqueLeafName);                if (NS_FAILED(rv))            return nsInstall::UNEXPECTED_ERROR;#ifdef XP_MACOSX        rv = doomedFileLocal->InitWithNativePath(doomedFilePath);        if (NS_FAILED(rv))            result = nsInstall::UNEXPECTED_ERROR;#endif        if (result == nsInstall::UNEXPECTED_ERROR)            return result;    }    // if doomedFile is gone move new file into place    doomedFile->Exists(&flagExists);    if ( !flagExists )    {        nsCOMPtr<nsIFile> parentofFinalFile;        nsCOMPtr<nsIFile> parentofReplacementFile;        nsCAutoString leafname;        doomedFile->GetParent(getter_AddRefs(parentofFinalFile));        replacementFile->GetParent(getter_AddRefs(parentofReplacementFile));        // XXX looks dangerous, the replacement file name may NOT be unique in the        // target directory if we have to move it! Either we should never move the        // files like this (i.e. error if not in the same dir) or we need to take        // a little more care in the move.        parentofReplacementFile->Equals(parentofFinalFile, &flagIsEqual);        if(!flagIsEqual)        {            NS_WARNING("File unpacked into a non-dest dir" );            replacementFile->GetNativeLeafName(leafname);            rv = replacementFile->MoveToNative(parentofFinalFile, leafname);            if (NS_FAILED(rv))                return nsInstall::UNEXPECTED_ERROR;            // The implementation of MoveToNative() on some platforms (osx and win32) resets            // the object to the 'moved to' filename.  This is incorrect behavior.  This            // implementation will be fixed in the future.  We need to take into account that            // fix by setting renamedDoomedFile to the filename that it was moved to above.            // See bug 200024.            rv = renamedDoomedFile->SetNativeLeafName(leafname);                    if (NS_FAILED(rv))                return nsInstall::UNEXPECTED_ERROR;        }        	        rv = doomedFile->GetNativeLeafName(leafname);        if ( NS_SUCCEEDED(rv))            rv = replacementFile->MoveToNative(parentofReplacementFile, leafname );        if ( NS_SUCCEEDED(rv) )         {            // we replaced the old file OK, now we have to            // get rid of it if it was renamed out of the way            result = DeleteFileNowOrSchedule( renamedDoomedFile );        }        else        {            // couldn't rename file, try to put old file back            renamedDoomedFile->GetParent(getter_AddRefs(parent));            renamedDoomedFile->MoveToNative(parent, leafname);        }    }    return result;}