From 117902cd24b79f98e916add66facbc9ba8af6ecd Mon Sep 17 00:00:00 2001 From: "sspitzer%mozilla.org" Date: Wed, 28 Mar 2007 05:19:06 +0000 Subject: [PATCH] fix for bug #375102: make localstore.rdf use nsISafeOutputStream (similar to bug #252053: make bookmarks use nsISafeOutputStream) r=vlad --- rdf/base/src/nsRDFXMLDataSource.cpp | 33 +++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/rdf/base/src/nsRDFXMLDataSource.cpp b/rdf/base/src/nsRDFXMLDataSource.cpp index 0c3325ab9ef..f5380e38716 100644 --- a/rdf/base/src/nsRDFXMLDataSource.cpp +++ b/rdf/base/src/nsRDFXMLDataSource.cpp @@ -757,17 +757,32 @@ RDFXMLDataSourceImpl::rdfXMLFlush(nsIURI *aURI) nsCOMPtr file; fileURL->GetFile(getter_AddRefs(file)); if (file) { - // if file doesn't exist, create it - (void)file->Create(nsIFile::NORMAL_FILE_TYPE, 0666); - + // get a safe output stream, so we don't clobber the datasource file unless + // all the writes succeeded. nsCOMPtr out; - rv = NS_NewLocalFileOutputStream(getter_AddRefs(out), file); + rv = NS_NewSafeLocalFileOutputStream(getter_AddRefs(out), + file, + PR_WRONLY | PR_CREATE_FILE, + /*octal*/ 0600, + 0); + if (NS_FAILED(rv)) return rv; + nsCOMPtr bufferedOut; - if (out) - NS_NewBufferedOutputStream(getter_AddRefs(bufferedOut), out, 4096); - if (bufferedOut) { - rv = Serialize(bufferedOut); - if (NS_FAILED(rv)) return rv; + rv = NS_NewBufferedOutputStream(getter_AddRefs(bufferedOut), out, 4096); + if (NS_FAILED(rv)) return rv; + + rv = Serialize(bufferedOut); + if (NS_FAILED(rv)) return rv; + + // All went ok. Maybe except for problems in Write(), but the stream detects + // that for us + nsCOMPtr safeStream = do_QueryInterface(bufferedOut, &rv); + if (NS_FAILED(rv)) return rv; + + rv = safeStream->Finish(); + if (NS_FAILED(rv)) { + NS_WARNING("failed to save datasource file! possible dataloss"); + return rv; } } }