comparison src/filelock.c @ 14:9ee227acff29 r19-15b90

Import from CVS: tag r19-15b90
author cvs
date Mon, 13 Aug 2007 08:48:42 +0200
parents ac2d302a0011
children 859a2309aef8
comparison
equal deleted inserted replaced
13:13c6d0aaafe5 14:9ee227acff29
67 MAKE_LOCK_NAME assumes you have already verified that Vlock_directory 67 MAKE_LOCK_NAME assumes you have already verified that Vlock_directory
68 is a string. */ 68 is a string. */
69 69
70 #ifndef HAVE_LONG_FILE_NAMES 70 #ifndef HAVE_LONG_FILE_NAMES
71 71
72 #define MAKE_LOCK_NAME(lock, file) \ 72 #define MAKE_LOCK_NAME(lock, file) \
73 (lock = (char *) alloca (14 + string_length (XSTRING (Vlock_directory)) + \ 73 (lock = (char *) alloca (14 + XSTRING_LENGTH (Vlock_directory) + 1), \
74 1), \
75 fill_in_lock_short_file_name (lock, (file))) 74 fill_in_lock_short_file_name (lock, (file)))
76 75
77 static void 76 static void
78 fill_in_lock_short_file_name (REGISTER char *lockfile, REGISTER Lisp_Object fn) 77 fill_in_lock_short_file_name (REGISTER char *lockfile, REGISTER Lisp_Object fn)
79 { 78 {
89 /* 7-bytes cyclic code for burst correction on byte-by-byte basis. 88 /* 7-bytes cyclic code for burst correction on byte-by-byte basis.
90 the used polynomial is D^7 + D^6 + D^3 +1. pot@cnuce.cnr.it */ 89 the used polynomial is D^7 + D^6 + D^3 +1. pot@cnuce.cnr.it */
91 90
92 crc.word[0] = crc.word[1] = 0; 91 crc.word[0] = crc.word[1] = 0;
93 92
94 for (p = string_data (XSTRING (fn)); new = *p++; ) 93 for (p = XSTRING_DATA (fn); new = *p++; )
95 { 94 {
96 new += crc.byte[6]; 95 new += crc.byte[6];
97 crc.byte[6] = crc.byte[5] + new; 96 crc.byte[6] = crc.byte[5] + new;
98 crc.byte[5] = crc.byte[4]; 97 crc.byte[5] = crc.byte[4];
99 crc.byte[4] = crc.byte[3]; 98 crc.byte[4] = crc.byte[3];
105 104
106 { 105 {
107 int need_slash = 0; 106 int need_slash = 0;
108 107
109 /* in case lock-directory doesn't end in / */ 108 /* in case lock-directory doesn't end in / */
110 if (string_byte (XSTRING (Vlock_directory), 109 if (XSTRING_BYTE (Vlock_directory,
111 string_length (XSTRING (Vlock_directory)) - 1) != '/') 110 XSTRING_LENGTH (Vlock_directory) - 1) != '/')
112 need_slash = 1; 111 need_slash = 1;
113 112
114 sprintf (lockfile, "%s%s%.2x%.2x%.2x%.2x%.2x%.2x%.2x", 113 sprintf (lockfile, "%s%s%.2x%.2x%.2x%.2x%.2x%.2x%.2x",
115 (char *) string_data (XSTRING (Vlock_directory)), 114 (char *) XSTRING_DATA (Vlock_directory),
116 need_slash ? "/" : "", 115 need_slash ? "/" : "",
117 crc.byte[0], crc.byte[1], crc.byte[2], crc.byte[3], 116 crc.byte[0], crc.byte[1], crc.byte[2], crc.byte[3],
118 crc.byte[4], crc.byte[5], crc.byte[6]); 117 crc.byte[4], crc.byte[5], crc.byte[6]);
119 } 118 }
120 } 119 }
121 120
122 #else /* defined HAVE_LONG_FILE_NAMES */ 121 #else /* defined HAVE_LONG_FILE_NAMES */
123 122
124 /* +2 for terminating null and possible extra slash */ 123 /* +2 for terminating null and possible extra slash */
125 #define MAKE_LOCK_NAME(lock, file) \ 124 #define MAKE_LOCK_NAME(lock, file) \
126 (lock = (char *) alloca (string_length (XSTRING (file)) + \ 125 (lock = (char *) alloca (XSTRING_LENGTH (file) + \
127 string_length (XSTRING (Vlock_directory)) + 2), \ 126 XSTRING_LENGTH (Vlock_directory) + 2), \
128 fill_in_lock_file_name (lock, (file))) 127 fill_in_lock_file_name (lock, (file)))
129 128
130 static void 129 static void
131 fill_in_lock_file_name (REGISTER char *lockfile, REGISTER Lisp_Object fn) 130 fill_in_lock_file_name (REGISTER char *lockfile, REGISTER Lisp_Object fn)
132 /* fn must be a Lisp_String! */ 131 /* fn must be a Lisp_String! */
133 { 132 {
134 REGISTER char *p; 133 REGISTER char *p;
135 134
136 CHECK_STRING (Vlock_directory); 135 CHECK_STRING (Vlock_directory);
137 136
138 strcpy (lockfile, (char *) string_data (XSTRING (Vlock_directory))); 137 strcpy (lockfile, (char *) XSTRING_DATA (Vlock_directory));
139 138
140 p = lockfile + strlen (lockfile); 139 p = lockfile + strlen (lockfile);
141 140
142 if (p == lockfile /* lock-directory is empty?? */ 141 if (p == lockfile /* lock-directory is empty?? */
143 || *(p - 1) != '/') /* in case lock-directory doesn't end in / */ 142 || *(p - 1) != '/') /* in case lock-directory doesn't end in / */
144 { 143 {
145 *p = '/'; 144 *p = '/';
146 p++; 145 p++;
147 } 146 }
148 147
149 strcpy (p, (char *) string_data (XSTRING (fn))); 148 strcpy (p, (char *) XSTRING_DATA (fn));
150 149
151 for (; *p; p++) 150 for (; *p; p++)
152 { 151 {
153 if (*p == '/') 152 if (*p == '/')
154 *p = '!'; 153 *p = '!';
237 /* User says take the lock */ 236 /* User says take the lock */
238 { 237 {
239 CHECK_STRING (Vsuperlock_file); 238 CHECK_STRING (Vsuperlock_file);
240 lock_superlock (lfname); 239 lock_superlock (lfname);
241 lock_file_1 (lfname, O_WRONLY); 240 lock_file_1 (lfname, O_WRONLY);
242 unlink ((char *) string_data (XSTRING (Vsuperlock_file))); 241 unlink ((char *) XSTRING_DATA (Vsuperlock_file));
243 goto done; 242 goto done;
244 } 243 }
245 /* User says ignore the lock */ 244 /* User says ignore the lock */
246 done: 245 done:
247 UNGCPRO; 246 UNGCPRO;
349 lock_superlock (lfname); 348 lock_superlock (lfname);
350 349
351 if (current_lock_owner_1 (lfname) == getpid ()) 350 if (current_lock_owner_1 (lfname) == getpid ())
352 unlink (lfname); 351 unlink (lfname);
353 352
354 unlink ((char *) string_data (XSTRING (Vsuperlock_file))); 353 unlink ((char *) XSTRING_DATA (Vsuperlock_file));
355 } 354 }
356 355
357 static void 356 static void
358 lock_superlock (CONST char *lfname) 357 lock_superlock (CONST char *lfname)
359 { 358 {
360 REGISTER int i, fd; 359 REGISTER int i, fd;
361 DIR *lockdir; 360 DIR *lockdir;
362 361
363 for (i = -20; i < 0 && 362 for (i = -20; i < 0 &&
364 (fd = open ((char *) string_data (XSTRING (Vsuperlock_file)), 363 (fd = open ((char *) XSTRING_DATA (Vsuperlock_file),
365 O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0; 364 O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0;
366 i++) 365 i++)
367 { 366 {
368 if (errno != EEXIST) 367 if (errno != EEXIST)
369 return; 368 return;
370 369
371 /* This seems to be necessary to prevent Emacs from hanging when the 370 /* This seems to be necessary to prevent Emacs from hanging when the
372 competing process has already deleted the superlock, but it's still 371 competing process has already deleted the superlock, but it's still
373 in the NFS cache. So we force NFS to synchronize the cache. */ 372 in the NFS cache. So we force NFS to synchronize the cache. */
374 lockdir = opendir ((char *) string_data (XSTRING (Vlock_directory))); 373 lockdir = opendir ((char *) XSTRING_DATA (Vlock_directory));
375 if (lockdir) 374 if (lockdir)
376 closedir (lockdir); 375 closedir (lockdir);
377 376
378 emacs_sleep (1); 377 emacs_sleep (1);
379 } 378 }
380 if (fd >= 0) 379 if (fd >= 0)
381 { 380 {
382 #ifdef USG 381 #ifdef USG
383 chmod ((char *) string_data (XSTRING (Vsuperlock_file)), 0666); 382 chmod ((char *) XSTRING_DATA (Vsuperlock_file), 0666);
384 #else 383 #else
385 fchmod (fd, 0666); 384 fchmod (fd, 0666);
386 #endif 385 #endif
387 write (fd, lfname, strlen (lfname)); 386 write (fd, lfname, strlen (lfname));
388 close (fd); 387 close (fd);