@@ -144,25 +144,16 @@ def add_files(
144144 tar_md5 : Optional [str ] = tarFileObject .md5 ()
145145 tarFileObject .close ()
146146 logger .info (f"{ ts_utc ()} : (add_files): Completed archive file { tfname } " )
147- if not skip_tars_md5 :
148- tar_tuple : TupleTarsRowNoId = (tfname , tarsize , tar_md5 )
149- logger .info ("tar name={}, tar size={}, tar md5={}" .format (* tar_tuple ))
150- if not tars_table_exists (cur ):
151- # Need to create tars table
152- create_tars_table (cur , con )
153- cur .execute ("insert into tars values (NULL,?,?,?)" , tar_tuple )
154- con .commit ()
155147
156148 # Transfer tar to HPSS
157149 if config .hpss is not None :
158150 hpss : str = config .hpss
159151 else :
160152 raise TypeError ("Invalid config.hpss={}" .format (config .hpss ))
161153
162- # NOTE: These lines could be added under an "if debug" condition
163- # logger.info(f"{ts_utc()}: CONTENTS of CACHE upon call to hpss_put:")
164- # process = subprocess.run(["ls", "-l", "zstash"], capture_output=True, text=True)
165- # print(process.stdout)
154+ logger .info (
155+ f"Contents of the cache prior to `hpss_put`: { os .listdir (os .path .join (cache , tfname ))} "
156+ )
166157
167158 logger .info (
168159 f"{ ts_utc ()} : DIVING: (add_files): Calling hpss_put to dispatch archive file { tfname } [keep, non_blocking] = [{ keep } , { non_blocking } ]"
@@ -172,7 +163,33 @@ def add_files(
172163 f"{ ts_utc ()} : SURFACE (add_files): Called hpss_put to dispatch archive file { tfname } "
173164 )
174165
175- # Update database with files that have been archived
166+ if not skip_tars_md5 :
167+ tar_tuple : TupleTarsRowNoId = (tfname , tarsize , tar_md5 )
168+ logger .info ("tar name={}, tar size={}, tar md5={}" .format (* tar_tuple ))
169+ if not tars_table_exists (cur ):
170+ # Need to create tars table
171+ create_tars_table (cur , con )
172+ # We're done adding files to the tar.
173+ # And we've transferred it to HPSS.
174+ # Now we can insert the tar into the database.
175+ cur .execute ("SELECT COUNT(*) FROM tars WHERE name = ?" , (tfname ,))
176+ if cur .fetchone ()[0 ] == 0 :
177+ # Typical case
178+ # Doesn't exist - insert new
179+ cur .execute ("INSERT INTO tars VALUES (NULL,?,?,?)" , tar_tuple )
180+ else :
181+ # Unusual case
182+ # Exists - update with new size and md5
183+ logger .warning (
184+ f"Possible database corruption. Updated existing tar { tfname } with new size { tarsize } "
185+ )
186+ cur .execute (
187+ "UPDATE tars SET size = ?, md5 = ? WHERE name = ?" ,
188+ (tarsize , tar_md5 , tfname ),
189+ )
190+ con .commit ()
191+
192+ # Update database with the individual files that have been archived
176193 # Add a row to the "files" table,
177194 # the last 6 columns matching the values of `archived`
178195 cur .executemany ("insert into files values (NULL,?,?,?,?,?,?)" , archived )
0 commit comments