self.background_procs.append(rproc)
return rproc
- def create_n_files(self, fs_path, count, sync=False):
+ def create_n_files(self, fs_path, count, sync=False, dirsync=False, unlink=False, finaldirsync=False):
+ """
+ Create n files.
+
+ :param sync: sync the file after writing
+ :param dirsync: sync the containing directory after closing the file
+ :param unlink: unlink the file after closing
+ :param finaldirsync: sync the containing directory after closing the last file
+ """
+
assert(self.is_mounted())
abs_path = os.path.join(self.hostfs_mntpt, fs_path)
- pyscript = dedent("""
- import sys
- import time
+ pyscript = dedent(f"""
import os
n = {count}
- abs_path = "{abs_path}"
+ path = "{abs_path}"
- if not os.path.exists(os.path.dirname(abs_path)):
- os.makedirs(os.path.dirname(abs_path))
+ dpath = os.path.dirname(path)
+ fnameprefix = os.path.basename(path)
+ os.makedirs(dpath, exist_ok=True)
- for i in range(0, n):
- fname = "{{0}}_{{1}}".format(abs_path, i)
- with open(fname, 'w') as f:
- f.write('content')
- if {sync}:
- f.flush()
- os.fsync(f.fileno())
- """).format(abs_path=abs_path, count=count, sync=str(sync))
+ try:
+ dirfd = os.open(dpath, os.O_DIRECTORY)
+
+ for i in range(n):
+ fpath = os.path.join(dpath, f"{{fnameprefix}}_{{i}}")
+ with open(fpath, 'w') as f:
+ f.write(f"{{i}}")
+ if {sync}:
+ f.flush()
+ os.fsync(f.fileno())
+ if {unlink}:
+ os.unlink(fpath)
+ if {dirsync}:
+ os.fsync(dirfd)
+ if {finaldirsync}:
+ os.fsync(dirfd)
+ finally:
+ os.close(dirfd)
+ """)
self.run_python(pyscript)
self.fs.wait_for_daemons()
# check data in file sytem is intact
filepath = os_path_join(self.mount_a.hostfs_mntpt, 'file_on_fs_0')
- self.assertEqual(self.mount_a.read_file(filepath), "content")
+ self.assertEqual(self.mount_a.read_file(filepath), "0")