From 46b092ea87078355db6865bcdaf1028c4c7d3ae3 Mon Sep 17 00:00:00 2001 From: Emile Sonneveld Date: Mon, 14 Oct 2024 23:44:20 +0200 Subject: [PATCH] Try writing to tmp path and then moving to avoid empty files. https://github.com/Open-EO/openeo-geotrellis-extensions/issues/329 --- .../openeo/geotrellis/geotiff/package.scala | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/openeo-geotrellis/src/main/scala/org/openeo/geotrellis/geotiff/package.scala b/openeo-geotrellis/src/main/scala/org/openeo/geotrellis/geotiff/package.scala index ea7f33b4..a0bfcb69 100644 --- a/openeo-geotrellis/src/main/scala/org/openeo/geotrellis/geotiff/package.scala +++ b/openeo-geotrellis/src/main/scala/org/openeo/geotrellis/geotiff/package.scala @@ -828,21 +828,27 @@ package object geotiff { } private def writeGeoTiff(geoTiff: MultibandGeoTiff, path: String): String = { + import java.nio.file.Files if (path.startsWith("s3:/")) { val correctS3Path = path.replaceFirst("s3:/(?!/)", "s3://") - import java.nio.file.Files val tempFile = Files.createTempFile(null, null) geoTiff.write(tempFile.toString, optimizedOrder = true) uploadToS3(tempFile, correctS3Path) } else { - geoTiff.write(path, optimizedOrder = true) - // Call fsync on the parent path to assure the fusemount is up-to data: - val channel = FileChannel.open(Path.of(path)) - // Ensure that all changes to the file are written to disk - channel.force(true) // The equivalent of Python's os.fsync + val tempFile = Files.createTempFile(null, null) + geoTiff.write(tempFile.toString, optimizedOrder = true) + + // Geotrellis writes the file piecewise and sometimes files are only partially written. + // Maybe a move operation is easier for the fusemount: + Files.move(tempFile, Path.of(path)) + + // Call fsync on the parent path to assure the fusemount is up-to-date. + // The equivalent of Python's os.fsync + FileChannel.open(Path.of(path)).force(true) + path }