NS_ERROR("failed to open cookies.txt for writing");
COOKIE_Write(){  if (!sCookieChanged) {    return NS_OK;  }  nsresult rv;  nsCOMPtr<nsIFile> file;  rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(file));  if (NS_SUCCEEDED(rv)) {    rv = file->AppendNative(NS_LITERAL_CSTRING(kCookieFileName));  }  if (NS_FAILED(rv)) {    return rv;  }  nsCOMPtr<nsIOutputStream> fileOutputStream;  rv = NS_NewLocalFileOutputStream(getter_AddRefs(fileOutputStream), file);  if (NS_FAILED(rv)) {    return rv;  }  // get a buffered output stream 4096 bytes big, to optimize writes  nsCOMPtr<nsIOutputStream> bufferedOutputStream;  rv = NS_NewBufferedOutputStream(getter_AddRefs(bufferedOutputStream), fileOutputStream, 4096);  if (NS_FAILED(rv)) {    return rv;  }  static const char kHeader[] =      "# HTTP Cookie File\n"      "# http://www.netscape.com/newsref/std/cookie_spec.html\n"      "# This is a generated file!  Do not edit.\n"      "# To delete cookies, use the Cookie Manager.\n\n";  // note: kTrue and kFalse have leading/trailing tabs already added  static const char kTrue[] = "\tTRUE\t";  static const char kFalse[] = "\tFALSE\t";  static const char kTab[] = "\t";  static const char kNew[] = "\n";  // create a new nsVoidArray to hold the cookie list, and sort it  // such that least-recently-used cookies come last  nsVoidArray sortedCookieList;  sortedCookieList = *sCookieList;  sortedCookieList.Sort(compareCookiesByLRU, nsnull);  bufferedOutputStream->Write(kHeader, sizeof(kHeader) - 1, &rv);  /* file format is:   *   * host \t isDomain \t path \t secure \t expires \t name \t cookie   *   * isDomain is "TRUE" or "FALSE"   * isSecure is "TRUE" or "FALSE"   * expires is a PRInt64 integer   * note 1: cookie can contain tabs.   * note 2: cookies are written in order of lastAccessed time:   *         most-recently used come first; least-recently-used come last.   */  cookie_CookieStruct *cookieInList;  nsInt64 currentTime = NOW_IN_SECONDS;  PRInt32 count = sortedCookieList.Count();  for (PRInt32 i = 0; i < count; ++i) {    cookieInList = NS_STATIC_CAST(cookie_CookieStruct*, sortedCookieList.ElementAt(i));    NS_ASSERTION(cookieInList, "corrupt cookie list");    // don't write entry if cookie has expired, or is a session cookie    if (cookieInList->isSession || nsInt64(cookieInList->expires) < currentTime) {      continue;    }    bufferedOutputStream->Write(cookieInList->host.get(), cookieInList->host.Length(), &rv);    if (cookieInList->isDomain) {      bufferedOutputStream->Write(kTrue, sizeof(kTrue) - 1, &rv);    } else {      bufferedOutputStream->Write(kFalse, sizeof(kFalse) - 1, &rv);    }    bufferedOutputStream->Write(cookieInList->path.get(), cookieInList->path.Length(), &rv);    if (cookieInList->isSecure) {      bufferedOutputStream->Write(kTrue, sizeof(kTrue) - 1, &rv);    } else {      bufferedOutputStream->Write(kFalse, sizeof(kFalse) - 1, &rv);    }    char dateString[22];    PRUint32 dateLen = PR_snprintf(dateString, sizeof(dateString), "%lld", cookieInList->expires);    bufferedOutputStream->Write(dateString, dateLen, &rv);    bufferedOutputStream->Write(kTab, sizeof(kTab) - 1, &rv);    bufferedOutputStream->Write(cookieInList->name.get(), cookieInList->name.Length(), &rv);    bufferedOutputStream->Write(kTab, sizeof(kTab) - 1, &rv);    bufferedOutputStream->Write(cookieInList->cookie.get(), cookieInList->cookie.Length(), &rv);    bufferedOutputStream->Write(kNew, sizeof(kNew) - 1, &rv);  }  sCookieChanged = PR_FALSE;  return NS_OK;}