Skip to content

Commit

Permalink
put the symbolic link name instead of the real name
Browse files Browse the repository at this point in the history
follow the symbolic links in depth.
  • Loading branch information
brahimibob committed Oct 23, 2017
1 parent dd1574b commit 662fcfe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
class EurostagDDB {

private final Map<String, Path> generators = new HashMap<>();

private static final Logger LOGGER = LoggerFactory.getLogger(EurostagDDB.class);
EurostagDDB(List<Path> ddbDirs) throws IOException {
for (Path ddbDir : ddbDirs) {
if (Files.isSymbolicLink(ddbDir)) {
Expand All @@ -37,15 +40,23 @@ class EurostagDDB {
Files.walkFileTree(ddbDir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (Files.isSymbolicLink(file)) {
file = Files.readSymbolicLink(file);
if (Files.isDirectory(file)) {
Files.walkFileTree(file, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, this);
String fileName = file.getFileName().toString();
Path tmpfile = file;
if (Files.isSymbolicLink(tmpfile)) {
tmpfile = Files.readSymbolicLink(file);
while (Files.isSymbolicLink(tmpfile)) {
tmpfile = Files.readSymbolicLink(tmpfile);
}
if (Files.isDirectory(tmpfile)) {
Files.walkFileTree(tmpfile, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, this);
}
}
if (Files.isRegularFile(file) && file.toString().endsWith(".tg")) {
String fileName = file.getFileName().toString();
generators.put(fileName.substring(0, fileName.length() - 3), file);
if (Files.isRegularFile(tmpfile) && tmpfile.toString().endsWith(".tg")) {
String key = fileName.substring(0, fileName.length() - 3);
if (generators.containsKey(key)) {
LOGGER.error("the processing has detected that the file " + fileName + " is present in " + tmpfile + " and " + generators.get(key));
}
generators.put(fileName.substring(0, fileName.length() - 3), tmpfile);
}
return super.visitFile(file, attrs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,29 @@ public void testEurostagDDB() throws IOException, URISyntaxException {
Files.createFile(file5);
Files.createDirectory(path3);
Files.createFile(p3file);
Files.createFile(fs.getPath("/path1/p3file.tg"));
Path ln2 = Files.createSymbolicLink(fs.getPath("/path2/ln2.tg"), file5);
Files.createSymbolicLink(fs.getPath("/path1/ln1.tg"), ln2);
List<Path> ddbDirs = new ArrayList<>();
ddbDirs.add(path1);
EurostagDDB eurostagDDB = new EurostagDDB(ddbDirs);
assertEquals(eurostagDDB.findGenerator("").getFileName().toString(), ".tg");
assertEquals(eurostagDDB.findGenerator("p3file").getFileName().toString(), "p3file.tg");
assertEquals(eurostagDDB.findGenerator("file4").getFileName().toString(), "file4.tg");
assertEquals(eurostagDDB.findGenerator("file1").getFileName().toString(), "file1.tg");
assertEquals(eurostagDDB.findGenerator("file5").getFileName().toString(), "file5.tg");
assertEquals(eurostagDDB.findGenerator("ln").getFileName().toString(), "file5.tg");
assertEquals(eurostagDDB.findGenerator("ln1").getFileName().toString(), "file5.tg");
}
}

@Test
public void testFindGenerator() throws IOException {
try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
Path path2 = fs.getPath("/path2");
Path filereal = fs.getPath("/path2/file5.tg");
Path file5 = fs.getPath("/path2/file5.tg");
Path path = fs.getPath("lnpath2");
Files.createDirectory(path2);
Files.createFile(filereal);
Files.createFile(file5);
Files.createSymbolicLink(path, path2);
List<Path> ddbDirs = new ArrayList<>();
ddbDirs.add(path);
Expand Down

0 comments on commit 662fcfe

Please sign in to comment.