Skip to content
Merged
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ project(':ds3-metadata') {
project(':ds3-sdk-integration') {
dependencies {
compile project(':ds3-sdk')
compile project(':ds3-metadata')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@


import com.google.common.collect.ImmutableMap;
import com.spectralogic.ds3client.helpers.FailureEventListener;
import com.spectralogic.ds3client.helpers.MetadataAccess;
import com.spectralogic.ds3client.helpers.events.FailureEvent;
import com.spectralogic.ds3client.metadata.interfaces.MetadataStore;
import com.spectralogic.ds3client.utils.Platform;

Expand All @@ -28,25 +30,47 @@
import java.nio.file.attribute.PosixFileAttributes;
import java.util.Map;

import com.spectralogic.ds3client.utils.StringExtensions;

/**
* Implementation of MetaDataAcess Interface
* Used to store meta data on Server
*/
public class MetadataAccessImpl implements MetadataAccess {
private final ImmutableMap<String, Path> fileMapper;
private final FailureEventListener failureEventListener;
private final String httpEndpoint;

public MetadataAccessImpl(final ImmutableMap<String, Path> fileMapper) {
this(fileMapper, null, null);
}

public MetadataAccessImpl(final ImmutableMap<String, Path> fileMapper,
final FailureEventListener failureEventListener,
final String httpEndpoint)
{
this.fileMapper = fileMapper;
this.failureEventListener = failureEventListener;
this.httpEndpoint = httpEndpoint;
}

@Override
public Map<String, String> getMetadataValue(final String filename) {
final Path file = fileMapper.get(filename);
try {
return storeMetaData(file);
} catch (final IOException e) {
throw new RuntimeException("Error recording metadata.", e);
} catch (final Throwable t) {
if (failureEventListener != null) {
failureEventListener.onFailure(FailureEvent.builder()
.withObjectNamed(filename)
.withCausalException(t)
.doingWhat(FailureEvent.FailureActivity.RecordingMetadata)
.usingSystemWithEndpoint(StringExtensions.getStringOrDefault(httpEndpoint, " "))
.build());
}
}

return ImmutableMap.of();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

import com.google.common.collect.ImmutableList;
import com.spectralogic.ds3client.exceptions.AggregateException;
import com.spectralogic.ds3client.helpers.FailureEventListener;
import com.spectralogic.ds3client.helpers.MetadataReceivedListener;
import com.spectralogic.ds3client.helpers.events.FailureEvent;
import com.spectralogic.ds3client.metadata.interfaces.MetadataRestore;
import com.spectralogic.ds3client.networking.Metadata;
import com.spectralogic.ds3client.utils.Guard;
import com.spectralogic.ds3client.utils.StringExtensions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -31,19 +34,36 @@ public class MetadataReceivedListenerImpl implements MetadataReceivedListener {
private final static Logger LOG = LoggerFactory.getLogger(MetadataReceivedListenerImpl.class);

private final String localFilePath;

private final FailureEventListener failureEventListener;
private final String httpEndpoint;

public MetadataReceivedListenerImpl(final String localFilePath) {
this(localFilePath, null, null);
}

public MetadataReceivedListenerImpl(final String localFilePath,
final FailureEventListener failureEventListener,
final String httpEndpoint)
{
this.localFilePath = localFilePath;
this.failureEventListener = failureEventListener;
this.httpEndpoint = httpEndpoint;
}

@Override
public void metadataReceived(final String filename, final Metadata metadata) {
try {
final String actualFilePath = MetaDataUtil.getRealFilePath(localFilePath, filename);
restoreMetaData(actualFilePath, metadata);
} catch (final IOException | InterruptedException e) {
throw new RuntimeException("Error restoring metadata.", e);
} catch (final Throwable t) {
if (failureEventListener != null) {
failureEventListener.onFailure(FailureEvent.builder()
.doingWhat(FailureEvent.FailureActivity.RestoringMetadata)
.withCausalException(t)
.withObjectNamed(filename)
.usingSystemWithEndpoint(StringExtensions.getStringOrDefault(httpEndpoint, " "))
.build());
}
}
}

Expand All @@ -66,23 +86,20 @@ private void restoreMetaData(final String objectName, final Metadata metadata) t
try {
metadataRestore.restoreUserAndOwner();
} catch (final Throwable t) {
LOG.error("Could not restore owner and owner information", t);
exceptionBuilder.add(t);
}

//restore creation and modified time based on OS
try {
metadataRestore.restoreFileTimes();
} catch (final Throwable t) {
LOG.error("Could not restore the file times", t);
exceptionBuilder.add(t);
}

//restore permissions based on OS
try {
metadataRestore.restorePermissions();
} catch (final Throwable t) {
LOG.error("Could not restore the file permissions", t);
exceptionBuilder.add(t);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* ******************************************************************************
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file.
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* ****************************************************************************
*/

package com.spectralogic.ds3client.metadata;

/**
* An exception specialized to let us filter on the specific circumstance
* when reading metadata from or writing metadata to a file on Windows
* fails.
*/
public class WindowsMetadataException extends RuntimeException {
public WindowsMetadataException() { }

public WindowsMetadataException(final String failureMessage) {
super(failureMessage);
}

public WindowsMetadataException(final String failureMessage, final Throwable cause) {
super(failureMessage, cause);
}

public WindowsMetadataException(final Throwable cause) {
super(cause);
}

protected WindowsMetadataException(final String failureMessage, final Throwable cause,
final boolean enableSuppression, final boolean writableStackTrace)
{
super(failureMessage, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,22 @@ class WindowsMetadataRestore extends AbstractMetadataRestore {

@Override
public void restoreUserAndOwner() throws IOException {
if (storedOS != null && storedOS.equals(localOS)) {
try {
if (storedOS != null && storedOS.equals(localOS)) {

final String ownerSid = getMetadataProperty(metadata, KEY_OWNER);
final String groupSid = getMetadataProperty(metadata, KEY_GROUP);
final String ownerSid = getMetadataProperty(metadata, KEY_OWNER);
final String groupSid = getMetadataProperty(metadata, KEY_GROUP);

if (!Guard.isStringNullOrEmpty(ownerSid) && !Guard.isStringNullOrEmpty(groupSid)) {
setOwnerIdAndGroupId(ownerSid, groupSid);
if (!Guard.isStringNullOrEmpty(ownerSid) && !Guard.isStringNullOrEmpty(groupSid)) {
setOwnerIdAndGroupId(ownerSid, groupSid);
} else {
LOG.warn("Cannot determine owner or group settings for {}", this.objectName);
}
} else {
LOG.warn("Cannot determine owner or group settings for {}", this.objectName);
LOG.warn("The OS settings for owner and group properties cannot be restored for {}", this.objectName);
}
} else {
LOG.warn("The OS settings for owner and group properties cannot be restored for {}", this.objectName);
} catch (final Throwable t) {
throw new WindowsMetadataException("Restoring user and owner.", t);
}
}

Expand All @@ -72,12 +76,16 @@ private static String getMetadataProperty(final Metadata metadata, final String

@Override
public void restorePermissions() throws IOException, InterruptedException {
if (storedOS != null && storedOS.equals(localOS)) {
setPermissionsForWindows();
} else {
LOG.warn("The OS settings for the file permissions cannot be restored for {}", this.objectName);
try {
if (storedOS != null && storedOS.equals(localOS)) {
setPermissionsForWindows();
} else {
LOG.warn("The OS settings for the file permissions cannot be restored for {}", this.objectName);
}
restoreFlags();
} catch (final Throwable t) {
throw new WindowsMetadataException("Restoring permissions.", t);
}
restoreFlags();
}

private void setPermissionsForWindows() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,20 @@ private void saveWindowsfilePermissions(final Path file) throws IOException {
final StringBuilder userDisplayList = new StringBuilder();
final Map<String, Set<Integer>> stringSetMap = new HashMap<>();
for (final AclEntry aclEntry : aclEntries) {
userDisplay = aclEntry.principal().getName().split("\\\\")[1];
/*
If a file has no Windoze dacl entries, as may happen on a network-mounted file system, there won't be a principal entry.
A principal is a combination of security provider, like NT AUTHORITY, and user name, e.g. NT AUTHORITY\Gracie.
This code is looking for the user name -- the second half of the principal. With no principal, there is no
second half of the principal.
*/
final String[] principalFields = aclEntry.principal().getName().split("\\\\");

if (principalFields.length < 2) {
continue;
}

userDisplay = principalFields[1];

Set<Integer> newSet = stringSetMap.get(userDisplay);
aclEntryPermissions = aclEntry.permissions();
if (newSet == null) {
Expand Down Expand Up @@ -211,8 +224,12 @@ private void saveWindowsfilePermissions(final Path file) throws IOException {

@Override
public void saveOSSpecificMetadata(final Path file, final BasicFileAttributes attrs) throws IOException {
saveWindowsfilePermissions(file);
saveWindowsDescriptors(file);
saveFlagMetaData(file);
try {
saveWindowsfilePermissions(file);
saveWindowsDescriptors(file);
saveFlagMetaData(file);
} catch (final Throwable t) {
throw new WindowsMetadataException("Saving OS-specific metadata.", t);
}
}
}
Loading