893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage sun.nio.fs;
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport static java.nio.file.StandardOpenOption.*;
893N/Aimport java.nio.ByteBuffer;
893N/Aimport java.nio.channels.FileChannel;
893N/Aimport java.io.IOException;
893N/Aimport java.util.*;
893N/Aimport sun.misc.Unsafe;
893N/A
893N/Aimport static sun.nio.fs.WindowsNativeDispatcher.*;
893N/Aimport static sun.nio.fs.WindowsConstants.*;
893N/A
893N/A/**
893N/A * Windows emulation of NamedAttributeView using Alternative Data Streams
893N/A */
893N/A
893N/Aclass WindowsUserDefinedFileAttributeView
893N/A extends AbstractUserDefinedFileAttributeView
893N/A{
893N/A private static final Unsafe unsafe = Unsafe.getUnsafe();
893N/A
893N/A // syntax to address named streams
893N/A private String join(String file, String name) {
893N/A if (name == null)
893N/A throw new NullPointerException("'name' is null");
893N/A return file + ":" + name;
893N/A }
893N/A private String join(WindowsPath file, String name) throws WindowsException {
893N/A return join(file.getPathForWin32Calls(), name);
893N/A }
893N/A
893N/A private final WindowsPath file;
893N/A private final boolean followLinks;
893N/A
893N/A WindowsUserDefinedFileAttributeView(WindowsPath file, boolean followLinks) {
893N/A this.file = file;
893N/A this.followLinks = followLinks;
893N/A }
893N/A
893N/A // enumerates the file streams using FindFirstStream/FindNextStream APIs.
893N/A private List<String> listUsingStreamEnumeration() throws IOException {
3471N/A List<String> list = new ArrayList<>();
893N/A try {
893N/A FirstStream first = FindFirstStream(file.getPathForWin32Calls());
893N/A if (first != null) {
893N/A long handle = first.handle();
893N/A try {
893N/A // first stream is always ::$DATA for files
893N/A String name = first.name();
893N/A if (!name.equals("::$DATA")) {
893N/A String[] segs = name.split(":");
893N/A list.add(segs[1]);
893N/A }
893N/A while ((name = FindNextStream(handle)) != null) {
893N/A String[] segs = name.split(":");
893N/A list.add(segs[1]);
893N/A }
893N/A } finally {
893N/A FindClose(handle);
893N/A }
893N/A }
893N/A } catch (WindowsException x) {
893N/A x.rethrowAsIOException(file);
893N/A }
893N/A return Collections.unmodifiableList(list);
893N/A }
893N/A
893N/A // enumerates the file streams by reading the stream headers using
893N/A // BackupRead
893N/A private List<String> listUsingBackupRead() throws IOException {
893N/A long handle = -1L;
893N/A try {
893N/A int flags = FILE_FLAG_BACKUP_SEMANTICS;
893N/A if (!followLinks && file.getFileSystem().supportsLinks())
893N/A flags |= FILE_FLAG_OPEN_REPARSE_POINT;
893N/A
893N/A handle = CreateFile(file.getPathForWin32Calls(),
893N/A GENERIC_READ,
893N/A FILE_SHARE_READ, // no write as we depend on file size
893N/A OPEN_EXISTING,
893N/A flags);
893N/A } catch (WindowsException x) {
893N/A x.rethrowAsIOException(file);
893N/A }
893N/A
893N/A // buffer to read stream header and stream name.
893N/A final int BUFFER_SIZE = 4096;
893N/A NativeBuffer buffer = null;
893N/A
893N/A // result with names of alternative data streams
3471N/A final List<String> list = new ArrayList<>();
893N/A
893N/A try {
893N/A buffer = NativeBuffers.getNativeBuffer(BUFFER_SIZE);
893N/A long address = buffer.address();
893N/A
893N/A /**
893N/A * typedef struct _WIN32_STREAM_ID {
893N/A * DWORD dwStreamId;
893N/A * DWORD dwStreamAttributes;
893N/A * LARGE_INTEGER Size;
893N/A * DWORD dwStreamNameSize;
893N/A * WCHAR cStreamName[ANYSIZE_ARRAY];
893N/A * } WIN32_STREAM_ID;
893N/A */
893N/A final int SIZEOF_STREAM_HEADER = 20;
893N/A final int OFFSETOF_STREAM_ID = 0;
893N/A final int OFFSETOF_STREAM_SIZE = 8;
893N/A final int OFFSETOF_STREAM_NAME_SIZE = 16;
893N/A
893N/A long context = 0L;
893N/A try {
893N/A for (;;) {
893N/A // read stream header
893N/A BackupResult result = BackupRead(handle, address,
893N/A SIZEOF_STREAM_HEADER, false, context);
893N/A context = result.context();
893N/A if (result.bytesTransferred() == 0)
893N/A break;
893N/A
893N/A int streamId = unsafe.getInt(address + OFFSETOF_STREAM_ID);
893N/A long streamSize = unsafe.getLong(address + OFFSETOF_STREAM_SIZE);
893N/A int nameSize = unsafe.getInt(address + OFFSETOF_STREAM_NAME_SIZE);
893N/A
893N/A // read stream name
893N/A if (nameSize > 0) {
893N/A result = BackupRead(handle, address, nameSize, false, context);
893N/A if (result.bytesTransferred() != nameSize)
893N/A break;
893N/A }
893N/A
893N/A // check for alternative data stream
893N/A if (streamId == BACKUP_ALTERNATE_DATA) {
893N/A char[] nameAsArray = new char[nameSize/2];
893N/A unsafe.copyMemory(null, address, nameAsArray,
893N/A Unsafe.ARRAY_CHAR_BASE_OFFSET, nameSize);
893N/A
893N/A String[] segs = new String(nameAsArray).split(":");
893N/A if (segs.length == 3)
893N/A list.add(segs[1]);
893N/A }
893N/A
893N/A // sparse blocks not currently handled as documentation
893N/A // is not sufficient on how the spase block can be skipped.
893N/A if (streamId == BACKUP_SPARSE_BLOCK) {
893N/A throw new IOException("Spare blocks not handled");
893N/A }
893N/A
893N/A // seek to end of stream
893N/A if (streamSize > 0L) {
893N/A BackupSeek(handle, streamSize, context);
893N/A }
893N/A }
893N/A } catch (WindowsException x) {
893N/A // failed to read or seek
893N/A throw new IOException(x.errorString());
893N/A } finally {
893N/A // release context
893N/A if (context != 0L) {
893N/A try {
893N/A BackupRead(handle, 0L, 0, true, context);
893N/A } catch (WindowsException ignore) { }
893N/A }
893N/A }
893N/A } finally {
893N/A if (buffer != null)
893N/A buffer.release();
893N/A CloseHandle(handle);
893N/A }
893N/A return Collections.unmodifiableList(list);
893N/A }
893N/A
893N/A @Override
893N/A public List<String> list() throws IOException {
893N/A if (System.getSecurityManager() != null)
893N/A checkAccess(file.getPathForPermissionCheck(), true, false);
893N/A // use stream APIs on Windwos Server 2003 and newer
893N/A if (file.getFileSystem().supportsStreamEnumeration()) {
893N/A return listUsingStreamEnumeration();
893N/A } else {
893N/A return listUsingBackupRead();
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public int size(String name) throws IOException {
893N/A if (System.getSecurityManager() != null)
893N/A checkAccess(file.getPathForPermissionCheck(), true, false);
893N/A
893N/A // wrap with channel
893N/A FileChannel fc = null;
893N/A try {
3471N/A Set<OpenOption> opts = new HashSet<>();
893N/A opts.add(READ);
893N/A if (!followLinks)
893N/A opts.add(WindowsChannelFactory.OPEN_REPARSE_POINT);
893N/A fc = WindowsChannelFactory
893N/A .newFileChannel(join(file, name), null, opts, 0L);
893N/A } catch (WindowsException x) {
893N/A x.rethrowAsIOException(join(file.getPathForPermissionCheck(), name));
893N/A }
893N/A try {
893N/A long size = fc.size();
893N/A if (size > Integer.MAX_VALUE)
893N/A throw new ArithmeticException("Stream too large");
893N/A return (int)size;
893N/A } finally {
893N/A fc.close();
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public int read(String name, ByteBuffer dst) throws IOException {
893N/A if (System.getSecurityManager() != null)
893N/A checkAccess(file.getPathForPermissionCheck(), true, false);
893N/A
893N/A // wrap with channel
893N/A FileChannel fc = null;
893N/A try {
3471N/A Set<OpenOption> opts = new HashSet<>();
893N/A opts.add(READ);
893N/A if (!followLinks)
893N/A opts.add(WindowsChannelFactory.OPEN_REPARSE_POINT);
893N/A fc = WindowsChannelFactory
893N/A .newFileChannel(join(file, name), null, opts, 0L);
893N/A } catch (WindowsException x) {
893N/A x.rethrowAsIOException(join(file.getPathForPermissionCheck(), name));
893N/A }
893N/A
893N/A // read to EOF (nothing we can do if I/O error occurs)
893N/A try {
893N/A if (fc.size() > dst.remaining())
893N/A throw new IOException("Stream too large");
893N/A int total = 0;
893N/A while (dst.hasRemaining()) {
893N/A int n = fc.read(dst);
893N/A if (n < 0)
893N/A break;
893N/A total += n;
893N/A }
893N/A return total;
893N/A } finally {
893N/A fc.close();
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public int write(String name, ByteBuffer src) throws IOException {
893N/A if (System.getSecurityManager() != null)
893N/A checkAccess(file.getPathForPermissionCheck(), false, true);
893N/A
893N/A /**
893N/A * Creating a named stream will cause the unnamed stream to be created
893N/A * if it doesn't already exist. To avoid this we open the unnamed stream
893N/A * for reading and hope it isn't deleted/moved while we create or
893N/A * replace the named stream. Opening the file without sharing options
893N/A * may cause sharing violations with other programs that are accessing
893N/A * the unnamed stream.
893N/A */
893N/A long handle = -1L;
893N/A try {
893N/A int flags = FILE_FLAG_BACKUP_SEMANTICS;
893N/A if (!followLinks)
893N/A flags |= FILE_FLAG_OPEN_REPARSE_POINT;
893N/A
893N/A handle = CreateFile(file.getPathForWin32Calls(),
893N/A GENERIC_READ,
893N/A (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE),
893N/A OPEN_EXISTING,
893N/A flags);
893N/A } catch (WindowsException x) {
893N/A x.rethrowAsIOException(file);
893N/A }
893N/A try {
3471N/A Set<OpenOption> opts = new HashSet<>();
893N/A if (!followLinks)
893N/A opts.add(WindowsChannelFactory.OPEN_REPARSE_POINT);
893N/A opts.add(CREATE);
893N/A opts.add(WRITE);
893N/A opts.add(StandardOpenOption.TRUNCATE_EXISTING);
893N/A FileChannel named = null;
893N/A try {
893N/A named = WindowsChannelFactory
893N/A .newFileChannel(join(file, name), null, opts, 0L);
893N/A } catch (WindowsException x) {
893N/A x.rethrowAsIOException(join(file.getPathForPermissionCheck(), name));
893N/A }
893N/A // write value (nothing we can do if I/O error occurs)
893N/A try {
893N/A int rem = src.remaining();
893N/A while (src.hasRemaining()) {
893N/A named.write(src);
893N/A }
893N/A return rem;
893N/A } finally {
893N/A named.close();
893N/A }
893N/A } finally {
893N/A CloseHandle(handle);
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public void delete(String name) throws IOException {
893N/A if (System.getSecurityManager() != null)
893N/A checkAccess(file.getPathForPermissionCheck(), false, true);
893N/A
893N/A String path = WindowsLinkSupport.getFinalPath(file, followLinks);
893N/A String toDelete = join(path, name);
893N/A try {
893N/A DeleteFile(toDelete);
893N/A } catch (WindowsException x) {
893N/A x.rethrowAsIOException(toDelete);
893N/A }
893N/A }
893N/A}