nsBookmarksService::WriteBookmarks(nsIFile* aBookmarksFile, nsIRDFDataSource* aDataSource, nsIRDFResource *aRoot)
nsBookmarksService::WriteBookmarks(nsIRDFDataSource *ds, nsIRDFResource *root)
nsBookmarksService::WriteBookmarks(nsIFile* aBookmarksFile,                                   nsIRDFDataSource* aDataSource,                                   nsIRDFResource *aRoot){    if (!aBookmarksFile || !aDataSource || !aRoot)        return NS_ERROR_NULL_POINTER;    nsCOMPtr<nsIFile> tempFile;    nsresult rv = aBookmarksFile->Clone(getter_AddRefs(tempFile));    NS_ENSURE_SUCCESS(rv, rv);    rv = tempFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, /*octal*/ 0600);    NS_ENSURE_SUCCESS(rv, rv);    nsCOMPtr<nsIOutputStream> out;    rv = NS_NewLocalFileOutputStream(getter_AddRefs(out),                                     tempFile,                                     PR_WRONLY,                                     /*octal*/ 0600,                                     0);    if (NS_FAILED(rv))    {        tempFile->Remove(PR_FALSE);        return rv;    }    // We need a buffered output stream for performance.    // See bug 202477.    nsCOMPtr<nsIOutputStream> strm;    rv = NS_NewBufferedOutputStream(getter_AddRefs(strm),                                    out,                                    4096);    if (NS_FAILED(rv))    {        tempFile->Remove(PR_FALSE);        return rv;    }    PRUint32 dummy;    rv = strm->Write(kFileIntro, sizeof(kFileIntro)-1, &dummy);    nsCOMArray<nsIRDFResource> parentArray;    rv |= WriteBookmarksContainer(aDataSource, strm, aRoot, 0, parentArray);    strm->Close();    out->Close();    if (NS_FAILED(rv))    {        tempFile->Remove(PR_FALSE);        return rv;    }    // If we wrote to the file successfully (i.e. if the disk wasn't full)     // then move the temp file to the bookmarks file so it takes its place.     PRBool equals;    rv = tempFile->Equals(aBookmarksFile, &equals);    if (NS_FAILED(rv)) {        tempFile->Remove(PR_FALSE);        return rv;    }    if (!equals) {        nsCOMPtr<nsIFile> bookmarkParentDir;        rv = aBookmarksFile->GetParent(getter_AddRefs(bookmarkParentDir));        if (NS_FAILED(rv)) {            tempFile->Remove(PR_FALSE);            return rv;        }        nsAutoString bookmarkLeafName;        rv = aBookmarksFile->GetLeafName(bookmarkLeafName);        if (NS_FAILED(rv)) {            tempFile->Remove(PR_FALSE);            return rv;        }        rv = tempFile->MoveTo(bookmarkParentDir, bookmarkLeafName);        if (NS_FAILED(rv))        {            tempFile->Remove(PR_FALSE);            return rv;        }    }    mDirty = PR_FALSE;    return NS_OK;}