Truncate existing files before overwriting.

This fixes a flake in testConfigTtl test. When a config's TTL expires,
data is written to disk. In the test's tearDown, when the config is
removed, data is again written to disk. The name of the file written is
prefixed by the current wallclock time in seconds. If the second write
happens within the same second, data will be written to the same file.
The file is overwritten without being cleared first so if the new
content is smaller than the existing one, there is extra garbage bytes
left in the file. This causes proto parsing to fail when the file is read.

This change adds a flag to clear the file contents before writing to it.

Bug: 333187642
Test: atest --rerun-until-failure 50
CtsStatsdHostTestCases: android.cts.statsd.metadata.MetadataTests\#testConfigTtl
Change-Id: Ie770ed1eb63eb4e8bb346bdfd7eab6aa80721428
3 files changed