final Path path = Paths.get(_path);
if (exists(path)) {
if (!duplicateFlag.equals(DuplicateType.UPDATE)) {
throw new CloudsyncException("Item '" + item.getPath() + "' already exists. Try to specify another '--duplicate' behavior.");
}
if ((!item.isType(ItemType.FOLDER) || !isDir(path))) {
try {
Files.delete(path);
} catch (final IOException e) {
throw new CloudsyncException("Can't clear " + item.getTypeName() + " on '" + item.getPath() + "'", e);
}
}
}
if (item.isType(ItemType.FOLDER)) {
if (!exists(path)) {
try {
Files.createDirectory(path);
} catch (final IOException e) {
throw new CloudsyncException("Can't create " + item.getTypeName() + " '" + item.getPath() + "'", e);
}
}
} else {
if (item.getParent() != null) {
final Path parentPath = Paths.get(localPath + Item.SEPARATOR + item.getParent().getPath());
if (!isDir(parentPath)) {
throw new CloudsyncException("Parent directory of " + item.getTypeName() + " '" + item.getPath() + "' is missing.");
}
}
if (item.isType(ItemType.LINK)) {
try {
final byte[] data = handler.getRemoteDecryptedBinary(item);
// if (!createChecksum(data).equals(item.getChecksum())) {
// throw new
// CloudsyncException("restored filechecksum differs from the original filechecksum");
// }
final String link = new String(data);
Files.createSymbolicLink(path, Paths.get(link));
} catch (final IOException e) {
throw new CloudsyncException("Unexpected error during local update of " + item.getTypeName() + " '" + item.getPath() + "'", e);
}
} else if (item.isType(ItemType.FILE)) {
try {
final byte[] data = handler.getRemoteDecryptedBinary(item);
if (!createChecksum(data).equals(item.getChecksum())) {
throw new CloudsyncException("restored filechecksum differs from the original filechecksum");
}
if (item.getFilesize() != data.length) {
throw new CloudsyncException("restored filesize differs from the original filesize");
}
final FileOutputStream fos = new FileOutputStream(path.toFile());
try {
fos.write(data);
} finally {
fos.close();
}
} catch (final IOException e) {
throw new CloudsyncException("Unexpected error during local update of " + item.getTypeName() + " '" + item.getPath() + "'", e);
}
} else {
throw new CloudsyncException("Unsupported type " + item.getTypeName() + "' on '" + item.getPath() + "'");
}
}
try {
if (item.isType(ItemType.LINK)) {
// Files.setLastModifiedTime(path, item.getModifyTime());
} else {
Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setTimes(item.getModifyTime(), item.getAccessTime(), item.getCreationTime());
}
} catch (final IOException e) {
throw new CloudsyncException("Can't set create, modify and access time of " + item.getTypeName() + " '" + item.getPath() + "'", e);
}
if (permissionType.equals(PermissionType.SET) || permissionType.equals(PermissionType.TRY)) {
final UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
Map<String, String[]> attributes = item.getAttributes();
for (String type : attributes.keySet()) {
GroupPrincipal group;
UserPrincipal principal;
try {
String[] values = attributes.get(type);
switch (type) {
case Item.ATTRIBUTE_POSIX:
PosixFileAttributeView posixView = Files.getFileAttributeView(path, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
if (posixView != null) {
group = lookupService.lookupPrincipalByGroupName(values[0]);
posixView.setGroup(group);
principal = lookupService.lookupPrincipalByName(values[1]);
posixView.setOwner(principal);
if (values.length > 2)
posixView.setPermissions(toPermissions(Integer.parseInt(values[2])));
} else {
String msg = "Can't restore 'posix' permissions on '" + item.getPath() + "'. They are not supported.";
if (permissionType.equals(PermissionType.TRY))
LOGGER.log(Level.WARNING, msg);
else
throw new CloudsyncException(msg + "\n try to run with '--permissions try'");
}
break;
case Item.ATTRIBUTE_DOS:
DosFileAttributeView dosView = Files.getFileAttributeView(path, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
if (dosView != null) {
dosView.setArchive(Boolean.parseBoolean(values[0]));
dosView.setHidden(Boolean.parseBoolean(values[1]));
dosView.setReadOnly(Boolean.parseBoolean(values[2]));
dosView.setSystem(Boolean.parseBoolean(values[3]));
} else {
String msg = "Can't restore 'dos' permissions on '" + item.getPath() + "'. They are not supported.";
if (permissionType.equals(PermissionType.TRY))
LOGGER.log(Level.WARNING, msg);
else
throw new CloudsyncException(msg + "\n try to run with '--permissions try'");
}
break;
case Item.ATTRIBUTE_ACL:
AclFileAttributeView aclView = Files.getFileAttributeView(path, AclFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
if (aclView != null) {
List<AclEntry> acls = aclView.getAcl();
for (int i = 0; i < values.length; i = i + 4) {
Builder aclEntryBuilder = AclEntry.newBuilder();
aclEntryBuilder.setType(AclEntryType.valueOf(values[i]));
aclEntryBuilder.setPrincipal(lookupService.lookupPrincipalByName(values[i + 1]));
Set<AclEntryFlag> flags = new HashSet<AclEntryFlag>();
for (String flag : StringUtils.splitPreserveAllTokens(values[i + 2], ",")) {
flags.add(AclEntryFlag.valueOf(flag));
}
if (flags.size() > 0)
aclEntryBuilder.setFlags(flags);
Set<AclEntryPermission> aclPermissions = new HashSet<AclEntryPermission>();
for (String flag : StringUtils.splitPreserveAllTokens(values[i + 3], ",")) {
aclPermissions.add(AclEntryPermission.valueOf(flag));
}
if (aclPermissions.size() > 0)
aclEntryBuilder.setPermissions(aclPermissions);
acls.add(aclEntryBuilder.build());
}
aclView.setAcl(acls);
} else {
String msg = "Can't restore 'acl' permissions on '" + item.getPath() + "'. They are not supported.";
if (permissionType.equals(PermissionType.TRY))
LOGGER.log(Level.WARNING, msg);
else
throw new CloudsyncException(msg + "\n try to run with '--permissions try'");
}
break;
case Item.ATTRIBUTE_OWNER:
FileOwnerAttributeView ownerView = Files.getFileAttributeView(path, FileOwnerAttributeView.class, LinkOption.NOFOLLOW_LINKS);
if (ownerView != null) {
principal = lookupService.lookupPrincipalByName(values[0]);
ownerView.setOwner(principal);
} else {
String msg = "Can't restore 'owner' permissions on '" + item.getPath() + "'. They are not supported.";
if (permissionType.equals(PermissionType.TRY))
LOGGER.log(Level.WARNING, msg);
else
throw new CloudsyncException(msg + "\n try to run with '--permissions try'");
}
break;
}
} catch (final UserPrincipalNotFoundException e) {
if (!LocalFilesystemConnector.principal_state.containsKey(e.getName())) {
LocalFilesystemConnector.principal_state.put(e.getName(), true);
LOGGER.log(Level.WARNING, "principal with name '" + e.getName() + "' not exists");
}
String msg = "Principal '" + e.getName() + "' on '" + item.getPath() + "' not found.";
if (permissionType.equals(PermissionType.TRY))
LOGGER.log(Level.WARNING, msg);
else
throw new CloudsyncException(msg + "\n try to run with '--permissions try'");
} catch (final IOException e) {
String msg = "Can't set permissions of '" + item.getPath() + "'.";
if (permissionType.equals(PermissionType.TRY))
LOGGER.log(Level.WARNING, msg);
else
throw new CloudsyncException(msg + "\n try to run with '--permissions try'");
}
}
}
}