portnum = l.getPortnum()
# record which port we're listening on, so we can grab the same one
# next time
- open(self._portnumfile, "w").write("%d\n" % portnum)
+ fileutil.write_atomically(self._portnumfile, "%d\n" % portnum, mode="")
base_location = ",".join([ "%s:%d" % (addr, portnum)
for addr in local_addresses ])
d.addCallback(_cb2)
def _node_has_started():
+ # this depends upon both files being created atomically
return os.path.exists(NODE_URL_FILE) and os.path.exists(PORTNUM_FILE)
d.addCallback(lambda res: self.poll(_node_has_started))
# 'tahoe stop' command takes a while.
def _stop(res):
fileutil.write(HOTLINE_FILE, "")
- self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
+ self.failUnless(os.path.exists(TWISTD_PID_FILE),
+ (TWISTD_PID_FILE,
+ os.listdir(os.path.dirname(TWISTD_PID_FILE))))
return self.run_bintahoe(["--quiet", "stop", c1])
d.addCallback(_stop)
fileutil.rm_dir(basedir)
fileutil.remove_if_possible(fn) # should survive errors
+ def test_write_atomically(self):
+ basedir = "util/FileUtil/test_write_atomically"
+ fileutil.make_dirs(basedir)
+ fn = os.path.join(basedir, "here")
+ fileutil.write_atomically(fn, "one")
+ self.failUnlessEqual(fileutil.read(fn), "one")
+ fileutil.write_atomically(fn, "two", mode="") # non-binary
+ self.failUnlessEqual(fileutil.read(fn), "two")
+
def test_open_or_create(self):
basedir = "util/FileUtil/test_open_or_create"
fileutil.make_dirs(basedir)
remove_if_possible(dest)
os.rename(source, dest)
+def write_atomically(target, contents, mode="b"):
+ f = open(target+".tmp", "w"+mode)
+ f.write(contents)
+ f.close()
+ move_into_place(target+".tmp", target)
+
def write(path, data):
wf = open(path, "wb")
try:
if nodeurl_path:
def _write_nodeurl_file(ign):
# this file will be created with default permissions
- fileutil.write(nodeurl_path, self.getURL() + "\n")
+ line = self.getURL() + "\n"
+ fileutil.write_atomically(nodeurl_path, line, mode="")
self._started.addCallback(_write_nodeurl_file)
def getURL(self):