HBASE-13250 chown of ExportSnapshot does not cover all path and files (He Liangliang)

This commit is contained in:
tedyu 2015-09-16 13:04:01 -07:00
parent bd26386dc7
commit 08eabb89f6
1 changed files with 36 additions and 1 deletions

View File

@ -271,7 +271,7 @@ public class ExportSnapshot extends Configured implements Tool {
context.getCounter(Counter.BYTES_EXPECTED).increment(inputStat.getLen()); context.getCounter(Counter.BYTES_EXPECTED).increment(inputStat.getLen());
// Ensure that the output folder is there and copy the file // Ensure that the output folder is there and copy the file
outputFs.mkdirs(outputPath.getParent()); createOutputPath(outputPath.getParent());
FSDataOutputStream out = outputFs.create(outputPath, true); FSDataOutputStream out = outputFs.create(outputPath, true);
try { try {
copyData(context, inputStat.getPath(), in, outputPath, out, inputStat.getLen()); copyData(context, inputStat.getPath(), in, outputPath, out, inputStat.getLen());
@ -288,6 +288,23 @@ public class ExportSnapshot extends Configured implements Tool {
} }
} }
/**
* Create the output folder and optionally set ownership.
*/
private void createOutputPath(final Path path) throws IOException {
if (filesUser == null && filesGroup == null) {
outputFs.mkdirs(path);
} else {
Path parent = path.getParent();
if (!outputFs.exists(parent) && !parent.isRoot()) {
createOutputPath(parent);
}
outputFs.mkdirs(path);
// override the owner when non-null user/group is specified
outputFs.setOwner(path, filesUser, filesGroup);
}
}
/** /**
* Try to Preserve the files attribute selected by the user copying them from the source file * Try to Preserve the files attribute selected by the user copying them from the source file
* This is only required when you are exporting as a different user than "hbase" or on a system * This is only required when you are exporting as a different user than "hbase" or on a system
@ -802,6 +819,21 @@ public class ExportSnapshot extends Configured implements Tool {
SnapshotReferenceUtil.verifySnapshot(conf, fs, snapshotDir, snapshotDesc); SnapshotReferenceUtil.verifySnapshot(conf, fs, snapshotDir, snapshotDesc);
} }
/**
* Set path ownership.
*/
private void setOwner(final FileSystem fs, final Path path, final String user,
final String group, final boolean recursive) throws IOException {
if (user != null || group != null) {
if (recursive && fs.isDirectory(path)) {
for (FileStatus child : fs.listStatus(path)) {
setOwner(fs, child.getPath(), user, group, recursive);
}
}
fs.setOwner(path, user, group);
}
}
/** /**
* Execute the export snapshot by copying the snapshot metadata, hfiles and wals. * Execute the export snapshot by copying the snapshot metadata, hfiles and wals.
* @return 0 on success, and != 0 upon failure. * @return 0 on success, and != 0 upon failure.
@ -925,6 +957,9 @@ public class ExportSnapshot extends Configured implements Tool {
try { try {
LOG.info("Copy Snapshot Manifest"); LOG.info("Copy Snapshot Manifest");
FileUtil.copy(inputFs, snapshotDir, outputFs, initialOutputSnapshotDir, false, false, conf); FileUtil.copy(inputFs, snapshotDir, outputFs, initialOutputSnapshotDir, false, false, conf);
if (filesUser != null || filesGroup != null) {
setOwner(outputFs, snapshotTmpDir, filesUser, filesGroup, true);
}
} catch (IOException e) { } catch (IOException e) {
throw new ExportSnapshotException("Failed to copy the snapshot directory: from=" + throw new ExportSnapshotException("Failed to copy the snapshot directory: from=" +
snapshotDir + " to=" + initialOutputSnapshotDir, e); snapshotDir + " to=" + initialOutputSnapshotDir, e);