# Created file disappears if directory is not synced.
create /foo
----

open /foo
----

crash-clone 0 fs0
----

switch-fs fs0
----

open /foo
----
error: open /foo: file does not exist

switch-fs initial
----

# Create directory and a file in it and write and read from it.
mkdirall /bar
----

create /bar/y
----

f.stat.name
----
y

# Write some data; read it back.
f.write
abcde
----

f.close
----

open /bar/y
----

f.read 5
----
abcde

f.close
----

open /bar
----

# Resetting causes both the directory and file to disappear.
crash-clone 0 fs1
----

switch-fs fs1
----

open-dir /bar
----
error: open /bar: file does not exist

open /bar/y
----
error: open bar/y: file does not exist

switch-fs initial
----

# Create the directory and file again. Link the file to another file in the same
# dir, and to a file in the root dir. Sync the root dir. After reset, the
# created dir and the file in the root dir are the only ones visible.
mkdirall /bar
----

create /bar/y
----

open-dir /
----

f.sync
----

f.close
----

link /bar/y /bar/z
----

link /bar/y /z
----

open-dir /
----

f.sync
----

f.close
----

crash-clone 0 fs2
----

switch-fs fs2
----

open-dir /bar
----

open /bar/y
----
error: open /bar/y: file does not exist

open /bar/z
----
error: open /bar/z: file does not exist

open /z
----

switch-fs initial
----

# Create the file in the directory again and this time sync /bar directory. The
# file is preserved after reset.
create /bar/y
----

open-dir /bar
----

f.sync
----

f.close
----

crash-clone 0 fs3
----

switch-fs fs3
----

open-dir /bar
----

open /bar/y
----

switch-fs initial
----

# Unsynced data in the file is lost on reset.
create /bar/y
----

f.write
a
----

f.sync
----

f.write
b
----

f.close
----

open-dir /bar
----

f.sync
----

f.close
----

crash-clone 0 fs4
----

switch-fs fs4
----

open /bar/y
----

f.read 1
----
a

f.read 1
----
error: EOF

f.close
----

switch-fs initial
----

# reuse-for-write works correctly in that unsynced data does not overwrite
# previous contents when a crash happens.
create /z
----

f.write
abcdefgh
----

f.sync
----

f.close
----

reuse-for-write /z /y
----

f.write
x
----

f.sync
----

# Will be lost.
f.write
y
----

f.close
----

open-dir /
----

f.sync
----

f.close
----

crash-clone 0 fs5
----

switch-fs fs5
----

open /y
----

f.read 8
----
xbcdefgh

f.close
----
