Skip to content

Commit 0456215

Browse files
author
shabtaisharon
committed
Merge pull request #144 from rpmoore/sym_link
Added logic to the FileObjectPutter to make it aware of symlink files…
2 parents 9f46fdb + 61f430b commit 0456215

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

ds3-sdk/src/main/java/com/spectralogic/ds3client/helpers/FileObjectPutter.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.nio.channels.FileChannel;
2222
import java.nio.channels.SeekableByteChannel;
23+
import java.nio.file.Files;
2324
import java.nio.file.Path;
2425
import java.nio.file.StandardOpenOption;
2526

@@ -39,6 +40,16 @@ public FileObjectPutter(final Path root) {
3940

4041
@Override
4142
public SeekableByteChannel buildChannel(final String key) throws IOException {
42-
return FileChannel.open(this.root.resolve(key), StandardOpenOption.READ);
43+
44+
final Path path = this.root.resolve(key);
45+
46+
return FileChannel.open(resolveForSymbolic(path), StandardOpenOption.READ);
47+
}
48+
49+
private static Path resolveForSymbolic(final Path path) throws IOException {
50+
if (Files.isSymbolicLink(path)) {
51+
return Files.readSymbolicLink(path);
52+
}
53+
return path;
4354
}
4455
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.spectralogic.ds3client.helpers;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
import java.nio.ByteBuffer;
7+
import java.nio.channels.SeekableByteChannel;
8+
import java.nio.charset.Charset;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.StandardOpenOption;
12+
13+
import static org.hamcrest.CoreMatchers.is;
14+
import static org.hamcrest.CoreMatchers.notNullValue;
15+
import static org.junit.Assert.assertThat;
16+
17+
public class FileObjectPutter_Test {
18+
19+
final private static String testString = "This is some test data.";
20+
final private static byte[] testData = testString.getBytes(Charset.forName("UTF-8"));
21+
22+
@Test
23+
public void testSymlink() throws IOException {
24+
final Path tempDir = Files.createTempDirectory("ds3_file_object_putter_");
25+
final Path tempPath = Files.createTempFile(tempDir, "temp_", ".txt");
26+
27+
try {
28+
try (final SeekableByteChannel channel = Files.newByteChannel(tempPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
29+
channel.write(ByteBuffer.wrap(testData));
30+
}
31+
32+
final Path symLinkPath = tempDir.resolve("sym_" + tempPath.getFileName().toString());
33+
Files.createSymbolicLink(symLinkPath, tempPath);
34+
35+
try {
36+
final FileObjectPutter putter = new FileObjectPutter(tempDir);
37+
38+
final SeekableByteChannel newChannel = putter.buildChannel(symLinkPath.getFileName().toString());
39+
assertThat(newChannel, is(notNullValue()));
40+
41+
final ByteBuffer buff = ByteBuffer.allocate(testData.length);
42+
assertThat(newChannel.read(buff), is(testData.length));
43+
44+
assertThat(new String(buff.array(), Charset.forName("UTF-8")), is(testString));
45+
} finally {
46+
Files.deleteIfExists(symLinkPath);
47+
}
48+
} finally {
49+
Files.deleteIfExists(tempPath);
50+
Files.deleteIfExists(tempDir);
51+
}
52+
}
53+
54+
@Test
55+
public void testRegularFile() throws IOException {
56+
final Path tempDir = Files.createTempDirectory("ds3_file_object_putter_");
57+
final Path tempPath = Files.createTempFile(tempDir, "temp_", ".txt");
58+
59+
try {
60+
try (final SeekableByteChannel channel = Files.newByteChannel(tempPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
61+
channel.write(ByteBuffer.wrap(testData));
62+
}
63+
final FileObjectPutter putter = new FileObjectPutter(tempDir);
64+
65+
final SeekableByteChannel newChannel = putter.buildChannel(tempPath.getFileName().toString());
66+
assertThat(newChannel, is(notNullValue()));
67+
68+
final ByteBuffer buff = ByteBuffer.allocate(testData.length);
69+
assertThat(newChannel.read(buff), is(testData.length));
70+
71+
assertThat(new String(buff.array(), Charset.forName("UTF-8")), is(testString));
72+
73+
} finally {
74+
Files.deleteIfExists(tempPath);
75+
Files.deleteIfExists(tempDir);
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)