spring-integration icon indicating copy to clipboard operation
spring-integration copied to clipboard

When deleteSourceFiles is true, the setPermissions method of FileWritingMessageHandler should be called.

Open coverseed opened this issue 1 year ago • 3 comments

Expected Behavior

chmod should need to work when the FileWritingMessageHandler's deleteSourceFiles property is true. Current Behavior

When the deleteSourceFiles property of FileWritingMessageHandler is true, chmod does not work. Context

My code is as follows:

@Bean
public IntegrationFlow pacToZipFlow(MessageChannel packToZipChannel,  FileSynchronizerProperties fileSynchronizerProperties) {
	return IntegrationFlow
			.from(packToZipChannel)
			.transformWith(transformer -> transformer.transformer(new ZipTransformer()))
			.handle(Files
					.outboundAdapter(new File(fileSynchronizerProperties.outputDirectory()))
					.fileNameGenerator(message -> System.currentTimeMillis() + FileConsts.ZIP_FILE_SUFFIX)
					.deleteSourceFiles(true)
					.chmod(0700)
			)
			.get();
}

When deleteSourceFiles is true, the setPermissions(resultFile) method is not called in the handleFileMessage method of FileWritingMessageHandler.

coverseed avatar Jul 01 '24 13:07 coverseed

The logic is like this:

		if (this.deleteSourceFiles && originalFile != null && !originalFile.delete()) {
			throw new IllegalStateException("Could not delete original file: " + originalFile);
		}

		setPermissions(resultFile);
	}

	/**
	 * Set permissions on newly written files.
	 * @param resultFile the file.
	 * @throws IOException any exception.
	 * @since 5.0
	 */
	protected void setPermissions(File resultFile) throws IOException {

That setPermissions is for a newly created file. It has nothing to do with original which you are going to delete.

As you see that setPermissions() is not called only in case of original file delete failure.

Do you mean the setPermissions() has to be unconditional? Or what is your request about, please?

artembilan avatar Jul 01 '24 14:07 artembilan

When I call the handleFileMessage() method, and !FileExistsMode.APPEND.equals(this.fileExistsMode) && this.deleteSourceFiles is true, the rename() method is called directly without calling the setPermissions() method. Can you provide support for this?

	private File handleFileMessage(File sourceFile, File tempFile, File resultFile, Message<?> requestMessage)
			throws IOException {

		if (!FileExistsMode.APPEND.equals(this.fileExistsMode) && this.deleteSourceFiles) {
			rename(sourceFile, resultFile);
			return resultFile;
		}
		else {
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
			return handleInputStreamMessage(bis, sourceFile, tempFile, resultFile, requestMessage);
		}
	}

coverseed avatar Jul 01 '24 15:07 coverseed

Yeah... That use-case is missed. Good catch!

I can fix it quickly, but wonder if you are willing to contribute: https://github.com/spring-projects/spring-integration/blob/main/CONTRIBUTING.adoc.

Looks like that setPermissions() is supposed to be called after rename().

artembilan avatar Jul 01 '24 15:07 artembilan