From de2723270f9a04d99137a3f6a7d6ba1e90b5d01b Mon Sep 17 00:00:00 2001 From: teodor Date: Mon, 7 Jul 2008 14:24:17 +0000 Subject: [PATCH] Use flock call instead of open's flags O_EXLOCK/O_SHLOCK --- sfxstr.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sfxstr.c b/sfxstr.c index 36d1f31..92c8233 100644 --- a/sfxstr.c +++ b/sfxstr.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -1019,9 +1020,11 @@ SFSWriteDump(SFSTree *info, char *filename) { off_t size = info->totalen + SFSTDHSZ; SFSTreeDumpHeader dh; - if ( (fd = open(filename, O_RDWR|O_CREAT|O_TRUNC|O_EXLOCK, 0666)) < 0 ) + if ( (fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0 ) tlog(TL_CRIT|TL_EXIT, "Can not open file '%s': %s", strerror(errno)); + if ( flock(fd, LOCK_EX) < 0 ) + tlog(TL_CRIT|TL_EXIT, "flock failed: %s", strerror(errno)); if ( lseek(fd, size, SEEK_SET) < 0 ) tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno)); @@ -1049,6 +1052,7 @@ SFSWriteDump(SFSTree *info, char *filename) { writeNode(&wp, fd, info->node); } + flock(fd, LOCK_UN); close(fd); } @@ -1100,8 +1104,10 @@ SFSReadDump(SFSTree *info, char *filename) { memset(info,0,sizeof(SFSTree)); - if ( (fd = open(filename, O_RDONLY|O_SHLOCK)) < 0 ) + if ( (fd = open(filename, O_RDONLY)) < 0 ) tlog(TL_CRIT|TL_EXIT, "Can not open file '%s': %s", strerror(errno)); + if ( flock(fd, LOCK_SH) < 0 ) + tlog(TL_CRIT|TL_EXIT, "flock failed: %s", strerror(errno)); if ( read(fd, &dh, SFSTDHSZ) != SFSTDHSZ ) tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno)); @@ -1123,6 +1129,7 @@ SFSReadDump(SFSTree *info, char *filename) { info->node = readNode(info, fd, buf, bufsize); tfree(buf); + flock(fd, LOCK_UN); close(fd); } -- 2.37.3