char*   leafname = nsnull; nsCOMPtr<nsIFile>      tmpFile;
nsCOMPtr<nsIFile>      renamedDoomedFile;
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)    char*   leafname = nsnull;    nsCOMPtr<nsIFile>      tmpFile;    nsCOMPtr<nsILocalFile> tmpLocalFile;    nsCOMPtr<nsIFile> parent;        doomedFile->Clone(getter_AddRefs(tmpFile));    tmpFile->Exists(&flagExists);    if ( flagExists )    {        tmpLocalFile = do_QueryInterface(tmpFile, &rv); // Convert to an nsILocalFile        MakeUnique(tmpLocalFile);                       //   for the call to MakeUnique                tmpLocalFile->GetParent(getter_AddRefs(parent)); //get the parent for later use in MoveTo        tmpLocalFile->GetLeafName(&leafname);            //this is the new "unique" leafname        doomedFile->Clone(getter_AddRefs(tmpFile));  // recreate the tmpFile as a doomedFile        tmpFile->MoveTo(parent, leafname);                tmpFile = parent;              //MoveTo on Mac doesn't reset the tmpFile object to         tmpFile->Append(leafname);     //the new name or location. That's why there's this                                        //explict assignment and Append call.        if (leafname) nsCRT::free( leafname );    }    // if doomedFile is gone move new file into place    doomedFile->Exists(&flagExists);    if ( !flagExists )    {        nsCOMPtr<nsIFile> parentofFinalFile;        nsCOMPtr<nsIFile> parentofReplacementFile;        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_WARN_IF_FALSE( 0, "File unpacked into a non-dest dir" );            replacementFile->GetLeafName(&leafname);            rv = replacementFile->MoveTo(parentofFinalFile, leafname);        }        else        	rv = NS_OK;        	        doomedFile->GetLeafName(&leafname);        replacementFile->GetParent(getter_AddRefs(parent));        if ( NS_SUCCEEDED(rv) )            rv = replacementFile->MoveTo(parent, leafname );        if ( NS_SUCCEEDED(rv) )         {            // we replaced the old file OK, now we have to            // get rid of it permanently            result = DeleteFileNowOrSchedule( tmpFile );        }        else        {            // couldn't rename file, try to put old file back            tmpFile->GetParent(getter_AddRefs(parent));            tmpFile->MoveTo(parent, leafname);        }        nsCRT::free( leafname );    }    return result;}