imapext-2007

view 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 source
1 /* ========================================================================
2 * Copyright 1988-2006 University of Washington
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *
11 * ========================================================================
12 */
14 /*
15 * Program: Exclusive create of a file, NFS kludge version
16 *
17 * Author: Mark Crispin
18 * Networks and Distributed Computing
19 * Computing & Communications
20 * University of Washington
21 * Administration Building, AG-44
22 * Seattle, WA 98195
23 * Internet: MRC@CAC.Washington.EDU
24 *
25 * Date: 17 December 1999
26 * Last Edited: 30 August 2006
27 */
30 /* SUN-OS had an NFS, As kludgy as an albatross;
31 * And everywhere that it was installed, It was a total loss.
32 * -- MRC 9/25/91
33 */
35 /* Exclusive create of a file
36 * Accepts: file name
37 * Return: T if success, NIL if failed, -1 if retry
38 */
40 long crexcl (char *name)
41 {
42 long ret = -1;
43 int i;
44 char hitch[MAILTMPLEN];
45 struct stat sb;
46 int mask = umask (0);
47 /* build hitching post file name */
48 sprintf (hitch,"%s.%lu.%d.",name,(unsigned long) time (0),getpid ());
49 i = strlen (hitch); /* append local host name */
50 gethostname (hitch + i,(MAILTMPLEN - i) - 1);
51 /* try to get hitching-post file */
52 if ((i = open (hitch,O_WRONLY|O_CREAT|O_EXCL,(int) shlock_mode)) >= 0) {
53 close (i); /* close the hitching-post */
54 /* Note: link() may return an error even if it actually succeeded. So we
55 * always check for success via the link count, and ignore the error if
56 * the link count is right.
57 */
58 /* tie hitching-post to lock */
59 i = link (hitch,name) ? errno : 0;
60 /* success if link count now 2 */
61 if (!stat (hitch,&sb) && (sb.st_nlink == 2)) ret = LONGT;
62 else if (i == EPERM) { /* links not allowed? */
63 /* Probably a FAT filesystem on Linux. It can't be NFS, so try creating
64 * the lock file directly.
65 */
66 if ((i = open (name,O_WRONLY|O_CREAT|O_EXCL,(int) shlock_mode)) >= 0){
67 close (i); /* close the file */
68 ret = LONGT; /* success */
69 }
70 /* fail unless error is EEXIST */
71 else if (errno != EEXIST) ret = NIL;
72 }
73 unlink (hitch); /* flush hitching post */
74 }
75 /* fail unless error is EEXIST */
76 else if (errno != EEXIST) ret = NIL;
77 umask (mask); /* restore previous mask */
78 return ret;
79 }

UW-IMAP'd extensions by yuuji