use of java.nio.channels.SeekableByteChannel in project elasticsearch by elastic.
the class SharedClusterSnapshotRestoreIT method testListCorruptedSnapshot.
public void testListCorruptedSnapshot() throws Exception {
Client client = client();
Path repo = randomRepoPath();
logger.info("--> creating repository at {}", repo.toAbsolutePath());
assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", repo).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
createIndex("test-idx-1", "test-idx-2", "test-idx-3");
logger.info("--> indexing some data");
indexRandom(true, client().prepareIndex("test-idx-1", "doc").setSource("foo", "bar"), client().prepareIndex("test-idx-2", "doc").setSource("foo", "bar"), client().prepareIndex("test-idx-3", "doc").setSource("foo", "bar"));
logger.info("--> creating 2 snapshots");
CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-1").setWaitForCompletion(true).setIndices("test-idx-*").get();
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-2").setWaitForCompletion(true).setIndices("test-idx-*").get();
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
logger.info("--> truncate snapshot file to make it unreadable");
Path snapshotPath = repo.resolve("snap-" + createSnapshotResponse.getSnapshotInfo().snapshotId().getUUID() + ".dat");
try (SeekableByteChannel outChan = Files.newByteChannel(snapshotPath, StandardOpenOption.WRITE)) {
outChan.truncate(randomInt(10));
}
logger.info("--> get snapshots request should return both snapshots");
List<SnapshotInfo> snapshotInfos = client.admin().cluster().prepareGetSnapshots("test-repo").setIgnoreUnavailable(true).get().getSnapshots();
assertThat(snapshotInfos.size(), equalTo(1));
assertThat(snapshotInfos.get(0).state(), equalTo(SnapshotState.SUCCESS));
assertThat(snapshotInfos.get(0).snapshotId().getName(), equalTo("test-snap-1"));
try {
client.admin().cluster().prepareGetSnapshots("test-repo").setIgnoreUnavailable(false).get().getSnapshots();
} catch (SnapshotException ex) {
assertThat(ex.getRepositoryName(), equalTo("test-repo"));
assertThat(ex.getSnapshotName(), equalTo("test-snap-2"));
}
}
use of java.nio.channels.SeekableByteChannel in project google-cloud-java by GoogleCloudPlatform.
the class CloudStorageFileSystemProviderTest method testWriteOnClose.
@Test
public void testWriteOnClose() throws Exception {
Path path = Paths.get(URI.create("gs://greenbean/adipose"));
try (SeekableByteChannel chan = Files.newByteChannel(path, WRITE)) {
// writing lots of contents to defeat channel-internal buffering.
for (int i = 0; i < 9999; i++) {
for (String s : FILE_CONTENTS) {
chan.write(ByteBuffer.wrap(s.getBytes(UTF_8)));
}
}
try {
Files.size(path);
// we shouldn't make it to this line. Not using thrown.expect because
// I still want to run a few lines after the exception.
assertThat(false).isTrue();
} catch (NoSuchFileException nsf) {
// that's what we wanted, we're good.
}
}
// channel now closed, the file should be there and with the new contents.
assertThat(Files.exists(path)).isTrue();
assertThat(Files.size(path)).isGreaterThan(100L);
}
use of java.nio.channels.SeekableByteChannel in project jdk8u_jdk by JetBrains.
the class CheckPermissions method main.
public static void main(String[] args) throws IOException {
final Path testdir = Paths.get(System.getProperty("test.dir", ".")).toAbsolutePath();
final Path tmpdir = Paths.get(System.getProperty("java.io.tmpdir"));
Path file = createFile(testdir.resolve("file1234"));
try {
LoggingSecurityManager.install();
// -- check access --
prepare();
exists(file);
assertCheckRead(file);
prepare();
isReadable(file);
assertCheckRead(file);
prepare();
isWritable(file);
assertCheckWrite(file);
prepare();
isExecutable(file);
assertCheckExec(file);
// -- copy --
Path target = testdir.resolve("target1234");
prepare();
copy(file, target);
try {
assertCheckRead(file);
assertCheckWrite(target);
} finally {
delete(target);
}
if (TestUtil.supportsLinks(testdir)) {
Path link = testdir.resolve("link1234");
createSymbolicLink(link, file);
try {
prepare();
copy(link, target, LinkOption.NOFOLLOW_LINKS);
try {
assertCheckRead(link);
assertCheckWrite(target);
assertCheckPermission(new LinkPermission("symbolic"));
} finally {
delete(target);
}
prepare();
readSymbolicLink(link);
assertCheckPermission(new FilePermission(link.toString(), "readlink"));
} finally {
delete(link);
}
}
// -- createDirectory --
Path subdir = testdir.resolve("subdir1234");
prepare();
createDirectory(subdir);
try {
assertCheckWrite(subdir);
} finally {
delete(subdir);
}
// -- createFile --
Path fileToCreate = testdir.resolve("file7890");
prepare();
createFile(fileToCreate);
try {
assertCheckWrite(fileToCreate);
} finally {
delete(fileToCreate);
}
if (TestUtil.supportsLinks(testdir)) {
prepare();
Path link = testdir.resolve("link1234");
createSymbolicLink(link, file);
try {
assertCheckWrite(link);
assertCheckPermission(new LinkPermission("symbolic"));
} finally {
delete(link);
}
}
if (TestUtil.supportsLinks(testdir)) {
prepare();
Path link = testdir.resolve("entry234");
createLink(link, file);
try {
assertCheckWrite(link);
assertCheckPermission(new LinkPermission("hard"));
} finally {
delete(link);
}
}
// -- createTempFile --
prepare();
Path tmpfile1 = createTempFile("foo", null);
try {
assertCheckWriteToDirectory(tmpdir);
} finally {
delete(tmpfile1);
}
prepare();
Path tmpfile2 = createTempFile(testdir, "foo", ".tmp");
try {
assertCheckWriteToDirectory(testdir);
} finally {
delete(tmpfile2);
}
// -- createTempDirectory --
prepare();
Path tmpdir1 = createTempDirectory("foo");
try {
assertCheckWriteToDirectory(tmpdir);
} finally {
delete(tmpdir1);
}
prepare();
Path tmpdir2 = createTempDirectory(testdir, "foo");
try {
assertCheckWriteToDirectory(testdir);
} finally {
delete(tmpdir2);
}
// -- delete/deleteIfExists --
Path fileToDelete = testdir.resolve("file7890");
createFile(fileToDelete);
prepare();
delete(fileToDelete);
assertCheckDelete(fileToDelete);
createFile(fileToDelete);
prepare();
// file exists
deleteIfExists(fileToDelete);
assertCheckDelete(fileToDelete);
prepare();
// file does not exist
deleteIfExists(fileToDelete);
assertCheckDelete(fileToDelete);
// -- exists/notExists --
prepare();
exists(file);
assertCheckRead(file);
prepare();
notExists(file);
assertCheckRead(file);
// -- getFileStore --
prepare();
getFileStore(file);
assertCheckRead(file);
assertCheckPermission(new RuntimePermission("getFileStoreAttributes"));
// -- isSameFile --
prepare();
isSameFile(file, testdir);
assertCheckRead(file);
assertCheckRead(testdir);
// -- move --
Path target2 = testdir.resolve("target1234");
prepare();
move(file, target2);
try {
assertCheckWrite(file);
assertCheckWrite(target2);
} finally {
// restore file
move(target2, file);
}
// -- newByteChannel --
prepare();
try (SeekableByteChannel sbc = newByteChannel(file)) {
assertCheckRead(file);
}
prepare();
try (SeekableByteChannel sbc = newByteChannel(file, WRITE)) {
assertCheckWrite(file);
}
prepare();
try (SeekableByteChannel sbc = newByteChannel(file, READ, WRITE)) {
assertCheckRead(file);
assertCheckWrite(file);
}
prepare();
try (SeekableByteChannel sbc = newByteChannel(file, DELETE_ON_CLOSE)) {
assertCheckRead(file);
assertCheckDelete(file);
}
// restore file
createFile(file);
// -- newInputStream/newOutptuStream --
prepare();
try (InputStream in = newInputStream(file)) {
assertCheckRead(file);
}
prepare();
try (OutputStream out = newOutputStream(file)) {
assertCheckWrite(file);
}
// -- newDirectoryStream --
prepare();
try (DirectoryStream<Path> stream = newDirectoryStream(testdir)) {
assertCheckRead(testdir);
if (stream instanceof SecureDirectoryStream<?>) {
Path entry;
SecureDirectoryStream<Path> sds = (SecureDirectoryStream<Path>) stream;
// newByteChannel
entry = file.getFileName();
prepare();
try (SeekableByteChannel sbc = sds.newByteChannel(entry, EnumSet.of(READ))) {
assertCheckRead(file);
}
prepare();
try (SeekableByteChannel sbc = sds.newByteChannel(entry, EnumSet.of(WRITE))) {
assertCheckWrite(file);
}
// deleteFile
entry = file.getFileName();
prepare();
sds.deleteFile(entry);
assertCheckDelete(file);
// restore file
createFile(testdir.resolve(entry));
// deleteDirectory
entry = Paths.get("subdir1234");
createDirectory(testdir.resolve(entry));
prepare();
sds.deleteDirectory(entry);
assertCheckDelete(testdir.resolve(entry));
// move
entry = Paths.get("tempname1234");
prepare();
sds.move(file.getFileName(), sds, entry);
assertCheckWrite(file);
assertCheckWrite(testdir.resolve(entry));
// restore file
sds.move(entry, sds, file.getFileName());
// newDirectoryStream
entry = Paths.get("subdir1234");
createDirectory(testdir.resolve(entry));
try {
prepare();
sds.newDirectoryStream(entry).close();
assertCheckRead(testdir.resolve(entry));
} finally {
delete(testdir.resolve(entry));
}
// getFileAttributeView to access attributes of directory
testBasicFileAttributeView(sds.getFileAttributeView(BasicFileAttributeView.class), testdir);
testPosixFileAttributeView(sds.getFileAttributeView(PosixFileAttributeView.class), testdir);
// getFileAttributeView to access attributes of entry
entry = file.getFileName();
testBasicFileAttributeView(sds.getFileAttributeView(entry, BasicFileAttributeView.class), file);
testPosixFileAttributeView(sds.getFileAttributeView(entry, PosixFileAttributeView.class), file);
} else {
System.out.println("SecureDirectoryStream not tested");
}
}
// -- toAbsolutePath --
prepare();
file.getFileName().toAbsolutePath();
assertCheckPropertyAccess("user.dir");
// -- toRealPath --
prepare();
file.toRealPath();
assertCheckRead(file);
prepare();
file.toRealPath(LinkOption.NOFOLLOW_LINKS);
assertCheckRead(file);
prepare();
Paths.get(".").toRealPath();
assertCheckPropertyAccess("user.dir");
prepare();
Paths.get(".").toRealPath(LinkOption.NOFOLLOW_LINKS);
assertCheckPropertyAccess("user.dir");
try (WatchService watcher = FileSystems.getDefault().newWatchService()) {
prepare();
testdir.register(watcher, StandardWatchEventKinds.ENTRY_DELETE);
assertCheckRead(testdir);
}
// -- getAttribute/setAttribute/readAttributes --
prepare();
getAttribute(file, "size");
assertCheckRead(file);
prepare();
setAttribute(file, "lastModifiedTime", FileTime.fromMillis(System.currentTimeMillis()));
assertCheckWrite(file);
prepare();
readAttributes(file, "*");
assertCheckRead(file);
// -- BasicFileAttributeView --
testBasicFileAttributeView(getFileAttributeView(file, BasicFileAttributeView.class), file);
// -- PosixFileAttributeView --
{
PosixFileAttributeView view = getFileAttributeView(file, PosixFileAttributeView.class);
if (view != null && getFileStore(file).supportsFileAttributeView(PosixFileAttributeView.class)) {
testPosixFileAttributeView(view, file);
} else {
System.out.println("PosixFileAttributeView not tested");
}
}
// -- DosFileAttributeView --
{
DosFileAttributeView view = getFileAttributeView(file, DosFileAttributeView.class);
if (view != null && getFileStore(file).supportsFileAttributeView(DosFileAttributeView.class)) {
prepare();
view.readAttributes();
assertCheckRead(file);
prepare();
view.setArchive(false);
assertCheckWrite(file);
prepare();
view.setHidden(false);
assertCheckWrite(file);
prepare();
view.setReadOnly(false);
assertCheckWrite(file);
prepare();
view.setSystem(false);
assertCheckWrite(file);
} else {
System.out.println("DosFileAttributeView not tested");
}
}
// -- FileOwnerAttributeView --
{
FileOwnerAttributeView view = getFileAttributeView(file, FileOwnerAttributeView.class);
if (view != null && getFileStore(file).supportsFileAttributeView(FileOwnerAttributeView.class)) {
prepare();
UserPrincipal owner = view.getOwner();
assertCheckRead(file);
assertCheckPermission(new RuntimePermission("accessUserInformation"));
prepare();
view.setOwner(owner);
assertCheckWrite(file);
assertCheckPermission(new RuntimePermission("accessUserInformation"));
} else {
System.out.println("FileOwnerAttributeView not tested");
}
}
// -- UserDefinedFileAttributeView --
{
UserDefinedFileAttributeView view = getFileAttributeView(file, UserDefinedFileAttributeView.class);
if (view != null && getFileStore(file).supportsFileAttributeView(UserDefinedFileAttributeView.class)) {
prepare();
view.write("test", ByteBuffer.wrap(new byte[100]));
assertCheckWrite(file);
assertCheckPermission(new RuntimePermission("accessUserDefinedAttributes"));
prepare();
view.read("test", ByteBuffer.allocate(100));
assertCheckRead(file);
assertCheckPermission(new RuntimePermission("accessUserDefinedAttributes"));
prepare();
view.size("test");
assertCheckRead(file);
assertCheckPermission(new RuntimePermission("accessUserDefinedAttributes"));
prepare();
view.list();
assertCheckRead(file);
assertCheckPermission(new RuntimePermission("accessUserDefinedAttributes"));
prepare();
view.delete("test");
assertCheckWrite(file);
assertCheckPermission(new RuntimePermission("accessUserDefinedAttributes"));
} else {
System.out.println("UserDefinedFileAttributeView not tested");
}
}
// -- AclFileAttributeView --
{
AclFileAttributeView view = getFileAttributeView(file, AclFileAttributeView.class);
if (view != null && getFileStore(file).supportsFileAttributeView(AclFileAttributeView.class)) {
prepare();
List<AclEntry> acl = view.getAcl();
assertCheckRead(file);
assertCheckPermission(new RuntimePermission("accessUserInformation"));
prepare();
view.setAcl(acl);
assertCheckWrite(file);
assertCheckPermission(new RuntimePermission("accessUserInformation"));
} else {
System.out.println("AclFileAttributeView not tested");
}
}
// -- UserPrincipalLookupService
UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
UserPrincipal owner = getOwner(file);
prepare();
lookupService.lookupPrincipalByName(owner.getName());
assertCheckPermission(new RuntimePermission("lookupUserInformation"));
try {
UserPrincipal group = readAttributes(file, PosixFileAttributes.class).group();
prepare();
lookupService.lookupPrincipalByGroupName(group.getName());
assertCheckPermission(new RuntimePermission("lookupUserInformation"));
} catch (UnsupportedOperationException ignore) {
System.out.println("lookupPrincipalByGroupName not tested");
}
} finally {
deleteIfExists(file);
}
}
use of java.nio.channels.SeekableByteChannel in project google-cloud-java by GoogleCloudPlatform.
the class ITGcsNio method testWrite.
@Test
public void testWrite() throws IOException {
CloudStorageFileSystem testBucket = getTestBucket();
Path path = testBucket.getPath(PREFIX + randomSuffix());
// file shouldn't exist initially. If it does it's either because it's a leftover
// from a previous run (so we should delete the file)
// or because we're misconfigured and pointing to an actually important file
// (so we should absolutely not delete it).
// So if the file's here, don't try to fix it automatically, let the user deal with it.
assertThat(Files.exists(path)).isFalse();
try {
Files.write(path, FILE_CONTENTS, UTF_8);
// now it does.
assertThat(Files.exists(path)).isTrue();
// let's check that the contents is OK.
ByteArrayOutputStream wantBytes = new ByteArrayOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(wantBytes, UTF_8));
for (String content : FILE_CONTENTS) {
writer.println(content);
}
writer.close();
SeekableByteChannel chan = Files.newByteChannel(path, StandardOpenOption.READ);
byte[] gotBytes = new byte[(int) chan.size()];
readFully(chan, gotBytes);
assertThat(gotBytes).isEqualTo(wantBytes.toByteArray());
} finally {
// let's not leave files around
Files.deleteIfExists(path);
}
}
use of java.nio.channels.SeekableByteChannel in project google-cloud-java by GoogleCloudPlatform.
the class ITGcsNio method testSeek.
@Test
public void testSeek() throws IOException {
CloudStorageFileSystem testBucket = getTestBucket();
Path path = testBucket.getPath(BIG_FILE);
int size = BIG_SIZE;
byte[] contents = randomContents(size);
byte[] sample = new byte[100];
byte[] wanted;
byte[] wanted2;
SeekableByteChannel chan = Files.newByteChannel(path, StandardOpenOption.READ);
assertThat(chan.size()).isEqualTo(size);
// check seek
int dest = size / 2;
chan.position(dest);
readFully(chan, sample);
wanted = Arrays.copyOfRange(contents, dest, dest + 100);
assertThat(wanted).isEqualTo(sample);
// now go back and check the beginning
// (we do 2 locations because 0 is sometimes a special case).
chan.position(0);
readFully(chan, sample);
wanted2 = Arrays.copyOf(contents, 100);
assertThat(wanted2).isEqualTo(sample);
// if the two spots in the file have the same contents, then this isn't a good file for this
// test.
assertThat(wanted).isNotEqualTo(wanted2);
}
Aggregations