Skip to content

Commit b5af7fa

Browse files
authored
Merge pull request #426 from GraciesPadre/metadata_refactor
Metadata refactor
2 parents 08200d6 + 0471c01 commit b5af7fa

32 files changed

+805
-551
lines changed

ds3-sdk/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,5 @@ dependencies {
8383
compile group: 'net.java.dev.jna', name: 'jna-platform', version: '4.2.2'
8484
compile group: 'net.java.dev.jna', name: 'jna', version: '4.2.2'
8585
testCompile 'org.hamcrest:hamcrest-library:1.3'
86+
testCompile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
8687
}

ds3-sdk/src/main/java/com/spectralogic/ds3client/helpers/FileObjectPutter.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.spectralogic.ds3client.helpers;
1717

1818
import com.spectralogic.ds3client.helpers.Ds3ClientHelpers.ObjectChannelBuilder;
19+
import com.spectralogic.ds3client.helpers.strategy.StrategyUtils;
1920

2021
import java.io.IOException;
2122
import java.nio.channels.FileChannel;
@@ -44,21 +45,6 @@ public SeekableByteChannel buildChannel(final String key) throws IOException {
4445

4546
final Path path = this.root.resolve(key);
4647

47-
return FileChannel.open(resolveForSymbolic(path), StandardOpenOption.READ);
48-
}
49-
50-
private static Path resolveForSymbolic(final Path path) throws IOException {
51-
if (Files.isSymbolicLink(path)) {
52-
final Path simLink = Files.readSymbolicLink(path);
53-
if (!simLink.isAbsolute()) {
54-
// Resolve the path such that the path is relative to the symbolically
55-
// linked file's directory
56-
final Path symLinkParent = path.toAbsolutePath().getParent();
57-
return symLinkParent.resolve(simLink);
58-
}
59-
60-
return simLink;
61-
}
62-
return path;
48+
return FileChannel.open(StrategyUtils.resolveForSymbolic(path), StandardOpenOption.READ);
6349
}
6450
}

ds3-sdk/src/main/java/com/spectralogic/ds3client/helpers/strategy/StrategyUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
2626

27+
import java.io.IOException;
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
2730
import java.util.List;
2831
import java.util.UUID;
2932

@@ -85,4 +88,19 @@ private static ImmutableList<BulkObject> filterObjects(final List<BulkObject> li
8588
}
8689
return builder.build();
8790
}
91+
92+
public static Path resolveForSymbolic(final Path path) throws IOException {
93+
if (Files.isSymbolicLink(path)) {
94+
final Path simLink = Files.readSymbolicLink(path);
95+
if (!simLink.isAbsolute()) {
96+
// Resolve the path such that the path is relative to the symbolically
97+
// linked file's directory
98+
final Path symLinkParent = path.toAbsolutePath().getParent();
99+
return symLinkParent.resolve(simLink);
100+
}
101+
102+
return simLink;
103+
}
104+
return path;
105+
}
88106
}
Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,34 @@
1313
* ****************************************************************************
1414
*/
1515

16-
package com.spectralogic.ds3client.metadata.interfaces;
16+
package com.spectralogic.ds3client.metadata;
1717

18+
import com.spectralogic.ds3client.metadata.interfaces.MetadataRestore;
1819
import com.spectralogic.ds3client.networking.Metadata;
1920

21+
import java.io.IOException;
2022
import java.nio.file.Files;
2123
import java.nio.file.Paths;
2224
import java.nio.file.attribute.BasicFileAttributeView;
2325
import java.nio.file.attribute.FileTime;
26+
import java.util.concurrent.Callable;
2427
import java.util.concurrent.Executors;
2528
import java.util.concurrent.ScheduledExecutorService;
2629
import java.util.concurrent.TimeUnit;
2730

28-
import static com.spectralogic.ds3client.utils.MetadataKeyConstants.*;
29-
import static com.spectralogic.ds3client.utils.MetadataKeyConstants.KEY_LAST_MODIFIED_TIME;
30-
31-
32-
public abstract class AbstractMetadataRestore implements MetadataRestore {
31+
import static com.spectralogic.ds3client.metadata.MetadataKeyConstants.KEY_ACCESS_TIME;
32+
import static com.spectralogic.ds3client.metadata.MetadataKeyConstants.KEY_CREATION_TIME;
33+
import static com.spectralogic.ds3client.metadata.MetadataKeyConstants.KEY_LAST_MODIFIED_TIME;
34+
import static com.spectralogic.ds3client.metadata.MetadataKeyConstants.KEY_OS;
3335

36+
abstract class AbstractMetadataRestore implements MetadataRestore {
3437
protected String storedOS;
3538
protected Metadata metadata;
3639
protected String objectName;
3740
protected String localOS;
38-
protected MetadataRestoreListener metadataRestoreListener;
41+
3942
@Override
40-
public void restoreFileTimes() {
43+
public void restoreFileTimes() throws IOException, InterruptedException {
4144
String creationTime = null;
4245
if(metadata.get(KEY_CREATION_TIME).size()>0) {
4346
creationTime = metadata.get(KEY_CREATION_TIME).get(0);
@@ -52,16 +55,18 @@ public void restoreFileTimes() {
5255
if(metadata.get(KEY_LAST_MODIFIED_TIME).size()>0) {
5356
modifiedTime = metadata.get(KEY_LAST_MODIFIED_TIME).get(0);
5457
}
58+
5559
if (modifiedTime != null && creationTime != null && accessTime != null) {
5660
final String modifiedTimes = modifiedTime;
5761
final String creationTimes = creationTime;
5862
final String accessTimes = accessTime;
5963
final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
6064

61-
executorService.schedule(new Runnable() {
65+
executorService.schedule(new Callable<Void>() {
6266
@Override
63-
public void run() {
67+
public Void call() throws Exception {
6468
setFileTimes(objectName, creationTimes, modifiedTimes, accessTimes);
69+
return null;
6570
}
6671
}, 10, TimeUnit.SECONDS);
6772
}
@@ -81,19 +86,16 @@ public void restoreOSName() {
8186
* @param lastModifiedTime modified time got from server
8287
* @param lastAccessedTime last aceess time got from server
8388
*/
84-
private void setFileTimes(final String filePath,final String creationTime,final String lastModifiedTime ,final String lastAccessedTime) {
85-
try {
86-
final BasicFileAttributeView attributes = Files.getFileAttributeView(Paths.get(filePath), BasicFileAttributeView.class);
87-
final FileTime timeCreation = FileTime.fromMillis(Long.parseLong(creationTime));
88-
final FileTime timeModified = FileTime.fromMillis(Long.parseLong(lastModifiedTime));
89-
final FileTime timeAccessed = FileTime.fromMillis(Long.parseLong(lastAccessedTime));
90-
attributes.setTimes(timeModified, timeAccessed, timeCreation);
91-
}
92-
catch (final Exception e)
93-
{
94-
LOG.error("Unable to restore creation and modified time", e);
95-
metadataRestoreListener.metadataRestoreFailed("Unable to restore creation and modified time ::"+e.getMessage());
96-
}
97-
89+
private void setFileTimes(final String filePath,
90+
final String creationTime,
91+
final String lastModifiedTime,
92+
final String lastAccessedTime)
93+
throws IOException
94+
{
95+
final BasicFileAttributeView attributes = Files.getFileAttributeView(Paths.get(filePath), BasicFileAttributeView.class);
96+
final FileTime timeCreation = FileTime.fromMillis(Long.parseLong(creationTime));
97+
final FileTime timeModified = FileTime.fromMillis(Long.parseLong(lastModifiedTime));
98+
final FileTime timeAccessed = FileTime.fromMillis(Long.parseLong(lastAccessedTime));
99+
attributes.setTimes(timeModified, timeAccessed, timeCreation);
98100
}
99101
}
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,42 @@
1313
* ****************************************************************************
1414
*/
1515

16-
package com.spectralogic.ds3client.metadata.interfaces;
16+
package com.spectralogic.ds3client.metadata;
1717

1818
import com.google.common.collect.ImmutableMap;
19+
import com.spectralogic.ds3client.metadata.interfaces.MetadataStore;
1920

2021
import java.nio.file.attribute.BasicFileAttributes;
2122

22-
import static com.spectralogic.ds3client.utils.MetadataKeyConstants.*;
23-
24-
public abstract class AbstractMetadataStore implements MetadataStore {
25-
26-
27-
protected ImmutableMap.Builder<String, String> mMetadataMap;
28-
29-
protected MetadataStoreListener metadataStoreListener;
23+
import static com.spectralogic.ds3client.metadata.MetadataKeyConstants.KEY_ACCESS_TIME;
24+
import static com.spectralogic.ds3client.metadata.MetadataKeyConstants.KEY_CREATION_TIME;
25+
import static com.spectralogic.ds3client.metadata.MetadataKeyConstants.KEY_LAST_MODIFIED_TIME;
26+
import static com.spectralogic.ds3client.metadata.MetadataKeyConstants.KEY_OS;
27+
import static com.spectralogic.ds3client.metadata.MetadataKeyConstants.METADATA_PREFIX;
3028

29+
abstract class AbstractMetadataStore implements MetadataStore {
30+
protected ImmutableMap.Builder<String, String> metadataMap;
3131

3232
@Override
3333
public void saveCreationTimeMetaData(final BasicFileAttributes attr) {
34-
mMetadataMap.put(METADATA_PREFIX + KEY_CREATION_TIME, String.valueOf(attr.creationTime().toMillis()));
34+
metadataMap.put(METADATA_PREFIX + KEY_CREATION_TIME, String.valueOf(attr.creationTime().toMillis()));
3535
}
3636

3737
@Override
3838
public void saveAccessTimeMetaData(final BasicFileAttributes attr) {
39-
mMetadataMap.put(METADATA_PREFIX + KEY_ACCESS_TIME, String.valueOf(attr.lastAccessTime().toMillis()));
39+
metadataMap.put(METADATA_PREFIX + KEY_ACCESS_TIME, String.valueOf(attr.lastAccessTime().toMillis()));
4040

4141
}
4242

4343
@Override
4444
public void saveLastModifiedTime(final BasicFileAttributes attr) {
45-
mMetadataMap.put(METADATA_PREFIX + KEY_LAST_MODIFIED_TIME, String.valueOf(attr.lastModifiedTime().toMillis()));
45+
metadataMap.put(METADATA_PREFIX + KEY_LAST_MODIFIED_TIME, String.valueOf(attr.lastModifiedTime().toMillis()));
4646

4747
}
4848

49-
5049
@Override
5150
public String saveOSMetaData(final String osName) {
52-
mMetadataMap.put(METADATA_PREFIX + KEY_OS, osName);
51+
metadataMap.put(METADATA_PREFIX + KEY_OS, osName);
5352
return osName;
5453
}
5554
}

ds3-sdk/src/main/java/com/spectralogic/ds3client/metadata/MACMetadataRestore.java

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@
1515

1616
package com.spectralogic.ds3client.metadata;
1717

18-
import com.spectralogic.ds3client.metadata.interfaces.MetadataRestoreListener;
1918
import com.spectralogic.ds3client.networking.Metadata;
2019

20+
import java.io.IOException;
2121
import java.text.SimpleDateFormat;
2222
import java.util.Calendar;
2323

24-
import static com.spectralogic.ds3client.utils.MetadataKeyConstants.*;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
2526

27+
import static com.spectralogic.ds3client.metadata.MetadataKeyConstants.KEY_ACCESS_TIME;
28+
import static com.spectralogic.ds3client.metadata.MetadataKeyConstants.KEY_CREATION_TIME;
29+
import static com.spectralogic.ds3client.metadata.MetadataKeyConstants.KEY_LAST_MODIFIED_TIME;
2630

27-
public class MACMetadataRestore extends PosixMetadataRestore {
28-
public MACMetadataRestore(final Metadata metadata, final String filePath, final String localOS, final MetadataRestoreListener metadataRestoreListener) {
29-
super(metadata, filePath, localOS, metadataRestoreListener);
31+
class MACMetadataRestore extends PosixMetadataRestore {
32+
private static final Logger LOG = LoggerFactory.getLogger(MACMetadataRestore.class);
33+
34+
public MACMetadataRestore(final Metadata metadata, final String filePath, final String localOS) {
35+
super(metadata, filePath, localOS);
3036
}
3137

3238
/**
@@ -46,7 +52,7 @@ public static String getDate(final long milliSeconds, final String dateFormat) {
4652
}
4753

4854
@Override
49-
public void restoreFileTimes() {
55+
public void restoreFileTimes() throws IOException, InterruptedException {
5056
String creationTime = null;
5157
if (metadata.get(KEY_CREATION_TIME).size() > 0) {
5258
creationTime = metadata.get(KEY_CREATION_TIME).get(0);
@@ -63,7 +69,6 @@ public void restoreFileTimes() {
6369
restoreCreationTimeMAC(objectName, creationTime);
6470
restoreModifiedTimeMAC(objectName, modifiedTime);
6571
}
66-
6772
}
6873

6974
/**
@@ -72,21 +77,15 @@ public void restoreFileTimes() {
7277
* @param objectName path of the object where we need to restore
7378
* @param creationTime creation time got from server
7479
*/
75-
private void restoreCreationTimeMAC(final String objectName, final String creationTime) {
76-
try {
80+
private void restoreCreationTimeMAC(final String objectName, final String creationTime) throws IOException, InterruptedException {
7781
final ProcessBuilder processBuilder = new ProcessBuilder("touch", "-t", getDate(Long.parseLong(creationTime), "YYYYMMddHHmm"), objectName);
7882
final Process process = processBuilder.start();
7983
//Wait to get exit value
8084
final int exitValue = process.waitFor();
8185
if(exitValue != 0) {
82-
LOG.error("Unable to restore creation time::"+exitValue);
83-
metadataRestoreListener.metadataRestoreFailed("Unable to restore creation time ::"+exitValue);
86+
LOG.error("Unable to restore creation time:: "+exitValue);
87+
throw new IOException("Unable to restore creation time: " + exitValue);
8488
}
85-
} catch (final Exception e) {
86-
LOG.error("Unable to restore creation time", e);
87-
metadataRestoreListener.metadataRestoreFailed("Unable to restore creation time ::"+e.getMessage());
88-
}
89-
9089
}
9190

9291
/**
@@ -95,20 +94,15 @@ private void restoreCreationTimeMAC(final String objectName, final String creati
9594
* @param objectName path of the object where we need to restore
9695
* @param modifiedTime modified time need to restore
9796
*/
98-
private void restoreModifiedTimeMAC(final String objectName, final String modifiedTime) {
99-
try {
100-
final ProcessBuilder processBuilder = new ProcessBuilder("touch", "-mt", getDate(Long.parseLong(modifiedTime), "YYYYMMddHHmm"), objectName);
101-
final Process process = processBuilder.start();
102-
//Wait to get exit value
103-
final int exitValue = process.waitFor();
104-
if(exitValue != 0) {
105-
LOG.error("Unable to restore modified time::"+exitValue);
106-
metadataRestoreListener.metadataRestoreFailed("Unable to restore creation time ::"+exitValue);
107-
}
108-
} catch (final Exception e) {
109-
LOG.error("Unable to restore modified time", e);
110-
metadataRestoreListener.metadataRestoreFailed("Unable to restore modified time ::"+e.getMessage());
111-
}
97+
private void restoreModifiedTimeMAC(final String objectName, final String modifiedTime) throws IOException, InterruptedException {
98+
final ProcessBuilder processBuilder = new ProcessBuilder("touch", "-mt", getDate(Long.parseLong(modifiedTime), "YYYYMMddHHmm"), objectName);
99+
final Process process = processBuilder.start();
112100

101+
//Wait to get exit value
102+
final int exitValue = process.waitFor();
103+
if(exitValue != 0) {
104+
LOG.error("Unable to restore creation time:: "+exitValue);
105+
throw new IOException("Unable to restore creation time: " + exitValue);
106+
}
113107
}
114108
}

ds3-sdk/src/main/java/com/spectralogic/ds3client/utils/MetaDataUtil.java renamed to ds3-sdk/src/main/java/com/spectralogic/ds3client/metadata/MetaDataUtil.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@
1313
* ****************************************************************************
1414
*/
1515

16-
package com.spectralogic.ds3client.utils;
16+
package com.spectralogic.ds3client.metadata;
17+
18+
import com.spectralogic.ds3client.helpers.strategy.StrategyUtils;
1719

1820
import java.io.File;
21+
import java.io.IOException;
1922
import java.nio.file.FileSystem;
2023
import java.nio.file.Path;
24+
import java.nio.file.Paths;
2125
import java.util.Set;
2226

23-
public class MetaDataUtil {
27+
class MetaDataUtil {
2428

2529
public static Set<String> getSupportedFileAttributes(final Path file) {
2630
final FileSystem store = file.getFileSystem();
@@ -44,19 +48,10 @@ public static String getOS() {
4448
* @param filename : name of the file with logical folder
4549
* @return actualFilePath
4650
*/
47-
public static String getRealFilePath(final String localFilePath, final String filename) {
48-
final String filePath = localFilePath + "/" + filename;
49-
String fName = filePath;
50-
while (true) {
51-
final File file = new File(fName);
52-
if (file.exists()) {
53-
break;
54-
} else {
55-
final File parentFile = new File(file.getParent());
56-
fName = parentFile.getParent() + "/" + file.getName();
57-
}
58-
}
59-
return fName;
60-
}
51+
public static String getRealFilePath(final String localFilePath, final String filename) throws IOException {
52+
final Path directory = Paths.get(localFilePath);
53+
final Path resolvedPath = directory.resolve(filename);
6154

55+
return StrategyUtils.resolveForSymbolic(resolvedPath).toString();
56+
}
6257
}

0 commit comments

Comments
 (0)