]> code.delx.au - gnu-emacs/blobdiff - src/filelock.c
(Fnext_property_change): Fix previous change.
[gnu-emacs] / src / filelock.c
index 81e72432c069ca9b0db6ea6017d93e87a8b8a0b5..4c725253acd81dfc53b8ac3b49a8127fa3a0997f 100644 (file)
@@ -1,10 +1,10 @@
-/* Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
+/* Copyright (C) 1985, 1986, 1987, 1993, 1994 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
 GNU Emacs is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 1, or (at your option)
+the Free Software Foundation; either version 2, or (at your option)
 any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
@@ -19,21 +19,52 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 #include <sys/types.h>
 #include <sys/stat.h>
-#include "config.h"
+#include <config.h>
+
+#ifdef VMS
+#include "vms-pwd.h"
+#else
 #include <pwd.h>
+#endif
+
 #include <errno.h>
 #include <sys/file.h>
 #ifdef USG
 #include <fcntl.h>
 #endif /* USG */
 
-#undef NULL
 #include "lisp.h"
-#include "paths.h"
+#include <paths.h>
 #include "buffer.h"
 
+#ifdef SYSV_SYSTEM_DIR
+#include <dirent.h>
+#else /* not SYSV_SYSTEM_DIR */
+#ifdef NONSYSTEM_DIR_LIBRARY
+#include "ndir.h"
+#else /* not NONSYSTEM_DIR_LIBRARY */
+#ifdef MSDOS
+#include <dirent.h>
+#else
+#include <sys/dir.h>
+#endif
+#endif /* not NONSYSTEM_DIR_LIBRARY */
+#ifndef MSDOS
+extern DIR *opendir ();
+#endif /* not MSDOS */
+#endif /* not SYSV_SYSTEM_DIR */
+
 extern int errno;
 
+extern char *egetenv ();
+extern char *strcpy ();
+
+#ifdef DECLARE_GETPWUID_WITH_UID_T
+extern struct passwd *getpwuid (uid_t);
+#else
+extern struct passwd *getpwuid ();
+#endif
+
 #ifdef CLASH_DETECTION
   
 /* If system does not have symbolic links, it does not have lstat.
@@ -43,13 +74,94 @@ extern int errno;
 #define lstat stat
 #endif
 
+
+/* The name of the directory in which we keep lock files, with a '/'
+   appended.  */  
+char *lock_dir;
+
+/* The name of the file in the lock directory which is used to
+   arbitrate access to the entire directory.  */
+#define SUPERLOCK_NAME "!!!SuperLock!!!"
+
+/* The name of the superlock file.  This is SUPERLOCK_NAME appended to
+   lock_dir.  */
+char *superlock_file;
+
+/* Set LOCK to the name of the lock file for the filename FILE.
+   char *LOCK; Lisp_Object FILE;  */
+
+#ifndef HAVE_LONG_FILE_NAMES
+
+#define MAKE_LOCK_NAME(lock, file) \
+  (lock = (char *) alloca (14 + strlen (lock_dir) + 1), \
+   fill_in_lock_short_file_name (lock, (file)))
+
+
+fill_in_lock_short_file_name (lockfile, fn)
+     register char *lockfile;
+     register Lisp_Object fn;
+{
+  register union
+    {
+      unsigned int  word [2];
+      unsigned char byte [8];
+    } crc;
+  register unsigned char *p, new;
+  
+  /* 7-bytes cyclic code for burst correction on byte-by-byte basis.
+     the used polynomial is D^7 + D^6 + D^3 +1. pot@cnuce.cnr.it */
+
+  crc.word[0] = crc.word[1] = 0;
+
+  for (p = XSTRING (fn)->data; new = *p++; )
+    {
+      new += crc.byte[6];
+      crc.byte[6] = crc.byte[5] + new;
+      crc.byte[5] = crc.byte[4];
+      crc.byte[4] = crc.byte[3];
+      crc.byte[3] = crc.byte[2] + new;
+      crc.byte[2] = crc.byte[1];
+      crc.byte[1] = crc.byte[0];
+      crc.byte[0] = new;
+    }
+  sprintf (lockfile, "%s%.2x%.2x%.2x%.2x%.2x%.2x%.2x", lock_dir,
+          crc.byte[0], crc.byte[1], crc.byte[2], crc.byte[3],
+          crc.byte[4], crc.byte[5], crc.byte[6]);
+}
+
+#else /* defined HAVE_LONG_FILE_NAMES */
+
+#define MAKE_LOCK_NAME(lock, file) \
+  (lock = (char *) alloca (XSTRING (file)->size + strlen (lock_dir) + 1), \
+   fill_in_lock_file_name (lock, (file)))
+
+
+fill_in_lock_file_name (lockfile, fn)
+     register char *lockfile;
+     register Lisp_Object fn;
+{
+  register char *p;
+
+  strcpy (lockfile, lock_dir);
+
+  p = lockfile + strlen (lockfile);
+
+  strcpy (p, XSTRING (fn)->data);
+
+  for (; *p; p++)
+    {
+      if (*p == '/')
+       *p = '!';
+    }
+}
+#endif /* !defined HAVE_LONG_FILE_NAMES */
+
 static Lisp_Object
 lock_file_owner_name (lfname)
      char *lfname;
 {
   struct stat s;
   struct passwd *the_pw;
-  extern struct passwd *getpwuid ();
 
   if (lstat (lfname, &s) == 0)
     the_pw = getpwuid (s.st_uid);
@@ -78,6 +190,11 @@ lock_file_owner_name (lfname)
    and put in the Emacs lock directory.  */
 /* (ie., /ka/king/junk.tex -> /!/!ka!king!junk.tex). */
 
+/* If HAVE_LONG_FILE_NAMES is not defined, the lock file name is the hex
+   representation of a 14-bytes CRC generated from the file name
+   and put in the Emacs lock directory (not very nice, but it works).
+   (ie., /ka/king/junk.tex -> /!/12a82c62f1c6da). */
+
 void
 lock_file (fn)
      register Lisp_Object fn;
@@ -85,16 +202,16 @@ lock_file (fn)
   register Lisp_Object attack;
   register char *lfname;
 
-  /* Create the name of the lock-file for file fn */
-  lfname = (char *) alloca (XSTRING (fn)->size + strlen (PATH_LOCK) + 1);
-  fill_in_lock_file_name (lfname, fn);
+  MAKE_LOCK_NAME (lfname, fn);
 
-  /* See if this file is visited and has changed on disk since it was visited.  */
+  /* See if this file is visited and has changed on disk since it was
+     visited.  */
   {
-    register Lisp_Object subject_buf = Fget_file_buffer (fn);
-    if (!NULL (subject_buf)
-       && NULL (Fverify_visited_file_modtime (subject_buf))
-       && !NULL (Ffile_exists_p (fn)))
+    register Lisp_Object subject_buf;
+    subject_buf = Fget_file_buffer (fn);
+    if (!NILP (subject_buf)
+       && NILP (Fverify_visited_file_modtime (subject_buf))
+       && !NILP (Ffile_exists_p (fn)))
       call1 (intern ("ask-user-about-supersession-threat"), fn);
   }
 
@@ -106,36 +223,17 @@ lock_file (fn)
   /* Else consider breaking the lock */
   attack = call2 (intern ("ask-user-about-lock"), fn,
                  lock_file_owner_name (lfname));
-  if (!NULL (attack))
+  if (!NILP (attack))
     /* User says take the lock */
     {
       lock_superlock (lfname);
       lock_file_1 (lfname, O_WRONLY) ;
-      unlink (PATH_SUPERLOCK);
+      unlink (superlock_file);
       return;
     }
   /* User says ignore the lock */
 }
 
-fill_in_lock_file_name (lockfile, fn)
-     register char *lockfile;
-     register Lisp_Object fn;
-{
-  register char *p;
-
-  strcpy (lockfile, PATH_LOCK);
-
-  p = lockfile + strlen (lockfile);
-
-  strcpy (p, XSTRING (fn)->data);
-
-  for (; *p; p++)
-    {
-      if (*p == '/')
-       *p = '!';
-    }
-}
-
 /* Lock the lock file named LFNAME.
    If MODE is O_WRONLY, we do so even if it is already locked.
    If MODE is O_WRONLY | O_EXCL | O_CREAT, we do so only if it is free.
@@ -231,34 +329,41 @@ unlock_file (fn)
 {
   register char *lfname;
 
-  lfname = (char *) alloca (XSTRING (fn)->size + strlen (PATH_LOCK) + 1);
-  fill_in_lock_file_name (lfname, fn);
+  MAKE_LOCK_NAME (lfname, fn);
 
   lock_superlock (lfname);
 
   if (current_lock_owner_1 (lfname) == getpid ())
     unlink (lfname);
 
-  unlink (PATH_SUPERLOCK);
+  unlink (superlock_file);
 }
 
 lock_superlock (lfname)
      char *lfname;
 {
   register int i, fd;
+  DIR *lockdir;
 
-  for (i = -20; i < 0 && (fd = open (PATH_SUPERLOCK,
+  for (i = -20; i < 0 && (fd = open (superlock_file,
                                     O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0;
        i++)
     {
       if (errno != EEXIST)
        return;
+
+      /* This seems to be necessary to prevent Emacs from hanging when the
+        competing process has already deleted the superlock, but it's still
+        in the NFS cache.  So we force NFS to synchronize the cache.  */
+      if (lockdir = opendir (lock_dir))
+       closedir (lockdir);
+
       sleep (1);
     }
   if (fd >= 0)
     {
 #ifdef USG
-      chmod (PATH_SUPERLOCK, 0666);
+      chmod (superlock_file, 0666);
 #else
       fchmod (fd, 0666);
 #endif
@@ -273,13 +378,11 @@ unlock_all_files ()
   register Lisp_Object tail;
   register struct buffer *b;
 
-  for (tail = Vbuffer_alist; XGCTYPE (tail) == Lisp_Cons;
-       tail = XCONS (tail)->cdr)
+  for (tail = Vbuffer_alist; GC_CONSP (tail); tail = XCONS (tail)->cdr)
     {
       b = XBUFFER (XCONS (XCONS (tail)->car)->cdr);
-      if (XTYPE (b->filename) == Lisp_String &&
-         b->save_modified < BUF_MODIFF (b))
-       unlock_file (b->filename);
+      if (STRINGP (b->file_truename) && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b))
+       unlock_file (b->file_truename);
     }
 }
 
@@ -292,12 +395,12 @@ or else nothing is done if current buffer isn't visiting a file.")
   (fn)
      Lisp_Object fn;
 {
-  if (NULL (fn))
-    fn = current_buffer->filename;
+  if (NILP (fn))
+    fn = current_buffer->file_truename;
   else
     CHECK_STRING (fn, 0);
-  if (current_buffer->save_modified < MODIFF
-      && !NULL (fn))
+  if (SAVE_MODIFF < MODIFF
+      && !NILP (fn))
     lock_file (fn);
   return Qnil;    
 }
@@ -308,9 +411,9 @@ DEFUN ("unlock-buffer", Funlock_buffer, Sunlock_buffer,
 if it should normally be locked.")
   ()
 {
-  if (current_buffer->save_modified < MODIFF &&
-      XTYPE (current_buffer->filename) == Lisp_String)
-    unlock_file (current_buffer->filename);
+  if (SAVE_MODIFF < MODIFF
+      && STRINGP (current_buffer->file_truename))
+    unlock_file (current_buffer->file_truename);
   return Qnil;
 }
 
@@ -320,9 +423,9 @@ if it should normally be locked.")
 unlock_buffer (buffer)
      struct buffer *buffer;
 {
-  if (buffer->save_modified < BUF_MODIFF (buffer) &&
-      XTYPE (buffer->filename) == Lisp_String)
-    unlock_file (buffer->filename);
+  if (BUF_SAVE_MODIFF (buffer) < BUF_MODIFF (buffer)
+      && STRINGP (buffer->file_truename))
+    unlock_file (buffer->file_truename);
 }
 
 DEFUN ("file-locked-p", Ffile_locked_p, Sfile_locked_p, 0, 1, 0,
@@ -336,9 +439,7 @@ t if it is locked by you, else a string of the name of the locker.")
 
   fn = Fexpand_file_name (fn, Qnil);
 
-  /* Create the name of the lock-file for file filename */
-  lfname = (char *) alloca (XSTRING (fn)->size + strlen (PATH_LOCK) + 1);
-  fill_in_lock_file_name (lfname, fn);
+  MAKE_LOCK_NAME (lfname, fn);
 
   owner = current_lock_owner (lfname);
   if (owner <= 0)
@@ -349,6 +450,32 @@ t if it is locked by you, else a string of the name of the locker.")
   return (lock_file_owner_name (lfname));
 }
 
+\f
+/* Initialization functions.  */
+
+init_filelock ()
+{
+  char *new_name;
+
+  lock_dir = egetenv ("EMACSLOCKDIR");
+  if (! lock_dir)
+    lock_dir = PATH_LOCK;
+
+  /* Copy the name in case egetenv got it from a Lisp string.  */
+  new_name = (char *) xmalloc (strlen (lock_dir) + 2);
+  strcpy (new_name, lock_dir);
+  lock_dir = new_name;
+
+  /* Make sure it ends with a slash.  */
+  if (lock_dir[strlen (lock_dir) - 1] != '/')
+    strcat (lock_dir, "/");
+
+  superlock_file = (char *) xmalloc ((strlen (lock_dir)
+                                     + sizeof (SUPERLOCK_NAME)));
+  strcpy (superlock_file, lock_dir);
+  strcat (superlock_file, SUPERLOCK_NAME);
+}
+
 syms_of_filelock ()
 {
   defsubr (&Sunlock_buffer);