Skip to content

Commit

Permalink
Merge pull request #2571 in ITERATE/cyberduck from bugfix/TRAC-11797 …
Browse files Browse the repository at this point in the history
…to master

* commit '8e03e9d8d90c86e1e797f615b0bf364fd7da69e5':
  Search by version depending on argument.
  Add tests.
  Add test.
  Fail fast when no specific version is found.
  Do not override attributes of parameter.
  Make aware of file versions.
  • Loading branch information
dkocher committed Sep 11, 2021
2 parents 7dc78df + 8e03e9d commit 1207926
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public CachingAttributesFinderFeature(final Cache<Path> cache, final AttributesF
public PathAttributes find(final Path file, final ListProgressListener listener) throws BackgroundException {
if(cache.isCached(file.getParent())) {
final AttributedList<Path> list = cache.get(file.getParent());
final Path found = list.find(new SimplePathPredicate(file));
final Path found = list.find(new DefaultPathPredicate(file));
if(null != found) {
return found.attributes();
}
Expand All @@ -44,10 +44,10 @@ public PathAttributes find(final Path file, final ListProgressListener listener)
if(cache != PathCache.empty()) {
final AttributedList<Path> list = cache.get(file.getParent());
if(list == AttributedList.<Path>emptyList()) {
cache.put(file.getParent(), new AttributedList<>(Collections.singletonList(file.withAttributes(attributes))));
cache.put(file.getParent(), new AttributedList<>(Collections.singletonList(new Path(file).withAttributes(attributes))));
}
else {
list.add(file.withAttributes(attributes));
list.add(new Path(file).withAttributes(attributes));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public boolean find(final Path file, final ListProgressListener listener) throws
if(!file.isRoot()) {
if(cache.isCached(file.getParent())) {
final AttributedList<Path> list = cache.get(file.getParent());
final Path found = list.find(new SimplePathPredicate(file));
final Path found = list.find(new DefaultPathPredicate(file));
return null != found;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,12 @@ public ListFilteringPredicate(final Session<?> session, final Path file) {

@Override
public boolean test(final Path f) {
if(StringUtils.isNotBlank(f.attributes().getVersionId())) {
if(StringUtils.isNotBlank(file.attributes().getVersionId())) {
// Search with specific version and region
if(new DefaultPathPredicate(file).test(f)) {
return true;
}
return new DefaultPathPredicate(file).test(f);
}
if(f.attributes().isDuplicate()) {
// Filter previous versions and delete markers
// Filter previous versions and delete markers when searching for no specific version
return false;
}
switch(session.getCaseSensitivity()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ public void findWithDefaultExist() throws Exception {
new DefaultAttributesFinderFeature(new NullSession(new Host(new TestProtocol())) {
@Override
public AttributedList<Path> list(final Path directory, final ListProgressListener listener) {
return new AttributedList<>(Collections.singletonList(file));
return new AttributedList<>(Collections.singletonList(new Path(directory, "f", EnumSet.of(Path.Type.file))));
}
}));
assertNotNull(feature.find(file, new DisabledListProgressListener()));
assertNotSame(file.attributes(), feature.find(file, new DisabledListProgressListener()));
assertEquals(file.attributes(), feature.find(file, new DisabledListProgressListener()));
assertEquals(1, cache.size());
assertTrue(cache.isCached(directory));
assertTrue(cache.get(directory).contains(file));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ch.cyberduck.core.shared;

/*
* Copyright (c) 2002-2021 iterate GmbH. All rights reserved.
* https://cyberduck.io/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

import ch.cyberduck.core.AlphanumericRandomStringService;
import ch.cyberduck.core.CachingAttributesFinderFeature;
import ch.cyberduck.core.DisabledLoginCallback;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.PathCache;
import ch.cyberduck.core.exception.NotfoundException;
import ch.cyberduck.core.features.AttributesFinder;
import ch.cyberduck.core.features.Delete;
import ch.cyberduck.core.s3.AbstractS3Test;
import ch.cyberduck.core.s3.S3DefaultDeleteFeature;
import ch.cyberduck.core.s3.S3TouchFeature;
import ch.cyberduck.core.transfer.TransferStatus;
import ch.cyberduck.test.IntegrationTest;

import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.util.Collections;
import java.util.EnumSet;

import static org.junit.Assert.*;

@Category(IntegrationTest.class)
public class CachingAttributesFinderFeatureTest extends AbstractS3Test {

@Test
public void testAttributes() throws Exception {
final PathCache cache = new PathCache(1);
final AttributesFinder f = new CachingAttributesFinderFeature(cache, new DefaultAttributesFinderFeature(session));
final String name = new AlphanumericRandomStringService().random();
final Path bucket = new Path("test-us-east-1-cyberduck", EnumSet.of(Path.Type.volume, Path.Type.directory));
final Path file = new S3TouchFeature(session).touch(new Path(bucket, name, EnumSet.of(Path.Type.file)), new TransferStatus());
assertNotSame(file.attributes(), f.find(file));
assertEquals(0L, f.find(file).getSize());
// Test cache
assertEquals(0L, f.find(file).getSize());
assertTrue(cache.containsKey(file.getParent()));
// Test wrong type
try {
f.find(new Path(bucket, name, EnumSet.of(Path.Type.directory)));
fail();
}
catch(NotfoundException e) {
// Expected
}
new S3DefaultDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
session.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package ch.cyberduck.core.shared;

/*
* Copyright (c) 2002-2021 iterate GmbH. All rights reserved.
* https://cyberduck.io/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

import ch.cyberduck.core.AlphanumericRandomStringService;
import ch.cyberduck.core.CachingAttributesFinderFeature;
import ch.cyberduck.core.DisabledConnectionCallback;
import ch.cyberduck.core.DisabledLoginCallback;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.PathAttributes;
import ch.cyberduck.core.PathCache;
import ch.cyberduck.core.exception.NotfoundException;
import ch.cyberduck.core.features.AttributesFinder;
import ch.cyberduck.core.features.Delete;
import ch.cyberduck.core.http.HttpResponseOutputStream;
import ch.cyberduck.core.io.SHA256ChecksumCompute;
import ch.cyberduck.core.s3.AbstractS3Test;
import ch.cyberduck.core.s3.S3AttributesFinderFeature;
import ch.cyberduck.core.s3.S3DefaultDeleteFeature;
import ch.cyberduck.core.s3.S3TouchFeature;
import ch.cyberduck.core.s3.S3WriteFeature;
import ch.cyberduck.core.transfer.TransferStatus;
import ch.cyberduck.test.IntegrationTest;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomUtils;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.model.StorageObject;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.io.ByteArrayInputStream;
import java.util.Collections;
import java.util.EnumSet;
import java.util.UUID;

import static org.junit.Assert.*;

@Category(IntegrationTest.class)
public class DefaultAttributesFinderFeatureTest extends AbstractS3Test {

@Test(expected = NotfoundException.class)
public void testNotFound() throws Exception {
new DefaultAttributesFinderFeature(session).find(new Path(new DefaultHomeFinderService(session).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file)));
}

@Test
public void testAttributes() throws Exception {
final PathCache cache = new PathCache(10);
final AttributesFinder f = new CachingAttributesFinderFeature(cache, new DefaultAttributesFinderFeature(session));
final String name = new AlphanumericRandomStringService().random();
final Path bucket = new Path("versioning-test-us-east-1-cyberduck", EnumSet.of(Path.Type.volume, Path.Type.directory));
final Path file = new S3TouchFeature(session).touch(new Path(bucket, name, EnumSet.of(Path.Type.file)), new TransferStatus());
final String initialVersion = file.attributes().getVersionId();
assertNotNull(initialVersion);
assertNotSame(file.attributes(), f.find(file));
assertEquals(0L, f.find(file).getSize());
// Test cache
assertEquals(0L, f.find(file).getSize());
// Test wrong type
try {
f.find(new Path(bucket, name, EnumSet.of(Path.Type.directory)));
fail();
}
catch(NotfoundException e) {
// Expected
}
// Overwrite with new version
final TransferStatus status = new TransferStatus();
final byte[] content = RandomUtils.nextBytes(12);
status.setChecksum(new SHA256ChecksumCompute().compute(new ByteArrayInputStream(content), status));
status.setLength(content.length);
final HttpResponseOutputStream<StorageObject> out = new S3WriteFeature(session).write(file, status, new DisabledConnectionCallback());
IOUtils.copy(new ByteArrayInputStream(content), out);
out.close();
assertEquals(initialVersion, f.find(file.withAttributes(new PathAttributes(file.attributes()).withVersionId(initialVersion))).getVersionId());
final String newVersion = ((S3Object) out.getStatus()).getVersionId();
assertEquals(newVersion, f.find(file.withAttributes(new PathAttributes(file.attributes()).withVersionId(newVersion))).getVersionId());
assertNotEquals(initialVersion, f.find(file.withAttributes(new PathAttributes(file.attributes()).withVersionId(newVersion))).getVersionId());
assertEquals(new S3AttributesFinderFeature(session).toAttributes(out.getStatus()).getVersionId(), f.find(file).getVersionId());
new S3DefaultDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
session.close();
}
}

0 comments on commit 1207926

Please sign in to comment.