[project @ 2001-05-18 16:54:04 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / cbits / lockFile.c
similarity index 63%
rename from ghc/lib/std/cbits/getLock.c
rename to ghc/lib/std/cbits/lockFile.c
index 7a82fbe..14a0a38 100644 (file)
@@ -1,29 +1,12 @@
 /* 
  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
  *
- * $Id: getLock.c,v 1.9 2001/04/02 16:10:32 rrt Exp $
+ * $Id: lockFile.c,v 1.1 2001/05/18 16:54:06 simonmar Exp $
  *
  * stdin/stout/stderr Runtime Support
  */
 
-#include "Rts.h"
-#include "stgio.h"
-
-#ifdef HAVE_SYS_TYPES_H
-#include <sys/types.h>
-#endif
-
-#ifdef HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif
+#include "HsStd.h"
 
 #ifndef FD_SETSIZE
 #define FD_SETSIZE 256
@@ -42,38 +25,17 @@ static int readLocks = 0;
 static int writeLocks = 0;
 
 int
-lockFile(fd, for_writing, exclusive)
-int fd;
-int for_writing;
-int exclusive;
+lockFile(int fd, int for_writing, int exclusive)
 {
     int i;
     struct stat sb;
 
-    while (fstat(fd, &sb) < 0) {
-       if (errno != EINTR) {
-#ifndef _WIN32
-           return -1;
-#else
-           /* fstat()ing socket fd's seems to fail with CRT's fstat(),
-              so let's just silently return and hope for the best..
-           */
-           return 0;
-#endif
-       }
-    }
-
-    /* Only lock regular files */
-    if (!S_ISREG(sb.st_mode))
-       return 0;
-    
     if (for_writing) {
       /* opening a file for writing, check to see whether
          we don't have any read locks on it already.. */
       for (i = 0; i < readLocks; i++) {
         if (readLock[i].inode == sb.st_ino && readLock[i].device == sb.st_dev) {
 #ifndef __MINGW32__
-           errno = EAGAIN;
            return -1;
 #else
            break;    
@@ -88,7 +50,6 @@ int exclusive;
        for (i = 0; i < writeLocks; i++) {
          if (writeLock[i].inode == sb.st_ino && writeLock[i].device == sb.st_dev) {
 #ifndef __MINGW32__
-            errno = EAGAIN;
             return -1;
 #else
             break;
@@ -108,7 +69,6 @@ int exclusive;
       for (i = 0; i < writeLocks; i++) {
        if (writeLock[i].inode == sb.st_ino && writeLock[i].device == sb.st_dev) {
 #ifndef __MINGW32__
-            errno = EAGAIN;
             return -1;
 #else
             break;
@@ -131,8 +91,7 @@ int exclusive;
 }
 
 int
-unlockFile(fd)
-int fd;
+unlockFile(int fd)
 {
     int i;
 
@@ -154,33 +113,3 @@ int fd;
      /* Signal that we did not find an entry */
     return 1;
 }
-
-/* getLock() is used when opening the standard file descriptors */
-StgInt
-getLock(fd, for_writing)
-StgInt fd;
-StgInt for_writing;
-{
-    if (lockFile(fd, for_writing, 0) < 0) {
-       if (errno == EBADF)
-           return 0;
-       else {
-           cvtErrno();
-           switch (ghc_errno) {
-           default:
-               stdErrno();
-               break;
-           case GHC_EACCES:
-           case GHC_EAGAIN:
-               ghc_errtype = ERR_RESOURCEBUSY;
-               ghc_errstr = "file is locked";
-               break;
-           }
-           /* Not so sure we want to do this, since getLock() 
-           is only called on the standard file descriptors.. */
-           /*(void) close(fd); */
-           return -1;
-       }
-    }
-    return 1;
-}