Skip to content

Commit e6ce51f

Browse files
authored
Merge pull request #437 from rpmoore/3_2_12
Adding more logging for when file permissions cannot be restored, or …
2 parents 9408b66 + f24b696 commit e6ce51f

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
allprojects {
1717
group = 'com.spectralogic.ds3'
18-
version = '3.2.11'
18+
version = '3.2.12'
1919
}
2020

2121
subprojects {

ds3-metadata/src/main/java/com/spectralogic/ds3client/metadata/WindowsMetadataRestore.java

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
import com.google.common.collect.ImmutableList;
1919
import com.spectralogic.ds3client.metadata.jna.Advapi32;
2020
import com.spectralogic.ds3client.networking.Metadata;
21+
import com.spectralogic.ds3client.utils.Guard;
2122
import com.sun.jna.platform.win32.WinNT;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
2226
import java.io.File;
2327
import java.io.IOException;
2428
import java.nio.file.FileSystems;
@@ -36,6 +40,9 @@
3640
import static com.spectralogic.ds3client.metadata.MetadataKeyConstants.KEY_OWNER;
3741

3842
class WindowsMetadataRestore extends AbstractMetadataRestore {
43+
44+
private static final Logger LOG = LoggerFactory.getLogger(WindowsMetadataRestore.class);
45+
3946
WindowsMetadataRestore(final Metadata metadata, final String filePath, final String localOS) {
4047
this.metadata = metadata;
4148
this.objectName = filePath;
@@ -44,26 +51,31 @@ class WindowsMetadataRestore extends AbstractMetadataRestore {
4451

4552
@Override
4653
public void restoreUserAndOwner() throws IOException {
47-
if (storedOS.equals(localOS)) {
48-
String ownerSid = null;
49-
if (metadata.get(KEY_OWNER).size() > 0) {
50-
ownerSid = metadata.get(KEY_OWNER).get(0);
51-
}
52-
String groupSid = null;
53-
if (metadata.get(KEY_GROUP).size() > 0) {
54-
groupSid = metadata.get(KEY_GROUP).get(0);
55-
}
56-
if (ownerSid != null && groupSid != null && !ownerSid.equals("") && !groupSid.equals("")) {
57-
setOwnerIdandGroupId(ownerSid, groupSid);
54+
if (storedOS != null && storedOS.equals(localOS)) {
55+
56+
final String ownerSid = getMetadataProperty(metadata, KEY_OWNER);
57+
final String groupSid = getMetadataProperty(metadata, KEY_GROUP);
58+
59+
if (!Guard.isStringNullOrEmpty(ownerSid) && !Guard.isStringNullOrEmpty(groupSid)) {
60+
setOwnerIdAndGroupId(ownerSid, groupSid);
61+
} else {
62+
LOG.warn("Cannot determine owner or group settings for {}", this.objectName);
5863
}
64+
} else {
65+
LOG.warn("The OS settings for owner and group properties cannot be restored for {}", this.objectName);
5966
}
67+
}
6068

69+
private static String getMetadataProperty(final Metadata metadata, final String metadataName) {
70+
return metadata.get(metadataName).get(0);
6171
}
6272

6373
@Override
6474
public void restorePermissions() throws IOException, InterruptedException {
6575
if (storedOS != null && storedOS.equals(localOS)) {
6676
setPermissionsForWindows();
77+
} else {
78+
LOG.warn("The OS settings for the file permissions cannot be restored for {}", this.objectName);
6779
}
6880
restoreFlags();
6981
}
@@ -76,19 +88,26 @@ private void setPermissionsForWindows() throws IOException {
7688
final String userListDisplay;
7789
final String[] users;
7890
final String[] usersDisplay;
79-
if (metadata.get("ds3-userList").size() > 0) {
91+
if (Guard.isNotNullAndNotEmpty(metadata.get("ds3-userList"))) {
8092
userList = metadata.get("ds3-userList").get(0);
81-
if (metadata.get("ds3-userListDisplay").size() > 0) {
93+
if (Guard.isNotNullAndNotEmpty(metadata.get("ds3-userListDisplay"))) {
8294
userListDisplay = metadata.get("ds3-userListDisplay").get(0);
8395
users = userList.split("-");
8496
usersDisplay = userListDisplay.split("-");
8597
for (int i = 0; i < users.length; i++) {
86-
if (metadata.get("ds3-" + users[i]).size() > 0) {
87-
final String ownerPermission = metadata.get("ds3-" + users[i]).get(0);
98+
final String user = users[i];
99+
if (Guard.isNotNullAndNotEmpty(metadata.get("ds3-" + user))) {
100+
final String ownerPermission = metadata.get("ds3-" + user).get(0);
88101
restorePermissionByUser(ownerPermission, usersDisplay[i], aclEntryBuilder);
102+
} else {
103+
LOG.warn("Did not find any permissions for {} for file {}", user, this.objectName);
89104
}
90105
}
106+
} else {
107+
LOG.warn("There was not a 'userListDisplay' metadata entry for file {}, so we will not restore any permissions", this.objectName);
91108
}
109+
} else {
110+
LOG.warn("There was not a 'userList' metadata entry for file {}, so we will not restore any permissions", this.objectName);
92111
}
93112

94113
aclAttributeView.setAcl(aclEntryBuilder.build());
@@ -148,7 +167,7 @@ private void restorePermissionByUser(final String permission,
148167
* @param ownerSidId sid of the owner
149168
* @param groupSidId sid of the group
150169
*/
151-
private void setOwnerIdandGroupId(final String ownerSidId, final String groupSidId) throws IOException {
170+
private void setOwnerIdAndGroupId(final String ownerSidId, final String groupSidId) throws IOException {
152171
final int infoType = WinNT.OWNER_SECURITY_INFORMATION | WinNT.GROUP_SECURITY_INFORMATION;
153172
final WinNT.PSIDByReference referenceOwner = new WinNT.PSIDByReference();
154173
Advapi32.INSTANCE.ConvertStringSidToSid(ownerSidId, referenceOwner);
@@ -166,7 +185,11 @@ private void restoreFlags() throws IOException, InterruptedException {
166185
if (metadata.get(KEY_FLAGS).size() > 0) {
167186
final String flags = metadata.get(KEY_FLAGS).get(0);
168187
restoreFlagsWindows(flags);
188+
} else {
189+
LOG.warn("The file flag settings do not exist for file {} and will not be restored", this.objectName);
169190
}
191+
} else {
192+
LOG.warn("The OS settings for restoring the file flags cannot be done for {}", this.objectName);
170193
}
171194
}
172195

@@ -199,9 +222,12 @@ private void restoreFlagsWindows(final String flag) throws IOException, Interrup
199222
stringBuilder.append(" -I");
200223
stringBuilder.append(" -H");
201224
}
202-
stringBuilder.append(" " + "\"" + objectName + "\"");
225+
stringBuilder.append(" " + "\"").append(objectName).append("\"");
203226

204227
final Process p = Runtime.getRuntime().exec(stringBuilder.toString().split(" "));
205-
p.waitFor();
228+
final int returnCode = p.waitFor();
229+
if (returnCode != 0) {
230+
LOG.error("Restoring the flag settings for file {} was not successful", this.objectName);
231+
}
206232
}
207233
}

0 commit comments

Comments
 (0)