diff --git a/app.py b/app.py index a8a184a..e4dae99 100644 --- a/app.py +++ b/app.py @@ -97,8 +97,9 @@ def verify_node(d): if "/" in node: raise ValueError("invalid node name %s", node) try: - log.info("getting client config from %s", "config/" + node) - with open("config/" + node) as fh: + fn = "config/" + node + log.info("getting client config from %s", fn) + with open(fn) as fh: node_conf = json.load(fh) except Exception as e: log.warning("got %s opening %s", e, "config/" + node) @@ -112,8 +113,19 @@ def verify_node(d): if timestamp > node_conf["last"]: node_conf["last"] = timestamp os.replace("config/" + node, "config/" + node + ".old") - with open("config/" + node, "w") as fh: + tmpfn = fn + "." + str(os.getpid()) + oldfn = fn + ".old" + with open(tmpfn, "w") as fh: json.dump(node_conf, fh) # XXX + try: + os.unlink(oldfn) + except FileNotFoundError: + pass + try: + os.link(fn, oldfn) + except FileNotFoundError: + pass + os.rename(tmpfn, fn) return node else: abort(409, "timestamp out of sync")