5050import com .semmle .util .trap .pathtransformers .PathTransformer ;
5151
5252public class OdasaOutput {
53- // By default we use lockless TRAP writing, but this can be set
54- // if we want to use the old TRAP locking for any reason.
55- private final boolean use_trap_locking = Env .systemEnv ().getBoolean ("CODEQL_EXTRACTOR_JAVA_TRAP_LOCKING" , false );
56-
5753 // either these are set ...
5854 private final File trapFolder ;
5955 private final File sourceArchiveFolder ;
@@ -270,55 +266,36 @@ private void deleteTrapFileAndDependencies(IrElement sym, String signature) {
270266 * For functions for example, this means its parameter signature.
271267 */
272268 private TrapFileManager getMembersWriterForDecl (File trap , File trapFileBase , TrapClassVersion trapFileVersion , IrElement sym , String signature ) {
273- if (use_trap_locking ) {
274- TrapClassVersion currVersion = TrapClassVersion .fromSymbol (sym , log );
275- String shortName = sym instanceof IrDeclarationWithName ? ((IrDeclarationWithName )sym ).getName ().asString () : "(name unknown)" ;
276- if (trap .exists ()) {
277- // Only re-write an existing trap file if we encountered a newer version of the same class.
278- TrapClassVersion trapVersion = readVersionInfo (trap );
279- if (!currVersion .isValid ()) {
280- log .trace ("Not rewriting trap file for: " + shortName + " " + trapVersion + " " + currVersion + " " + trap );
281- } else if (currVersion .newerThan (trapVersion )) {
282- log .trace ("Rewriting trap file for: " + shortName + " " + trapVersion + " " + currVersion + " " + trap );
283- deleteTrapFileAndDependencies (sym , signature );
284- } else {
285- return null ;
286- }
287- } else {
288- log .trace ("Writing trap file for: " + shortName + " " + currVersion + " " + trap );
289- }
290- } else {
291- // If the TRAP file already exists then we
292- // don't need to write it.
293- if (trap .exists ()) {
294- log .trace ("Not rewriting trap file for " + trap .toString () + " as it exists" );
295- return null ;
296- }
297- // If the TRAP file was written in the past, and
298- // then renamed to its trap-old name, then we
299- // don't need to rewrite it only to rename it
300- // again.
301- File trapFileDir = trap .getParentFile ();
302- File trapOld = new File (trapFileDir , trap .getName ().replace (".trap.gz" , ".trap-old.gz" ));
303- if (trapOld .exists ()) {
304- log .trace ("Not rewriting trap file for " + trap .toString () + " as the trap-old exists" );
305- return null ;
306- }
307- // Otherwise, if any newer TRAP file has already
308- // been written then we don't need to write
309- // anything.
310- if (trapFileBase != null && trapFileVersion != null && trapFileDir .exists ()) {
311- String trapFileBaseName = trapFileBase .getName ();
312-
313- for (File f : FileUtil .list (trapFileDir )) {
314- String name = f .getName ();
315- Matcher m = selectClassVersionComponents .matcher (name );
316- if (m .matches () && m .group (1 ).equals (trapFileBaseName )) {
317- TrapClassVersion v = new TrapClassVersion (Integer .valueOf (m .group (2 )), Integer .valueOf (m .group (3 )), Long .valueOf (m .group (4 )), m .group (5 ));
318- if (v .newerThan (trapFileVersion )) {
319- log .trace ("Not rewriting trap file for " + trap .toString () + " as " + f .toString () + " exists" );
320- return null ;
321- }
269+ // If the TRAP file already exists then we
270+ // don't need to write it.
271+ if (trap .exists ()) {
272+ log .trace ("Not rewriting trap file for " + trap .toString () + " as it exists" );
273+ return null ;
274+ }
275+ // If the TRAP file was written in the past, and
276+ // then renamed to its trap-old name, then we
277+ // don't need to rewrite it only to rename it
278+ // again.
279+ File trapFileDir = trap .getParentFile ();
280+ File trapOld = new File (trapFileDir , trap .getName ().replace (".trap.gz" , ".trap-old.gz" ));
281+ if (trapOld .exists ()) {
282+ log .trace ("Not rewriting trap file for " + trap .toString () + " as the trap-old exists" );
283+ return null ;
284+ }
285+ // Otherwise, if any newer TRAP file has already
286+ // been written then we don't need to write
287+ // anything.
288+ if (trapFileBase != null && trapFileVersion != null && trapFileDir .exists ()) {
289+ String trapFileBaseName = trapFileBase .getName ();
290+
291+ for (File f : FileUtil .list (trapFileDir )) {
292+ String name = f .getName ();
293+ Matcher m = selectClassVersionComponents .matcher (name );
294+ if (m .matches () && m .group (1 ).equals (trapFileBaseName )) {
295+ TrapClassVersion v = new TrapClassVersion (Integer .valueOf (m .group (2 )), Integer .valueOf (m .group (3 )), Long .valueOf (m .group (4 )), m .group (5 ));
296+ if (v .newerThan (trapFileVersion )) {
297+ log .trace ("Not rewriting trap file for " + trap .toString () + " as " + f .toString () + " exists" );
298+ return null ;
322299 }
323300 }
324301 }
@@ -374,25 +351,6 @@ public void close() {
374351 }
375352
376353 writeTrapDependencies (trapDependenciesForClass );
377-
378- // If we are using TRAP locking then we
379- // need to write a metadata file.
380- if (use_trap_locking ) {
381- // Record major/minor version information for extracted class files.
382- // This is subsequently used to determine whether to re-extract (a newer version of) the same class.
383- File metadataFile = new File (trapFile .getAbsolutePath ().replace (".trap.gz" , ".metadata" ));
384- try {
385- Map <String , String > versionMap = new LinkedHashMap <>();
386- TrapClassVersion tcv = TrapClassVersion .fromSymbol (sym , log );
387- versionMap .put (MAJOR_VERSION , String .valueOf (tcv .getMajorVersion ()));
388- versionMap .put (MINOR_VERSION , String .valueOf (tcv .getMinorVersion ()));
389- versionMap .put (LAST_MODIFIED , String .valueOf (tcv .getLastModified ()));
390- versionMap .put (EXTRACTOR_NAME , tcv .getExtractorName ());
391- FileUtil .writePropertiesCSV (metadataFile , versionMap );
392- } catch (IOException e ) {
393- log .warn ("Could not save trap metadata file: " + metadataFile .getAbsolutePath (), e );
394- }
395- }
396354 }
397355 private void writeTrapDependencies (TrapDependencies trapDependencies ) {
398356 String dep = trapDependencies .trapFile ().replace (".trap.gz" , ".dep" );
@@ -480,22 +438,18 @@ private TrapLocker(IrElement decl, String signature, boolean fromSource) {
480438 trapFile = null ;
481439 } else {
482440 File normalTrapFile = getTrapFileForDecl (sym , signature );
483- if (use_trap_locking ) {
484- trapFile = normalTrapFile ;
485- } else {
486- // We encode the metadata into the filename, so that the
487- // TRAP filenames for different metadatas don't overlap.
488- if (fromSource )
489- trapFileVersion = new TrapClassVersion (0 , 0 , 0 , "kotlin" );
490- else
491- trapFileVersion = TrapClassVersion .fromSymbol (sym , log );
492- String baseName = normalTrapFile .getName ().replace (".trap.gz" , "" );
493- // If a class has lots of inner classes, then we get lots of files
494- // in a single directory. This makes our directory listings later slow.
495- // To avoid this, rather than using files named .../Foo*, we use .../Foo/Foo*.
496- trapFileBase = new File (new File (normalTrapFile .getParentFile (), baseName ), baseName );
497- trapFile = new File (trapFileBase .getPath () + '#' + trapFileVersion .toString () + ".trap.gz" );
498- }
441+ // We encode the metadata into the filename, so that the
442+ // TRAP filenames for different metadatas don't overlap.
443+ if (fromSource )
444+ trapFileVersion = new TrapClassVersion (0 , 0 , 0 , "kotlin" );
445+ else
446+ trapFileVersion = TrapClassVersion .fromSymbol (sym , log );
447+ String baseName = normalTrapFile .getName ().replace (".trap.gz" , "" );
448+ // If a class has lots of inner classes, then we get lots of files
449+ // in a single directory. This makes our directory listings later slow.
450+ // To avoid this, rather than using files named .../Foo*, we use .../Foo/Foo*.
451+ trapFileBase = new File (new File (normalTrapFile .getParentFile (), baseName ), baseName );
452+ trapFile = new File (trapFileBase .getPath () + '#' + trapFileVersion .toString () + ".trap.gz" );
499453 }
500454 }
501455 private TrapLocker (File jarFile ) {
@@ -510,9 +464,6 @@ private TrapLocker(String moduleName) {
510464 }
511465 public TrapFileManager getTrapFileManager () {
512466 if (trapFile !=null ) {
513- if (use_trap_locking ) {
514- lockTrapFile (trapFile );
515- }
516467 return getMembersWriterForDecl (trapFile , trapFileBase , trapFileVersion , sym , signature );
517468 } else {
518469 return null ;
@@ -522,23 +473,14 @@ public TrapFileManager getTrapFileManager() {
522473 @ Override
523474 public void close () {
524475 if (trapFile !=null ) {
525- try {
526- if (use_trap_locking ) {
527- unlockTrapFile (trapFile );
528- }
529- } catch (NestedError e ) {
530- log .warn ("Error unlocking trap file " + trapFile .getAbsolutePath (), e );
531- }
532-
533- // If we are writing TRAP file locklessly, then now that we
534- // have finished writing our TRAP file, we want to rename
535- // and TRAP file that matches our trapFileBase but doesn't
536- // have the latest metadata.
476+ // Now that we have finished writing our TRAP file, we want
477+ // to rename and TRAP file that matches our trapFileBase
478+ // but doesn't have the latest metadata.
537479 // Renaming it to trap-old means that it won't be imported,
538480 // but we can still use its presence to avoid future
539481 // invocations rewriting it, and it means that the information
540482 // is in the TRAP directory if we need it for debugging.
541- if (! use_trap_locking && sym != null ) {
483+ if (sym != null ) {
542484 File trapFileDir = trapFileBase .getParentFile ();
543485 String trapFileBaseName = trapFileBase .getName ();
544486
0 commit comments