From 0020bb2bc161b8aefc5462564c6ffcca720b4b53 Mon Sep 17 00:00:00 2001 From: Douglas Bagnall Date: Thu, 25 Nov 2021 09:26:54 +1300 Subject: [PATCH 1/2] samba-tool domain backup: cope better with dangling symlinks Our previous behaviour was to try to os.stat() the non-existent target. The new code greatly improves efficiency for this little task. BUG: https://bugzilla.samba.org/show_bug.cgi?id=14918 Signed-off-by: Douglas Bagnall --- python/samba/netcmd/domain_backup.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/python/samba/netcmd/domain_backup.py b/python/samba/netcmd/domain_backup.py index 81738196385..af8aa505a95 100644 --- a/python/samba/netcmd/domain_backup.py +++ b/python/samba/netcmd/domain_backup.py @@ -1109,6 +1109,7 @@ class cmd_domain_backup_offline(samba.netcmd.Command): # Recursively get all file paths in the backup directories all_files = [] + all_stats = set() for backup_dir in backup_dirs: for (working_dir, _, filenames) in os.walk(backup_dir): if working_dir.startswith(paths.sysvol): @@ -1126,7 +1127,13 @@ class cmd_domain_backup_offline(samba.netcmd.Command): # Ignore files that have already been added. This prevents # duplicates if one backup dir is a subdirectory of another, # or if backup dirs contain hardlinks. - if any(os.path.samefile(full_path, file) for file in all_files): + try: + s = os.stat(full_path) + except FileNotFoundError: + logger.info(f"{full_path} does not exist (dangling symlink?)") + continue + + if (s.st_ino, s.st_dev) in all_stats: continue # Assume existing backup files are from a previous backup. @@ -1140,6 +1147,7 @@ class cmd_domain_backup_offline(samba.netcmd.Command): continue all_files.append(full_path) + all_stats.add((s.st_ino, s.st_dev)) # We would prefer to open with FLG_RDONLY but then we can't # start a transaction which is the strong isolation we want -- 2.25.1 From c037d972b8709c3f22714eba6cb9a03f9129339b Mon Sep 17 00:00:00 2001 From: Douglas Bagnall Date: Wed, 1 Dec 2021 10:20:48 +1300 Subject: [PATCH 2/2] samba-tool domain backup: backup but do not follow symlinks BUG: https://bugzilla.samba.org/show_bug.cgi?id=14918 Signed-off-by: Douglas Bagnall --- python/samba/netcmd/domain_backup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/samba/netcmd/domain_backup.py b/python/samba/netcmd/domain_backup.py index af8aa505a95..6cb0e512595 100644 --- a/python/samba/netcmd/domain_backup.py +++ b/python/samba/netcmd/domain_backup.py @@ -1128,9 +1128,9 @@ class cmd_domain_backup_offline(samba.netcmd.Command): # duplicates if one backup dir is a subdirectory of another, # or if backup dirs contain hardlinks. try: - s = os.stat(full_path) + s = os.stat(full_path, follow_symlinks=False) except FileNotFoundError: - logger.info(f"{full_path} does not exist (dangling symlink?)") + logger.warning(f"{full_path} does not exist!") continue if (s.st_ino, s.st_dev) in all_stats: -- 2.25.1