From 748670d5d5a87276e0aa14f083ce4d4abbd8350c Mon Sep 17 00:00:00 2001 From: Thomas Herrmann Date: Tue, 1 Dec 2015 11:03:33 +0100 Subject: [PATCH 1/2] Prevent inconsistent data in json files. Dumping the json content directly into the final output files creates inconsistend data temporarily, which may cause strange effects. Writing to temporary files and atomically renaming them should fix this. Works only on unix-like systems where os.rename can overwrite existing files. --- backend.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/backend.py b/backend.py index 3ea2f5d..1813085 100755 --- a/backend.py +++ b/backend.py @@ -136,18 +136,22 @@ def extract_tunnel(nodes): batadv_graph = graph.merge_nodes(batadv_graph) batadv_graph = graph.to_undirected(batadv_graph) - # write processed data to dest dir - with open(nodes_fn, 'w') as f: + # write processed data to temporary files in dest dir + # and rename them atomically to prevent inconsistent data + with open(nodes_fn + '.tmp', 'w') as f: json.dump(nodedb, f) + os.rename(nodes_fn + '.tmp', nodes_fn) graph_out = {'batadv': json_graph.node_link_data(batadv_graph), 'version': GRAPH_VERSION} - with open(graph_fn, 'w') as f: + with open(graph_fn + '.tmp', 'w') as f: json.dump(graph_out, f) + os.rename(graph_fn + '.tmp', graph_fn) - with open(nodelist_fn, 'w') as f: + with open(nodelist_fn + 'tmp', 'w') as f: json.dump(export_nodelist(now, nodedb), f) + os.rename(nodelist_fn + '.tmp', nodelist_fn) # optional rrd graphs (trigger with --rrd) if params['rrd']: From 4c15be9c09dea92e21963d3e8d91c0d6152cdd04 Mon Sep 17 00:00:00 2001 From: Thomas Herrmann Date: Mon, 7 Dec 2015 10:56:01 +0100 Subject: [PATCH 2/2] Removed trailing whitespace in comment. --- backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend.py b/backend.py index 1813085..04a795a 100755 --- a/backend.py +++ b/backend.py @@ -137,7 +137,7 @@ def extract_tunnel(nodes): batadv_graph = graph.to_undirected(batadv_graph) # write processed data to temporary files in dest dir - # and rename them atomically to prevent inconsistent data + # and rename them atomically to prevent inconsistent data with open(nodes_fn + '.tmp', 'w') as f: json.dump(nodedb, f) os.rename(nodes_fn + '.tmp', nodes_fn)