ashishkumar50 commented on code in PR #4675:
URL: https://github.com/apache/ozone/pull/4675#discussion_r1190960156


##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/keys/DeleteKeyHandler.java:
##########
@@ -45,6 +64,81 @@ protected void execute(OzoneClient client, OzoneAddress 
address)
 
     OzoneVolume vol = client.getObjectStore().getVolume(volumeName);
     OzoneBucket bucket = vol.getBucket(bucketName);
-    bucket.deleteKey(keyName);
+
+    float hadoopTrashInterval = getConf().getFloat(
+        FS_TRASH_INTERVAL_KEY, FS_TRASH_INTERVAL_DEFAULT);
+
+    long trashInterval =
+            (long) (getConf().getFloat(
+                    OMConfigKeys.OZONE_FS_TRASH_INTERVAL_KEY,
+                    hadoopTrashInterval) * 10000);
+
+    // If Bucket layout is FSO and Trash is enabled
+    // In this case during delete operation move key to trash
+    if (bucket.getBucketLayout().isFileSystemOptimized() && trashInterval > 0
+        && !skipTrash && !keyName.contains(".Trash")) {
+
+      keyName = OzoneFSUtils.removeTrailingSlashIfNeeded(keyName);
+      try {
+        // Check if key exists in Ozone
+        OzoneKeyDetails key = bucket.getKey(keyName);
+        if (key == null) {
+          out().printf("Key not found %s", keyName);
+          return;
+        }
+        // Check whether directory is empty or not
+        Iterator<? extends OzoneKey> ozoneKeyIterator =
+            bucket.listKeys(keyName, keyName);
+        int count = 0;
+        while (ozoneKeyIterator.hasNext()) {
+          ozoneKeyIterator.next();
+          if (++count > 1) {
+            // Assume FSO Tree: /a/b1/c1/k1.txt
+            // And we are trying to delete key /a/b1/c1
+            // In this case count is 2  which is greater than 1
+            // /a/b1/c1/ and /a/b1/c1/k1.txt
+            out().printf("Directory is not empty");
+            return;
+          }
+        }
+      } catch (Exception e) {
+        out().printf("Key not found %s", keyName);
+        return;
+      }
+
+      final String username =
+              UserGroupInformation.getCurrentUser().getShortUserName();
+      Path trashRoot = new Path(OZONE_URI_DELIMITER, TRASH_PREFIX);
+      Path userTrash = new Path(trashRoot, username);
+      Path userTrashCurrent = new Path(userTrash, CURRENT);
+
+      String trashDirectory = (keyName.contains("/")
+          ? new Path(userTrashCurrent, keyName.substring(0,
+          keyName.lastIndexOf("/")))
+          : userTrashCurrent).toUri().getPath();
+
+      String toKeyName = new Path(userTrashCurrent, keyName).toUri().getPath();
+      OzoneKeyDetails toKeyDetails = null;
+      try {
+        // check whether key already exist in trash
+        toKeyDetails = bucket.getKey(toKeyName);
+      } catch (IOException e) {
+        // Key doesn't exist inside trash.
+      }
+
+      if (toKeyDetails != null) {
+        // if key(directory) already exist in trash, just delete the key
+        bucket.deleteKey(keyName);
+        return;
+      }
+      // Create directory inside trash
+      bucket.createDirectory(trashDirectory);

Review Comment:
   If the directory already exists and we create directory again it doesn't 
throw error to client because currently it discards DIRECTORY_ALREADY_EXISTS 
error.
   Anyway I have changed now to check if directory exist or not before creating.



-- 
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.

To unsubscribe, e-mail: [email protected]

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