if (dbfile.Exists()) { char buf[512]; PRInt32 bytesread;
nsXPIDLCString filePath; rv = historyFile->GetPath(getter_Copies(filePath)); NS_ENSURE_SUCCESS(rv, rv);
nsGlobalHistory::OpenDB(){  nsresult rv;  NS_WITH_SERVICE(nsIProfile, profile, kProfileCID, &rv);  if (NS_FAILED(rv)) return rv;  nsFileSpec dbfile;  rv = profile->GetCurrentProfileDir(&dbfile);  if (NS_FAILED(rv)) return rv;  // Urgh. No profile directory. Use tmp instead.  if (! dbfile.Exists()) {    // XXX Probably should warn user before doing this. Oh well.    dbfile = nsSpecialSystemDirectory(nsSpecialSystemDirectory::OS_TemporaryDirectory);  }  dbfile += "history.dat";  // Leaving XPCOM, entering Mork. The IID is a lie; the component  // manager appears to be used solely to get dynamic loading of the  // Mork DLL.  nsIMdbFactory* factory;  static NS_DEFINE_CID(kMorkCID, NS_MORK_CID);#ifdef BIENVENU_HAS_FIXED_THE_FACTORY  nsCOMPtr<nsIMdbFactoryFactory> factoryfactory;  rv = nsComponentManager::CreateInstance(kMorkCID,                                          nsnull,                                          nsCOMTypeInfo<nsIMdbFactoryFactory>::GetIID(),                                          getter_AddRefs(factoryfactory));  if (NS_FAILED(rv)) return rv;  rv = factoryfactory->GetMdbFactory(&factory);#else  rv = nsComponentManager::CreateInstance(kMorkCID,                                          nsnull,                                          nsCOMTypeInfo<nsISupports>::GetIID(),                                          (void**) &factory);#endif  NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create mork factory factory");  if (NS_FAILED(rv)) return rv;  mdb_err err;  err = factory->MakeEnv(nsnull, &mEnv);  NS_ASSERTION((err == 0), "unable to create mdb env");  if (err != 0) return NS_ERROR_FAILURE;  if (dbfile.Exists()) {    char buf[512];    PRInt32 bytesread;    {      // This is in its own block scope so that the file will be      // closed when the scope exits.      nsIOFileStream strm(dbfile);      bytesread = strm.read(buf, sizeof(buf));      if (strm.failed()) return strm.error();    }    mdbYarn header = { buf, bytesread, sizeof(buf), 0, 0, nsnull };    mdb_bool canopen;    mdbYarn outfmt;    err = factory->CanOpenFilePort(mEnv, dbfile.GetNativePathCString(), &header, &canopen, &outfmt);    // XXX possible that format out of date, in which case we should    // just re-write the file.    if ((err != 0) || !canopen) return NS_ERROR_FAILURE;    nsIMdbThumb* thumb = nsnull;    mdbOpenPolicy policy = { { 0, 0 }, 0, 0 };    err = factory->OpenFileStore(mEnv, nsnull, dbfile.GetNativePathCString(), &policy, &thumb);    if ((err != 0) || !thumb) return NS_ERROR_FAILURE;    mdb_count total;    mdb_count current;    mdb_bool done;    mdb_bool broken;    do {      err = thumb->DoMore(mEnv, &total, &current, &done, &broken);    } while ((err == 0) && !broken && !done);    if ((err == 0) && done) {      err = factory->ThumbToOpenStore(mEnv, thumb, &mStore);    }    thumb->CutStrongRef(mEnv);    thumb = nsnull;    if ((err != 0) || !done) return NS_ERROR_FAILURE;    rv = CreateTokens();    if (NS_FAILED(rv)) return rv;    mdbOid oid = { kToken_HistoryRowScope, 1 };    err = mStore->GetTable(mEnv, &oid, &mTable);    if (err != 0) return NS_ERROR_FAILURE;  }  else {    mdbOpenPolicy policy = { { 0, 0 }, 0, 0 };    err = factory->CreateNewFileStore(mEnv, 0, dbfile.GetNativePathCString(), &policy, &mStore);    if (err != 0) return NS_ERROR_FAILURE;    rv = CreateTokens();    if (NS_FAILED(rv)) return rv;    // Create the one and only table in the history db    err = mStore->NewTable(mEnv, kToken_HistoryRowScope, kToken_HistoryKind, PR_TRUE, nsnull, &mTable);    if (err != 0) return NS_ERROR_FAILURE;    // Force a commit now to get it written out.    nsMdbPtr<nsIMdbThumb> thumb(mEnv);    err = mStore->LargeCommit(mEnv, getter_Acquires(thumb));    if (err != 0) return NS_ERROR_FAILURE;    mdb_count total;    mdb_count current;    mdb_bool done;    mdb_bool broken;    do {      err = thumb->DoMore(mEnv, &total, &current, &done, &broken);    } while ((err == 0) && !broken && !done);    if ((err != 0) || !done) return NS_ERROR_FAILURE;  }  return NS_OK;}