From 8c7fb5ca02a1c46d27b27b09a0d724b30c25c7a7 Mon Sep 17 00:00:00 2001 From: Philippe Marschall Date: Mon, 3 Oct 2022 22:38:32 +0200 Subject: [PATCH] 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 --- .../batch/item/support/AbstractFileItemWriter.java | 6 ++++-- .../springframework/batch/item/xml/StaxEventItemWriter.java | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java index cf04d21eb..22ddbdd29 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -270,7 +270,9 @@ public abstract class AbstractFileItemWriter extends AbstractItemStreamItemWr state.close(); if (state.linesWritten == 0 && shouldDeleteIfEmpty) { try { - resource.getFile().delete(); + if (!resource.getFile().delete()) { + throw new ItemStreamException("Failed to delete empty file on close"); + } } catch (IOException e) { throw new ItemStreamException("Failed to delete empty file on close", e); diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java index 8af557693..fd7283f1f 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -735,7 +735,9 @@ public class StaxEventItemWriter extends AbstractItemStreamItemWriter } if (currentRecordCount == 0 && shouldDeleteIfEmpty) { try { - resource.getFile().delete(); + if (!resource.getFile().delete()) { + throw new ItemStreamException("Failed to delete empty file on close"); + } } catch (IOException e) { throw new ItemStreamException("Failed to delete empty file on close", e);