Skip to content

Commit 321c62a

Browse files
authored
Merge pull request #399 from sulabhjain1991/master
File Retention Properties
2 parents ade930d + 3f53d8c commit 321c62a

25 files changed

+2161
-2
lines changed

ds3-sdk/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,7 @@ dependencies {
8080
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.7.1'
8181
compile 'com.google.guava:guava:19.0'
8282
compile 'com.google.code.findbugs:annotations:3.0.1'
83+
compile group: 'net.java.dev.jna', name: 'jna-platform', version: '4.2.2'
84+
compile group: 'net.java.dev.jna', name: 'jna', version: '4.2.2'
8385
testCompile 'org.hamcrest:hamcrest-library:1.3'
8486
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.metadata;
17+
18+
import com.spectralogic.ds3client.metadata.interfaces.MetadataRestoreListener;
19+
import com.spectralogic.ds3client.networking.Metadata;
20+
21+
import java.text.SimpleDateFormat;
22+
import java.util.Calendar;
23+
24+
import static com.spectralogic.ds3client.utils.MetadataKeyConstants.*;
25+
26+
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);
30+
}
31+
32+
/**
33+
* Return date in specified format.
34+
*
35+
* @param milliSeconds Date in milliseconds
36+
* @param dateFormat Date format
37+
* @return String representing date in specified format
38+
*/
39+
public static String getDate(final long milliSeconds, final String dateFormat) {
40+
// Create a DateFormatter object for displaying date in specified format.
41+
final SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
42+
// Create a calendar object that will convert the date and time value in milliseconds to date.
43+
final Calendar calendar = Calendar.getInstance();
44+
calendar.setTimeInMillis(milliSeconds);
45+
return formatter.format(calendar.getTime());
46+
}
47+
48+
@Override
49+
public void restoreFileTimes() {
50+
String creationTime = null;
51+
if (metadata.get(KEY_CREATION_TIME).size() > 0) {
52+
creationTime = metadata.get(KEY_CREATION_TIME).get(0);
53+
}
54+
String accessTime = null;
55+
if (metadata.get(KEY_ACCESS_TIME).size() > 0) {
56+
accessTime = metadata.get(KEY_ACCESS_TIME).get(0);
57+
}
58+
String modifiedTime = null;
59+
if (metadata.get(KEY_LAST_MODIFIED_TIME).size() > 0) {
60+
modifiedTime = metadata.get(KEY_LAST_MODIFIED_TIME).get(0);
61+
}
62+
if (modifiedTime != null && creationTime != null && accessTime != null) {
63+
restoreCreationTimeMAC(objectName, creationTime);
64+
restoreModifiedTimeMAC(objectName, modifiedTime);
65+
}
66+
67+
}
68+
69+
/**
70+
* Restore creation time in mac only if target creation is before current creation time
71+
*
72+
* @param objectName path of the object where we need to restore
73+
* @param creationTime creation time got from server
74+
*/
75+
private void restoreCreationTimeMAC(final String objectName, final String creationTime) {
76+
try {
77+
final ProcessBuilder processBuilder = new ProcessBuilder("touch", "-t", getDate(Long.parseLong(creationTime), "YYYYMMddHHmm"), objectName);
78+
final Process process = processBuilder.start();
79+
//Wait to get exit value
80+
final int exitValue = process.waitFor();
81+
if(exitValue != 0) {
82+
LOG.error("Unable to restore creation time::"+exitValue);
83+
metadataRestoreListener.metadataRestoreFailed("Unable to restore creation time ::"+exitValue);
84+
}
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+
90+
}
91+
92+
/**
93+
* Restore modified time in case of MAC using touch command
94+
*
95+
* @param objectName path of the object where we need to restore
96+
* @param modifiedTime modified time need to restore
97+
*/
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+
}
112+
113+
}
114+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.metadata;
17+
18+
19+
import com.google.common.collect.ImmutableMap;
20+
import com.spectralogic.ds3client.helpers.Ds3ClientHelpers;
21+
import com.spectralogic.ds3client.metadata.interfaces.MetadataStore;
22+
import com.spectralogic.ds3client.metadata.interfaces.MetadataStoreListener;
23+
import com.spectralogic.ds3client.utils.MetaDataUtil;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
27+
import java.io.IOException;
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
30+
import java.nio.file.attribute.BasicFileAttributes;
31+
import java.nio.file.attribute.PosixFileAttributes;
32+
import java.util.Map;
33+
import java.util.Set;
34+
35+
/**
36+
* Implementation of MetaDataAcess Interface
37+
* Used to store meta data on Server
38+
*/
39+
public class MetadataAccessImpl implements Ds3ClientHelpers.MetadataAccess {
40+
static private final Logger LOG = LoggerFactory.getLogger(MetadataAccessImpl.class);
41+
private final Map<String, Path> fileMapper;
42+
private final MetadataStoreListener metadataStoreListener;
43+
44+
public MetadataAccessImpl(final Map<String, Path> fileMapper , final MetadataStoreListener metadataStoreListener) {
45+
this.fileMapper = fileMapper;
46+
this.metadataStoreListener = metadataStoreListener;
47+
}
48+
49+
@Override
50+
public Map<String, String> getMetadataValue(final String filename) {
51+
try {
52+
final Path file = fileMapper.get(filename);
53+
return storeMetaData(file).build();
54+
} catch (final Exception e) {
55+
LOG.error("failed to store Metadata", e);
56+
metadataStoreListener.onMetadataFailed("Unable to get MetaData"+e.getMessage());
57+
return null;
58+
}
59+
}
60+
61+
/**
62+
* @param file local path of file
63+
* @return map builder containing the data to be stored on server
64+
*/
65+
private ImmutableMap.Builder<String, String> storeMetaData(final Path file) {
66+
final ImmutableMap.Builder<String, String> metadata = new ImmutableMap.Builder<>();
67+
new ImmutableMap.Builder<String, String>();
68+
try {
69+
//get local os name
70+
final String localOSName = MetaDataUtil.getOS();
71+
final Set<String> setFileAttributes = MetaDataUtil.getSupportedFileAttributes(file);
72+
//get metadata store based on os type
73+
final MetadataStore metadataStore = new MetadataStoreFactory().getOsSpecificMetadataStore(localOSName, metadata,metadataStoreListener);
74+
metadataStore.saveOSMetaData(localOSName);
75+
76+
final BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
77+
final PosixFileAttributes attrPosix = Files.readAttributes(file, PosixFileAttributes.class);
78+
79+
metadataStore.saveCreationTimeMetaData(attr);
80+
metadataStore.saveAccessTimeMetaData(attr);
81+
metadataStore.saveLastModifiedTime(attr);
82+
83+
if(attrPosix != null)
84+
metadataStore.saveOSSpecificMetadata(file,attrPosix);
85+
else
86+
metadataStore.saveOSSpecificMetadata(file,attr);
87+
88+
} catch (final IOException ioe) {
89+
LOG.error("unable to get metadata", ioe);
90+
metadataStoreListener.onMetadataFailed("Unable to get MetaData"+ioe.getMessage());
91+
}
92+
return metadata;
93+
}
94+
95+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.metadata;
17+
18+
import com.spectralogic.ds3client.helpers.MetadataReceivedListener;
19+
import com.spectralogic.ds3client.metadata.interfaces.MetadataRestore;
20+
import com.spectralogic.ds3client.metadata.interfaces.MetadataRestoreListener;
21+
import com.spectralogic.ds3client.networking.Metadata;
22+
import com.spectralogic.ds3client.utils.MetaDataUtil;
23+
24+
public class MetadataReceivedListenerImpl implements MetadataReceivedListener {
25+
private String localFilePath = null;
26+
private final MetadataRestoreListener metadataRestoreListener;
27+
28+
public MetadataReceivedListenerImpl(final String localFilePath ,final MetadataRestoreListener metadataRestoreListener) {
29+
this.localFilePath = localFilePath;
30+
this.metadataRestoreListener = metadataRestoreListener;
31+
}
32+
33+
@Override
34+
public void metadataReceived(final String filename, final Metadata metadata) {
35+
final String actualFilePath = MetaDataUtil.getRealFilePath(localFilePath, filename);
36+
restoreMetaData(actualFilePath, metadata);
37+
}
38+
39+
/**
40+
* Restore the metadata to local file
41+
*
42+
* @param objectName name of the file to be restored
43+
* @param metadata metadata which needs to be set on local file
44+
*/
45+
private void restoreMetaData(final String objectName, final Metadata metadata) {
46+
final String osName = System.getProperty("os.name");
47+
//get metadatarestore on the basis of os
48+
final MetadataRestore metadataRestore = new MetadataRestoreFactory().getOSSpecificMetadataRestore(metadata, objectName, osName, metadataRestoreListener);
49+
//restore os name
50+
metadataRestore.restoreOSName();
51+
//restore user and owner based on OS
52+
metadataRestore.restoreUserAndOwner();
53+
//restore creation and modified time based on OS
54+
metadataRestore.restoreFileTimes();
55+
//restore permissions based on OS
56+
metadataRestore.restorePermissions();
57+
58+
59+
60+
}
61+
62+
63+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.metadata;
17+
18+
import com.spectralogic.ds3client.metadata.interfaces.MetadataRestore;
19+
import com.spectralogic.ds3client.metadata.interfaces.MetadataRestoreListener;
20+
import com.spectralogic.ds3client.networking.Metadata;
21+
22+
23+
public class MetadataRestoreFactory {
24+
public MetadataRestore getOSSpecificMetadataRestore(final Metadata metadata, final String filePath, final String localOS , final MetadataRestoreListener metadataRestoreListener) {
25+
if (localOS.contains("Windows")) {
26+
return new WindowsMetadataRestore(metadata, filePath, localOS, metadataRestoreListener);
27+
} else if (localOS.contains("Mac")) {
28+
return new MACMetadataRestore(metadata, filePath, localOS, metadataRestoreListener);
29+
} else {
30+
return new PosixMetadataRestore(metadata, filePath, localOS, metadataRestoreListener);
31+
}
32+
}
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.metadata;
17+
18+
import com.google.common.collect.ImmutableMap;
19+
import com.spectralogic.ds3client.metadata.interfaces.MetadataStore;
20+
import com.spectralogic.ds3client.metadata.interfaces.MetadataStoreListener;
21+
22+
23+
public class MetadataStoreFactory
24+
{
25+
public MetadataStore getOsSpecificMetadataStore(final String osName, final ImmutableMap.Builder<String, String> metadataMap,final MetadataStoreListener metadataStoreListener)
26+
{
27+
if(osName.contains("Windows")) {
28+
return new WindowsMetadataStore(metadataMap,metadataStoreListener);
29+
}
30+
else {
31+
return new PosixMetadataStore(metadataMap,metadataStoreListener);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)