Skip to content

Fix GeoPackage writer silently appending data when target file is in use - #1511

Open
adamnadolny-wizipisi wants to merge 1 commit into
halestudio:masterfrom
adamnadolny-wizipisi:fix/geopackage-writer-overwrite-1201
Open

Fix GeoPackage writer silently appending data when target file is in use#1511
adamnadolny-wizipisi wants to merge 1 commit into
halestudio:masterfrom
adamnadolny-wizipisi:fix/geopackage-writer-overwrite-1201

Conversation

@adamnadolny-wizipisi

@adamnadolny-wizipisi adamnadolny-wizipisi commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #1201

When the Overwrite target file if it exists option is enabled but the target .gpkg file cannot be deleted (e.g. it is locked by QGIS or another process on Windows), File.delete() returns False silently. The writer then opens the existing file and appends data instead of overwriting it — resulting in duplicated records with no error reported to the user.

Root cause

In GeopackageInstanceWriter.java (around line 202), the return value of File.delete() was not checked:

java // before fix file.delete(); // silently returns false if file is locked

Fix

Check the return value and throw IOException if deletion fails:

java if (!file.delete()) { throw new IOException("Cannot overwrite existing GeoPackage file (file may be in use): " + file.getAbsolutePath()); }

The exception is caught by the surrounding ry/catch in execute(), which marks the report as failed and records the error — so the user sees a clear error message instead of silently corrupted output.

Test

Added estOverwriteFailsWhenFileInUse() to GeopackageInstanceWriterTest:

  • Writes a GeoPackage file
  • Simulates "file in use" by holding an open RandomAccessFile handle (mirrors Windows file locking behaviour, e.g. when QGIS has the file open)
  • Asserts that the writer reports failure and records an error

Submitted by: The Wroclaw Institute of Spatial Information and Artificial Intelligence
(WIZIPISI — Wrocławski Instytut Zastosowań Informacji Przestrzennej i Sztucznej Inteligencji)

@stempler stempler left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the contribution. Looks good to me so far, now waiting for the workflow runs.
Please make sure to modify the commit message to follow conventional commits (fix: commit)

@adamnadolny-wizipisi
adamnadolny-wizipisi force-pushed the fix/geopackage-writer-overwrite-1201 branch 2 times, most recently from a4a7437 to 02ef5cc Compare June 23, 2026 07:19
…use (halestudio#1201)

When the 'overwriteTargetFile' option is enabled and the target .gpkg file
cannot be deleted (e.g. it is locked by QGIS or another process on Windows),
File.delete() returns false without throwing an exception. The writer then
proceeds to open the existing file and appends data instead of overwriting it.

Fix: check the return value of File.delete() and throw IOException if deletion
fails, so the caller receives a proper error report instead of silently
corrupted output.

Adds regression test testOverwriteFailsWhenFileInUse() that simulates a locked
file by holding an open RandomAccessFile handle during the second write attempt.
The test is skipped on non-Windows platforms where open handles do not prevent
file deletion.

Fixes halestudio#1201

Signed-off-by: The Wroclaw Institute of Spatial Information and Artificial Intelligence (WIZIPISI - Wrocławski Instytut Zastosowań Informacji Przestrzennej i Sztucznej Inteligencji) <adam.nadolny@wizipisi.ai>
@adamnadolny-wizipisi
adamnadolny-wizipisi force-pushed the fix/geopackage-writer-overwrite-1201 branch from 02ef5cc to d8563ad Compare June 23, 2026 08:59
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had activity in the last 60 days. It will be closed in two weeks if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the stale label Aug 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: geopackage writer overwrite functionality is not working when file is in use

2 participants