Skip to content

Commit

Permalink
Fix symlinks handling #45
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Sep 15, 2024
1 parent 18a00f4 commit b14f713
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/main/java/com/cmlteam/serv/TarUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Set;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.archivers.tar.TarConstants;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.io.IOUtils;

Expand Down Expand Up @@ -65,7 +66,13 @@ private static void addToArchiveCompression(
}

String entry = dir + File.separator + file.getName();
if (file.isFile()) {
if (Files.isSymbolicLink(file.toPath())) {
TarArchiveEntry archiveEntry = new TarArchiveEntry(entry, TarConstants.LF_SYMLINK);
archiveEntry.setLinkName(Files.readSymbolicLink(file.toPath()).toString());
archiveEntry.setMode(calcPermissions(file));
out.putArchiveEntry(archiveEntry);
out.closeArchiveEntry();
} else if (file.isFile()) {
TarArchiveEntry archiveEntry = new TarArchiveEntry(file, entry);
archiveEntry.setMode(calcPermissions(file));
out.putArchiveEntry(archiveEntry);
Expand All @@ -81,7 +88,8 @@ private static void addToArchiveCompression(
}
}
} else {
System.err.println("warning: not supported (symlink?): " + file);
// TODO this could be due to absent permission
System.err.println("warning: not supported: " + file);
}
}

Expand Down
18 changes: 13 additions & 5 deletions src/test/java/com/cmlteam/serv/SharingTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Pattern;

import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -275,7 +273,7 @@ void testHandleSymlinks_issue45(@TempDir Path tempDir) throws IOException {
Files.createSymbolicLink(symLink, Path.of("./",fname1));

assertEquals(0, exec("ls", "-l", inputFolder.toFile().getAbsolutePath())); // show symlink


serv = new Serv("-p", testPort, inputFolder.toFile().getAbsolutePath());

Expand All @@ -291,16 +289,26 @@ void testHandleSymlinks_issue45(@TempDir Path tempDir) throws IOException {
// THEN
Path resultExtractedFolder = createTestFolder(tempDir, "result_folder");

FileExtractUtils.extractTarOrTgz(resultFile, resultExtractedFolder.toFile());
assertEquals(
0,
exec(
"tar",
"-C",
resultExtractedFolder.toAbsolutePath().toString(),
"-xvf",
resultFile.getAbsolutePath()));

Path file1Res = resultExtractedFolder.resolve(fname1);

assertFilesEqual(file1, file1Res);
System.out.println("tar content:");
assertEquals(0, exec("tar", "-tvf", resultFile.getAbsolutePath())); // show tar content
System.out.println("dir listing:");
assertEquals(0, exec("ls", "-l", resultExtractedFolder.toFile().getAbsolutePath())); // show extracted files

Path symLinkRes = resultExtractedFolder.resolve(symLinkName);

assertTrue(Files.isSymbolicLink(symLinkRes));
assertEquals(file1Res.toAbsolutePath(), Files.readSymbolicLink(symLinkRes).toAbsolutePath());
assertTrue(Files.isSameFile(file1Res, resultExtractedFolder.resolve(Files.readSymbolicLink(symLinkRes))));
}
}

0 comments on commit b14f713

Please sign in to comment.