|
1 | 1 | package io.shiftleft.codepropertygraph.cpgloading
|
2 | 2 |
|
3 |
| -import better.files.File |
4 | 3 | import io.shiftleft.codepropertygraph.generated.Cpg
|
5 |
| -import io.shiftleft.codepropertygraph.generated.PropertyNames |
6 | 4 | import org.slf4j.{Logger, LoggerFactory}
|
7 | 5 |
|
8 |
| -import scala.util.Try |
| 6 | +import java.io.FileNotFoundException |
| 7 | +import java.nio.charset.StandardCharsets |
| 8 | +import java.nio.file.{Files, Path, Paths} |
| 9 | +import scala.util.Using |
9 | 10 |
|
10 | 11 | object CpgLoader {
|
| 12 | + private val logger: Logger = LoggerFactory.getLogger(getClass) |
11 | 13 |
|
12 |
| - private val logger: Logger = LoggerFactory.getLogger(classOf[CpgLoader]) |
| 14 | + /** Load a Code Property Graph from the given file */ |
| 15 | + def load(filename: String): Cpg = |
| 16 | + load(Paths.get(filename)) |
13 | 17 |
|
14 |
| - /** Load a Code Property Graph |
15 |
| - * |
16 |
| - * @param filename |
17 |
| - * name of file that stores the code property graph |
18 |
| - * @param config |
19 |
| - * loader configuration |
| 18 | + /** Load a Code Property Graph from the given file - persist in given second file. I.e. the given input file will not |
| 19 | + * be modified, all changes will be written to the given 'persistTo' file. * |
20 | 20 | */
|
21 |
| - def load(filename: String, config: CpgLoaderConfig = CpgLoaderConfig()): Cpg = |
22 |
| - new CpgLoader().load(filename, config) |
| 21 | + def load(from: String, persistTo: String): Cpg = |
| 22 | + load(Paths.get(from), Paths.get(persistTo)) |
23 | 23 |
|
24 |
| - /** Load Code Property Graph from an overflow DB file |
25 |
| - * |
26 |
| - * @param config |
27 |
| - * loader config |
| 24 | + /** Load a Code Property Graph from the given file |
28 | 25 | *
|
29 |
| - * This methods loads the CPG from an existing overflow DB file, specified in config.overflowDbConfig. In particular, |
30 |
| - * this config specifies the filename. For example, to load the database at "foo.db", you can issue the following: |
31 |
| - * |
32 |
| - * val odbConfig = Config.withDefaults().withStorageLocation(config.spPath) val config = |
33 |
| - * CpgLoaderConfig().withOverflowConfig(odbConfig) CpgLoader.loadFromOverflowDb(config) |
| 26 | + * Notes: |
| 27 | + * - detects the format as either flatgraph, overflowdb or proto |
| 28 | + * - a flatgraph storage opened straight away |
| 29 | + * - OverflowDb and proto formats are first converted to flatgraph, which is written to the `persistTo` file |
| 30 | + * - OverflowDb and proto formats are first converted to flatgraph, and therefor we create a new flatgraph storage |
| 31 | + * path, which can be obtained via `cpg.graph.storagePathMaybe` |
34 | 32 | */
|
35 |
| - def loadFromOverflowDb(config: CpgLoaderConfig = CpgLoaderConfig()): Cpg = { |
36 |
| - new CpgLoader().loadFromOverflowDb(config) |
| 33 | + def load(path: Path): Cpg = { |
| 34 | + val absolutePath = path.toAbsolutePath |
| 35 | + if (!Files.exists(absolutePath)) { |
| 36 | + throw new FileNotFoundException(s"given input file $absolutePath does not exist") |
| 37 | + } else if (isProtoFormat(absolutePath)) { |
| 38 | + load(path, persistTo = absolutePath.resolveSibling(s"${path.getFileName}.fg")) |
| 39 | + } else if (isOverflowDbFormat(absolutePath)) { |
| 40 | + load(absolutePath, persistTo = path.resolveSibling(s"${path.getFileName}.fg")) |
| 41 | + } else { |
| 42 | + // assuming it's flatgraph format |
| 43 | + Cpg.withStorage(absolutePath) |
| 44 | + } |
37 | 45 | }
|
38 | 46 |
|
39 |
| - /** Create any indexes necessary for quick access. |
40 |
| - * |
41 |
| - * @param cpg |
42 |
| - * the CPG to create indexes in |
43 |
| - */ |
44 |
| - def createIndexes(cpg: Cpg): Unit = |
45 |
| - new CpgLoader().createIndexes(cpg) |
46 |
| - |
47 |
| - /** Determine whether the CPG is a legacy (proto) CPG |
| 47 | + /** Load a Code Property Graph from the given file, but persist it in the given second file. I.e. the given input file |
| 48 | + * will not be modified, all changes will be written to the given 'persistTo' file. |
48 | 49 | *
|
49 |
| - * @param filename |
50 |
| - * name of the file to probe |
| 50 | + * Notes: |
| 51 | + * - if the given 'persistTo' file already exists, it will be overridden |
| 52 | + * - detects the format as either flatgraph, overflowdb or proto |
| 53 | + * - a flatgraph storage is copied to the `persistTo` file and then opened straight away |
| 54 | + * - OverflowDb and proto formats are first converted to flatgraph, which is written to the `persistTo` file |
51 | 55 | */
|
52 |
| - def isLegacyCpg(filename: String): Boolean = |
53 |
| - isLegacyCpg(File(filename)) |
54 |
| - |
55 |
| - /** Determine whether the CPG is a legacy (proto) CPG |
56 |
| - * |
57 |
| - * @param file |
58 |
| - * file to probe |
59 |
| - */ |
60 |
| - def isLegacyCpg(file: File): Boolean = { |
61 |
| - val bytes = file.bytes |
62 |
| - Try { |
63 |
| - bytes.next() == 'P' && bytes.next() == 'K' |
64 |
| - }.getOrElse(false) |
| 56 | + def load(from: Path, persistTo: Path): Cpg = { |
| 57 | + val absolutePath = from.toAbsolutePath |
| 58 | + if (persistTo != from) |
| 59 | + Files.deleteIfExists(persistTo) |
| 60 | + |
| 61 | + if (!Files.exists(absolutePath)) { |
| 62 | + throw new FileNotFoundException(s"given input file $absolutePath does not exist") |
| 63 | + } else if (isProtoFormat(absolutePath)) { |
| 64 | + logger.debug(s"Converting $from from proto cpg into new flatgraph storage: $persistTo") |
| 65 | + ProtoCpgLoader.loadFromProtoZip(absolutePath.toString, Option(persistTo)) |
| 66 | + } else if (isOverflowDbFormat(absolutePath)) { |
| 67 | + loadFromOverflowDb(absolutePath, persistTo) |
| 68 | + } else if (isFlatgraphFormat(absolutePath)) { |
| 69 | + Files.copy(absolutePath, persistTo) |
| 70 | + Cpg.withStorage(persistTo) |
| 71 | + } else { |
| 72 | + throw new AssertionError( |
| 73 | + s"unknown file format - we probed the first bytes but it didn't look like one of our known formats (proto.zip, flatgraph, overflowdb)" |
| 74 | + ) |
| 75 | + } |
65 | 76 | }
|
66 | 77 |
|
67 |
| -} |
| 78 | + /** Determine whether the CPG is a legacy (proto) CPG */ |
| 79 | + def isProtoFormat(path: Path): Boolean = |
| 80 | + probeFirstBytes(path, "PK") |
68 | 81 |
|
69 |
| -private class CpgLoader { |
| 82 | + /** Determine whether the CPG is a proto CPG */ |
| 83 | + def isProtoFormat(filename: String): Boolean = |
| 84 | + isProtoFormat(Paths.get(filename)) |
70 | 85 |
|
71 |
| - import CpgLoader.logger |
| 86 | + def isOverflowDbFormat(path: Path): Boolean = |
| 87 | + probeFirstBytes(path, "H:2") |
72 | 88 |
|
73 |
| - def load(filename: String, config: CpgLoaderConfig = CpgLoaderConfig.withoutOverflow): Cpg = { |
74 |
| - logger.debug("Loading " + filename) |
| 89 | + def isFlatgraphFormat(path: Path): Boolean = |
| 90 | + probeFirstBytes(path, "FLT GRPH") // flatgraph.storage.MagicBytesString |
75 | 91 |
|
76 |
| - val cpg = |
77 |
| - ProtoCpgLoader.loadFromProtoZip(filename, config.overflowDbConfig) |
78 |
| - if (config.createIndexes) { createIndexes(cpg) } |
79 |
| - cpg |
| 92 | + /** Load Code Property Graph from an overflow DB file, by first converting it into a flatgraph binary */ |
| 93 | + def loadFromOverflowDb(path: Path, persistTo: Path): Cpg = { |
| 94 | + logger.info(s"Converting $path from overflowdb to new flatgraph storage: $persistTo") |
| 95 | + flatgraph.convert.Convert.convertOdbToFlatgraph(overflowDbFile = path, outputFile = persistTo) |
| 96 | + Cpg.withStorage(persistTo) |
80 | 97 | }
|
81 | 98 |
|
82 |
| - def loadFromOverflowDb(config: CpgLoaderConfig = CpgLoaderConfig()): Cpg = { |
83 |
| - val cpg = Cpg.withConfig(config.overflowDbConfig) |
84 |
| - if (config.createIndexes) { createIndexes(cpg) } |
85 |
| - cpg |
| 99 | + /** Determine whether the CPG is a legacy (proto) CPG */ |
| 100 | + @deprecated("use `isProtoCpg` instead") |
| 101 | + def isLegacyCpg(filename: String): Boolean = |
| 102 | + isProtoFormat(Paths.get(filename)) |
| 103 | + |
| 104 | + /** Determine whether the CPG is a legacy (proto) CPG */ |
| 105 | + @deprecated("use `isProtoCpg` instead") |
| 106 | + def isLegacyCpg(path: Path): Boolean = |
| 107 | + isProtoFormat(path) |
| 108 | + |
| 109 | + private def probeFirstBytes(path: Path, probeFor: String): Boolean = { |
| 110 | + Using(Files.newInputStream(path)) { is => |
| 111 | + val buffer = new Array[Byte](probeFor.size) |
| 112 | + is.read(buffer) |
| 113 | + new String(buffer, StandardCharsets.UTF_8) == probeFor |
| 114 | + }.getOrElse(false) |
86 | 115 | }
|
87 | 116 |
|
88 |
| - def createIndexes(cpg: Cpg): Unit = |
89 |
| - cpg.graph.indexManager.createNodePropertyIndex(PropertyNames.FULL_NAME) |
90 |
| - |
91 | 117 | }
|
0 commit comments