From b523c448a1a39c2778b5c95d4209cf3c4587d031 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 5 Dec 2024 20:20:42 -0800 Subject: [PATCH] Fix error-prone warnings --- src/main/java/org/gaul/s3proxy/Main.java | 1 + .../UserMetadataReplacerBlobStore.java | 13 ++++++----- .../s3proxy/azureblob/AzureBlobStore.java | 22 +++++++++---------- .../s3proxy/crypto/DecryptionInputStream.java | 6 ++--- .../nio2blob/AbstractNio2BlobStore.java | 18 +++++---------- .../nio2blob/FilesystemNio2BlobStore.java | 16 ++------------ .../nio2blob/TransientNio2BlobStore.java | 3 ++- .../org/gaul/s3proxy/TierBlobStoreTest.java | 5 ----- .../UserMetadataReplacerBlobStoreTest.java | 5 ----- 9 files changed, 31 insertions(+), 58 deletions(-) diff --git a/src/main/java/org/gaul/s3proxy/Main.java b/src/main/java/org/gaul/s3proxy/Main.java index d0de4849..078d0461 100644 --- a/src/main/java/org/gaul/s3proxy/Main.java +++ b/src/main/java/org/gaul/s3proxy/Main.java @@ -74,6 +74,7 @@ private static final class Options { private boolean version; } + @SuppressWarnings("EqualsIncompatibleType") public static void main(String[] args) throws Exception { Console console = System.console(); if (console == null) { diff --git a/src/main/java/org/gaul/s3proxy/UserMetadataReplacerBlobStore.java b/src/main/java/org/gaul/s3proxy/UserMetadataReplacerBlobStore.java index 56ad737c..d092c3d2 100644 --- a/src/main/java/org/gaul/s3proxy/UserMetadataReplacerBlobStore.java +++ b/src/main/java/org/gaul/s3proxy/UserMetadataReplacerBlobStore.java @@ -80,8 +80,8 @@ public BlobMetadata blobMetadata(String container, String name) { var metadata = ImmutableMap.builder(); // TODO: duplication for (var entry : blobMetadata.getUserMetadata().entrySet()) { - metadata.put(replaceChars(entry.getKey(), toChars, fromChars), - replaceChars(entry.getValue(), toChars, fromChars)); + metadata.put(replaceChars(entry.getKey(), /*fromChars=*/ toChars, /*toChars=*/ fromChars), + replaceChars(entry.getValue(), /*fromChars=*/ toChars, /*toChars=*/ fromChars)); } ((MutableBlobMetadata) blobMetadata).setUserMetadata(metadata.build()); return blobMetadata; @@ -102,19 +102,20 @@ public Blob getBlob(String containerName, String name, var metadata = ImmutableMap.builder(); for (var entry : blob.getMetadata().getUserMetadata().entrySet()) { - metadata.put(replaceChars(entry.getKey(), toChars, fromChars), - replaceChars(entry.getValue(), toChars, fromChars)); + metadata.put(replaceChars(entry.getKey(), /*fromChars=*/ toChars, /*toChars=*/ fromChars), + replaceChars(entry.getValue(), /*fromChars=*/ toChars, /*toChars=*/ fromChars)); } blob.getMetadata().setUserMetadata(metadata.build()); return blob; } + @Override public MultipartUpload initiateMultipartUpload(String container, BlobMetadata blobMetadata, PutOptions overrides) { var metadata = ImmutableMap.builder(); for (var entry : blobMetadata.getUserMetadata().entrySet()) { - metadata.put(replaceChars(entry.getKey(), fromChars, toChars), - replaceChars(entry.getValue(), fromChars, toChars)); + metadata.put(replaceChars(entry.getKey(), /*fromChars=*/ fromChars, /*toChars=*/ toChars), + replaceChars(entry.getValue(), /*fromChars=*/ fromChars, /*toChars=*/ toChars)); } ((MutableBlobMetadata) blobMetadata).setUserMetadata(metadata.build()); return super.initiateMultipartUpload(container, blobMetadata, diff --git a/src/main/java/org/gaul/s3proxy/azureblob/AzureBlobStore.java b/src/main/java/org/gaul/s3proxy/azureblob/AzureBlobStore.java index b548dd3d..69d818b9 100644 --- a/src/main/java/org/gaul/s3proxy/azureblob/AzureBlobStore.java +++ b/src/main/java/org/gaul/s3proxy/azureblob/AzureBlobStore.java @@ -240,7 +240,7 @@ public void deleteContainer(String container) { try { blobServiceClient.deleteBlobContainer(container); } catch (BlobStorageException bse) { - if (bse.getErrorCode() != BlobErrorCode.CONTAINER_NOT_FOUND) { + if (!bse.getErrorCode().equals(BlobErrorCode.CONTAINER_NOT_FOUND)) { throw bse; } } @@ -259,7 +259,7 @@ public boolean deleteContainerIfEmpty(String container) { blobServiceClient.deleteBlobContainer(container); return true; } catch (BlobStorageException bse) { - if (bse.getErrorCode() == BlobErrorCode.CONTAINER_NOT_FOUND) { + if (bse.getErrorCode().equals(BlobErrorCode.CONTAINER_NOT_FOUND)) { return true; } throw bse; @@ -487,8 +487,8 @@ public void removeBlob(String container, String key) { try { client.delete(); } catch (BlobStorageException bse) { - if (bse.getErrorCode() != BlobErrorCode.BLOB_NOT_FOUND && - bse.getErrorCode() != BlobErrorCode.CONTAINER_NOT_FOUND) { + if (!bse.getErrorCode().equals(BlobErrorCode.BLOB_NOT_FOUND) && + !bse.getErrorCode().equals(BlobErrorCode.CONTAINER_NOT_FOUND)) { throw bse; } } @@ -524,8 +524,8 @@ protected boolean deleteAndVerifyContainerGone(String container) { public ContainerAccess getContainerAccess(String container) { var client = blobServiceClient.getBlobContainerClient(container); try { - return client.getAccessPolicy().getBlobAccessType() == - PublicAccessType.CONTAINER ? + return client.getAccessPolicy().getBlobAccessType().equals( + PublicAccessType.CONTAINER) ? ContainerAccess.PUBLIC_READ : ContainerAccess.PRIVATE; } catch (BlobStorageException bse) { @@ -728,15 +728,15 @@ private static String makeBlockId(int partNumber) { private void translateAndRethrowException(BlobStorageException bse, String container, @Nullable String key) { var code = bse.getErrorCode(); - if (code == BlobErrorCode.BLOB_NOT_FOUND) { + if (code.equals(BlobErrorCode.BLOB_NOT_FOUND)) { var exception = new KeyNotFoundException(container, key, ""); exception.initCause(bse); throw exception; - } else if (code == BlobErrorCode.CONTAINER_NOT_FOUND) { + } else if (code.equals(BlobErrorCode.CONTAINER_NOT_FOUND)) { var exception = new ContainerNotFoundException(container, ""); exception.initCause(bse); throw exception; - } else if (code == BlobErrorCode.CONDITION_NOT_MET) { + } else if (code.equals(BlobErrorCode.CONDITION_NOT_MET)) { var request = HttpRequest.builder() .method("GET") .endpoint(endpoint) @@ -746,7 +746,7 @@ private void translateAndRethrowException(BlobStorageException bse, .build(); throw new HttpResponseException( new HttpCommand(request), response, bse); - } else if (code == BlobErrorCode.INVALID_OPERATION) { + } else if (code.equals(BlobErrorCode.INVALID_OPERATION)) { var request = HttpRequest.builder() .method("GET") .endpoint(endpoint) @@ -756,7 +756,7 @@ private void translateAndRethrowException(BlobStorageException bse, .build(); throw new HttpResponseException( new HttpCommand(request), response, bse); - } else if (bse.getErrorCode() == BlobErrorCode.INVALID_RESOURCE_NAME) { + } else if (bse.getErrorCode().equals(BlobErrorCode.INVALID_RESOURCE_NAME)) { throw new IllegalArgumentException( "Invalid container name", bse); } diff --git a/src/main/java/org/gaul/s3proxy/crypto/DecryptionInputStream.java b/src/main/java/org/gaul/s3proxy/crypto/DecryptionInputStream.java index 376834bc..34ecd48c 100644 --- a/src/main/java/org/gaul/s3proxy/crypto/DecryptionInputStream.java +++ b/src/main/java/org/gaul/s3proxy/crypto/DecryptionInputStream.java @@ -19,7 +19,7 @@ import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; -import java.util.TreeMap; +import java.util.SortedMap; import javax.annotation.concurrent.ThreadSafe; import javax.crypto.Cipher; @@ -38,7 +38,7 @@ public class DecryptionInputStream extends FilterInputStream { private final SecretKey key; // the list of parts we expect in the stream - private final TreeMap parts; + private final SortedMap parts; /* the buffer holding data that have been read in from the underlying stream, but have not been processed by the cipher @@ -77,7 +77,7 @@ public class DecryptionInputStream extends FilterInputStream { * @throws IOException if cipher fails */ public DecryptionInputStream(InputStream is, SecretKey key, - TreeMap parts, int skipParts, + SortedMap parts, int skipParts, long skipPartBytes) throws IOException { super(is); in = is; diff --git a/src/main/java/org/gaul/s3proxy/nio2blob/AbstractNio2BlobStore.java b/src/main/java/org/gaul/s3proxy/nio2blob/AbstractNio2BlobStore.java index a5727016..0937b618 100644 --- a/src/main/java/org/gaul/s3proxy/nio2blob/AbstractNio2BlobStore.java +++ b/src/main/java/org/gaul/s3proxy/nio2blob/AbstractNio2BlobStore.java @@ -24,7 +24,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.DirectoryNotEmptyException; import java.nio.file.FileAlreadyExistsException; -import java.nio.file.FileSystem; import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; @@ -116,7 +115,6 @@ public abstract class AbstractNio2BlobStore extends BaseBlobStore { Hashing.md5().hashBytes(new byte[0]).asBytes(); private final Supplier> locations; - private final FileSystem fs; private final Path root; protected AbstractNio2BlobStore(BlobStoreContext context, BlobUtils blobUtils, @@ -124,10 +122,9 @@ protected AbstractNio2BlobStore(BlobStoreContext context, BlobUtils blobUtils, @Memoized Supplier> locations, PayloadSlicer slicer, @org.jclouds.location.Provider Supplier creds, - FileSystem fs, Path root) { + Path root) { super(context, blobUtils, defaultLocation, locations, slicer); this.locations = requireNonNull(locations, "locations"); - this.fs = fs; this.root = root; } @@ -230,7 +227,7 @@ private void listHelper(ImmutableSortedSet.Builder builder, for (var path : stream) { logger.debug("examining: {}", path); if (!path.toAbsolutePath().toString().startsWith(root + prefix)) { - continue; + // ignore } else if (Files.isDirectory(path)) { if (!"/".equals(delimiter)) { listHelper(builder, container, path, prefix, delimiter); @@ -238,7 +235,7 @@ private void listHelper(ImmutableSortedSet.Builder builder, // Add a prefix if the directory blob exists or if the delimiter causes us not to recuse. var view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class); - if (view != null && Set.copyOf(view.list()).contains(XATTR_CONTENT_MD5) || "/".equals(delimiter)) { + if ((view != null && Set.copyOf(view.list()).contains(XATTR_CONTENT_MD5)) || "/".equals(delimiter)) { var name = path.toString().substring((root.resolve(container) + "/").length()); logger.debug("adding prefix: {}", name); builder.add(new StorageMetadataImpl( @@ -419,7 +416,7 @@ public final Blob getBlob(String container, String key, GetOptions options) { } else if (range.endsWith("-")) { offset = Long.parseLong(range.substring(0, range.length() - 1)); } else if (range.contains("-")) { - String[] firstLast = range.split("\\-"); + String[] firstLast = range.split("\\-", 2); offset = Long.parseLong(firstLast[0]); last = Long.parseLong(firstLast[1]); } else { @@ -838,12 +835,7 @@ public final String completeMultipartUpload(MultipartUpload mpu, List creds, @Named(FilesystemConstants.PROPERTY_BASEDIR) String baseDir) { - this(context, blobUtils, defaultLocation, locations, slicer, creds, - baseDir, FileSystems.getDefault()); - } - - // Helper to create Path - private FilesystemNio2BlobStore(BlobStoreContext context, BlobUtils blobUtils, - Supplier defaultLocation, - @Memoized Supplier> locations, - PayloadSlicer slicer, - @org.jclouds.location.Provider Supplier creds, - @Named(FilesystemConstants.PROPERTY_BASEDIR) String baseDir, - FileSystem fs) { super(context, blobUtils, defaultLocation, locations, slicer, creds, - fs, fs.getPath(baseDir)); + // cannot be closed + FileSystems.getDefault().getPath(baseDir)); } } diff --git a/src/main/java/org/gaul/s3proxy/nio2blob/TransientNio2BlobStore.java b/src/main/java/org/gaul/s3proxy/nio2blob/TransientNio2BlobStore.java index 9800cf8f..3746505a 100644 --- a/src/main/java/org/gaul/s3proxy/nio2blob/TransientNio2BlobStore.java +++ b/src/main/java/org/gaul/s3proxy/nio2blob/TransientNio2BlobStore.java @@ -55,7 +55,8 @@ private TransientNio2BlobStore(BlobStoreContext context, BlobUtils blobUtils, PayloadSlicer slicer, @org.jclouds.location.Provider Supplier creds, FileSystem fs) { + // TODO: close fs? super(context, blobUtils, defaultLocation, locations, slicer, creds, - fs, fs.getPath("")); + fs.getPath("")); } } diff --git a/src/test/java/org/gaul/s3proxy/TierBlobStoreTest.java b/src/test/java/org/gaul/s3proxy/TierBlobStoreTest.java index a77fbb1b..f9c15bd7 100644 --- a/src/test/java/org/gaul/s3proxy/TierBlobStoreTest.java +++ b/src/test/java/org/gaul/s3proxy/TierBlobStoreTest.java @@ -31,14 +31,9 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; @SuppressWarnings("UnstableApiUsage") public final class TierBlobStoreTest { - private static final Logger logger = - LoggerFactory.getLogger(TierBlobStoreTest.class); - private BlobStoreContext context; private BlobStore blobStore; private String containerName; diff --git a/src/test/java/org/gaul/s3proxy/UserMetadataReplacerBlobStoreTest.java b/src/test/java/org/gaul/s3proxy/UserMetadataReplacerBlobStoreTest.java index 32cef42c..d11f5e92 100644 --- a/src/test/java/org/gaul/s3proxy/UserMetadataReplacerBlobStoreTest.java +++ b/src/test/java/org/gaul/s3proxy/UserMetadataReplacerBlobStoreTest.java @@ -29,14 +29,9 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; @SuppressWarnings("UnstableApiUsage") public final class UserMetadataReplacerBlobStoreTest { - private static final Logger logger = - LoggerFactory.getLogger(UserMetadataReplacerBlobStoreTest.class); - private BlobStoreContext context; private BlobStore blobStore; private String containerName;