From c072d01f9d86553c2ad88e2f642fe4a3c2ca6586 Mon Sep 17 00:00:00 2001
From: Fabrice Le Goff <fabrice.le.goff@cern.ch>
Date: Wed, 12 Jul 2023 13:46:22 +0200
Subject: [PATCH] handle case where info file is corrupted

---
 Script/cs/StorageBackends/eosstorage.py |  2 --
 Script/cs/Threads/CopyThread.py         |  2 +-
 Script/cs/Threads/ManagerThread.py      | 22 +++++++++++++++-------
 3 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/Script/cs/StorageBackends/eosstorage.py b/Script/cs/StorageBackends/eosstorage.py
index 9574130..781358d 100644
--- a/Script/cs/StorageBackends/eosstorage.py
+++ b/Script/cs/StorageBackends/eosstorage.py
@@ -85,8 +85,6 @@ def remove(dstfile, stager, logger=None):
 
 
 def backgroundcopy(srcfile, dstfile, stager, logger=None):
-    del logger #unused args
-
     dstfile = 'root://%s/%s' % (stager, dstfile)
     cmd = ['xrdcp', '-f', '--nopbar', srcfile, dstfile]
 
diff --git a/Script/cs/Threads/CopyThread.py b/Script/cs/Threads/CopyThread.py
index 75959ae..4aea7c1 100644
--- a/Script/cs/Threads/CopyThread.py
+++ b/Script/cs/Threads/CopyThread.py
@@ -113,7 +113,7 @@ class CopyThread(threading.Thread):
                 fmd = self.copyqueue.get_nowait() # fmd: file meta data
             except queue.Empty:
                 return
-            self.logger.debug('got file from queue: %s', fmd.file_name)
+            self.logger.debug('got file from queue: %s', fmd)
 
             # Check if filesystem was locked while the file was in the copyqueue
             if dir_is_locked(os.path.dirname(fmd.file_name), self.conf.SrcDirs,
diff --git a/Script/cs/Threads/ManagerThread.py b/Script/cs/Threads/ManagerThread.py
index 82ae442..0d0b974 100644
--- a/Script/cs/Threads/ManagerThread.py
+++ b/Script/cs/Threads/ManagerThread.py
@@ -346,13 +346,21 @@ class ManagerThread(threading.Thread):
                     # 'StageHost' is for backward compatibility
                     eosinstance = line.split()[2]
 
-            self.logger.debug('filemetadata: remote dir = %s, eos instance = %s',
-                    remotedir, eosinstance)
-
-            if not infofile.endswith(Constants.copied_ext):
-                os.rename(infofile, filename + Constants.tobecopied_ext)
-
-            return FileMetaData.FileMetaData(filename, remotedir, eosinstance, mtime)
+            if remotedir == '' or eosinstance == '':
+                # empty file, invalid data, etc.
+                self.logger.warning(
+                        'invalid metadata in %s (remotedir="%s", eosinstance="%s")'
+                        ': deleting infofile and getting metadata from scratch'
+                        , infofile, remotedir, eosinstance)
+                os.remove(infofile)
+            else:
+                self.logger.debug('filemetadata: remote dir = %s, eos instance = %s',
+                        remotedir, eosinstance)
+
+                if not infofile.endswith(Constants.copied_ext):
+                    os.rename(infofile, filename + Constants.tobecopied_ext)
+
+                return FileMetaData.FileMetaData(filename, remotedir, eosinstance, mtime)
 
         try:
             parsed = self.parser(filename)
-- 
GitLab