From: teodor Date: Mon, 7 Jul 2008 14:24:17 +0000 (+0000) Subject: Use flock call instead of open's flags O_EXLOCK/O_SHLOCK X-Git-Url: http://sigaev.ru/git/gitweb.cgi?a=commitdiff_plain;h=de2723270f9a04d99137a3f6a7d6ba1e90b5d01b;p=tedtools.git Use flock call instead of open's flags O_EXLOCK/O_SHLOCK --- 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); }