private List<OwnerAndPermission> replicateAncestorsOwnerAndPermission(FileSystem originFs, Path originPath, FileSystem targetFs, Path destinationPath) throws IOException { List<OwnerAndPermission> ancestorOwnerAndPermissions = Lists.newArrayList(); Path currentOriginPath = originPath.getParent(); Path currentTargetPath = destinationPath.getParent(); while (currentOriginPath != null && currentTargetPath != null && currentOriginPath.getName().equals(currentTargetPath.getName())) { Optional<FileStatus> targetFileStatus = this.configuration.getCopyContext().getFileStatus(targetFs, currentTargetPath); if (targetFileStatus.isPresent()) { return ancestorOwnerAndPermissions; } ancestorOwnerAndPermissions .add(resolveReplicatedOwnerAndPermission(originFs, currentOriginPath, this.configuration)); currentOriginPath = currentOriginPath.getParent(); currentTargetPath = currentTargetPath.getParent(); } return ancestorOwnerAndPermissions; }
/** * Compute the correct {@link OwnerAndPermission} obtained from replicating source owner and permissions and applying * the {@link PreserveAttributes} rules for fromPath and every ancestor up to but excluding toPath. * * @return A list of the computed {@link OwnerAndPermission}s starting from fromPath, up to but excluding toPath. * @throws IOException if toPath is not an ancestor of fromPath. */ public static List<OwnerAndPermission> resolveReplicatedOwnerAndPermissionsRecursively(FileSystem sourceFs, Path fromPath, Path toPath, CopyConfiguration copyConfiguration) throws IOException { if (!PathUtils.isAncestor(toPath, fromPath)) { throw new IOException(String.format("toPath %s must be an ancestor of fromPath %s.", toPath, fromPath)); } List<OwnerAndPermission> ownerAndPermissions = Lists.newArrayList(); Path currentPath = fromPath; while (PathUtils.isAncestor(toPath, currentPath.getParent())) { ownerAndPermissions.add(resolveReplicatedOwnerAndPermission(sourceFs, currentPath, copyConfiguration)); currentPath = currentPath.getParent(); } return ownerAndPermissions; }
properties.put(ConfigurationKeys.DATA_PUBLISHER_FINAL_DIR, "/final/dir"); OwnerAndPermission ownerAndPermission = CopyableFile.resolveReplicatedOwnerAndPermission(fs, path, new CopyConfiguration.CopyConfigurationBuilder(fs, properties).build()); Assert.assertEquals(ownerAndPermission.getOwner(), null); Assert.assertEquals(ownerAndPermission.getFsPermission(), null); ownerAndPermission = CopyableFile.resolveReplicatedOwnerAndPermission(fs, path, new CopyConfiguration.CopyConfigurationBuilder(fs, properties).targetGroup(Optional.of("target")).build()); Assert.assertEquals(ownerAndPermission.getOwner(), null); Assert.assertEquals(ownerAndPermission.getFsPermission(), null); ownerAndPermission = CopyableFile.resolveReplicatedOwnerAndPermission(fs, path, new CopyConfiguration.CopyConfigurationBuilder(fs, properties).targetGroup(Optional.of("target")). preserve(PreserveAttributes.fromMnemonicString("ug")).build()); Assert.assertEquals(ownerAndPermission.getFsPermission(), null); ownerAndPermission = CopyableFile.resolveReplicatedOwnerAndPermission(fs, path, new CopyConfiguration.CopyConfigurationBuilder(fs, properties).preserve(PreserveAttributes.fromMnemonicString("ug")).build()); Assert.assertEquals(ownerAndPermission.getOwner(), "owner"); ownerAndPermission = CopyableFile.resolveReplicatedOwnerAndPermission(fs, path, new CopyConfiguration.CopyConfigurationBuilder(fs, properties).preserve(PreserveAttributes.fromMnemonicString("ugp")).build()); Assert.assertEquals(ownerAndPermission.getOwner(), "owner");
private List<OwnerAndPermission> replicateAncestorsOwnerAndPermission(FileSystem originFs, Path originPath, FileSystem targetFs, Path destinationPath) throws IOException { List<OwnerAndPermission> ancestorOwnerAndPermissions = Lists.newArrayList(); Path currentOriginPath = originPath.getParent(); Path currentTargetPath = destinationPath.getParent(); while (currentOriginPath != null && currentTargetPath != null && currentOriginPath.getName().equals(currentTargetPath.getName())) { Optional<FileStatus> targetFileStatus = this.configuration.getCopyContext().getFileStatus(targetFs, currentTargetPath); if (targetFileStatus.isPresent()) { return ancestorOwnerAndPermissions; } ancestorOwnerAndPermissions .add(resolveReplicatedOwnerAndPermission(originFs, currentOriginPath, this.configuration)); currentOriginPath = currentOriginPath.getParent(); currentTargetPath = currentTargetPath.getParent(); } return ancestorOwnerAndPermissions; }
/** * Compute the correct {@link OwnerAndPermission} obtained from replicating source owner and permissions and applying * the {@link PreserveAttributes} rules for fromPath and every ancestor up to but excluding toPath. * * @return A list of the computed {@link OwnerAndPermission}s starting from fromPath, up to but excluding toPath. * @throws IOException if toPath is not an ancestor of fromPath. */ public static List<OwnerAndPermission> resolveReplicatedOwnerAndPermissionsRecursively(FileSystem sourceFs, Path fromPath, Path toPath, CopyConfiguration copyConfiguration) throws IOException { if (!PathUtils.isAncestor(toPath, fromPath)) { throw new IOException(String.format("toPath %s must be an ancestor of fromPath %s.", toPath, fromPath)); } List<OwnerAndPermission> ownerAndPermissions = Lists.newArrayList(); Path currentPath = fromPath; while (PathUtils.isAncestor(toPath, currentPath.getParent())) { ownerAndPermissions.add(resolveReplicatedOwnerAndPermission(sourceFs, currentPath, copyConfiguration)); currentPath = currentPath.getParent(); } return ownerAndPermissions; }