LOG(("Failed to create file %s\n", mozlockstr.get()));
PRBool ObtainSpoolLock(const char *spoolnameStr,                       int seconds /* number of seconds to retry */){    // How to lock:    // step 1: create SPOOLNAME.mozlock    //        1a: can remove it if it already exists (probably crash-droppings)    // step 2: hard-link SPOOLNAME.mozlock to SPOOLNAME.lock for NFS atomicity    //        2a: if SPOOLNAME.lock is >60sec old then nuke it from orbit    //        2b: repeat step 2 until retry-count expired or hard-link succeeds    // step 3: remove SPOOLNAME.mozlock    // step 4: If step 2 hard-link failed, fail hard; we do not hold the lock    // DONE.    //    // (step 2a not yet implemented)        nsCAutoString mozlockstr(spoolnameStr);    mozlockstr.Append(".mozlock");    nsCAutoString lockstr(spoolnameStr);    lockstr.Append(".lock");    nsresult rv;    // Create nsFileSpec and nsILocalFile for the spool.mozlock file    nsFileSpec tmplocspec(mozlockstr.get());    nsCOMPtr<nsILocalFile> tmplocfile;    rv = NS_FileSpecToIFile(&tmplocspec, getter_AddRefs(tmplocfile));    if (NS_FAILED(rv))        return PR_FALSE;    // THOUGHT: hmm, perhaps use MakeUnique to generate us a unique mozlock?    // ... perhaps not, MakeUnique implementation looks racey -- use mktemp()?    // step 1: create SPOOLNAME.mozlock#ifdef MOVEMAIL_DEBUG    fprintf(stderr, "\n ...... maker(%s) ......\n",            mozlockstr.get());#endif    rv = tmplocfile->Create(nsIFile::NORMAL_FILE_TYPE, 0666);    if ( (NS_FAILED(rv) &&          rv != NS_ERROR_FILE_ALREADY_EXISTS) ||         !tmplocfile) {        // can't create our .mozlock file... game over already#ifdef MOVEMAIL_DEBUG        fprintf(stderr, "\n cannot create blah.mozlock file! \n");#endif        return PR_FALSE;    }    // step 2: hard-link .mozlock file to .lock file (this wackiness    //         is necessary for non-racey locking on NFS-mounted spool dirs)    // n.b. XPCOM utilities don't support hard-linking yet, so we    // skip out to <unistd.h> and the POSIX interface for link()    int link_result = 0;    int retry_count = 0;        do {        link_result =            link(mozlockstr.get(),lockstr.get());        retry_count++;#ifdef MOVEMAIL_DEBUG        fprintf(stderr, "[try#%d] ", retry_count);#endif        if ((seconds > 0) &&            (link_result == -1)) {            // pause 1sec, waiting for .lock to go away            PRIntervalTime sleepTime = 1000; // 1 second            PR_Sleep(sleepTime);        }    } while ((link_result == -1) && (retry_count < seconds));#ifdef MOVEMAIL_DEBUG    fprintf(stderr, "<<link result: %d>>", link_result);#endif    // step 3: remove .mozlock file, in any case    rv = tmplocfile->Remove(PR_FALSE /* non-recursive */);#ifdef MOVEMAIL_DEBUG    if (NS_FAILED(rv)) {        // Could not delete our .mozlock file... very unusual, but        // not fatal.        fprintf(stderr,                "\nBizarre, could not delete our .mozlock file.  Oh well.\n");    }#endif#ifdef MOVEMAIL_DEBUG    fprintf(stderr, "::got to the end! %s\n",            (link_result == 0) ? "GOT LOCK" : "DID NOT GET LOCK");#endif    // step 4: now we know whether we succeeded or failed    if (link_result == 0)        return PR_TRUE; // got the lock.    else        return PR_FALSE; // didn't.  :(}