From 08eabb89f60b821362efaba2701ddb9db5ff8b32 Mon Sep 17 00:00:00 2001 From: tedyu Date: Wed, 16 Sep 2015 13:04:01 -0700 Subject: [PATCH] HBASE-13250 chown of ExportSnapshot does not cover all path and files (He Liangliang) --- .../hadoop/hbase/snapshot/ExportSnapshot.java | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/ExportSnapshot.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/ExportSnapshot.java index 132e187a5b62..5595fc992073 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/ExportSnapshot.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/ExportSnapshot.java @@ -271,7 +271,7 @@ private void copyFile(final Context context, final SnapshotFileInfo inputInfo, context.getCounter(Counter.BYTES_EXPECTED).increment(inputStat.getLen()); // Ensure that the output folder is there and copy the file - outputFs.mkdirs(outputPath.getParent()); + createOutputPath(outputPath.getParent()); FSDataOutputStream out = outputFs.create(outputPath, true); try { copyData(context, inputStat.getPath(), in, outputPath, out, inputStat.getLen()); @@ -288,6 +288,23 @@ private void copyFile(final Context context, final SnapshotFileInfo inputInfo, } } + /** + * 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 * This is only required when you are exporting as a different user than "hbase" or on a system @@ -802,6 +819,21 @@ private void verifySnapshot(final Configuration baseConf, 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. * @return 0 on success, and != 0 upon failure. @@ -925,6 +957,9 @@ public int run(String[] args) throws IOException { try { LOG.info("Copy Snapshot Manifest"); FileUtil.copy(inputFs, snapshotDir, outputFs, initialOutputSnapshotDir, false, false, conf); + if (filesUser != null || filesGroup != null) { + setOwner(outputFs, snapshotTmpDir, filesUser, filesGroup, true); + } } catch (IOException e) { throw new ExportSnapshotException("Failed to copy the snapshot directory: from=" + snapshotDir + " to=" + initialOutputSnapshotDir, e);