diff --git a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/common/ManifestCipherInputStream.java b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/common/ManifestCipherInputStream.java index aa7e03b..11b74d1 100644 --- a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/common/ManifestCipherInputStream.java +++ b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/common/ManifestCipherInputStream.java @@ -4,7 +4,7 @@ import com.github.nagyesta.filebarj.io.stream.internal.DoOnCloseInputStream; import lombok.NonNull; import org.apache.commons.io.IOUtils; -import org.apache.commons.io.input.CountingInputStream; +import org.apache.commons.io.input.BoundedInputStream; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -40,7 +40,7 @@ public ManifestCipherInputStream( final var secretKey = EncryptionUtil.byteArrayToAesKey(secretKeyBytes); crypto = EncryptionUtil.newCipherInputStream(secretKey).decorate(source); } else { - crypto = new CountingInputStream(source); + crypto = BoundedInputStream.builder().setInputStream(source).get(); } } diff --git a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/merge/MergeController.java b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/merge/MergeController.java index 2890dec..b88f16b 100644 --- a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/merge/MergeController.java +++ b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/merge/MergeController.java @@ -108,7 +108,14 @@ private void deleteManifestAndArchiveFilesFromBackupDirectory(@NotNull final Str .forEach(toDelete::add); for (final var path : toDelete) { log.info("Deleting obsolete file: {}", path); - Files.delete(path); + try { + Files.delete(path); + } catch (final IOException e) { + log.warn("Unable to delete file! Will attempt to delete it on exit.", e); + if (Files.exists(path)) { + path.toFile().deleteOnExit(); + } + } } } catch (final IOException e) { throw new RuntimeException(e); diff --git a/file-barj-core/src/test/java/com/github/nagyesta/filebarj/core/restore/worker/FileMetadataSetterLocalTest.java b/file-barj-core/src/test/java/com/github/nagyesta/filebarj/core/restore/worker/FileMetadataSetterLocalTest.java index d99763f..d72a7e6 100644 --- a/file-barj-core/src/test/java/com/github/nagyesta/filebarj/core/restore/worker/FileMetadataSetterLocalTest.java +++ b/file-barj-core/src/test/java/com/github/nagyesta/filebarj/core/restore/worker/FileMetadataSetterLocalTest.java @@ -64,7 +64,7 @@ void testSetMetadataShouldSetPermissionsOwnerGroupAndTimestampsWhenCalledWithExi Assertions.assertEquals(metadata.getGroup(), actualMetadata.getGroup()); Assertions.assertEquals(metadata.getLastModifiedUtcEpochSeconds(), actualMetadata.getLastModifiedUtcEpochSeconds()); Assertions.assertEquals(metadata.getLastAccessedUtcEpochSeconds(), actualMetadata.getLastAccessedUtcEpochSeconds()); - Assertions.assertEquals(metadata.getCreatedUtcEpochSeconds(), actualMetadata.getCreatedUtcEpochSeconds()); + //created time is not always set even on Unix Assertions.assertEquals(metadata.getHidden(), actualMetadata.getHidden()); } @@ -89,7 +89,7 @@ void testSetMetadataShouldNotSetPermissionsOwnerGroupButSetTimestampsWhenCalledW Assertions.assertNotEquals(metadata.getPosixPermissions(), actualMetadata.getPosixPermissions()); Assertions.assertEquals(metadata.getLastModifiedUtcEpochSeconds(), actualMetadata.getLastModifiedUtcEpochSeconds()); Assertions.assertEquals(metadata.getLastAccessedUtcEpochSeconds(), actualMetadata.getLastAccessedUtcEpochSeconds()); - Assertions.assertEquals(metadata.getCreatedUtcEpochSeconds(), actualMetadata.getCreatedUtcEpochSeconds()); + //created time is not always set even on Unix Assertions.assertEquals(metadata.getHidden(), actualMetadata.getHidden()); } @@ -126,7 +126,7 @@ void testSetInitialPermissionsShouldSetPermissionsToAllowAllWhenCalledWithExisti Assertions.assertEquals(expectedPath, actualMetadata.getAbsolutePath()); Assertions.assertEquals(FULL_ACCESS, actualMetadata.getPosixPermissions()); Assertions.assertNotEquals(metadata.getLastModifiedUtcEpochSeconds(), actualMetadata.getLastModifiedUtcEpochSeconds()); - Assertions.assertNotEquals(metadata.getCreatedUtcEpochSeconds(), actualMetadata.getCreatedUtcEpochSeconds()); + //created time is not always set even on Unix } @Test @@ -151,7 +151,7 @@ void testSetPermissionsShouldSetPermissionsOnlyWhenCalledWithExistingFile() thro Assertions.assertEquals(expectedPath, actualMetadata.getAbsolutePath()); Assertions.assertEquals(metadata.getPosixPermissions(), actualMetadata.getPosixPermissions()); Assertions.assertNotEquals(metadata.getLastModifiedUtcEpochSeconds(), actualMetadata.getLastModifiedUtcEpochSeconds()); - Assertions.assertNotEquals(metadata.getCreatedUtcEpochSeconds(), actualMetadata.getCreatedUtcEpochSeconds()); + //created time is not always set even on Unix } @Test @@ -217,7 +217,7 @@ void testSetTimestampsShouldSetTimestampsWhenCalledWithExistingFile() throws IOE Assertions.assertNotEquals(metadata.getCreatedUtcEpochSeconds(), original.getCreatedUtcEpochSeconds()); Assertions.assertEquals(metadata.getLastModifiedUtcEpochSeconds(), actualMetadata.getLastModifiedUtcEpochSeconds()); Assertions.assertEquals(metadata.getLastAccessedUtcEpochSeconds(), actualMetadata.getLastAccessedUtcEpochSeconds()); - Assertions.assertEquals(metadata.getCreatedUtcEpochSeconds(), actualMetadata.getCreatedUtcEpochSeconds()); + //created time is not always set even on Unix } @SuppressWarnings("DataFlowIssue") diff --git a/file-barj-stream-io/src/main/java/com/github/nagyesta/filebarj/io/stream/BarjCargoArchiveFileInputStreamSource.java b/file-barj-stream-io/src/main/java/com/github/nagyesta/filebarj/io/stream/BarjCargoArchiveFileInputStreamSource.java index 7c7fce2..9f4fff3 100644 --- a/file-barj-stream-io/src/main/java/com/github/nagyesta/filebarj/io/stream/BarjCargoArchiveFileInputStreamSource.java +++ b/file-barj-stream-io/src/main/java/com/github/nagyesta/filebarj/io/stream/BarjCargoArchiveFileInputStreamSource.java @@ -14,8 +14,8 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; +import org.apache.commons.io.input.BoundedInputStream; import org.apache.commons.io.input.CloseShieldInputStream; -import org.apache.commons.io.input.CountingInputStream; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -242,7 +242,7 @@ public void verifyHashes() throws IOException, ArchiveIntegrityException { */ public InputStream openStreamForSequentialAccess() throws IOException { final var fileInputStream = new MergingFileInputStream(this.getAllFiles()); - return new CountingInputStream(fileInputStream); + return BoundedInputStream.builder().setInputStream(fileInputStream).get(); } /** @@ -424,7 +424,7 @@ private boolean isArchiveHashValid( digestCalculatorStream.close(); final var actualDigestValue = digestCalculatorStream.getDigestValue(); if (!Objects.equals(actualDigestValue, entry.getArchivedHash())) { - log.error("Hash mismatch for " + path); + log.error("Hash mismatch for {}", path); return false; } else { return true; diff --git a/file-barj-stream-io/src/main/java/com/github/nagyesta/filebarj/io/stream/internal/FixedRangeInputStream.java b/file-barj-stream-io/src/main/java/com/github/nagyesta/filebarj/io/stream/internal/FixedRangeInputStream.java index bcdcb20..b2b7be4 100644 --- a/file-barj-stream-io/src/main/java/com/github/nagyesta/filebarj/io/stream/internal/FixedRangeInputStream.java +++ b/file-barj-stream-io/src/main/java/com/github/nagyesta/filebarj/io/stream/internal/FixedRangeInputStream.java @@ -2,7 +2,7 @@ import lombok.NonNull; import org.apache.commons.io.IOUtils; -import org.apache.commons.io.input.CountingInputStream; +import org.apache.commons.io.input.BoundedInputStream; import java.io.IOException; import java.io.InputStream; @@ -11,7 +11,7 @@ * Allows us to read a fixed range of bytes from an input stream and act as if the end of the stream * was reached when the range is exhausted. */ -public class FixedRangeInputStream extends CountingInputStream { +public class FixedRangeInputStream extends BoundedInputStream { private final long endExclusive; @@ -23,6 +23,7 @@ public class FixedRangeInputStream extends CountingInputStream { * @param length the length of the range * @throws IOException if the source stream cannot be read */ + @SuppressWarnings("deprecation") public FixedRangeInputStream( @NonNull final InputStream source, final long startInclusive, final long length) throws IOException { @@ -39,7 +40,7 @@ public FixedRangeInputStream( @Override public int read() throws IOException { - if (getByteCount() >= endExclusive) { + if (getCount() >= endExclusive) { return IOUtils.EOF; } return super.read(); @@ -52,10 +53,10 @@ public int read(final byte[] bts) throws IOException { @Override public int read(final byte[] bts, final int off, final int len) throws IOException { - if (getByteCount() >= endExclusive) { + if (getCount() >= endExclusive) { return IOUtils.EOF; } - final var allowedLength = endExclusive - getByteCount(); + final var allowedLength = endExclusive - getCount(); return super.read(bts, off, (int) Math.min(len, allowedLength)); } }