Skip to content

Commit bcc68f1

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 061608f commit bcc68f1

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 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.
@@ -48,11 +48,11 @@
4848
* This class provides common features like restart, force sync, append etc.
4949
* The location of the output file is defined by a {@link Resource} which must
5050
* represent a writable file.<br>
51-
*
51+
*
5252
* Uses buffered writer to improve performance.<br>
53-
*
53+
*
5454
* The implementation is <b>not</b> thread-safe.
55-
*
55+
*
5656
* @author Waseem Malik
5757
* @author Tomas Slanina
5858
* @author Robert Kasanicky
@@ -109,7 +109,7 @@ public abstract class AbstractFileItemWriter<T> extends AbstractItemStreamItemWr
109109
* be lost if the OS crashes in between a write and a cache flush. Setting
110110
* to true may result in slower performance for usage patterns involving many
111111
* frequent writes.
112-
*
112+
*
113113
* @param forceSync the flag value to set
114114
*/
115115
public void setForceSync(boolean forceSync) {
@@ -127,7 +127,7 @@ public void setLineSeparator(String lineSeparator) {
127127

128128
/**
129129
* Setter for resource. Represents a file that can be written.
130-
*
130+
*
131131
* @param resource the resource to be written to
132132
*/
133133
@Override
@@ -151,7 +151,7 @@ public void setEncoding(String newEncoding) {
151151
* except on restart. If set to false and {@link #setAppendAllowed(boolean)
152152
* appendAllowed} is also false then there will be an exception when the
153153
* stream is opened to prevent existing data being potentially corrupted.
154-
*
154+
*
155155
* @param shouldDeleteIfExists the flag value to set
156156
*/
157157
public void setShouldDeleteIfExists(boolean shouldDeleteIfExists) {
@@ -164,7 +164,7 @@ public void setShouldDeleteIfExists(boolean shouldDeleteIfExists) {
164164
* {@link #setShouldDeleteIfExists(boolean) shouldDeleteIfExists} is
165165
* automatically set to false, so that flag should not be set explicitly.
166166
* Defaults value is false.
167-
*
167+
*
168168
* @param append the flag value to set
169169
*/
170170
public void setAppendAllowed(boolean append) {
@@ -174,7 +174,7 @@ public void setAppendAllowed(boolean append) {
174174
/**
175175
* Flag to indicate that the target file should be deleted if no lines have
176176
* been written (other than header and footer) on close. Defaults to false.
177-
*
177+
*
178178
* @param shouldDeleteIfEmpty the flag value to set
179179
*/
180180
public void setShouldDeleteIfEmpty(boolean shouldDeleteIfEmpty) {
@@ -186,7 +186,7 @@ public void setShouldDeleteIfEmpty(boolean shouldDeleteIfEmpty) {
186186
* provided {@link ExecutionContext} during the {@link ItemStream} call to
187187
* update. Setting this to false means that it will always start at the
188188
* beginning on a restart.
189-
*
189+
*
190190
* @param saveState if true, state will be persisted
191191
*/
192192
public void setSaveState(boolean saveState) {
@@ -229,7 +229,7 @@ public void setTransactional(boolean transactional) {
229229
/**
230230
* Writes out a string followed by a "new line", where the format of the new
231231
* line separator is determined by the underlying operating system.
232-
*
232+
*
233233
* @param items list of items to be written to output stream
234234
* @throws Exception if an error occurs while writing items to the output stream
235235
*/
@@ -283,7 +283,9 @@ public void close() {
283283
state.close();
284284
if (state.linesWritten == 0 && shouldDeleteIfEmpty) {
285285
try {
286-
resource.getFile().delete();
286+
if (!resource.getFile().delete()) {
287+
throw new ItemStreamException("Failed to delete empty file on close");
288+
}
287289
}
288290
catch (IOException e) {
289291
throw new ItemStreamException("Failed to delete empty file on close", e);
@@ -297,7 +299,7 @@ public void close() {
297299
/**
298300
* Initialize the reader. This method may be called multiple times before
299301
* close is called.
300-
*
302+
*
301303
* @see ItemStream#open(ExecutionContext)
302304
*/
303305
@Override
@@ -535,7 +537,7 @@ public void write(String line) throws IOException {
535537

536538
/**
537539
* Truncate the output at the last known good point.
538-
*
540+
*
539541
* @throws IOException if unable to work with file
540542
*/
541543
public void truncate() throws IOException {

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.
@@ -761,7 +761,9 @@ public void close() {
761761
}
762762
if (currentRecordCount == 0 && shouldDeleteIfEmpty) {
763763
try {
764-
resource.getFile().delete();
764+
if (!resource.getFile().delete()) {
765+
throw new ItemStreamException("Failed to delete empty file on close");
766+
}
765767
}
766768
catch (IOException e) {
767769
throw new ItemStreamException("Failed to delete empty file on close", e);

0 commit comments

Comments
 (0)