rakeshadr commented on a change in pull request #1557:
URL: https://github.com/apache/ozone/pull/1557#discussion_r523079959



##########
File path: 
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OzoneFSUtils.java
##########
@@ -162,4 +162,46 @@ public static boolean isImmediateChild(String parentKey, 
String childKey) {
 
     return parentPath.equals(childParent);
   }
+
+  /**
+   * The function returns parent directory from the given absolute path. For
+   * example, the given key path '/a/b/c/d/e/file1' then it returns parent
+   * directory name as 'e'.
+   *
+   * @param keyName key name
+   */
+  public static String getParentDir(@Nonnull String keyName) {
+    java.nio.file.Path fileName = Paths.get(keyName).getParent();
+    if (fileName != null) {
+      return fileName.toString();
+    }
+    // failed to converts a path key
+    return keyName;

Review comment:
       This is done to avoid any NPE and I thought below logic will throw 
exception. Does that make sense to you?
   
     ```
     OzoneFileStatus toKeyParentDirStatus = getOMKeyInfoIfExists(metaMgr,
               volumeName, bucketName, toKeyParentDir, 0);
       // check if the immediate parent exists
       if (toKeyParentDirStatus == null || toKeyParentDirStatus.isFile()) {
         throw new OMException(String.format(
                 "Failed to rename %s to %s, %s is a file", fromKeyName, 
toKeyName,
                 toKeyParentDir), OMException.ResultCodes.KEY_RENAME_ERROR);
       }
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to