Skip to content

Commit

Permalink
Fix error-prone warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
gaul committed Dec 6, 2024
1 parent 5a9e46c commit b523c44
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 58 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/gaul/s3proxy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public BlobMetadata blobMetadata(String container, String name) {
var metadata = ImmutableMap.<String, String>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;
Expand All @@ -102,19 +102,20 @@ public Blob getBlob(String containerName, String name,

var metadata = ImmutableMap.<String, String>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.<String, String>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,
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/org/gaul/s3proxy/azureblob/AzureBlobStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Integer, PartPadding> parts;
private final SortedMap<Integer, PartPadding> parts;

/* the buffer holding data that have been read in from the
underlying stream, but have not been processed by the cipher
Expand Down Expand Up @@ -77,7 +77,7 @@ public class DecryptionInputStream extends FilterInputStream {
* @throws IOException if cipher fails
*/
public DecryptionInputStream(InputStream is, SecretKey key,
TreeMap<Integer, PartPadding> parts, int skipParts,
SortedMap<Integer, PartPadding> parts, int skipParts,
long skipPartBytes) throws IOException {
super(is);
in = is;
Expand Down
18 changes: 5 additions & 13 deletions src/main/java/org/gaul/s3proxy/nio2blob/AbstractNio2BlobStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -116,18 +115,16 @@ public abstract class AbstractNio2BlobStore extends BaseBlobStore {
Hashing.md5().hashBytes(new byte[0]).asBytes();

private final Supplier<Set<? extends Location>> locations;
private final FileSystem fs;
private final Path root;

protected AbstractNio2BlobStore(BlobStoreContext context, BlobUtils blobUtils,
Supplier<Location> defaultLocation,
@Memoized Supplier<Set<? extends Location>> locations,
PayloadSlicer slicer,
@org.jclouds.location.Provider Supplier<Credentials> creds,
FileSystem fs, Path root) {
Path root) {
super(context, blobUtils, defaultLocation, locations, slicer);
this.locations = requireNonNull(locations, "locations");
this.fs = fs;
this.root = root;
}

Expand Down Expand Up @@ -230,15 +227,15 @@ private void listHelper(ImmutableSortedSet.Builder<StorageMetadata> 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);
}

// 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(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -838,12 +835,7 @@ public final String completeMultipartUpload(MultipartUpload mpu, List<MultipartP
md5Hasher.putBytes(BaseEncoding.base16().lowerCase().decode(eTag));
}
}
var mpuETag = new StringBuilder("\"")
.append(md5Hasher.hash())
.append("-")
.append(parts.size())
.append("\"")
.toString();
var mpuETag = "\"" + md5Hasher.hash() + "-" + parts.size() + "\"";
var blobBuilder = blobBuilder(mpu.blobName())
.userMetadata(mpu.blobMetadata().getUserMetadata())
.payload(new MultiBlobInputStream(this, metas.build()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.gaul.s3proxy.nio2blob;

import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.util.Set;

Expand All @@ -43,19 +42,8 @@ public final class FilesystemNio2BlobStore extends AbstractNio2BlobStore {
PayloadSlicer slicer,
@org.jclouds.location.Provider Supplier<Credentials> 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<Location> defaultLocation,
@Memoized Supplier<Set<? extends Location>> locations,
PayloadSlicer slicer,
@org.jclouds.location.Provider Supplier<Credentials> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ private TransientNio2BlobStore(BlobStoreContext context, BlobUtils blobUtils,
PayloadSlicer slicer,
@org.jclouds.location.Provider Supplier<Credentials> creds,
FileSystem fs) {
// TODO: close fs?
super(context, blobUtils, defaultLocation, locations, slicer, creds,
fs, fs.getPath(""));
fs.getPath(""));
}
}
5 changes: 0 additions & 5 deletions src/test/java/org/gaul/s3proxy/TierBlobStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b523c44

Please sign in to comment.