From: Christoph Egger Date: Thu, 18 Feb 2016 13:19:52 +0000 (+0100) Subject: fix locking X-Git-Url: https://git.siccegge.de//index.cgi?p=tools.git;a=commitdiff_plain;h=fb9d703d86716511800073d1c11784cab547a789 fix locking --- diff --git a/tls/addrecord b/tls/addrecord index fa4c840..8d171c3 100644 --- a/tls/addrecord +++ b/tls/addrecord @@ -73,25 +73,29 @@ class Addrecord: def _update_records(self, sort, records): to_remove = [ i.split()[0] for i in records ] - with open('%s/%s.m4' % (sort, self._host), 'r') as oldzone: - fcntl.flock(oldzone, fcntl.LOCK_EX) - lines = oldzone.readlines() - lines = [ line for line in lines if line == '\n' or line.split()[0] not in to_remove ] - - lines.append('\n') - lines.append("; Last updated %s by %s\n" % (datetime.datetime.utcnow().isoformat(), - self._host)) - lines = lines + records - - with open('%s/%s.m4.new' % (sort, self._host), 'w') as newzone: - newtext = ''.join(lines) - newtext = re.sub(r'\n[\n]+', '\n\n', newtext) - newtext = re.sub(r'\n;.*\n\n;', '\n;', newtext) - newzone.write(newtext) - - os.rename('%s/%s.m4.new' % (sort, self._host), - '%s/%s.m4' % (sort, self._host)) - fcntl.flock(oldzone, fcntl.LOCK_UN) + with open('%s/%s.m4.lock' % (sort, self._host), 'w') as lockfd: + fcntl.flock(lockfd, fcntl.LOCK_EX) + with open('%s/%s.m4' % (sort, self._host), 'r') as oldzone: + lines = oldzone.readlines() + lines = [ line for line in lines + if line == '\n' or line.split()[0] not in to_remove ] + + lines.append('\n') + lines.append("; Last updated %s by %s\n" + % (datetime.datetime.utcnow().isoformat(), + self._host)) + lines = lines + records + + with open('%s/%s.m4.new' % (sort, self._host), 'w') as newzone: + newtext = ''.join(lines) + newtext = re.sub(r'\n[\n]+', '\n\n', newtext) + newtext = re.sub(r'\n;.*\n\n;', '\n;', newtext) + newzone.write(newtext) + + os.rename('%s/%s.m4.new' % (sort, self._host), + '%s/%s.m4' % (sort, self._host)) + + fcntl.flock(lockfd, fcntl.LOCK_UN) def main():