Remove output_file before generating it
aosp/3478832 recently switched some fsv_meta files to symlinks.
The symlink creation and file writing operations in most programming
languages don't work properly when the destination is already exists
and is of the opposite type. (symlinking on top of an existing file/
symlink would fail, file writing would write through the symlink)
Remove the output file first.
This wasn't noticed origionally because it only happens on certain
types of incremental builds, and is also fixed if you run an
`m installclean`.
Bug: 394404628
Test: m
Change-Id: I7f900d52b81bb696e597f20f20a092987e42db2e
diff --git a/tools/releasetools/fsverity_metadata_generator.py b/tools/releasetools/fsverity_metadata_generator.py
index 8448237..50e23e7 100644
--- a/tools/releasetools/fsverity_metadata_generator.py
+++ b/tools/releasetools/fsverity_metadata_generator.py
@@ -236,6 +236,12 @@
'We require that all .fsv_meta files follow this convention regardless of if it\'s a link or '
'not. However {args.input} had a different output file: {args.output}')
+ # remove the output file first, as switching between a file and a symlink can be complicated
+ try:
+ os.remove(output_file)
+ except FileNotFoundError:
+ pass
+
if os.path.islink(args.input):
target = os.readlink(args.input) + '.fsv_meta'
os.symlink(target, output_file)