Ruby 1.9.2p136(2010-12-25revision30365)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# missing/flock.c

Go to the documentation of this file.

    00001 #include "ruby/config.h"
    00002
    00003 #if defined _WIN32
    00004 #elif defined HAVE_FCNTL && defined HAVE_FCNTL_H
    00005
    00006 /* These are the flock() constants.  Since this sytems doesn't have
    00007    flock(), the values of the constants are probably not available.
    00008 */
    00009 # ifndef LOCK_SH
    00010 #  define LOCK_SH 1
    00011 # endif
    00012 # ifndef LOCK_EX
    00013 #  define LOCK_EX 2
    00014 # endif
    00015 # ifndef LOCK_NB
    00016 #  define LOCK_NB 4
    00017 # endif
    00018 # ifndef LOCK_UN
    00019 #  define LOCK_UN 8
    00020 # endif
    00021
    00022 #include <fcntl.h>
    00023 #include <unistd.h>
    00024 #include <errno.h>
    00025
    00026 int
    00027 flock(int fd, int operation)
    00028 {
    00029     struct flock lock;
    00030
    00031     switch (operation & ~LOCK_NB) {
    00032     case LOCK_SH:
    00033         lock.l_type = F_RDLCK;
    00034         break;
    00035     case LOCK_EX:
    00036         lock.l_type = F_WRLCK;
    00037         break;
    00038     case LOCK_UN:
    00039         lock.l_type = F_UNLCK;
    00040         break;
    00041     default:
    00042         errno = EINVAL;
    00043         return -1;
    00044     }
    00045     lock.l_whence = SEEK_SET;
    00046     lock.l_start = lock.l_len = 0L;
    00047
    00048     return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &lock);
    00049 }
    00050
    00051 #elif defined(HAVE_LOCKF)
    00052
    00053 #include <unistd.h>
    00054 #include <errno.h>
    00055
    00056 /*  Emulate flock() with lockf() or fcntl().  This is just to increase
    00057     portability of scripts.  The calls might not be completely
    00058     interchangeable.  What's really needed is a good file
    00059     locking module.
    00060 */
    00061
    00062 # ifndef F_ULOCK
    00063 #  define F_ULOCK       0       /* Unlock a previously locked region */
    00064 # endif
    00065 # ifndef F_LOCK
    00066 #  define F_LOCK        1       /* Lock a region for exclusive use */
    00067 # endif
    00068 # ifndef F_TLOCK
    00069 #  define F_TLOCK       2       /* Test and lock a region for exclusive use */
    00070 # endif
    00071 # ifndef F_TEST
    00072 #  define F_TEST        3       /* Test a region for other processes locks */
    00073 # endif
    00074
    00075 /* These are the flock() constants.  Since this sytems doesn't have
    00076    flock(), the values of the constants are probably not available.
    00077 */
    00078 # ifndef LOCK_SH
    00079 #  define LOCK_SH 1
    00080 # endif
    00081 # ifndef LOCK_EX
    00082 #  define LOCK_EX 2
    00083 # endif
    00084 # ifndef LOCK_NB
    00085 #  define LOCK_NB 4
    00086 # endif
    00087 # ifndef LOCK_UN
    00088 #  define LOCK_UN 8
    00089 # endif
    00090
    00091 int
    00092 flock(int fd, int operation)
    00093 {
    00094     switch (operation) {
    00095
    00096         /* LOCK_SH - get a shared lock */
    00097       case LOCK_SH:
    00098         rb_notimplement();
    00099         return -1;
    00100         /* LOCK_EX - get an exclusive lock */
    00101       case LOCK_EX:
    00102         return lockf (fd, F_LOCK, 0);
    00103
    00104         /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
    00105       case LOCK_SH|LOCK_NB:
    00106         rb_notimplement();
    00107         return -1;
    00108         /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
    00109       case LOCK_EX|LOCK_NB:
    00110         return lockf (fd, F_TLOCK, 0);
    00111
    00112         /* LOCK_UN - unlock */
    00113       case LOCK_UN:
    00114         return lockf (fd, F_ULOCK, 0);
    00115
    00116         /* Default - can't decipher operation */
    00117       default:
    00118         errno = EINVAL;
    00119         return -1;
    00120     }
    00121 }
    00122 #else
    00123 int
    00124 flock(int fd, int operation)
    00125 {
    00126     rb_notimplement();
    00127     return -1;
    00128 }
    00129 #endif
    00130