Search in sources :

Example 1 with REPARSE_DATA_BUFFER

use of com.sun.jna.platform.win32.Ntifs.REPARSE_DATA_BUFFER in project jna by java-native-access.

the class Kernel32Test method testDeviceIoControlFsctlReparse.

/**
     * NOTE: Due to process elevation, this test must be run as administrator
     * @throws IOException
     */
public void testDeviceIoControlFsctlReparse() throws IOException {
    Path folder = Files.createTempDirectory("testDeviceIoControlFsctlReparse_FOLDER");
    Path link = Files.createTempDirectory("testDeviceIoControlFsctlReparse_LINK");
    File delFolder = folder.toFile();
    delFolder.deleteOnExit();
    File delLink = link.toFile();
    delLink.deleteOnExit();
    // Required for FSCTL_SET_REPARSE_POINT
    Advapi32Util.Privilege restore = new Advapi32Util.Privilege(WinNT.SE_RESTORE_NAME);
    try {
        restore.enable();
        HANDLE hFile = Kernel32.INSTANCE.CreateFile(link.toAbsolutePath().toString(), WinNT.GENERIC_READ | WinNT.FILE_WRITE_ATTRIBUTES | WinNT.FILE_WRITE_EA, WinNT.FILE_SHARE_READ | WinNT.FILE_SHARE_WRITE | WinNT.FILE_SHARE_DELETE, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_DIRECTORY | WinNT.FILE_FLAG_BACKUP_SEMANTICS | WinNT.FILE_FLAG_OPEN_REPARSE_POINT, null);
        if (WinBase.INVALID_HANDLE_VALUE.equals(hFile)) {
            fail("CreateFile failed with " + Kernel32.INSTANCE.GetLastError());
        }
        try {
            SymbolicLinkReparseBuffer symLinkReparseBuffer = new SymbolicLinkReparseBuffer(folder.getFileName().toString(), folder.getFileName().toString(), Ntifs.SYMLINK_FLAG_RELATIVE);
            REPARSE_DATA_BUFFER lpBuffer = new REPARSE_DATA_BUFFER(WinNT.IO_REPARSE_TAG_SYMLINK, (short) 0, symLinkReparseBuffer);
            assertTrue(Kernel32.INSTANCE.DeviceIoControl(hFile, FSCTL_SET_REPARSE_POINT, lpBuffer.getPointer(), lpBuffer.getSize(), null, 0, null, null));
            Memory p = new Memory(REPARSE_DATA_BUFFER.sizeOf());
            IntByReference lpBytes = new IntByReference();
            assertTrue(Kernel32.INSTANCE.DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT, null, 0, p, (int) p.size(), lpBytes, null));
            // Is a reparse point
            lpBuffer = new REPARSE_DATA_BUFFER(p);
            assertTrue(lpBytes.getValue() > 0);
            assertTrue(lpBuffer.ReparseTag == WinNT.IO_REPARSE_TAG_SYMLINK);
            assertEquals(folder.getFileName().toString(), lpBuffer.u.symLinkReparseBuffer.getPrintName());
            assertEquals(folder.getFileName().toString(), lpBuffer.u.symLinkReparseBuffer.getSubstituteName());
        } finally {
            Kernel32Util.closeHandle(hFile);
        }
    } finally {
        restore.close();
    }
}
Also used : Path(java.nio.file.Path) REPARSE_DATA_BUFFER(com.sun.jna.platform.win32.Ntifs.REPARSE_DATA_BUFFER) IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) SymbolicLinkReparseBuffer(com.sun.jna.platform.win32.Ntifs.SymbolicLinkReparseBuffer) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Aggregations

Memory (com.sun.jna.Memory)1 REPARSE_DATA_BUFFER (com.sun.jna.platform.win32.Ntifs.REPARSE_DATA_BUFFER)1 SymbolicLinkReparseBuffer (com.sun.jna.platform.win32.Ntifs.SymbolicLinkReparseBuffer)1 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)1 IntByReference (com.sun.jna.ptr.IntByReference)1 File (java.io.File)1 Path (java.nio.file.Path)1