# HG changeset patch # User mas01cr # Date 1250026938 0 # Node ID 4eedc18634f554312666487f38516328ffba085a # Parent 9119f2fa3efe2d588d23c1ba8246f14974cc221d Use _locking() to emulate fcntl() locks This is a rather terrible emulation; _locking() doesn't support nonexclusive locks, and also doesn't document any useful return codes. Oh well; it's a start. diff -r 9119f2fa3efe -r 4eedc18634f5 audioDB-internals.h --- a/audioDB-internals.h Tue Aug 11 21:42:13 2009 +0000 +++ b/audioDB-internals.h Tue Aug 11 21:42:18 2009 +0000 @@ -1,12 +1,23 @@ +#if defined(WIN32) +#include +#endif +#if !defined(WIN32) #include +#endif #include #include #include +#if defined(WIN32) +#include +#endif #include #include #include #include +#if defined(WIN32) +#include +#endif #include #include diff -r 9119f2fa3efe -r 4eedc18634f5 audioDB.h --- a/audioDB.h Tue Aug 11 21:42:13 2009 +0000 +++ b/audioDB.h Tue Aug 11 21:42:18 2009 +0000 @@ -278,8 +278,6 @@ void cleanup(); ~audioDB(); int processArgs(const unsigned argc, const char* argv[]); - void get_lock(int fd, bool exclusive); - void release_lock(int fd); void create(const char* dbName); void insert(const char* dbName, const char* inFile); void batchinsert(const char* dbName, const char* inFile); diff -r 9119f2fa3efe -r 4eedc18634f5 close.cpp --- a/close.cpp Tue Aug 11 21:42:13 2009 +0000 +++ b/close.cpp Tue Aug 11 21:42:18 2009 +0000 @@ -13,6 +13,7 @@ if(adb->cached_lsh) { delete adb->cached_lsh; } + divest_lock(adb->fd); close(adb->fd); free(adb); } diff -r 9119f2fa3efe -r 4eedc18634f5 common.cpp --- a/common.cpp Tue Aug 11 21:42:13 2009 +0000 +++ b/common.cpp Tue Aug 11 21:42:18 2009 +0000 @@ -10,18 +10,6 @@ } #endif -void audioDB::get_lock(int fd, bool exclusive) { - if(acquire_lock(fd, exclusive)) { - error("fcntl lock error", "", "fcntl"); - } -} - -void audioDB::release_lock(int fd) { - if (divest_lock(fd)) { - error("fcntl unlock error", "", "fcntl"); - } -} - void audioDB::error(const char* a, const char* b, const char *sysFunc) { diff -r 9119f2fa3efe -r 4eedc18634f5 create.cpp --- a/create.cpp Tue Aug 11 21:42:13 2009 +0000 +++ b/create.cpp Tue Aug 11 21:42:18 2009 +0000 @@ -49,9 +49,6 @@ if ((fd = open(path, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) { goto error; } - if (acquire_lock(fd, true)) { - goto error; - } header = (adb_header_t *) malloc(sizeof(adb_header_t)); if(!header) { diff -r 9119f2fa3efe -r 4eedc18634f5 lock.cpp --- a/lock.cpp Tue Aug 11 21:42:13 2009 +0000 +++ b/lock.cpp Tue Aug 11 21:42:18 2009 +0000 @@ -4,13 +4,14 @@ #include "audioDB-internals.h" int acquire_lock(int fd, bool exclusive) { +#if !defined(WIN32) struct flock lock; int status; lock.l_type = exclusive ? F_WRLCK : F_RDLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; - lock.l_len = 0; /* "the whole file" */ + lock.l_len = ADB_HEADER_SIZE; retry: do { @@ -26,15 +27,31 @@ } } return 0; +#else + /* _locking() only supports exclusive locks */ + int status; + + retry: + status = _locking(fd, _LK_NBLCK, ADB_HEADER_SIZE); + if(status) { + Sleep(1000); + goto retry; + } + return 0; +#endif } int divest_lock(int fd) { +#if !defined(WIN32) struct flock lock; lock.l_type = F_UNLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; - lock.l_len = 0; + lock.l_len = ADB_HEADER_SIZE; return fcntl(fd, F_SETLKW, &lock); +#else + return _locking(fd, _LK_UNLCK, ADB_HEADER_SIZE); +#endif }