airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From machris...@apache.org
Subject [airavata-django-portal-sdk] branch master updated: AIRAVATA-3389 Skip broken symlinks in user storage
Date Tue, 17 Nov 2020 20:18:12 GMT
This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal-sdk.git


The following commit(s) were added to refs/heads/master by this push:
     new 226d740  AIRAVATA-3389 Skip broken symlinks in user storage
226d740 is described below

commit 226d740e3d70369aba49f052b85bf5cb1110e2c9
Author: Marcus Christie <machrist@iu.edu>
AuthorDate: Tue Nov 17 15:17:42 2020 -0500

    AIRAVATA-3389 Skip broken symlinks in user storage
---
 airavata_django_portal_sdk/user_storage.py |  9 ++++++
 tests/test_user_storage.py                 | 51 ++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/airavata_django_portal_sdk/user_storage.py b/airavata_django_portal_sdk/user_storage.py
index d98c508..42f6b6b 100644
--- a/airavata_django_portal_sdk/user_storage.py
+++ b/airavata_django_portal_sdk/user_storage.py
@@ -366,6 +366,10 @@ def listdir(request, path):
         files_data = []
         for f in files:
             user_rel_path = os.path.join(path, f)
+            if not datastore.exists(request.user.username, user_rel_path):
+                logger.warning(f"listdir skipping {request.user.username}:{user_rel_path},
"
+                               "does not exist (broken symlink?)")
+                continue
             created_time = datastore.get_created_time(
                 request.user.username, user_rel_path
             )
@@ -430,6 +434,11 @@ def list_experiment_dir(request, experiment_id, path=None):
         files_data = []
         for f in files:
             user_rel_path = os.path.join(path, f)
+            if not datastore.exists(exp_owner, user_rel_path):
+                logger.warning(
+                    f"list_experiment_dir skipping {exp_owner}:{user_rel_path}, "
+                    "does not exist (broken symlink?)")
+                continue
             created_time = datastore.get_created_time(
                 exp_owner, user_rel_path
             )
diff --git a/tests/test_user_storage.py b/tests/test_user_storage.py
index 9f637d4..30aacf3 100644
--- a/tests/test_user_storage.py
+++ b/tests/test_user_storage.py
@@ -180,3 +180,54 @@ class CopyInputFileUploadTests(BaseTestCase):
                 os.path.dirname(replica_copy_filepath),
                 os.path.join(tmpdirname, self.user.username, "tmp"),
                 msg="Verify input file copied to user's tmp dir")
+
+
+class ListDirTests(BaseTestCase):
+    def test_listdir(self):
+        "Verify basic listdir functionality"
+        with tempfile.TemporaryDirectory() as tmpdirname, \
+                self.settings(GATEWAY_DATA_STORE_DIR=tmpdirname,
+                              GATEWAY_DATA_STORE_HOSTNAME="gateway.com"):
+            # create test file
+            test_file_path = os.path.join(
+                tmpdirname, self.user.username, "foo.ext")
+            os.makedirs(os.path.dirname(test_file_path))
+            with open(test_file_path, 'wb') as f:
+                f.write(b"123")
+            # create test directory
+            os.makedirs(os.path.join(tmpdirname, self.user.username, "testdir"))
+
+            dirs, files = user_storage.listdir(self.request, "")
+            self.assertEqual(len(dirs), 1)
+            self.assertEqual("testdir", dirs[0]["name"])
+            self.assertEqual(len(files), 1)
+            self.assertEqual("foo.ext", files[0]["name"])
+            
+    def test_listdir_broken_symlink(self):
+        "Test that broken symlinks are ignored"
+        with tempfile.TemporaryDirectory() as tmpdirname, \
+                self.settings(GATEWAY_DATA_STORE_DIR=tmpdirname,
+                              GATEWAY_DATA_STORE_HOSTNAME="gateway.com"):
+            # create test file
+            test_file_path = os.path.join(
+                tmpdirname, self.user.username, "foo.ext")
+            os.makedirs(os.path.dirname(test_file_path))
+            with open(test_file_path, 'wb') as f:
+                f.write(b"123")
+            # create test directory
+            os.makedirs(os.path.join(tmpdirname, self.user.username, "testdir"))
+            # create broken symlink
+            not_existent = os.path.join("path", "does", "not", "exist")
+            self.assertFalse(os.path.exists(not_existent))
+            os.symlink(not_existent, os.path.join(tmpdirname, self.user.username, "broken-symlink"))
+            self.assertIn("broken-symlink", os.listdir(os.path.join(tmpdirname, self.user.username)))
+            # os.path.exists returns False for broken symbolic link
+            self.assertFalse(os.path.exists(os.path.join(tmpdirname, self.user.username,
"broken-symlink")))
+
+            dirs, files = user_storage.listdir(self.request, "")
+            # verify test file and directory are returned, but not broken-symlink
+            self.assertEqual(len(dirs), 1)
+            self.assertEqual("testdir", dirs[0]["name"])
+            self.assertEqual(len(files), 1)
+            self.assertEqual("foo.ext", files[0]["name"])
+        pass


Mime
View raw message