Skip to content

Commit

Permalink
Replace Path.toFile() (#1158)
Browse files Browse the repository at this point in the history
In some use cases it's preferable to have for example the nanorc configs inside a jar. Although a lot of the code base use `java.nio.file.Path`, a couple of places still call `Path.toFile()`, which isn't supported for NIO's ZIP file system, which can be created for a `jar:` URL, so that the contents of the jar can be used as a (read only) file system.

This PR replaces occurences of `Path.toFile()` and replaces the `File` operations with NIO's `Files`.
  • Loading branch information
snazy authored Jan 24, 2025
1 parent f869217 commit 57030d4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 33 deletions.
11 changes: 6 additions & 5 deletions builtins/src/main/java/org/jline/builtins/ConfigurationPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.jline.builtins;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class ConfigurationPath {
Expand All @@ -33,9 +34,9 @@ public ConfigurationPath(Path appConfig, Path userConfig) {
*/
public Path getConfig(String name) {
Path out = null;
if (userConfig != null && userConfig.resolve(name).toFile().exists()) {
if (userConfig != null && Files.exists(userConfig.resolve(name))) {
out = userConfig.resolve(name);
} else if (appConfig != null && appConfig.resolve(name).toFile().exists()) {
} else if (appConfig != null && Files.exists(appConfig.resolve(name))) {
out = appConfig.resolve(name);
}
return out;
Expand All @@ -62,10 +63,10 @@ public Path getUserConfig(String name) throws IOException {
public Path getUserConfig(String name, boolean create) throws IOException {
Path out = null;
if (userConfig != null) {
if (!userConfig.resolve(name).toFile().exists() && create) {
userConfig.resolve(name).toFile().createNewFile();
if (!Files.exists(userConfig.resolve(name)) && create) {
Files.createFile(userConfig.resolve(name));
}
if (userConfig.resolve(name).toFile().exists()) {
if (Files.exists(userConfig.resolve(name))) {
out = userConfig.resolve(name);
}
}
Expand Down
2 changes: 1 addition & 1 deletion builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -2105,7 +2105,7 @@ private boolean save(String name) throws IOException {
return false;
}
} else if (!Files.exists(newPath)) {
newPath.toFile().createNewFile();
Files.createFile(newPath);
}
Path t = Files.createTempFile("jline-", ".temp");
try (OutputStream os = Files.newOutputStream(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public Map<String, Boolean> scripts() {
}
}
for (Path p : scripts) {
String name = p.toFile().getName();
String name = p.getFileName().toString();
int idx = name.lastIndexOf(".");
out.put(name.substring(0, idx), name.substring(idx + 1).equals(scriptExtension));
}
Expand Down Expand Up @@ -395,7 +395,7 @@ public ScriptFile(String command, String cmdLine, String[] args) {
for (String e : scriptExtensions()) {
String file = command + "." + e;
Path path = Paths.get(p, file);
if (path.toFile().exists()) {
if (Files.exists(path)) {
script = path;
scriptExtension(command);
found = true;
Expand Down Expand Up @@ -959,7 +959,7 @@ private Object slurpcmd(CommandInput input) {
: engine.getSerializationFormats().get(0);
try {
Path path = Paths.get(arg);
if (path.toFile().exists()) {
if (Files.exists(path)) {
if (!format.equals(SLURP_FORMAT_TEXT)) {
out = slurp(path, encoding, format);
} else {
Expand Down
34 changes: 13 additions & 21 deletions demo/src/main/java/org/apache/felix/gogo/jline/Posix.java
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ protected Map<String, Object> readAttributes(Path path) {
}
}
attrs.computeIfAbsent("isExecutable", s -> Files.isExecutable(path));
attrs.computeIfAbsent("permissions", s -> getPermissionsFromFile(path.toFile()));
attrs.computeIfAbsent("permissions", s -> getPermissionsFromFile(path));
return attrs;
}
}
Expand Down Expand Up @@ -2138,27 +2138,19 @@ private static boolean isWindowsExecutable(String fileName) {
* the file is readable/writable/executable. If so, then <U>all</U> the
* relevant permissions are set (i.e., owner, group and others)
*/
private static Set<PosixFilePermission> getPermissionsFromFile(File f) {
Set<PosixFilePermission> perms = EnumSet.noneOf(PosixFilePermission.class);
if (f.canRead()) {
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.OTHERS_READ);
}

if (f.canWrite()) {
perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.GROUP_WRITE);
perms.add(PosixFilePermission.OTHERS_WRITE);
}

if (f.canExecute() || (OSUtils.IS_WINDOWS && isWindowsExecutable(f.getName()))) {
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
private static Set<PosixFilePermission> getPermissionsFromFile(Path f) {
try {
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(f);
if (OSUtils.IS_WINDOWS && isWindowsExecutable(f.getFileName().toString())) {
perms = new HashSet<>(perms);
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
}
return perms;
} catch (IOException e) {
throw new RuntimeException(e);
}

return perms;
}

public static Map<String, String> getLsColorMap(CommandSession session) {
Expand Down
5 changes: 3 additions & 2 deletions groovy/src/main/groovy/org/jline/groovy/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package org.jline.groovy
import org.codehaus.groovy.runtime.HandleMetaClass
import org.codehaus.groovy.runtime.typehandling.GroovyCastException

import java.nio.file.Files
import java.nio.file.Path
import org.jline.script.GroovyEngine.Format
import groovy.json.JsonOutput
Expand Down Expand Up @@ -62,9 +63,9 @@ class Utils {

static void persist(Path file, Object object, Format format) {
if (format == Format.JSON) {
file.toFile().write(JsonOutput.toJson(object))
Files.writeString(file, JsonOutput.toJson(object))
} else if (format == Format.NONE) {
file.toFile().write(toString(object))
Files.writeString(file, toString(object))
} else {
throw new IllegalArgumentException()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public void purge() throws IOException {
public void write(Path file, boolean incremental) throws IOException {
Path path = file != null ? file : getPath();
if (path != null && Files.exists(path)) {
path.toFile().delete();
Files.deleteIfExists(path);
}
internalWrite(path, incremental ? getLastLoaded(path) : 0);
}
Expand Down

0 comments on commit 57030d4

Please sign in to comment.