Skip to content

Commit af14f36

Browse files
authored
Merge pull request github#12289 from hvitved/util/file-system
Util: Add shared file system implementation
2 parents da459c4 + ad37523 commit af14f36

File tree

4 files changed

+245
-324
lines changed

4 files changed

+245
-324
lines changed

csharp/ql/lib/qlpack.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ upgrades: upgrades
88
dependencies:
99
codeql/ssa: ${workspace}
1010
codeql/tutorial: ${workspace}
11+
codeql/util: ${workspace}
1112
dataExtensions:
1213
- ext/*.model.yml
1314
- ext/generated/*.model.yml

csharp/ql/lib/semmle/code/csharp/File.qll

Lines changed: 16 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -3,184 +3,34 @@
33
*/
44

55
private import Comments
6+
private import codeql.util.FileSystem
67

7-
/** A file or folder. */
8-
class Container extends @container {
9-
/**
10-
* Gets the absolute, canonical path of this container, using forward slashes
11-
* as path separator.
12-
*
13-
* The path starts with a _root prefix_ followed by zero or more _path
14-
* segments_ separated by forward slashes.
15-
*
16-
* The root prefix is of one of the following forms:
17-
*
18-
* 1. A single forward slash `/` (Unix-style)
19-
* 2. An upper-case drive letter followed by a colon and a forward slash,
20-
* such as `C:/` (Windows-style)
21-
* 3. Two forward slashes, a computer name, and then another forward slash,
22-
* such as `//FileServer/` (UNC-style)
23-
*
24-
* Path segments are never empty (that is, absolute paths never contain two
25-
* contiguous slashes, except as part of a UNC-style root prefix). Also, path
26-
* segments never contain forward slashes, and no path segment is of the
27-
* form `.` (one dot) or `..` (two dots).
28-
*
29-
* Note that an absolute path never ends with a forward slash, except if it is
30-
* a bare root prefix, that is, the path has no path segments. A container
31-
* whose absolute path has no segments is always a `Folder`, not a `File`.
32-
*/
33-
string getAbsolutePath() { none() }
34-
35-
/**
36-
* Gets a URL representing the location of this container.
37-
*
38-
* For more information see [Providing URLs](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/#providing-urls).
39-
*/
40-
string getURL() { none() }
41-
42-
/**
43-
* Gets the relative path of this file or folder from the root folder of the
44-
* analyzed source location. The relative path of the root folder itself is
45-
* the empty string.
46-
*
47-
* This has no result if the container is outside the source root, that is,
48-
* if the root folder is not a reflexive, transitive parent of this container.
49-
*/
50-
string getRelativePath() {
51-
exists(string absPath, string pref |
52-
absPath = this.getAbsolutePath() and sourceLocationPrefix(pref)
53-
|
54-
absPath = pref and result = ""
55-
or
56-
absPath = pref.regexpReplaceAll("/$", "") + "/" + result and
57-
not result.matches("/%")
58-
)
59-
}
60-
61-
/**
62-
* Gets the base name of this container including extension, that is, the last
63-
* segment of its absolute path, or the empty string if it has no segments.
64-
*
65-
* Here are some examples of absolute paths and the corresponding base names
66-
* (surrounded with quotes to avoid ambiguity):
67-
*
68-
* <table border="1">
69-
* <tr><th>Absolute path</th><th>Base name</th></tr>
70-
* <tr><td>"/tmp/tst.cs"</td><td>"tst.cs"</td></tr>
71-
* <tr><td>"C:/Program Files (x86)"</td><td>"Program Files (x86)"</td></tr>
72-
* <tr><td>"/"</td><td>""</td></tr>
73-
* <tr><td>"C:/"</td><td>""</td></tr>
74-
* <tr><td>"D:/"</td><td>""</td></tr>
75-
* <tr><td>"//FileServer/"</td><td>""</td></tr>
76-
* </table>
77-
*/
78-
string getBaseName() {
79-
result = this.getAbsolutePath().regexpCapture(".*/(([^/]*?)(?:\\.([^.]*))?)", 1)
80-
}
81-
82-
/**
83-
* Gets the extension of this container, that is, the suffix of its base name
84-
* after the last dot character, if any.
85-
*
86-
* In particular,
87-
*
88-
* - if the name does not include a dot, there is no extension, so this
89-
* predicate has no result;
90-
* - if the name ends in a dot, the extension is the empty string;
91-
* - if the name contains multiple dots, the extension follows the last dot.
92-
*
93-
* Here are some examples of absolute paths and the corresponding extensions
94-
* (surrounded with quotes to avoid ambiguity):
95-
*
96-
* <table border="1">
97-
* <tr><th>Absolute path</th><th>Extension</th></tr>
98-
* <tr><td>"/tmp/tst.cs"</td><td>"cs"</td></tr>
99-
* <tr><td>"/tmp/.classpath"</td><td>"classpath"</td></tr>
100-
* <tr><td>"/bin/bash"</td><td>not defined</td></tr>
101-
* <tr><td>"/tmp/tst2."</td><td>""</td></tr>
102-
* <tr><td>"/tmp/x.tar.gz"</td><td>"gz"</td></tr>
103-
* </table>
104-
*/
105-
string getExtension() {
106-
result = this.getAbsolutePath().regexpCapture(".*/([^/]*?)(\\.([^.]*))?", 3)
107-
}
108-
109-
/**
110-
* Gets the stem of this container, that is, the prefix of its base name up to
111-
* (but not including) the last dot character if there is one, or the entire
112-
* base name if there is not.
113-
*
114-
* Here are some examples of absolute paths and the corresponding stems
115-
* (surrounded with quotes to avoid ambiguity):
116-
*
117-
* <table border="1">
118-
* <tr><th>Absolute path</th><th>Stem</th></tr>
119-
* <tr><td>"/tmp/tst.cs"</td><td>"tst"</td></tr>
120-
* <tr><td>"/tmp/.classpath"</td><td>""</td></tr>
121-
* <tr><td>"/bin/bash"</td><td>"bash"</td></tr>
122-
* <tr><td>"/tmp/tst2."</td><td>"tst2"</td></tr>
123-
* <tr><td>"/tmp/x.tar.gz"</td><td>"x.tar"</td></tr>
124-
* </table>
125-
*/
126-
string getStem() {
127-
result = this.getAbsolutePath().regexpCapture(".*/([^/]*?)(?:\\.([^.]*))?", 1)
128-
}
129-
130-
/** Gets the parent container of this file or folder, if any. */
131-
Container getParentContainer() { containerparent(result, this) }
8+
private module Input implements InputSig {
9+
abstract class ContainerBase extends @container {
10+
abstract string getAbsolutePath();
13211

133-
/** Gets a file or sub-folder in this container. */
134-
Container getAChildContainer() { this = result.getParentContainer() }
12+
ContainerBase getParentContainer() { containerparent(result, this) }
13513

136-
/** Gets a file in this container. */
137-
File getAFile() { result = this.getAChildContainer() }
138-
139-
/** Gets the file in this container that has the given `baseName`, if any. */
140-
File getFile(string baseName) {
141-
result = this.getAFile() and
142-
result.getBaseName() = baseName
14+
string toString() { result = this.getAbsolutePath() }
14315
}
14416

145-
/** Gets a sub-folder in this container. */
146-
Folder getAFolder() { result = this.getAChildContainer() }
147-
148-
/** Gets the sub-folder in this container that has the given `baseName`, if any. */
149-
Folder getFolder(string baseName) {
150-
result = this.getAFolder() and
151-
result.getBaseName() = baseName
17+
class FolderBase extends ContainerBase, @folder {
18+
override string getAbsolutePath() { folders(this, result) }
15219
}
15320

154-
/** Gets the file or sub-folder in this container that has the given `name`, if any. */
155-
Container getChildContainer(string name) {
156-
result = this.getAChildContainer() and
157-
result.getBaseName() = name
21+
class FileBase extends ContainerBase, @file {
22+
override string getAbsolutePath() { files(this, result) }
15823
}
15924

160-
/** Gets the file in this container that has the given `stem` and `extension`, if any. */
161-
File getFile(string stem, string extension) {
162-
result = this.getAChildContainer() and
163-
result.getStem() = stem and
164-
result.getExtension() = extension
165-
}
25+
predicate hasSourceLocationPrefix = sourceLocationPrefix/1;
26+
}
16627

167-
/** Gets a sub-folder contained in this container. */
168-
Folder getASubFolder() { result = this.getAChildContainer() }
28+
private module Impl = Make<Input>;
16929

170-
/**
171-
* Gets a textual representation of the path of this container.
172-
*
173-
* This is the absolute path of the container.
174-
*/
175-
string toString() { result = this.getAbsolutePath() }
176-
}
30+
class Container = Impl::Container;
17731

17832
/** A folder. */
179-
class Folder extends Container, @folder {
180-
override string getAbsolutePath() { folders(this, result) }
181-
182-
override string getURL() { result = "folder://" + this.getAbsolutePath() }
183-
}
33+
class Folder extends Container, Impl::Folder { }
18434

18535
bindingset[flag]
18636
private predicate fileHasExtractionFlag(File f, int flag) {
@@ -191,9 +41,7 @@ private predicate fileHasExtractionFlag(File f, int flag) {
19141
}
19242

19343
/** A file. */
194-
class File extends Container, @file {
195-
override string getAbsolutePath() { files(this, result) }
196-
44+
class File extends Container, Impl::File {
19745
/** Gets the number of lines in this file. */
19846
int getNumberOfLines() { numlines(this, result, _, _) }
19947

0 commit comments

Comments
 (0)