Skip to content

Commit

Permalink
'#1861 Implementation of FileVisitor for correctly get a PathWrapper
Browse files Browse the repository at this point in the history
reference to the file being tested in isDirectory method.
  • Loading branch information
patrickdalla committed Oct 26, 2023
1 parent b7372c8 commit 8afca07
Showing 1 changed file with 58 additions and 12 deletions.
70 changes: 58 additions & 12 deletions iped-utils/src/main/java/iped/utils/FileInputStreamFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.NotDirectoryException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;

import iped.io.SeekableInputStream;
import iped.utils.fsw.PathWrapper;
Expand All @@ -35,7 +38,7 @@ public Path getPath(String subPath) {
}
if (IS_WINDOWS) {
// workaround for https://github.com/sepinf-inc/IPED/issues/1861
if (isDirectory(file)) {
if (isDirectory(source, file)) {
try {
file = Files.createTempDirectory("iped").toFile();
file.deleteOnExit();
Expand All @@ -57,20 +60,63 @@ public Path getPath(String subPath) {
}
}

boolean isDirectory(File f) {
class FilePathVisitor implements FileVisitor<Path> {
public File fileToFind;
public Path foundPath;

public FilePathVisitor(File fileToFind) {
this.fileToFind = fileToFind;
}

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("Entering:" + dir);
if (!fileToFind.getCanonicalPath().startsWith(dir.toFile().getCanonicalPath())) {
return FileVisitResult.SKIP_SUBTREE;
}
if (dir.toFile().getCanonicalPath().equals(fileToFind.getCanonicalPath())) {
foundPath = dir;
System.out.println("Found:" + dir);
return FileVisitResult.TERMINATE;
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("Visiting:" + file);
if (file.toFile().getCanonicalPath().equals(fileToFind.getCanonicalPath())) {
foundPath = file;
System.out.println("Found:" + file);
return FileVisitResult.TERMINATE;
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
}

boolean isDirectory(Path source, File f) {
String strpath = f.getAbsolutePath();
if (strpath.endsWith(" ")) {
Path parent = new PathWrapper(f.getParentFile().toPath());

Path startingDir = PathWrapper.create(source);
Path path = null;
try (DirectoryStream<Path> ds = Files.newDirectoryStream(parent)) {
for (Path child : ds) {
if (child.getFileName().toString().equals(f.getName())) {
path = child;
break;
}
}
} catch (IOException e1) {
e1.printStackTrace();
try {
FilePathVisitor filePathVisitor = new FilePathVisitor(f);
System.out.println("Finding:" + f.getCanonicalPath());
Files.walkFileTree(startingDir, filePathVisitor);
path = filePathVisitor.foundPath;
} catch (IOException e2) {
e2.printStackTrace();
}

if (path == null) {
Expand Down

0 comments on commit 8afca07

Please sign in to comment.