aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvg <vgm+dev@devys.org>2021-01-12 11:23:37 +0100
committervg <vgm+dev@devys.org>2021-01-12 11:23:37 +0100
commit34e11a6770519cfabcd3af25d931bfc11b63ffce (patch)
tree97a7e3cf6bf65f4826e958afbb2ca2d1b5ac1303
parente79dd839dad0e33a0998d27441523fd732775cf5 (diff)
downloadscripts-34e11a6770519cfabcd3af25d931bfc11b63ffce.tar.gz
scripts-34e11a6770519cfabcd3af25d931bfc11b63ffce.tar.bz2
scripts-34e11a6770519cfabcd3af25d931bfc11b63ffce.zip
add file locking for simplest cases protection
-rwxr-xr-xscripts/xattr_user_id5
1 files changed, 4 insertions, 1 deletions
diff --git a/scripts/xattr_user_id b/scripts/xattr_user_id
index 28a1d99..7badd9d 100755
--- a/scripts/xattr_user_id
+++ b/scripts/xattr_user_id
@@ -46,9 +46,9 @@ Options:
### standard modules
import contextlib
+import fcntl
import functools
import itertools
-import operator
import os
import sys
@@ -86,6 +86,9 @@ def main():
last_used_id = 0
if args['--state']:
with contextlib.suppress(FileNotFoundError):
+ lock_fd = open(args['--state'], 'r') # locked until program exits
+ # limitation: if file does not exists yet, file won't be locked
+ fcntl.flock(lock_fd, fcntl.LOCK_EX)
with open(args['--state'], encoding='utf8') as stream:
last_used_id = int(stream.read())