Fix bug # 113894 (and 36972 and 79013 and probably others): get nsFileSpec usage out of RDF, and fix RDF persistence on Mac if there is a slash in the path. Various code from myself, darin, waterson and tingley. Various r/sr include darin, waterson

git-svn-id: svn://10.0.0.236/trunk@112612 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
rjc%netscape.com 2002-01-24 01:29:39 +00:00
parent 8c95eeda39
commit 1e20881de4
3 changed files with 73 additions and 37 deletions

View File

@ -68,18 +68,22 @@ NS_IMETHODIMP nsIOService::GetURLSpecFromFile(nsIFile* aFile, char * *aURL)
*aURL = nsnull;
nsresult rv;
char* ePath = nsnull;
nsXPIDLCString ePath;
nsCAutoString escPath;
rv = aFile->GetPath(&ePath);
rv = aFile->GetPath(getter_Copies(ePath));
if (NS_SUCCEEDED(rv)) {
SwapSlashColon(ePath);
SwapSlashColon((char*)ePath.get());
// Escape the path with the directory mask
rv = nsStdEscape(ePath, esc_Directory+esc_Forced, escPath);
if (NS_SUCCEEDED(rv)) {
// colons [originally slashes, before SwapSlashColon() usage above]
// need encoding
escPath.ReplaceSubstring(":", "%2F");
if (escPath[escPath.Length() - 1] != '/') {
PRBool dir;
@ -98,7 +102,6 @@ NS_IMETHODIMP nsIOService::GetURLSpecFromFile(nsIFile* aFile, char * *aURL)
rv = *aURL ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
}
CRTFREEIF(ePath);
return rv;
}
@ -136,6 +139,12 @@ NS_IMETHODIMP nsIOService::InitFileFromURLSpec(nsIFile* aFile, const char * aURL
{
nsStdEscape(directory, esc_Directory, component);
path += component;
// "%2F"s need to become slashes, while all other slashes need to
// become colons. If we start out by changing "%2F"s to colons, we
// can then rely on SwapSlashColon() to do what we need
path.ReplaceSubstring("%2F", ":");
SwapSlashColon((char*)path.get());
}
if (fileBaseName)

View File

@ -88,8 +88,11 @@
*/
#include "nsFileSpec.h"
#include "nsFileStream.h"
#include "nsIFileStreams.h"
#include "nsIOutputStream.h"
#include "nsIFile.h"
#include "nsIFileChannel.h"
#include "nsAString.h"
#include "nsIDTD.h"
#include "nsIRDFPurgeableDataSource.h"
#include "nsIInputStream.h"
@ -819,17 +822,27 @@ RDFXMLDataSourceImpl::Flush(void)
}
}
// XXX Replace this with channels someday soon...
nsFileURL url(mOriginalURLSpec, PR_TRUE);
nsFileSpec path(url);
// Is it a file? If so, we can write to it. Some day, it'd be nice
// if we didn't care what kind of stream this was...
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(mURL);
if (fileURL) {
nsCOMPtr<nsIFile> file;
fileURL->GetFile(getter_AddRefs(file));
nsOutputFileStream out(path);
if (! out.is_open())
return NS_ERROR_FAILURE;
if (file) {
nsCOMPtr<nsIOutputStream> out;
NS_NewLocalFileOutputStream(getter_AddRefs(out), file);
nsCOMPtr<nsIOutputStream> outIStream = out.GetIStream();
if (NS_FAILED(rv = Serialize(outIStream)))
goto done;
nsCOMPtr<nsIOutputStream> bufferedOut;
if (out)
NS_NewBufferedOutputStream(getter_AddRefs(bufferedOut), out, 4096);
if (bufferedOut) {
rv = Serialize(bufferedOut);
if (NS_FAILED(rv)) return rv;
}
}
}
mIsDirty = PR_FALSE;

View File

@ -42,8 +42,10 @@
*/
#include "nsIFileSpec.h"
#include "nsFileStream.h"
#include "nsNetUtil.h"
#include "nsIURI.h"
#include "nsIIOService.h"
#include "nsIOutputStream.h"
#include "nsIComponentManager.h"
#include "nsILocalStore.h"
#include "nsIRDFDataSource.h"
@ -396,34 +398,42 @@ LocalStoreImpl::LoadData()
// directory. Bomb if we can't find it.
nsCOMPtr<nsIFile> aFile;
nsCOMPtr<nsIFileSpec> tempSpec;
rv = NS_GetSpecialDirectory(NS_APP_LOCALSTORE_50_FILE, getter_AddRefs(aFile));
if (NS_FAILED(rv)) return rv;
// Turn the nsIFile into a nsFileSpec.
// This is evil! nsOutputFileStream needs
// to take an nsILocalFile (bug #46394)
nsXPIDLCString pathBuf;
rv = aFile->GetPath(getter_Copies(pathBuf));
nsCOMPtr<nsIURI> aURI;
rv = NS_NewFileURI(getter_AddRefs(aURI), aFile);
if (NS_FAILED(rv)) return rv;
nsFileSpec spec((const char *)pathBuf);
if (! spec.Exists()) {
{
nsOutputFileStream os(spec);
PRBool fileExistsFlag = PR_FALSE;
(void)aFile->Exists(&fileExistsFlag);
if (!fileExistsFlag) {
nsCOMPtr<nsIOutputStream> outStream;
rv = NS_NewLocalFileOutputStream(getter_AddRefs(outStream), aFile);
if (NS_FAILED(rv))
return rv;
os << "<?xml version=\"1.0\"?>" << nsEndl;
os << "<RDF:RDF xmlns:RDF=\"" << RDF_NAMESPACE_URI << "\"" << nsEndl;
os << " xmlns:NC=\"" << NC_NAMESPACE_URI << "\">" << nsEndl;
os << " <!-- Empty -->" << nsEndl;
os << "</RDF:RDF>" << nsEndl;
}
const char defaultRDF[] =
"<?xml version=\"1.0\"?>\n" \
"<RDF:RDF xmlns:RDF=\"" RDF_NAMESPACE_URI "\"\n" \
" xmlns:NC=\"" NC_NAMESPACE_URI "\">\n" \
" <!-- Empty -->\n" \
"</RDF:RDF>\n";
PRUint32 count;
rv = outStream->Write(defaultRDF, sizeof(defaultRDF)-1, &count);
if (NS_FAILED(rv))
return rv;
if (count != sizeof(defaultRDF)-1)
return NS_ERROR_UNEXPECTED;
// Okay, now see if the file exists _for real_. If it's still
// not there, it could be that the profile service gave us
// back a read-only directory. Whatever.
if (! spec.Exists())
fileExistsFlag = PR_FALSE;
(void)aFile->Exists(&fileExistsFlag);
if (!fileExistsFlag)
return NS_ERROR_UNEXPECTED;
}
@ -433,7 +443,11 @@ LocalStoreImpl::LoadData()
nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner, &rv);
if (NS_FAILED(rv)) return rv;
rv = remote->Init((const char*) nsFileURL(spec));
nsXPIDLCString spec;
rv = aURI->GetSpec(getter_Copies(spec));
if (NS_FAILED(rv)) return rv;
rv = remote->Init(spec);
if (NS_FAILED(rv)) return rv;
// Read the datasource synchronously.