Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python File Concept #2100

Merged
merged 156 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
156 commits
Select commit Hold shift + click to select a range
88feea3
Hotfix for python imports
oxisto Dec 9, 2024
3fcbc29
Revert "Hotfix for python imports"
maximiliankaul Dec 11, 2024
a80d841
initial commit
maximiliankaul Dec 12, 2024
0fc9df1
bump
maximiliankaul Dec 12, 2024
08212e4
bump
maximiliankaul Dec 12, 2024
9dfd275
Merge branch 'main' into mk/concepts
maximiliankaul Dec 12, 2024
452c3b0
bump
maximiliankaul Dec 12, 2024
451761e
enable default passes
maximiliankaul Dec 12, 2024
0c8891c
Added log4j xml
oxisto Dec 12, 2024
ace3b58
bump
maximiliankaul Dec 12, 2024
530440e
move back to component pass
maximiliankaul Dec 12, 2024
2659b0b
getting ready for cpg-core push
maximiliankaul Dec 12, 2024
310401e
Merge remote-tracking branch 'origin/main' into mk/concepts
maximiliankaul Dec 12, 2024
82e48c2
Adding new Overlay and Concept nodes to the graph.
maximiliankaul Dec 12, 2024
3f78158
Merge branch 'mk/core-concepts' into mk/concepts
maximiliankaul Dec 12, 2024
53ce35f
bump
maximiliankaul Dec 12, 2024
c4a2dc7
code review
maximiliankaul Dec 13, 2024
478d8c4
Merge branch 'mk/core-concepts' into mk/concepts
maximiliankaul Dec 13, 2024
61191b9
fix merge
maximiliankaul Dec 13, 2024
35dea83
Merge branch 'main' into mk/core-concepts
maximiliankaul Dec 13, 2024
d399b1f
Merge branch 'mk/core-concepts' into mk/concepts
maximiliankaul Dec 13, 2024
23e1ead
Merge remote-tracking branch 'origin/main' into mk/core-concepts
maximiliankaul Dec 13, 2024
dae2388
Merge branch 'mk/core-concepts' into mk/concepts
maximiliankaul Dec 13, 2024
ff63c17
rename cpgNode and move to OverlayingNode
maximiliankaul Dec 16, 2024
7404c6e
Merge branch 'mk/core-concepts' into mk/concepts
maximiliankaul Dec 16, 2024
da57726
merge: rename
maximiliankaul Dec 16, 2024
67e3a2a
Merge branch 'main' into mk/core-concepts
maximiliankaul Dec 16, 2024
98bee1f
Merge branch 'main' into mk/concepts
maximiliankaul Jan 13, 2025
91bc0e1
fix merge problems
maximiliankaul Jan 14, 2025
124ae93
Merge branch 'main' into mk/concepts
maximiliankaul Jan 14, 2025
5f50f71
make variables open
maximiliankaul Jan 14, 2025
aa29053
move concept and operation node to (unpublished) cpg-concepts module
maximiliankaul Jan 14, 2025
390c06c
initial skeleton for concept and operation nodes
maximiliankaul Jan 14, 2025
a769275
formatting
maximiliankaul Jan 14, 2025
432d081
disable test until other pr is merged
maximiliankaul Jan 14, 2025
c637a07
Merge branch 'mk/coreConcepts++' into mk/conceptsModule
maximiliankaul Jan 14, 2025
a33c4b2
re-enable test
maximiliankaul Jan 14, 2025
47707e0
Merge branch 'mk/core-concepts' into mk/concepts
maximiliankaul Jan 14, 2025
342a23e
Merge branch 'mk/coreConcepts++' into mk/concepts
maximiliankaul Jan 14, 2025
fa57a2a
fix build system config
maximiliankaul Jan 14, 2025
d91680b
Merge branch 'mk/conceptsModule' into mk/concepts
maximiliankaul Jan 14, 2025
e10c2ca
fix imports
maximiliankaul Jan 14, 2025
d121b15
fix imports
maximiliankaul Jan 14, 2025
af9143a
add build.gradle.kts
maximiliankaul Jan 14, 2025
1b1d235
add log config
maximiliankaul Jan 14, 2025
c0d2b0c
add extension functions
maximiliankaul Jan 14, 2025
5cb63e6
Remove obsolete comment
maximiliankaul Jan 14, 2025
bfa1de6
Merge branch 'mk/conceptsModule' into mk/conceptsLogFile
maximiliankaul Jan 14, 2025
2f494d9
move tests to integrationTest
maximiliankaul Jan 14, 2025
8560c82
make cpg-concepts always available
maximiliankaul Jan 14, 2025
1efbf74
Merge branch 'mk/conceptsModule' into mk/conceptsLogFile
maximiliankaul Jan 14, 2025
f39d29c
update extension function
maximiliankaul Jan 14, 2025
d26b340
remove "open" as overriding does not work with delegates
maximiliankaul Jan 14, 2025
a79d588
change underlying node handling
maximiliankaul Jan 14, 2025
8ac4d62
Merge branch 'mk/conceptsModule' into mk/conceptsLogFile
maximiliankaul Jan 14, 2025
808ecf3
make operationNodes return a set
maximiliankaul Jan 14, 2025
8d3ee1b
use find for dependency
maximiliankaul Jan 14, 2025
e789924
make operationNodes return a set
maximiliankaul Jan 14, 2025
2b19f8c
use find for dependency
maximiliankaul Jan 14, 2025
536457d
Merge branch 'mk/conceptsModule' into mk/conceptsLogFile
maximiliankaul Jan 14, 2025
10144ad
fix doc string
maximiliankaul Jan 14, 2025
a020699
Merge branch 'mk/conceptsModule' into mk/conceptsLogFile
maximiliankaul Jan 14, 2025
daacfba
Merge branch 'main' into mk/conceptsModule
maximiliankaul Jan 14, 2025
deeec1d
Merge branch 'mk/conceptsModule' into mk/conceptsLogFile
maximiliankaul Jan 14, 2025
142c8df
mark tests as integration tests because they depend on python
maximiliankaul Jan 14, 2025
88b5d02
add dummy nodes to make the test compile
maximiliankaul Jan 15, 2025
5bff848
initial draft of Node.belongsToConcept
maximiliankaul Jan 15, 2025
87e4b0a
cleanup
maximiliankaul Jan 15, 2025
ace50fc
Add cpg-concepts module (#1932)
maximiliankaul Jan 15, 2025
8843b55
Merge branch 'main' into mk/conceptsLogFile
maximiliankaul Feb 21, 2025
f2fd994
Merge branch 'main' into mk/conceptsLogFile
maximiliankaul Feb 25, 2025
3f26e70
improve assertion
maximiliankaul Feb 25, 2025
32a1867
improve assertion
maximiliankaul Feb 25, 2025
48c8e44
fixed logic bug
maximiliankaul Feb 25, 2025
c6156e0
fixed logic bug
maximiliankaul Feb 25, 2025
76e47d0
remove not needed inline reified
maximiliankaul Feb 25, 2025
ba26d49
remove not needed inline reified
maximiliankaul Feb 25, 2025
3bdd65a
doc++
maximiliankaul Feb 25, 2025
d8fcdf0
Merge branch 'mk/assertionWithContract' into mk/conceptsLogFile
maximiliankaul Feb 25, 2025
5857de2
do not persist broken name
maximiliankaul Feb 25, 2025
41d3b7c
Merge branch 'mk/builtinsNameFix' into mk/conceptsLogFile
maximiliankaul Feb 25, 2025
1d00282
bump
maximiliankaul Feb 25, 2025
7010173
use simplenameconverter
maximiliankaul Feb 25, 2025
4b5e39f
cleanup merge error
maximiliankaul Feb 25, 2025
5d8acb2
Merge branch 'mk/builtinsNameFix' into mk/conceptsLogFile
maximiliankaul Feb 25, 2025
2926949
Merge branch 'main' into mk/conceptsLogFile
maximiliankaul Feb 26, 2025
8f443ef
example 1 + 2 pass
maximiliankaul Feb 26, 2025
d85f273
more tests
maximiliankaul Feb 26, 2025
24ffd32
Fix: importedFrom now checks the import instead of name
maximiliankaul Feb 26, 2025
eff38d4
Merge branch 'mk/fixImportedFrom' into mk/conceptsLogFile
maximiliankaul Feb 26, 2025
8cba44e
Merge branch 'main' into mk/conceptsLogFile
maximiliankaul Mar 3, 2025
a35e5fd
cleanup
maximiliankaul Mar 3, 2025
b0e0a1f
all classes in 1 file and FileNode -> File
maximiliankaul Mar 3, 2025
15b5846
Merge branch 'main' into mk/conceptFile
maximiliankaul Mar 3, 2025
7c629fc
bump
maximiliankaul Mar 4, 2025
d0aa692
bump
maximiliankaul Mar 4, 2025
0482d7f
bump
maximiliankaul Mar 4, 2025
d78bb98
file caching to prevent dupliacte File nodes
maximiliankaul Mar 6, 2025
191691f
bump
maximiliankaul Mar 6, 2025
9bc976e
Merge branch 'main' into mk/conceptFile
maximiliankaul Mar 6, 2025
53e14a7
simplify && doc
maximiliankaul Mar 6, 2025
725e21a
simplify
maximiliankaul Mar 6, 2025
22e4471
bump
maximiliankaul Mar 6, 2025
0d4fcc4
Argument parsing helper function
maximiliankaul Mar 6, 2025
6997019
Merge branch 'mk/callArgHelper' into mk/conceptFile
maximiliankaul Mar 6, 2025
f44f4c7
switch to new extension function
maximiliankaul Mar 6, 2025
1e30d40
evaluateAs<T>
maximiliankaul Mar 6, 2025
5341e86
Name change
maximiliankaul Mar 6, 2025
3a8b5cf
remove bad check
maximiliankaul Mar 6, 2025
75362fb
Make log public
oxisto Mar 6, 2025
8588e4c
Using evaluateAs
oxisto Mar 6, 2025
0f6c95f
Merge branch 'mk/callArgHelper' into mk/conceptFile
maximiliankaul Mar 6, 2025
13c156c
fix rename
maximiliankaul Mar 6, 2025
1fccded
Merge branch 'main' into mk/conceptFile
maximiliankaul Mar 6, 2025
47f5e6f
cleanup
maximiliankaul Mar 7, 2025
d4141cd
cleanup
maximiliankaul Mar 7, 2025
148539b
cleanup
maximiliankaul Mar 7, 2025
3eaddd8
no default concept passes for neo4j
maximiliankaul Mar 7, 2025
38a0e58
doc
maximiliankaul Mar 7, 2025
b9d6011
doc
maximiliankaul Mar 7, 2025
0d2895c
Merge branch 'main' into mk/conceptFile
maximiliankaul Mar 7, 2025
825622a
removed not needed file
maximiliankaul Mar 7, 2025
7c5e401
doc++
maximiliankaul Mar 10, 2025
517d79f
join all args
maximiliankaul Mar 10, 2025
bea173e
fix overwriting bug
maximiliankaul Mar 10, 2025
009ffd9
handleCall -> handleCallExpression
maximiliankaul Mar 10, 2025
e9bf03a
make operations sound like an action
maximiliankaul Mar 10, 2025
2555248
don't ignore half of the dfg
maximiliankaul Mar 11, 2025
0333ac6
Removing Python dependency in cpg-concepts.
maximiliankaul Mar 11, 2025
21b1338
explicit dependency on DFG
maximiliankaul Mar 11, 2025
61d971f
don't use query api
maximiliankaul Mar 11, 2025
655451a
doc++
maximiliankaul Mar 11, 2025
43dd3f4
check language
maximiliankaul Mar 11, 2025
938ad15
DeleteFile
maximiliankaul Mar 11, 2025
58d6155
Merge branch 'main' into mk/conceptFile
maximiliankaul Mar 11, 2025
9c94f91
doc++
maximiliankaul Mar 11, 2025
cfe0a75
doc
maximiliankaul Mar 11, 2025
dbdc141
remove FileRead target
maximiliankaul Mar 11, 2025
046976f
only one write argument
maximiliankaul Mar 11, 2025
6cdda9f
ignore blocked tests
maximiliankaul Mar 11, 2025
733937b
Merge branch 'main' into mk/conceptFile
maximiliankaul Mar 11, 2025
edceff6
WIP: concepts flow queries
maximiliankaul Mar 11, 2025
5123d4b
use this.language.evaluator
maximiliankaul Mar 11, 2025
7f2af8e
new unknown file name
maximiliankaul Mar 11, 2025
1f70f6e
Merge branch 'main' into mk/conceptFile
maximiliankaul Mar 12, 2025
bd558e5
re-enable test
maximiliankaul Mar 12, 2025
088469a
doc++
maximiliankaul Mar 12, 2025
15a8edd
rename file to avoid conflict
maximiliankaul Mar 12, 2025
ebf2adb
ignore files with not parsable names
maximiliankaul Mar 12, 2025
6bf0736
doc++
maximiliankaul Mar 12, 2025
309088d
bump
maximiliankaul Mar 12, 2025
7496ee1
remove file from Node and remove FilenameMapper
maximiliankaul Mar 12, 2025
b11a34f
use location instead of file
maximiliankaul Mar 12, 2025
7b0c222
Merge branch 'mk/noNameInNodes' into mk/conceptFile
maximiliankaul Mar 12, 2025
edfa9a3
rename fileConcept field to file
maximiliankaul Mar 12, 2025
16828b9
Merge branch 'main' into mk/conceptFile
maximiliankaul Mar 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os

with open('example.txt', 'r') as file:
content = file.read()

os.remove('example.txt')

with open('example.txt', 'w') as file:
file.write(content)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os

flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
with os.open('example.txt', flags, 0o600) as fh:
fh.write('foo')
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import os

flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
with os.open('example.txt', flags, 0o600) as fh:
fh.write('foo')
os.chmod('example.txt', 0o511)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
with open('example.txt', 'r') as file:
content = file.read()
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if foo:
file = open("foo")
else:
file = open("bar")

file.read()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
with open('example.txt', 'w') as file:
file.write('Hello world!')
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

if __name__ == '__main__':
file = "/tmp/foo.txt"
fh = open(file, "w+")
fh.write("Writing to the file before chmod ain't a good idea...")
os.chmod(file, 0o600)
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
package de.fraunhofer.aisec.cpg.graph.concepts

import de.fraunhofer.aisec.cpg.graph.Name
import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.OverlayNode

Expand All @@ -43,5 +44,6 @@ abstract class Operation(

init {
this.underlyingNode = underlyingNode
this::class.simpleName?.let { name = Name(it) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Copyright (c) 2024, Fraunhofer AISEC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $$$$$$\ $$$$$$$\ $$$$$$\
* $$ __$$\ $$ __$$\ $$ __$$\
* $$ / \__|$$ | $$ |$$ / \__|
* $$ | $$$$$$$ |$$ |$$$$\
* $$ | $$ ____/ $$ |\_$$ |
* $$ | $$\ $$ | $$ | $$ |
* \$$$$$ |$$ | \$$$$$ |
* \______/ \__| \______/
*
*/
package de.fraunhofer.aisec.cpg.graph.concepts.file

import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.concepts.Concept
import de.fraunhofer.aisec.cpg.graph.concepts.Operation

/**
* This interface indicates that the corresponding node is connected to a file concept or operation.
*/
interface IsFile

/**
* Represents a file access mode flag.
*
* man 0p fcntl.h
*
* ```
* The <fcntl.h> header shall define the following symbolic constants for use as the file access modes for open(), openat(), and fcntl(). The values shall be unique, except that O_EXEC and O_SEARCH may have equal values. The values shall be suitable for use in #if preprocessing directives.
* O_EXEC Open for execute only (non-directory files). The result is unspecified if this flag is applied to a directory.
* O_RDONLY Open for reading only.
* O_RDWR Open for reading and writing.
* O_SEARCH Open directory for search only. The result is unspecified if this flag is applied to a non-directory file.
* O_WRONLY Open for writing only.
* ```
*/
enum class FileAccessModeFlags(val value: Long) : IsFile {
// O_EXEC(-1), not supported
O_RDONLY(0),
O_RDWR(2),
// O_SEARCH(-1), not supported
O_WRONLY(1),
}

/** The bit-mask to be used to get the [FileAccessModeFlags] from an entire flags value. */
const val O_ACCMODE_MODE_MASK = 3L

/**
* Represents a file.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param fileName The name of the file e.g. `foo/bar/example.txt`
*/
class File(underlyingNode: Node, val fileName: String) :
Concept(underlyingNode = underlyingNode), IsFile

/**
* Represents setting flags on a file. For example when opening the file.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param concept The corresponding [File] node.
* @param flags A set of file flags (see [FileAccessModeFlags]).
*/
class SetFileFlags(
underlyingNode: Node,
override val concept: File,
val flags: Set<FileAccessModeFlags>,
) : FileOperation(underlyingNode = underlyingNode, file = concept), IsFile

/**
* Represents setting the umask, for example with the `mode` parameter in a Python `os.open` call or
* a `chmod` call.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param concept The corresponding [File] node.
* @param mask The file mask in UNIX notation (i.e. 0o644)
*/
class SetFileMask(underlyingNode: Node, override val concept: File, val mask: Long) :
FileOperation(underlyingNode = underlyingNode, file = concept), IsFile

/**
* Represents closing a file.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param concept The corresponding [File] node.
*/
class CloseFile(underlyingNode: Node, override val concept: File) :
FileOperation(underlyingNode = underlyingNode, file = concept), IsFile

/**
* Represents deleting a file.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param concept The corresponding [File] node.
*/
class DeleteFile(underlyingNode: Node, override val concept: File) :
FileOperation(underlyingNode = underlyingNode, file = concept), IsFile

/**
* Represents opening a file. This is usually done with the same underlying node the [concept] field
* is attached to.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param concept The corresponding [File] node.
*/
class OpenFile(underlyingNode: Node, override val concept: File) :
FileOperation(underlyingNode = underlyingNode, file = concept), IsFile

/**
* Represents reading from a file.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param concept The corresponding [File] node.
*/
class ReadFile(underlyingNode: Node, override val concept: File) :
FileOperation(underlyingNode = underlyingNode, file = concept), IsFile

/**
* Represents writing to a file.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param concept The corresponding [File] node.
* @param what The node being written to the file.
*/
class WriteFile(underlyingNode: Node, override val concept: File, val what: Node) :
FileOperation(underlyingNode = underlyingNode, file = concept), IsFile

/**
* All [File] [Operation]s inherit from this class. This makes the [file] field available for
* [FileOperation], resulting in easier to read queries (one can use [FileOperation.file] instead of
* [Operation.concept]). There is no logic involved - just a simple forwarding of the field.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param file The corresponding [File] node.
*/
abstract class FileOperation(underlyingNode: Node, file: File) :
Operation(underlyingNode = underlyingNode, concept = file) {
/**
* The corresponding [File] [Concept] node. This is a convenience field and has the same effect
* as using [Operation.concept].
*/
val file: File
get() = this.concept as File
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright (c) 2024, Fraunhofer AISEC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $$$$$$\ $$$$$$$\ $$$$$$\
* $$ __$$\ $$ __$$\ $$ __$$\
* $$ / \__|$$ | $$ |$$ / \__|
* $$ | $$$$$$$ |$$ |$$$$\
* $$ | $$ ____/ $$ |\_$$ |
* $$ | $$\ $$ | $$ | $$ |
* \$$$$$ |$$ | \$$$$$ |
* \______/ \__| \______/
*
*/
package de.fraunhofer.aisec.cpg.graph.concepts.file

import de.fraunhofer.aisec.cpg.graph.MetadataProvider
import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.NodeBuilder
import de.fraunhofer.aisec.cpg.graph.codeAndLocationFrom

/**
* Creates a new [File] node. This node represents a file on a hard-disk somewhere.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param fileName The name of the file e.g. `foo/bar/example.txt`
* @return The new [File] node.
*/
fun MetadataProvider.newFile(underlyingNode: Node, fileName: String): File {
val node = File(underlyingNode = underlyingNode, fileName = fileName)
node.codeAndLocationFrom(underlyingNode)

NodeBuilder.log(node)
return node
}

/**
* Creates a new [OpenFile] node. This node represents opening a file.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param file The [File] this operation is opening.
* @return The new [OpenFile] node.
*/
fun MetadataProvider.newFileOpen(underlyingNode: Node, file: File): OpenFile {
val node = OpenFile(underlyingNode = underlyingNode, concept = file)
node.codeAndLocationFrom(underlyingNode)

NodeBuilder.log(node)
return node
}

/**
* Creates a new [SetFileMask] node. This node represents changing a files permissions.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param file The [File] this operation is modifying.
* @param mask The file mask to set (in UNIX notation).
* @return The new [SetFileMask] node.
*/
fun MetadataProvider.newFileSetMask(underlyingNode: Node, file: File, mask: Long): SetFileMask {
val node = SetFileMask(underlyingNode = underlyingNode, concept = file, mask = mask)
node.codeAndLocationFrom(underlyingNode)

NodeBuilder.log(node)
return node
}

/**
* Creates a new [SetFileFlags] node.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param file The [File] this operation is working on.
* @param flags The file flags to set (in UNIX notation).
* @return The new [SetFileFlags] node.
*/
fun MetadataProvider.newFileSetFlags(
underlyingNode: Node,
file: File,
flags: Set<FileAccessModeFlags>,
): SetFileFlags {
val node = SetFileFlags(underlyingNode = underlyingNode, concept = file, flags = flags)
node.codeAndLocationFrom(underlyingNode)

NodeBuilder.log(node)
return node
}

/**
* Creates a new [CloseFile] node.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param file The [File] this operation is closing.
* @return The new [CloseFile] node.
*/
fun MetadataProvider.newFileClose(underlyingNode: Node, file: File): CloseFile {
val node = CloseFile(underlyingNode = underlyingNode, concept = file)
node.codeAndLocationFrom(underlyingNode)

NodeBuilder.log(node)
return node
}

/**
* Creates a new [DeleteFile] node.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param file The [File] this operation is deleting.
* @return The new [DeleteFile] node.
*/
fun MetadataProvider.newFileDelete(underlyingNode: Node, file: File): DeleteFile {
val node = DeleteFile(underlyingNode = underlyingNode, concept = file)
node.codeAndLocationFrom(underlyingNode)

NodeBuilder.log(node)
return node
}

/**
* Creates a new [ReadFile] node and attaches the DFG from the corresponding [file] to the new node
* and then from [this] to the created node.
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param file The [File] this operation is reading from.
* @return The new [ReadFile] node.
*/
fun MetadataProvider.newFileRead(underlyingNode: Node, file: File): ReadFile {
val node = ReadFile(underlyingNode = underlyingNode, concept = file)
node.codeAndLocationFrom(underlyingNode)

// add DFG
file.nextDFG += node
node.nextDFG += underlyingNode

NodeBuilder.log(node)
return node
}

/**
* Creates a new [WriteFile] node and attaches the DFG from [what] to [this] and then from the new
* node to the [file].
*
* @param underlyingNode The underlying CPG node (usually a [CallExpression]).
* @param file The [File] this operation is writing to.
* @param what A node being written to the [file] (usually the argument of a `write` call).
* @return The new [WriteFile] node.
*/
fun MetadataProvider.newFileWrite(underlyingNode: Node, file: File, what: Node): WriteFile {
val node = WriteFile(underlyingNode = underlyingNode, concept = file, what = what)
node.codeAndLocationFrom(underlyingNode)

// add DFG
what.nextDFG += node
node.nextDFG += file

NodeBuilder.log(node)
return node
}
Loading
Loading