4949import com .semmle .util .trap .dependencies .TrapSet ;
5050import com .semmle .util .trap .pathtransformers .PathTransformer ;
5151
52+ import com .github .codeql .Compression ;
53+
5254public class OdasaOutput {
5355 private final File trapFolder ;
5456 private final File sourceArchiveFolder ;
@@ -63,16 +65,18 @@ public class OdasaOutput {
6365 private final boolean trackClassOrigins ;
6466
6567 private final Logger log ;
68+ private final Compression compression ;
6669
6770 /** DEBUG only: just use the given file as the root for TRAP, source archive etc */
68- OdasaOutput (File outputRoot , Logger log ) {
71+ OdasaOutput (File outputRoot , Compression compression , Logger log ) {
6972 this .trapFolder = new File (outputRoot , "trap" );
7073 this .sourceArchiveFolder = new File (outputRoot , "src_archive" );
7174 this .trackClassOrigins = false ;
7275 this .log = log ;
76+ this .compression = compression ;
7377 }
7478
75- public OdasaOutput (boolean trackClassOrigins , Logger log ) {
79+ public OdasaOutput (boolean trackClassOrigins , Compression compression , Logger log ) {
7680 String trapFolderVar = Env .systemEnv ().getFirstNonEmpty ("CODEQL_EXTRACTOR_JAVA_TRAP_DIR" , Var .TRAP_FOLDER .name ());
7781 if (trapFolderVar == null ) {
7882 throw new ResourceError ("CODEQL_EXTRACTOR_JAVA_TRAP_DIR was not set" );
@@ -85,6 +89,7 @@ public OdasaOutput(boolean trackClassOrigins, Logger log) {
8589 this .sourceArchiveFolder = new File (sourceArchiveVar );
8690 this .trackClassOrigins = trackClassOrigins ;
8791 this .log = log ;
92+ this .compression = compression ;
8893 }
8994
9095 public File getTrapFolder () {
@@ -180,18 +185,18 @@ private File getTrapFileForJarFile(File jarFile) {
180185 return null ;
181186 return FileUtil .appendAbsolutePath (
182187 currentSpecFileEntry .getTrapFolder (),
183- JARS_DIR + "/" + PathTransformer .std ().fileAsDatabaseString (jarFile ) + ".trap.gz" );
188+ JARS_DIR + "/" + PathTransformer .std ().fileAsDatabaseString (jarFile ) + ".trap" + compression . getExtension () );
184189 }
185190
186191 private File getTrapFileForModule (String moduleName ) {
187192 return FileUtil .appendAbsolutePath (
188193 currentSpecFileEntry .getTrapFolder (),
189- MODULES_DIR + "/" + moduleName + ".trap.gz" );
194+ MODULES_DIR + "/" + moduleName + ".trap" + compression . getExtension () );
190195 }
191196
192197 private File trapFileFor (File file ) {
193198 return FileUtil .appendAbsolutePath (currentSpecFileEntry .getTrapFolder (),
194- PathTransformer .std ().fileAsDatabaseString (file ) + ".trap.gz" );
199+ PathTransformer .std ().fileAsDatabaseString (file ) + ".trap" + compression . getExtension () );
195200 }
196201
197202 private File getTrapFileForDecl (IrElement sym , String signature ) {
@@ -214,7 +219,7 @@ private String trapFilePathForDecl(IrElement sym, String signature) {
214219 binaryName .replace ('.' , '/' ) +
215220 signature +
216221 ".members" +
217- ".trap.gz" ;
222+ ".trap" + compression . getExtension () ;
218223 return result ;
219224 }
220225
@@ -245,7 +250,7 @@ private TrapFileManager getMembersWriterForDecl(File trap, File trapFileBase, Tr
245250 // don't need to rewrite it only to rename it
246251 // again.
247252 File trapFileDir = trap .getParentFile ();
248- File trapOld = new File (trapFileDir , trap .getName ().replace (".trap.gz" , ".trap-old.gz" ));
253+ File trapOld = new File (trapFileDir , trap .getName ().replace (".trap" + compression . getExtension () , ".trap-old" + compression . getExtension () ));
249254 if (trapOld .exists ()) {
250255 log .trace ("Not rewriting trap file for " + trap .toString () + " as the trap-old exists" );
251256 return null ;
@@ -272,7 +277,7 @@ private TrapFileManager getMembersWriterForDecl(File trap, File trapFileBase, Tr
272277 }
273278
274279 private TrapFileManager trapWriter (File trapFile , IrElement sym , String signature ) {
275- if (!trapFile .getName ().endsWith (".trap.gz" ))
280+ if (!trapFile .getName ().endsWith (".trap" + compression . getExtension () ))
276281 throw new CatastrophicError ("OdasaOutput only supports writing to compressed trap files" );
277282 String relative = FileUtil .relativePath (trapFile , currentSpecFileEntry .getTrapFolder ());
278283 trapFile .getParentFile ().mkdirs ();
@@ -321,7 +326,7 @@ public void close() {
321326 writeTrapDependencies (trapDependenciesForClass );
322327 }
323328 private void writeTrapDependencies (TrapDependencies trapDependencies ) {
324- String dep = trapDependencies .trapFile ().replace (".trap.gz" , ".dep" );
329+ String dep = trapDependencies .trapFile ().replace (".trap" + compression . getExtension () , ".dep" );
325330 trapDependencies .save (
326331 currentSpecFileEntry .getTrapFolder ().toPath ().resolve (dep ));
327332 }
@@ -335,7 +340,7 @@ public void setHasError() {
335340 * Trap file locking.
336341 */
337342
338- private final Pattern selectClassVersionComponents = Pattern .compile ("(.*)#(-?[0-9]+)\\ .(-?[0-9]+)-(-?[0-9]+)-(.*)\\ .trap\\ .gz " );
343+ private final Pattern selectClassVersionComponents = Pattern .compile ("(.*)#(-?[0-9]+)\\ .(-?[0-9]+)-(-?[0-9]+)-(.*)\\ .trap.* " );
339344
340345 /**
341346 * <b>CAUTION</b>: to avoid the potential for deadlock between multiple concurrent extractor processes,
@@ -412,12 +417,12 @@ private TrapLocker(IrElement decl, String signature, boolean fromSource) {
412417 trapFileVersion = new TrapClassVersion (0 , 0 , 0 , "kotlin" );
413418 else
414419 trapFileVersion = TrapClassVersion .fromSymbol (sym , log );
415- String baseName = normalTrapFile .getName ().replace (".trap.gz" , "" );
420+ String baseName = normalTrapFile .getName ().replace (".trap" + compression . getExtension () , "" );
416421 // If a class has lots of inner classes, then we get lots of files
417422 // in a single directory. This makes our directory listings later slow.
418423 // To avoid this, rather than using files named .../Foo*, we use .../Foo/Foo*.
419424 trapFileBase = new File (new File (normalTrapFile .getParentFile (), baseName ), baseName );
420- trapFile = new File (trapFileBase .getPath () + '#' + trapFileVersion .toString () + ".trap.gz" );
425+ trapFile = new File (trapFileBase .getPath () + '#' + trapFileVersion .toString () + ".trap" + compression . getExtension () );
421426 }
422427 }
423428 private TrapLocker (File jarFile ) {
@@ -488,7 +493,7 @@ public int compare(Pair<File, TrapClassVersion> p1, Pair<File, TrapClassVersion>
488493 for (Pair <File , TrapClassVersion > p : pairs ) {
489494 if (!latestVersion .equals (p .snd ())) {
490495 File f = p .fst ();
491- File fOld = new File (f .getParentFile (), f .getName ().replace (".trap.gz" , ".trap-old.gz" ));
496+ File fOld = new File (f .getParentFile (), f .getName ().replace (".trap" + compression . getExtension () , ".trap-old" + compression . getExtension () ));
492497 // We aren't interested in whether or not this succeeds;
493498 // it may fail because a concurrent extractor has already
494499 // renamed it.
0 commit comments