The prototype of Files.createSymbolicLink()
is:
static Path createSymbolicLink(Path link, Path target,
FileAttribute<?>... attrs)
Well... Why?
A symlink is nothing else than an inode whose content is a string pointing to... Well, whatever. The target can even not be valid at all.
Out of curiosity, I tried this (yes, it runs as root, but this is on purpose here):
root@alustriel:/tmp# cat Test.java
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.*;
public final class Test {
public static void main(final String... args) throws IOException {
final UserPrincipalLookupService lookupService =
FileSystems.getDefault().getUserPrincipalLookupService();
final UserPrincipal user = lookupService.lookupPrincipalByName("fge");
final FileAttribute<UserPrincipal> attr =
new FileAttribute<UserPrincipal>() {
@Override
public String name() {
return "owner:owner";
}
@Override
public UserPrincipal value() {
return user;
}
};
final Path target = Paths.get("whocares, I don't exist anyway");
final Path symlink = Paths.get("/tmp/meh");
Files.createSymbolicLink(symlink, target, attr);
}
}
root@alustriel:/tmp# javac Test.java
root@alustriel:/tmp# java Test
Exception in thread "main" java.lang.UnsupportedOperationException: 'owner:owner' not supported as initial attribute
at sun.nio.fs.UnixFileModeAttribute.toUnixMode(UnixFileModeAttribute.java:74)
at sun.nio.fs.UnixFileSystemProvider.createSymbolicLink(UnixFileSystemProvider.java:440)
at java.nio.file.Files.createSymbolicLink(Files.java:996)
at Test.main(Test.java:41)
Well, I kind of expected that.
The only situation I know where a symlink's permissions matters is in Linux's /proc/*/fd/ since the permissions of the symlink match the permissions with which the file descriptor was opened. But this is a systems generated symlink and is nothing like what the user can generate.
Is there any OS where a symlink's permissions actuall affect the behavior of the symlink and/or its target in any way, shape or form?
edit: well, I could change the owner of a symlink with chown
but... Again, what's the point?
The Oracle docs say that this is just for future use:
On a Mac, permissions seem to be settable, though they don't seem to have much of an effect. I created a file readable only by root, and two symlinks to it: one world-readable, one readable only by root. I could
cat
the file using all three, butls -l
only listed the link target for the world-readable link when I ran it as my user:I couldn't find any other differences in behavior, but it's something, so perhaps you'll be able to use
createSymbolicLink
to create such a symlink in the future.