imapext-2007
diff src/osdep/unix/crx_nfs.c @ 0:ada5e610ab86
imap-2007e
author | yuuji@gentei.org |
---|---|
date | Mon, 14 Sep 2009 15:17:45 +0900 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/osdep/unix/crx_nfs.c Mon Sep 14 15:17:45 2009 +0900 1.3 @@ -0,0 +1,79 @@ 1.4 +/* ======================================================================== 1.5 + * Copyright 1988-2006 University of Washington 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * 1.14 + * ======================================================================== 1.15 + */ 1.16 + 1.17 +/* 1.18 + * Program: Exclusive create of a file, NFS kludge version 1.19 + * 1.20 + * Author: Mark Crispin 1.21 + * Networks and Distributed Computing 1.22 + * Computing & Communications 1.23 + * University of Washington 1.24 + * Administration Building, AG-44 1.25 + * Seattle, WA 98195 1.26 + * Internet: MRC@CAC.Washington.EDU 1.27 + * 1.28 + * Date: 17 December 1999 1.29 + * Last Edited: 30 August 2006 1.30 + */ 1.31 + 1.32 + 1.33 +/* SUN-OS had an NFS, As kludgy as an albatross; 1.34 + * And everywhere that it was installed, It was a total loss. 1.35 + * -- MRC 9/25/91 1.36 + */ 1.37 + 1.38 +/* Exclusive create of a file 1.39 + * Accepts: file name 1.40 + * Return: T if success, NIL if failed, -1 if retry 1.41 + */ 1.42 + 1.43 +long crexcl (char *name) 1.44 +{ 1.45 + long ret = -1; 1.46 + int i; 1.47 + char hitch[MAILTMPLEN]; 1.48 + struct stat sb; 1.49 + int mask = umask (0); 1.50 + /* build hitching post file name */ 1.51 + sprintf (hitch,"%s.%lu.%d.",name,(unsigned long) time (0),getpid ()); 1.52 + i = strlen (hitch); /* append local host name */ 1.53 + gethostname (hitch + i,(MAILTMPLEN - i) - 1); 1.54 + /* try to get hitching-post file */ 1.55 + if ((i = open (hitch,O_WRONLY|O_CREAT|O_EXCL,(int) shlock_mode)) >= 0) { 1.56 + close (i); /* close the hitching-post */ 1.57 + /* Note: link() may return an error even if it actually succeeded. So we 1.58 + * always check for success via the link count, and ignore the error if 1.59 + * the link count is right. 1.60 + */ 1.61 + /* tie hitching-post to lock */ 1.62 + i = link (hitch,name) ? errno : 0; 1.63 + /* success if link count now 2 */ 1.64 + if (!stat (hitch,&sb) && (sb.st_nlink == 2)) ret = LONGT; 1.65 + else if (i == EPERM) { /* links not allowed? */ 1.66 + /* Probably a FAT filesystem on Linux. It can't be NFS, so try creating 1.67 + * the lock file directly. 1.68 + */ 1.69 + if ((i = open (name,O_WRONLY|O_CREAT|O_EXCL,(int) shlock_mode)) >= 0){ 1.70 + close (i); /* close the file */ 1.71 + ret = LONGT; /* success */ 1.72 + } 1.73 + /* fail unless error is EEXIST */ 1.74 + else if (errno != EEXIST) ret = NIL; 1.75 + } 1.76 + unlink (hitch); /* flush hitching post */ 1.77 + } 1.78 + /* fail unless error is EEXIST */ 1.79 + else if (errno != EEXIST) ret = NIL; 1.80 + umask (mask); /* restore previous mask */ 1.81 + return ret; 1.82 +}