Skip to content

Commit d7bbc79

Browse files
committed
Correctly handle symlinks transparently
1 parent 274d04b commit d7bbc79

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResource.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,10 @@ public boolean isSymbolicLink()
227227
return getAttributes().isSymbolicLink();
228228
}
229229

230+
protected DeferredFileOutputStream getDfos()
231+
{
232+
return dfos;
233+
}
234+
230235
private static final InputStreamTransformer identityTransformer = AbstractPlexusIoResourceCollection.identityTransformer;
231236
}
Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,94 @@
11
package org.codehaus.plexus.components.io.resources;
22

3+
import org.apache.commons.io.output.DeferredFileOutputStream;
34
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
45
import org.codehaus.plexus.components.io.attributes.SymlinkUtils;
56
import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier;
67

78
import javax.annotation.Nonnull;
89
import java.io.File;
910
import java.io.IOException;
11+
import java.nio.file.Path;
1012

1113
public class PlexusIoSymlinkResource
1214
extends PlexusIoFileResource
1315
implements SymlinkDestinationSupplier
1416
{
15-
private final File symnlinkDestination;
17+
private final String symLinkDestination;
18+
private final PlexusIoResource targetResource;
1619

1720
PlexusIoSymlinkResource( @Nonnull File symlinkfile, String name, @Nonnull PlexusIoResourceAttributes attrs )
1821
throws IOException
1922
{
2023
super( symlinkfile, name, attrs );
21-
this.symnlinkDestination = null;
24+
Path path = symlinkfile.toPath();
25+
Path linkPath = java.nio.file.Files.readSymbolicLink( path );
26+
symLinkDestination = linkPath.toString();
27+
targetResource = ResourceFactory.createResource( path.resolveSibling( linkPath ).toFile() );
2228
}
2329

2430
public String getSymlinkDestination()
2531
throws IOException
2632
{
27-
return symnlinkDestination == null ? SymlinkUtils.readSymbolicLink( getFile() ).getPath() : symnlinkDestination.getPath();
33+
return targetResource.getName();
34+
}
35+
36+
public PlexusIoResource getTarget()
37+
{
38+
return targetResource;
39+
}
40+
41+
public PlexusIoResource getLink() throws IOException
42+
{
43+
return new PlexusIoFileResource( getFile(), getName(), getAttributes() );
44+
}
45+
46+
@Override
47+
public long getSize()
48+
{
49+
DeferredFileOutputStream dfos = getDfos();
50+
if ( dfos == null )
51+
{
52+
return targetResource.getSize();
53+
}
54+
else if ( dfos.isInMemory() )
55+
{
56+
return dfos.getByteCount();
57+
}
58+
else
59+
{
60+
return dfos.getFile().length();
61+
}
62+
}
63+
64+
@Override
65+
public boolean isDirectory()
66+
{
67+
return targetResource.isDirectory();
68+
}
69+
70+
@Override
71+
public boolean isExisting()
72+
{
73+
return targetResource.isExisting();
74+
}
75+
76+
@Override
77+
public boolean isFile()
78+
{
79+
return targetResource.isFile();
80+
}
81+
82+
@Override
83+
public long getLastModified()
84+
{
85+
return targetResource.getLastModified();
86+
}
87+
88+
@Nonnull
89+
@Override
90+
public PlexusIoResourceAttributes getAttributes()
91+
{
92+
return super.getAttributes();
2893
}
2994
}

src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoPlexusIoFileResourceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void testRealSymlink()
2121
final File file = new File( "src/test/resources/symlinks/src/symDir" );
2222
PlexusIoResourceAttributes attrs = FileAttributes.uncached( file );
2323
assertTrue( attrs.isSymbolicLink() );
24-
PlexusIoFileResource r = new PlexusIoFileResource( file, "symDir", attrs );
24+
PlexusIoResource r = ResourceFactory.createResource( file );
2525
assertTrue( r.isSymbolicLink() );
2626
assertTrue( r.isDirectory() );
2727
final File target = SymlinkUtils.readSymbolicLink( file );

0 commit comments

Comments
 (0)