Skip to content

Commit 653844c

Browse files
committed
Java: Use shared FileSystem library.
1 parent 3949914 commit 653844c

File tree

1 file changed

+18
-157
lines changed

1 file changed

+18
-157
lines changed

java/ql/lib/semmle/code/FileSystem.qll

Lines changed: 18 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,37 @@
11
/** Provides classes for working with files and folders. */
22

33
import Location
4+
private import codeql.util.FileSystem
45

5-
/** A file or folder. */
6-
class Container extends @container, Top {
7-
/**
8-
* Gets the absolute, canonical path of this container, using forward slashes
9-
* as path separator.
10-
*
11-
* The path starts with a _root prefix_ followed by zero or more _path
12-
* segments_ separated by forward slashes.
13-
*
14-
* The root prefix is of one of the following forms:
15-
*
16-
* 1. A single forward slash `/` (Unix-style)
17-
* 2. An upper-case drive letter followed by a colon and a forward slash,
18-
* such as `C:/` (Windows-style)
19-
* 3. Two forward slashes, a computer name, and then another forward slash,
20-
* such as `//FileServer/` (UNC-style)
21-
*
22-
* Path segments are never empty (that is, absolute paths never contain two
23-
* contiguous slashes, except as part of a UNC-style root prefix). Also, path
24-
* segments never contain forward slashes, and no path segment is of the
25-
* form `.` (one dot) or `..` (two dots).
26-
*
27-
* Note that an absolute path never ends with a forward slash, except if it is
28-
* a bare root prefix, that is, the path has no path segments. A container
29-
* whose absolute path has no segments is always a `Folder`, not a `File`.
30-
*/
31-
abstract string getAbsolutePath();
6+
private module Input implements InputSig {
7+
abstract class ContainerBase extends @container {
8+
abstract string getAbsolutePath();
329

33-
/**
34-
* Gets a URL representing the location of this container.
35-
*
36-
* For more information see [Providing URLs](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/#providing-urls).
37-
*/
38-
abstract string getURL();
10+
ContainerBase getParentContainer() { containerparent(result, this) }
3911

40-
/**
41-
* Gets the relative path of this file or folder from the root folder of the
42-
* analyzed source location. The relative path of the root folder itself is
43-
* the empty string.
44-
*
45-
* This has no result if the container is outside the source root, that is,
46-
* if the root folder is not a reflexive, transitive parent of this container.
47-
*/
48-
string getRelativePath() {
49-
exists(string absPath, string pref |
50-
absPath = this.getAbsolutePath() and sourceLocationPrefix(pref)
51-
|
52-
absPath = pref and result = ""
53-
or
54-
absPath = pref.regexpReplaceAll("/$", "") + "/" + result and
55-
not result.matches("/%")
56-
)
12+
string toString() { result = this.getAbsolutePath() }
5713
}
5814

59-
/**
60-
* Gets the base name of this container including extension, that is, the last
61-
* segment of its absolute path, or the empty string if it has no segments.
62-
*
63-
* Here are some examples of absolute paths and the corresponding base names
64-
* (surrounded with quotes to avoid ambiguity):
65-
*
66-
* <table border="1">
67-
* <tr><th>Absolute path</th><th>Base name</th></tr>
68-
* <tr><td>"/tmp/tst.java"</td><td>"tst.java"</td></tr>
69-
* <tr><td>"C:/Program Files (x86)"</td><td>"Program Files (x86)"</td></tr>
70-
* <tr><td>"/"</td><td>""</td></tr>
71-
* <tr><td>"C:/"</td><td>""</td></tr>
72-
* <tr><td>"D:/"</td><td>""</td></tr>
73-
* <tr><td>"//FileServer/"</td><td>""</td></tr>
74-
* </table>
75-
*/
76-
string getBaseName() {
77-
result = this.getAbsolutePath().regexpCapture(".*/(([^/]*?)(?:\\.([^.]*))?)", 1)
15+
class FolderBase extends ContainerBase, @folder {
16+
override string getAbsolutePath() { folders(this, result) }
7817
}
7918

80-
/**
81-
* Gets the extension of this container, that is, the suffix of its base name
82-
* after the last dot character, if any.
83-
*
84-
* In particular,
85-
*
86-
* - if the name does not include a dot, there is no extension, so this
87-
* predicate has no result;
88-
* - if the name ends in a dot, the extension is the empty string;
89-
* - if the name contains multiple dots, the extension follows the last dot.
90-
*
91-
* Here are some examples of absolute paths and the corresponding extensions
92-
* (surrounded with quotes to avoid ambiguity):
93-
*
94-
* <table border="1">
95-
* <tr><th>Absolute path</th><th>Extension</th></tr>
96-
* <tr><td>"/tmp/tst.java"</td><td>"java"</td></tr>
97-
* <tr><td>"/tmp/.classpath"</td><td>"classpath"</td></tr>
98-
* <tr><td>"/bin/bash"</td><td>not defined</td></tr>
99-
* <tr><td>"/tmp/tst2."</td><td>""</td></tr>
100-
* <tr><td>"/tmp/x.tar.gz"</td><td>"gz"</td></tr>
101-
* </table>
102-
*/
103-
string getExtension() {
104-
result = this.getAbsolutePath().regexpCapture(".*/([^/]*?)(\\.([^.]*))?", 3)
105-
}
106-
107-
/**
108-
* Gets the stem of this container, that is, the prefix of its base name up to
109-
* (but not including) the last dot character if there is one, or the entire
110-
* base name if there is not.
111-
*
112-
* Here are some examples of absolute paths and the corresponding stems
113-
* (surrounded with quotes to avoid ambiguity):
114-
*
115-
* <table border="1">
116-
* <tr><th>Absolute path</th><th>Stem</th></tr>
117-
* <tr><td>"/tmp/tst.java"</td><td>"tst"</td></tr>
118-
* <tr><td>"/tmp/.classpath"</td><td>""</td></tr>
119-
* <tr><td>"/bin/bash"</td><td>"bash"</td></tr>
120-
* <tr><td>"/tmp/tst2."</td><td>"tst2"</td></tr>
121-
* <tr><td>"/tmp/x.tar.gz"</td><td>"x.tar"</td></tr>
122-
* </table>
123-
*/
124-
string getStem() {
125-
result = this.getAbsolutePath().regexpCapture(".*/([^/]*?)(?:\\.([^.]*))?", 1)
19+
class FileBase extends ContainerBase, @file {
20+
override string getAbsolutePath() { files(this, result) }
12621
}
12722

128-
/** Gets the parent container of this file or folder, if any. */
129-
Container getParentContainer() { containerparent(result, this) }
130-
131-
/** Gets a file or sub-folder in this container. */
132-
Container getAChildContainer() { this = result.getParentContainer() }
133-
134-
/** Gets a file in this container. */
135-
File getAFile() { result = this.getAChildContainer() }
136-
137-
/** Gets the file in this container that has the given `baseName`, if any. */
138-
File getFile(string baseName) {
139-
result = this.getAFile() and
140-
result.getBaseName() = baseName
141-
}
142-
143-
/** Gets a sub-folder in this container. */
144-
Folder getAFolder() { result = this.getAChildContainer() }
23+
predicate hasSourceLocationPrefix = sourceLocationPrefix/1;
24+
}
14525

146-
/** Gets the sub-folder in this container that has the given `baseName`, if any. */
147-
Folder getFolder(string baseName) {
148-
result = this.getAFolder() and
149-
result.getBaseName() = baseName
150-
}
26+
private module Impl = Make<Input>;
15127

152-
/**
153-
* Gets a textual representation of this container.
154-
*
155-
* The default implementation gets the absolute path to the container, but subclasses may override
156-
* to provide a different result. To get the absolute path of any `Container`, call
157-
* `Container.getAbsolutePath()` directly.
158-
*/
159-
override string toString() { result = this.getAbsolutePath() }
28+
/** A file or folder. */
29+
class Container extends Impl::Container, Top {
30+
override string toString() { result = Impl::Container.super.toString() }
16031
}
16132

16233
/** A folder. */
163-
class Folder extends Container, @folder {
164-
override string getAbsolutePath() { folders(this, result) }
165-
166-
/** Gets the URL of this folder. */
167-
override string getURL() { result = "folder://" + this.getAbsolutePath() }
168-
34+
class Folder extends Container, Impl::Folder {
16935
override string getAPrimaryQlClass() { result = "Folder" }
17036
}
17137

@@ -174,12 +40,7 @@ class Folder extends Container, @folder {
17440
*
17541
* Note that `File` extends `Container` as it may be a `jar` file.
17642
*/
177-
class File extends Container, @file {
178-
override string getAbsolutePath() { files(this, result) }
179-
180-
/** Gets the URL of this file. */
181-
override string getURL() { result = "file://" + this.getAbsolutePath() + ":0:0:0:0" }
182-
43+
class File extends Container, Impl::File {
18344
override string getAPrimaryQlClass() { result = "File" }
18445

18546
/** Holds if this is a (Java or Kotlin) source file. */

0 commit comments

Comments
 (0)