Skip to content

Commit 8c7fb5c

Browse files
marschallfmbenhassine
authored andcommitted
Check the return value of File.delete()
Check the return value of File.delete() and throw an exception if deleting a file fails. Issue #4203
1 parent 15f3637 commit 8c7fb5c

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -270,7 +270,9 @@ public void close() {
270270
state.close();
271271
if (state.linesWritten == 0 && shouldDeleteIfEmpty) {
272272
try {
273-
resource.getFile().delete();
273+
if (!resource.getFile().delete()) {
274+
throw new ItemStreamException("Failed to delete empty file on close");
275+
}
274276
}
275277
catch (IOException e) {
276278
throw new ItemStreamException("Failed to delete empty file on close", e);

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -735,7 +735,9 @@ public void close() {
735735
}
736736
if (currentRecordCount == 0 && shouldDeleteIfEmpty) {
737737
try {
738-
resource.getFile().delete();
738+
if (!resource.getFile().delete()) {
739+
throw new ItemStreamException("Failed to delete empty file on close");
740+
}
739741
}
740742
catch (IOException e) {
741743
throw new ItemStreamException("Failed to delete empty file on close", e);

0 commit comments

Comments
 (0)