|
1 | 1 | package org.codehaus.plexus.components.io.resources;
|
2 | 2 |
|
| 3 | +import org.apache.commons.io.output.DeferredFileOutputStream; |
3 | 4 | import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
|
4 | 5 | import org.codehaus.plexus.components.io.attributes.SymlinkUtils;
|
5 | 6 | import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier;
|
6 | 7 |
|
7 | 8 | import javax.annotation.Nonnull;
|
8 | 9 | import java.io.File;
|
9 | 10 | import java.io.IOException;
|
| 11 | +import java.nio.file.Path; |
10 | 12 |
|
11 | 13 | public class PlexusIoSymlinkResource
|
12 | 14 | extends PlexusIoFileResource
|
13 | 15 | implements SymlinkDestinationSupplier
|
14 | 16 | {
|
15 |
| - private final File symnlinkDestination; |
| 17 | + private final String symLinkDestination; |
| 18 | + private final PlexusIoResource targetResource; |
16 | 19 |
|
17 | 20 | PlexusIoSymlinkResource( @Nonnull File symlinkfile, String name, @Nonnull PlexusIoResourceAttributes attrs )
|
18 | 21 | throws IOException
|
19 | 22 | {
|
20 | 23 | 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() ); |
22 | 28 | }
|
23 | 29 |
|
24 | 30 | public String getSymlinkDestination()
|
25 | 31 | throws IOException
|
26 | 32 | {
|
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(); |
28 | 93 | }
|
29 | 94 | }
|
0 commit comments