From 2dd5b9a538b0cd637cefd5ca6e11e3b114cc2bce Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 24 Mar 2006 13:47:29 +0000 Subject: [PATCH] Move flock.c from 1.4 to 1.3. Bug #529 --- data1/d1_read.c | 3 +- examples/gils/records/esdd0006.grs | 3 +- include/Makefile.am | 11 +- include/zebra-flock.h | 50 +++++++ index/Makefile.am | 6 +- index/index.h | 15 +-- index/lockidx.c | 261 ------------------------------------ index/lockutil.c | 185 ------------------------- index/zebraapi.c | 15 ++- test/marcxml/record.abs | 4 +- test/marcxml/zebra.cfg | 13 +- util/Makefile.am | 4 +- util/flock.c | 164 ++++++++++++++++++++++ 13 files changed, 253 insertions(+), 481 deletions(-) create mode 100644 include/zebra-flock.h delete mode 100644 index/lockidx.c delete mode 100644 index/lockutil.c create mode 100644 util/flock.c diff --git a/data1/d1_read.c b/data1/d1_read.c index 3f87878..2aa821a 100644 --- a/data1/d1_read.c +++ b/data1/d1_read.c @@ -1,4 +1,4 @@ -/* $Id: d1_read.c,v 1.8.2.2 2005-04-23 16:31:54 adam Exp $ +/* $Id: d1_read.c,v 1.8.2.3 2006-03-24 13:47:29 adam Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004 Index Data Aps @@ -1126,6 +1126,7 @@ int data1_iconv (data1_handle dh, NMEM m, data1_node *n, const char *tocode, const char *fromcode) { + yaz_log(LOG_DEBUG, "data1_iconv tocode=%s fromcode=%s", tocode, fromcode); if (yaz_matchstr (tocode, fromcode)) { WRBUF wrbuf = wrbuf_alloc(); diff --git a/examples/gils/records/esdd0006.grs b/examples/gils/records/esdd0006.grs index 6726dd2..dcd525a 100644 --- a/examples/gils/records/esdd0006.grs +++ b/examples/gils/records/esdd0006.grs @@ -1,7 +1,8 @@ + -UTAH EARTHQUAKE EPICENTERS +UTAH EARTHQUAKE EPICENTERS æ <Acronym> UUCCSEIS </Acronym> diff --git a/include/Makefile.am b/include/Makefile.am index 6ee6333..2784f93 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,10 +1,9 @@ noinst_HEADERS = bfile.h bset.h charmap.h d1_attset.h d1_map.h \ -data1.h dfa.h dict.h direntz.h isam-codec.h isamb.h isamc.h isamg.h \ -isam.h isams.h mfile.h passwddb.h recctrl.h \ -res.h rsbetween.h rsbool.h rset.h rsisamb.h rsisamc.h rsisam.h \ -rsisams.h rsm_or.h rsnull.h rsprox.h rstemp.h set.h \ -sortidx.h str.h zebra-lock.h zebramap.h zebrautl.h \ -zebra_xpath.h +data1.h dfa.h dict.h direntz.h isamb.h isamc.h isamd.h isamg.h isam.h \ +isams.h mfile.h passwddb.h recctrl.h res.h rsbetween.h rsbool.h rset.h \ +rsisamb.h rsisamc.h rsisamd.h rsisam.h rsisams.h rsm_or.h rsnull.h \ +rsprox.h rstemp.h set.h sortidx.h str.h zebra-lock.h zebramap.h zebrautl.h \ +zebra_xpath.h zebra-flock.h include_HEADERS = zebraapi.h zebraver.h diff --git a/include/zebra-flock.h b/include/zebra-flock.h new file mode 100644 index 0000000..e01f59b --- /dev/null +++ b/include/zebra-flock.h @@ -0,0 +1,50 @@ +/* $Id: zebra-flock.h,v 1.1.2.1 2006-03-24 13:47:29 adam Exp $ + Copyright (C) 1995-2005 + Index Data ApS + +This file is part of the Zebra server. + +Zebra 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 2, or (at your option) any later +version. + +Zebra is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Zebra; see the file LICENSE.zebra. If not, write to the +Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA +02111-1307, USA. +*/ + +#include <yaz/yconfig.h> + +#ifndef FLOCK_H +#define FLOCK_H + +YAZ_BEGIN_CDECL + +typedef struct zebra_lock_info *ZebraLockHandle; + +YAZ_EXPORT +ZebraLockHandle zebra_lock_create(const char *dir, const char *file); + +YAZ_EXPORT +void zebra_lock_destroy (ZebraLockHandle h); + +YAZ_EXPORT +int zebra_unlock (ZebraLockHandle h); +YAZ_EXPORT +char *zebra_mk_fname (const char *dir, const char *name); + +YAZ_EXPORT +int zebra_lock_w (ZebraLockHandle h); +YAZ_EXPORT +int zebra_lock_r (ZebraLockHandle h); + +YAZ_END_CDECL + +#endif diff --git a/index/Makefile.am b/index/Makefile.am index 0da7de0..867246a 100644 --- a/index/Makefile.am +++ b/index/Makefile.am @@ -1,11 +1,11 @@ -## $Id: Makefile.am,v 1.24 2004-08-04 08:35:23 adam Exp $ +## $Id: Makefile.am,v 1.23.2.1 2006-03-24 13:47:29 adam Exp $ noinst_PROGRAMS = apitest kdump noinst_LIBRARIES = libzebra.a libzebra_a_SOURCES = dir.c dirs.c trav.c kinput.c kcompare.c \ - attribute.c symtab.c recindex.c recstat.c lockutil.c \ + attribute.c symtab.c recindex.c recstat.c \ zebraapi.c zinfo.c invstat.c sortidx.c compact.c zsets.c zrpn.c \ rank1.c trunc.c retrieve.c extract.c livcode.c \ index.h recindex.h recindxp.h \ @@ -23,7 +23,7 @@ zebraidx_SOURCES = main.c zebrasrv_SOURCES = zserver.c zebrash_SOURCES = zebrash.c apitest_SOURCES = apitest.c -kdump_SOURCES = kdump.c +kdump_SOURCES=kdump.c kcompare.c AM_CPPFLAGS = -I$(srcdir)/../include $(YAZINC) $(TCL_INCLUDE) -DDEFAULT_PROFILE_PATH=\"$(pkgdatadir)/tab\" diff --git a/index/index.h b/index/index.h index 1a63874..6a9a274 100644 --- a/index/index.h +++ b/index/index.h @@ -1,4 +1,4 @@ -/* $Id: index.h,v 1.109.2.1 2005-05-09 19:57:38 adam Exp $ +/* $Id: index.h,v 1.109.2.2 2006-03-24 13:47:29 adam Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003 Index Data Aps @@ -50,6 +50,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include "zinfo.h" #include <passwddb.h> #include <rset.h> +#include <zebra-flock.h> YAZ_BEGIN_CDECL @@ -152,19 +153,7 @@ int zebraIndexWait (ZebraHandle zh, int commitPhase); #define FNAME_ORG_LOCK "zebraorg.LCK" #define FNAME_TOUCH_TIME "zebraidx.time" -typedef struct zebra_lock_info *ZebraLockHandle; -ZebraLockHandle zebra_lock_create(const char *dir, - const char *file, int excl_flag); -void zebra_lock_destroy (ZebraLockHandle h); -int zebra_lock (ZebraLockHandle h); -int zebra_lock_nb (ZebraLockHandle h); -int zebra_unlock (ZebraLockHandle h); -int zebra_lock_fd (ZebraLockHandle h); void zebra_lock_prefix (Res res, char *dst); -char *zebra_mk_fname (const char *dir, const char *name); - -int zebra_lock_w (ZebraLockHandle h); -int zebra_lock_r (ZebraLockHandle h); void zebra_load_atts (data1_handle dh, Res res); diff --git a/index/lockidx.c b/index/lockidx.c deleted file mode 100644 index 994f495..0000000 --- a/index/lockidx.c +++ /dev/null @@ -1,261 +0,0 @@ -/* $Id: lockidx.c,v 1.22 2002-08-02 19:26:55 adam Exp $ - Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002 - Index Data Aps - -This file is part of the Zebra server. - -Zebra 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 2, or (at your option) any later -version. - -Zebra is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or -FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -for more details. - -You should have received a copy of the GNU General Public License -along with Zebra; see the file LICENSE.zebra. If not, write to the -Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA -02111-1307, USA. -*/ - - -#include <stdio.h> -#include <assert.h> -#ifdef WIN32 -#include <io.h> -#else -#include <unistd.h> -#endif -#include <fcntl.h> -#include <string.h> -#include <errno.h> - -#include "index.h" -#include "zserver.h" - -static ZebraLockHandle server_lock_main = NULL; -static ZebraLockHandle server_lock_cmt = NULL; -static ZebraLockHandle server_lock_org = NULL; - -int zebraIndexWait (ZebraHandle zh, int commitPhase) -{ - ZebraLockHandle h; - - if (server_lock_cmt) - zebra_unlock (server_lock_cmt); - else - { - char path[1024]; - - zebra_lock_prefix (zh->service->res, path); - strcat (path, FNAME_COMMIT_LOCK); - server_lock_cmt = zebra_lock_create (path, 1); - if (!server_lock_cmt) - { - logf (LOG_WARN|LOG_ERRNO, "cannot create lock %s", path); - return -1; - } - } - if (server_lock_org) - zebra_unlock (server_lock_org); - else - { - char path[1024]; - - zebra_lock_prefix (zh->service->res, path); - strcat (path, FNAME_ORG_LOCK); - server_lock_org = zebra_lock_create (path, 1); - if (!server_lock_org) - { - logf (LOG_WARN|LOG_ERRNO, "cannot create lock %s", path); - return -1; - } - } - if (commitPhase) - h = server_lock_cmt; - else - h = server_lock_org; - if (zebra_lock_nb (h)) - { -#ifndef WIN32 - if (errno != EWOULDBLOCK) - { - logf (LOG_FATAL|LOG_ERRNO, "flock"); - exit (1); - } -#endif - if (commitPhase) - logf (LOG_LOG, "Waiting for lock cmt"); - else - logf (LOG_LOG, "Waiting for lock org"); - if (zebra_lock (h) == -1) - { - logf (LOG_FATAL, "flock"); - exit (1); - } - } - zebra_unlock (h); - return 0; -} - - -void zebraIndexLockMsg (ZebraHandle zh, const char *str) -{ - char path[1024]; - int l, r, fd; - - assert (server_lock_main); - fd = zebra_lock_fd (server_lock_main); - lseek (fd, 0L, SEEK_SET); - l = strlen(str); - r = write (fd, str, l); - if (r != l) - { - logf (LOG_FATAL|LOG_ERRNO, "write lock file"); - exit (1); - } - zebra_lock_prefix (zh->service->res, path); - strcat (path, FNAME_TOUCH_TIME); - fd = creat (path, 0666); - close (fd); -} - -void zebraIndexUnlock (ZebraHandle zh) -{ - char path[1024]; - - zebra_lock_prefix (zh->service->res, path); - strcat (path, FNAME_MAIN_LOCK); -#ifdef WIN32 - zebra_lock_destroy (server_lock_main); - if (unlink (path) && errno != ENOENT) - logf (LOG_WARN|LOG_ERRNO, "unlink %s failed", path); -#else - if (unlink (path) && errno != ENOENT) - logf (LOG_WARN|LOG_ERRNO, "unlink %s failed", path); - zebra_lock_destroy (server_lock_main); -#endif - server_lock_main = 0; -} - -int zebraIndexLock (BFiles bfs, ZebraHandle zh, int commitNow, - const char *rval) -{ - char path[1024]; - char buf[256]; - int r; - - if (server_lock_main) - return 0; - - zebra_lock_prefix (zh->service->res, path); - strcat (path, FNAME_MAIN_LOCK); - while (1) - { - server_lock_main = zebra_lock_create (path, 2); - if (!server_lock_main) - { - server_lock_main = zebra_lock_create (path, 1); - if (!server_lock_main) - { - logf (LOG_FATAL, "couldn't obtain indexer lock"); - exit (1); - } - if (zebra_lock_nb (server_lock_main) == -1) - { -#ifdef WIN32 - logf (LOG_LOG, "waiting for other index process"); - zebra_lock (server_lock_main); - zebra_unlock (server_lock_main); - zebra_lock_destroy (server_lock_main); - continue; -#else - if (errno == EWOULDBLOCK) - { - logf (LOG_LOG, "waiting for other index process"); - zebra_lock (server_lock_main); - zebra_unlock (server_lock_main); - zebra_lock_destroy (server_lock_main); - continue; - } - else - { - logf (LOG_FATAL|LOG_ERRNO, "flock %s", path); - exit (1); - } -#endif - } - else - { - int fd = zebra_lock_fd (server_lock_main); - - logf (LOG_WARN, "unlocked %s", path); - r = read (fd, buf, 256); - if (r == 0) - { - logf (LOG_WARN, "zero length %s", path); - zebra_lock_destroy (server_lock_main); - unlink (path); - continue; - } - else if (r == -1) - { - logf (LOG_FATAL|LOG_ERRNO, "read %s", path); - exit (1); - } - if (*buf == 'r') - { - logf (LOG_WARN, "previous transaction didn't" - " reach commit"); - zebra_lock_destroy (server_lock_main); - bf_commitClean (bfs, rval); - unlink (path); - continue; - } - else if (*buf == 'd') - { - logf (LOG_WARN, "commit file wan't deleted after commit"); - zebra_lock_destroy (server_lock_main); - bf_commitClean (bfs, rval); - unlink (path); - continue; - } - else if (*buf == 'w') - { - logf (LOG_WARN, - "The lock file indicates that your index is"); - logf (LOG_WARN, "inconsistent. Perhaps the indexer"); - logf (LOG_WARN, "terminated abnormally in the previous"); - logf (LOG_WARN, "run. You can try to proceed by"); - logf (LOG_WARN, "deleting the file %s", path); - exit (1); - } - else if (*buf == 'c') - { - if (commitNow) - { - unlink (path); - zebra_lock_destroy (server_lock_main); - continue; - } - logf (LOG_FATAL, "previous transaction didn't" - " finish commit. Commit now!"); - exit (1); - } - else - { - logf (LOG_FATAL, "unknown id 0x%02x in %s", *buf, - path); - exit (1); - } - } - } - else - break; - } - zebra_lock (server_lock_main); - return 0; -} - diff --git a/index/lockutil.c b/index/lockutil.c deleted file mode 100644 index 2550322..0000000 --- a/index/lockutil.c +++ /dev/null @@ -1,185 +0,0 @@ -/* $Id: lockutil.c,v 1.18 2004-01-22 11:27:21 adam Exp $ - Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002 - Index Data Aps - -This file is part of the Zebra server. - -Zebra 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 2, or (at your option) any later -version. - -Zebra is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or -FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -for more details. - -You should have received a copy of the GNU General Public License -along with Zebra; see the file LICENSE.zebra. If not, write to the -Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA -02111-1307, USA. -*/ - - -#include <stdio.h> -#include <assert.h> -#include <string.h> -#include <errno.h> -#include <fcntl.h> -#include <sys/types.h> -#ifdef WIN32 -#include <io.h> -#include <sys/locking.h> -#else -#include <unistd.h> -#endif - -#include "index.h" - -struct zebra_lock_info { - int fd; - int excl_flag; -}; - -char *zebra_mk_fname (const char *dir, const char *name) -{ - int dlen = dir ? strlen(dir) : 0; - char *fname = xmalloc (dlen + strlen(name) + 3); - -#ifdef WIN32 - if (dlen) - { - int last_one = dir[dlen-1]; - - if (!strchr ("/\\:", last_one)) - sprintf (fname, "%s\\%s", dir, name); - else - sprintf (fname, "%s%s", dir, name); - } - else - sprintf (fname, "%s", name); -#else - if (dlen) - { - int last_one = dir[dlen-1]; - - if (!strchr ("/", last_one)) - sprintf (fname, "%s/%s", dir, name); - else - sprintf (fname, "%s%s", dir, name); - } - else - sprintf (fname, "%s", name); -#endif - return fname; -} - -ZebraLockHandle zebra_lock_create (const char *dir, - const char *name, int excl_flag) -{ - char *fname = zebra_mk_fname(dir, name); - ZebraLockHandle h = (ZebraLockHandle) xmalloc (sizeof(*h)); - - h->excl_flag = excl_flag; - h->fd = -1; - - -#ifdef WIN32 - if (!h->excl_flag) - h->fd = open (name, O_BINARY|O_RDONLY); - if (h->fd == -1) - h->fd = open (fname, ((h->excl_flag > 1) ? O_EXCL : 0)| - (O_BINARY|O_CREAT|O_RDWR), 0666); -#else - h->fd= open (fname, ((h->excl_flag > 1) ? O_EXCL : 0)| - (O_BINARY|O_CREAT|O_RDWR), 0666); -#endif - if (h->fd == -1) - { - if (h->excl_flag <= 1) - logf (LOG_WARN|LOG_ERRNO, "open %s", fname); - xfree (h); - h = 0; - } - xfree (fname); - return h; -} - -void zebra_lock_destroy (ZebraLockHandle h) -{ - if (!h) - return; - if (h->fd != -1) - close (h->fd); - xfree (h); -} - -void zebra_lock_prefix (Res res, char *path) -{ - const char *lock_dir = res_get_def (res, "lockDir", ""); - - strcpy (path, lock_dir); - if (*path && path[strlen(path)-1] != '/') - strcat (path, "/"); -} - -#ifndef WIN32 -static int unixLock (int fd, int type, int cmd) -{ - struct flock area; - area.l_type = type; - area.l_whence = SEEK_SET; - area.l_len = area.l_start = 0L; - return fcntl (fd, cmd, &area); -} -#endif - -int zebra_lock_w (ZebraLockHandle h) -{ -#ifdef WIN32 - return _locking (h->fd, _LK_LOCK, 1); -#else - return unixLock (h->fd, F_WRLCK, F_SETLKW); -#endif -} - -int zebra_lock_r (ZebraLockHandle h) -{ -#ifdef WIN32 - return _locking (h->fd, _LK_LOCK, 1); -#else - return unixLock (h->fd, F_RDLCK, F_SETLKW); -#endif -} - -int zebra_lock (ZebraLockHandle h) -{ -#ifdef WIN32 - return _locking (h->fd, _LK_LOCK, 1); -#else - return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLKW); -#endif -} - -int zebra_lock_nb (ZebraLockHandle h) -{ -#ifdef WIN32 - return _locking (h->fd, _LK_NBLCK, 1); -#else - return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLK); -#endif -} - -int zebra_unlock (ZebraLockHandle h) -{ -#ifdef WIN32 - return _locking (h->fd, _LK_UNLCK, 1); -#else - return unixLock (h->fd, F_UNLCK, F_SETLKW); -#endif -} - -int zebra_lock_fd (ZebraLockHandle h) -{ - return h->fd; -} diff --git a/index/zebraapi.c b/index/zebraapi.c index 44a4aa0..3a1e164 100644 --- a/index/zebraapi.c +++ b/index/zebraapi.c @@ -1,4 +1,4 @@ -/* $Id: zebraapi.c,v 1.120.2.10 2005-09-09 14:22:12 adam Exp $ +/* $Id: zebraapi.c,v 1.120.2.11 2006-03-24 13:47:29 adam Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004 Index Data Aps @@ -638,11 +638,11 @@ static int zebra_select_register (ZebraHandle zh, const char *new_reg) res_set (zh->res, "lockDir", zh->path_reg); sprintf (fname, "norm.%s.LCK", zh->reg_name); zh->lock_normal = - zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0); + zebra_lock_create (res_get(zh->res, "lockDir"), fname); sprintf (fname, "shadow.%s.LCK", zh->reg_name); zh->lock_shadow = - zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0); + zebra_lock_create (res_get(zh->res, "lockDir"), fname); if (!zh->lock_normal || !zh->lock_shadow) { @@ -2147,3 +2147,12 @@ int zebra_sort_by_specstr (ZebraHandle zh, return sort_status; } +void zebra_lock_prefix (Res res, char *path) +{ + const char *lock_dir = res_get_def (res, "lockDir", ""); + + strcpy (path, lock_dir); + if (*path && path[strlen(path)-1] != '/') + strcat (path, "/"); +} + diff --git a/test/marcxml/record.abs b/test/marcxml/record.abs index b145558..6adbc4f 100644 --- a/test/marcxml/record.abs +++ b/test/marcxml/record.abs @@ -1,4 +1,4 @@ -# $Id: record.abs,v 1.3.2.3 2006-02-06 13:19:55 adam Exp $ +# $Id: record.abs,v 1.3.2.4 2006-03-24 13:47:30 adam Exp $ name marcxml attset bib1.att @@ -10,9 +10,9 @@ marc usmarc.mar xpath disable -#melm 010$a identifier-standard,identifier-standard:p xelm /record/controlfield[@tag="001"] Material-type:w:range(data,3,1) xelm /record/controlfield[@tag="008"] Code-Language:w +melm 010$a identifier-standard,identifier-standard:p xelm /record/datafield[@tag="100"]/subfield[@code="a"] author:w,author:s xelm /record/datafield[@tag="245"]/subfield title:w diff --git a/test/marcxml/zebra.cfg b/test/marcxml/zebra.cfg index 9503768..005f754 100644 --- a/test/marcxml/zebra.cfg +++ b/test/marcxml/zebra.cfg @@ -1,5 +1,5 @@ # Simple Zebra configuration file -# $Id: zebra.cfg,v 1.2 2004-06-15 09:43:33 adam Exp $ +# $Id: zebra.cfg,v 1.2.2.1 2006-03-24 13:47:30 adam Exp $ # # Where the schema files, attribute files, etc are located. profilePath: ${srcdir:-.}:${srcdir:-.}/../../tab @@ -9,9 +9,14 @@ attset: bib1.att attset: gils.att attset: explain.att +perm.anonymous: rw + recordtype: grs.xml -#storekeys: 1 -#storedata: 1 -#recordId: (bib1,identifier-standard) +storekeys: 1 +storedata: 1 +recordId: (bib1,identifier-standard) isam: b + +shadow: shadow:1G +register: register:1G diff --git a/util/Makefile.am b/util/Makefile.am index 10ee531..f03f3a8 100644 --- a/util/Makefile.am +++ b/util/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am,v 1.9 2004-08-04 08:35:27 adam Exp $ +## $Id: Makefile.am,v 1.8.2.1 2006-03-24 13:47:30 adam Exp $ noinst_LIBRARIES = libutil.a @@ -9,6 +9,6 @@ EXTRA_DIST = zebrasrv.rh AM_CPPFLAGS = -I$(srcdir)/../include $(YAZINC) -DDEFAULT_PROFILE_PATH=\"$(pkgdatadir)/tab\" LDADD = libutil.a $(YAZLIB) $(TCL_LIB) -libutil_a_SOURCES = res.c charmap.c zebramap.c passwddb.c zebra-lock.c dirent.c xpath.c atoi_zn.c +libutil_a_SOURCES = res.c flock.c charmap.c zebramap.c passwddb.c zebra-lock.c dirent.c xpath.c passtest_SOURCES = passtest.c diff --git a/util/flock.c b/util/flock.c new file mode 100644 index 0000000..62ac8f4 --- /dev/null +++ b/util/flock.c @@ -0,0 +1,164 @@ +/* $Id: flock.c,v 1.4.2.1 2006-03-24 13:47:30 adam Exp $ + Copyright (C) 1995-2005 + Index Data ApS + +This file is part of the Zebra server. + +Zebra 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 2, or (at your option) any later +version. + +Zebra is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Zebra; see the file LICENSE.zebra. If not, write to the +Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA +02111-1307, USA. +*/ + + +#include <stdio.h> +#include <assert.h> +#include <string.h> +#include <errno.h> +#include <fcntl.h> +#include <sys/types.h> +#ifdef WIN32 +#include <io.h> +#include <sys/locking.h> +#endif +#if HAVE_UNISTD_H +#include <unistd.h> +#endif + +#include <zebra-flock.h> +#include <yaz/xmalloc.h> +#include <yaz/log.h> + +struct zebra_lock_info { + int fd; + char *fname; +}; + +int log_level = 0 /* YLOG_LOG|YLOG_FLUSH */; + +char *zebra_mk_fname (const char *dir, const char *name) +{ + int dlen = dir ? strlen(dir) : 0; + char *fname = xmalloc (dlen + strlen(name) + 3); + +#ifdef WIN32 + if (dlen) + { + int last_one = dir[dlen-1]; + + if (!strchr ("/\\:", last_one)) + sprintf (fname, "%s\\%s", dir, name); + else + sprintf (fname, "%s%s", dir, name); + } + else + sprintf (fname, "%s", name); +#else + if (dlen) + { + int last_one = dir[dlen-1]; + + if (!strchr ("/", last_one)) + sprintf (fname, "%s/%s", dir, name); + else + sprintf (fname, "%s%s", dir, name); + } + else + sprintf (fname, "%s", name); +#endif + return fname; +} + +ZebraLockHandle zebra_lock_create (const char *dir, const char *name) +{ + char *fname = zebra_mk_fname(dir, name); + ZebraLockHandle h = (ZebraLockHandle) xmalloc (sizeof(*h)); + + h->fd = -1; +#ifdef WIN32 + h->fd = open (name, O_BINARY|O_RDONLY); + if (h->fd == -1) + h->fd = open (fname, (O_BINARY|O_CREAT|O_RDWR), 0666); +#else + h->fd= open (fname, (O_BINARY|O_CREAT|O_RDWR), 0666); +#endif + if (h->fd == -1) + { + xfree (h); + h = 0; + } + h->fname = fname; + yaz_log(log_level, "zebra_lock_create fd=%d p=%p fname=%s", h->fd, h, h->fname); + return h; +} + +void zebra_lock_destroy (ZebraLockHandle h) +{ + if (!h) + return; + yaz_log(log_level, "zebra_lock_destroy fd=%d p=%p fname=%s", h->fd, h, h->fname); + if (h->fd != -1) + close (h->fd); + xfree (h->fname); + xfree (h); +} + +#ifndef WIN32 +static int unixLock (int fd, int type, int cmd) +{ + struct flock area; + area.l_type = type; + area.l_whence = SEEK_SET; + area.l_len = area.l_start = 0L; + return fcntl (fd, cmd, &area); +} +#endif + +int zebra_lock_w (ZebraLockHandle h) +{ + int r; + yaz_log(log_level, "zebra_lock_w fd=%d p=%p fname=%s", h->fd, h, h->fname); +#ifdef WIN32 + while ((r = _locking (h->fd, _LK_LOCK, 1))) + ; +#else + r = unixLock (h->fd, F_WRLCK, F_SETLKW); +#endif + yaz_log(log_level, "zebra_lock_w fd=%d p=%p fname=%s OK", h->fd, h, h->fname); + return r; +} + +int zebra_lock_r (ZebraLockHandle h) +{ + int r; + yaz_log(log_level, "zebra_lock_r fd=%d p=%p fname=%s", h->fd, h, h->fname); +#ifdef WIN32 + while ((r = _locking (h->fd, _LK_LOCK, 1))) + ; +#else + r = unixLock (h->fd, F_RDLCK, F_SETLKW); +#endif + yaz_log(log_level, "zebra_lock_r fd=%d p=%p fname=%s OK", h->fd, h, h->fname); + return r; +} + +int zebra_unlock (ZebraLockHandle h) +{ + yaz_log(log_level, "zebra_unlock fd=%d p=%p fname=%s", h->fd, h, h->fname); +#ifdef WIN32 + return _locking (h->fd, _LK_UNLCK, 1); +#else + return unixLock (h->fd, F_UNLCK, F_SETLKW); +#endif +} + -- 1.7.10.4