Skip to content

Commit

Permalink
Fix an exception thrown when creating a temporary file on Windows pla…
Browse files Browse the repository at this point in the history
…tform using FileStorage API
  • Loading branch information
besidev committed Oct 3, 2024
1 parent 568b12d commit 258bdf8
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions jpro-file/src/main/java/one/jpro/platform/file/FileStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -23,17 +24,29 @@
*/
public final class FileStorage {

static final FileSystem DEFAULT_FILE_SYSTEM = FileSystems.getDefault();

/**
* Determines if the default file system supports POSIX file attribute views.
*/
private static final boolean isPosix = FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
static final boolean isPosix = DEFAULT_FILE_SYSTEM.supportedFileAttributeViews().contains("posix");

/**
* The directory path to store temporary files. Defaults to a 'tmp' directory within a '.jpro' directory
* in the user's home directory.
*/
public static final Path JPRO_TMP_DIR = Path.of(System.getProperty("user.home"), ".jpro", "tmp");

static {
try {
if (Files.notExists(JPRO_TMP_DIR)) {
Files.createDirectories(JPRO_TMP_DIR);
}
} catch (IOException e) {
throw new ExceptionInInitializerError("Failed to create JPRO temporary directory: " + e.getMessage());
}
}

/**
* Inner class to hold the default POSIX file and directory permissions.
* Permissions are OWNER_READ and OWNER_WRITE.
Expand All @@ -58,11 +71,25 @@ private static Path generatePath(String fileName, String fileType, Path dir) {
int lastIndexOf = fileName.lastIndexOf('.');
if (lastIndexOf > 0) {
s = fileName.substring(0, lastIndexOf) + fileType;
} else if (lastIndexOf == -1) {
s = fileName + fileType;
}

// Create the directories if they don't exist
if (Files.notExists(dir)) {
try {
Files.createDirectories(dir);
} catch (IOException ex) {
throw new IllegalArgumentException("Invalid directory path: " + dir, ex);
}
}

Path name = dir.getFileSystem().getPath(s);
// the generated name should be a simple file name
if (name.getParent() != null)
throw new IllegalArgumentException("Invalid fileName or fileType");
// Ensure the generated name is a simple file name
if (name.getParent() != null) {
throw new IllegalArgumentException("Invalid fileName: " + s + ", it should not contain directory separators.");
}

return dir.resolve(name);
}

Expand All @@ -85,13 +112,17 @@ public static Path createTempFile(@Nullable Path dir,
fileType = Objects.requireNonNullElse(fileType, ".tmp");

FileAttribute<?>[] attrs = null;
if (isPosix && (dir.getFileSystem() == FileSystems.getDefault())) {
if (isPosix && (dir.getFileSystem() == DEFAULT_FILE_SYSTEM)) {
attrs = new FileAttribute<?>[1];
attrs[0] = PosixPermissions.filePermissions;
}

Path path = generatePath(fileName, fileType, dir);
return Files.createFile(path, attrs);
final Path path = generatePath(fileName, fileType, dir);
if (Files.exists(path)) {
return path;
} else {
return (attrs != null) ? Files.createFile(path, attrs) : Files.createFile(path);
}
}

private FileStorage() {
Expand Down

0 comments on commit 258bdf8

Please sign in to comment.