Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified WinioctlUtil for simplicity #740

Merged
merged 1 commit into from
Dec 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Features
* [#732](https://github.com/java-native-access/jna/pull/732): Added `com.sun.jna.platform.win32.Ntifs` with Reparse Point structures and defines - [@amarcionek](https://github.com/amarcionek).
* [#732](https://github.com/java-native-access/jna/pull/732): Added initialization of FILETIME from LARGE_INTEGER - [@amarcionek](https://github.com/amarcionek).
* [#732](https://github.com/java-native-access/jna/pull/732): Added `GetFileInformationByHandleEx` and `SetFileInformationByHandle` to `com.sun.jna.platform.win32.Kernel32` - [@amarcionek](https://github.com/amarcionek).
* [#740](https://github.com/java-native-access/jna/pull/740): Modified `com.sun.jna.platform.win32.WinioctlUtil` for simplicity dealing with FSCTL_* codes - [@amarcionek](https://github.com/amarcionek).

Bug Fixes
---------
Expand Down
60 changes: 25 additions & 35 deletions contrib/platform/src/com/sun/jna/platform/win32/WinioctlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,43 +43,33 @@ public static int CTL_CODE(int DeviceType, int Function, int Method, int Access)
return ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method);
}

/**
* Base interface for a Winiotcl function used to construct the control code
*/
public interface WinioctlFunction {
/**
* @return the control code given the IOTCL's parameters
*/
public abstract int getControlCode();
}
public static final int FSCTL_GET_COMPRESSION = CTL_CODE(
Winioctl.FILE_DEVICE_FILE_SYSTEM,
Winioctl.FSCTL_GET_COMPRESSION,
Winioctl.METHOD_BUFFERED,
Winioctl.FILE_ANY_ACCESS);

public static class FSCTL_GET_COMPRESSION implements WinioctlFunction {
public int getControlCode() {
return WinioctlUtil.CTL_CODE(Winioctl.FILE_DEVICE_FILE_SYSTEM, Winioctl.FSCTL_GET_COMPRESSION, Winioctl.METHOD_BUFFERED, Winioctl.FILE_ANY_ACCESS);
}
}
public static final int FSCTL_SET_COMPRESSION = CTL_CODE(
Winioctl.FILE_DEVICE_FILE_SYSTEM,
Winioctl.FSCTL_SET_COMPRESSION,
Winioctl.METHOD_BUFFERED,
WinNT.FILE_READ_DATA | WinNT.FILE_WRITE_DATA);

public static class FSCTL_SET_COMPRESSION implements WinioctlFunction {
public int getControlCode() {
return WinioctlUtil.CTL_CODE(Winioctl.FILE_DEVICE_FILE_SYSTEM, Winioctl.FSCTL_SET_COMPRESSION, Winioctl.METHOD_BUFFERED, WinNT.FILE_READ_DATA | WinNT.FILE_WRITE_DATA);
}
}
public static final int FSCTL_SET_REPARSE_POINT = CTL_CODE(
Winioctl.FILE_DEVICE_FILE_SYSTEM,
Winioctl.FSCTL_SET_REPARSE_POINT,
Winioctl.METHOD_BUFFERED,
Winioctl.FILE_SPECIAL_ACCESS);

public static class FSCTL_SET_REPARSE_POINT implements WinioctlFunction {
public int getControlCode() {
return WinioctlUtil.CTL_CODE(Winioctl.FILE_DEVICE_FILE_SYSTEM, Winioctl.FSCTL_SET_REPARSE_POINT, Winioctl.METHOD_BUFFERED, Winioctl.FILE_SPECIAL_ACCESS);
}
}

public static class FSCTL_GET_REPARSE_POINT implements WinioctlFunction {
public int getControlCode() {
return WinioctlUtil.CTL_CODE(Winioctl.FILE_DEVICE_FILE_SYSTEM, Winioctl.FSCTL_GET_REPARSE_POINT, Winioctl.METHOD_BUFFERED, Winioctl.FILE_ANY_ACCESS);
}
}
public static final int FSCTL_GET_REPARSE_POINT = CTL_CODE(
Winioctl.FILE_DEVICE_FILE_SYSTEM,
Winioctl.FSCTL_GET_REPARSE_POINT,
Winioctl.METHOD_BUFFERED,
Winioctl.FILE_ANY_ACCESS);

public static class FSCTL_DELETE_REPARSE_POINT implements WinioctlFunction {
public int getControlCode() {
return WinioctlUtil.CTL_CODE(Winioctl.FILE_DEVICE_FILE_SYSTEM, Winioctl.FSCTL_DELETE_REPARSE_POINT, Winioctl.METHOD_BUFFERED, Winioctl.FILE_SPECIAL_ACCESS);
}
}
public static final int FSCTL_DELETE_REPARSE_POINT = CTL_CODE(
Winioctl.FILE_DEVICE_FILE_SYSTEM,
Winioctl.FSCTL_DELETE_REPARSE_POINT,
Winioctl.METHOD_BUFFERED,
Winioctl.FILE_SPECIAL_ACCESS);
}
19 changes: 10 additions & 9 deletions contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
package com.sun.jna.platform.win32;

import static com.sun.jna.platform.win32.WinioctlUtil.FSCTL_GET_COMPRESSION;
import static com.sun.jna.platform.win32.WinioctlUtil.FSCTL_GET_REPARSE_POINT;
import static com.sun.jna.platform.win32.WinioctlUtil.FSCTL_SET_COMPRESSION;
import static com.sun.jna.platform.win32.WinioctlUtil.FSCTL_SET_REPARSE_POINT;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
Expand Down Expand Up @@ -62,10 +67,6 @@
import com.sun.jna.platform.win32.WinNT.MEMORY_BASIC_INFORMATION;
import com.sun.jna.platform.win32.WinNT.OSVERSIONINFO;
import com.sun.jna.platform.win32.WinNT.OSVERSIONINFOEX;
import com.sun.jna.platform.win32.WinioctlUtil.FSCTL_GET_COMPRESSION;
import com.sun.jna.platform.win32.WinioctlUtil.FSCTL_GET_REPARSE_POINT;
import com.sun.jna.platform.win32.WinioctlUtil.FSCTL_SET_COMPRESSION;
import com.sun.jna.platform.win32.WinioctlUtil.FSCTL_SET_REPARSE_POINT;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.ShortByReference;

Expand Down Expand Up @@ -587,7 +588,7 @@ public void testDeviceIoControlFsctlCompression() throws IOException {
IntByReference lpBytes = new IntByReference();

if (false == Kernel32.INSTANCE.DeviceIoControl(hFile,
new FSCTL_GET_COMPRESSION().getControlCode(),
FSCTL_GET_COMPRESSION,
null,
0,
lpBuffer.getPointer(),
Expand All @@ -602,7 +603,7 @@ public void testDeviceIoControlFsctlCompression() throws IOException {
lpBuffer = new ShortByReference((short)WinNT.COMPRESSION_FORMAT_LZNT1);

if (false == Kernel32.INSTANCE.DeviceIoControl(hFile,
new FSCTL_SET_COMPRESSION().getControlCode(),
FSCTL_SET_COMPRESSION,
lpBuffer.getPointer(),
USHORT.SIZE,
null,
Expand All @@ -613,7 +614,7 @@ public void testDeviceIoControlFsctlCompression() throws IOException {
}

if (false == Kernel32.INSTANCE.DeviceIoControl(hFile,
new FSCTL_GET_COMPRESSION().getControlCode(),
FSCTL_GET_COMPRESSION,
null,
0,
lpBuffer.getPointer(),
Expand Down Expand Up @@ -666,7 +667,7 @@ public void testDeviceIoControlFsctlReparse() throws IOException {
REPARSE_DATA_BUFFER lpBuffer = new REPARSE_DATA_BUFFER(WinNT.IO_REPARSE_TAG_SYMLINK, (short) 0, symLinkReparseBuffer);

assertTrue(Kernel32.INSTANCE.DeviceIoControl(hFile,
new FSCTL_SET_REPARSE_POINT().getControlCode(),
FSCTL_SET_REPARSE_POINT,
lpBuffer.getPointer(),
lpBuffer.getSize(),
null,
Expand All @@ -677,7 +678,7 @@ public void testDeviceIoControlFsctlReparse() throws IOException {
Memory p = new Memory(REPARSE_DATA_BUFFER.sizeOf());
IntByReference lpBytes = new IntByReference();
assertTrue(Kernel32.INSTANCE.DeviceIoControl(hFile,
new FSCTL_GET_REPARSE_POINT().getControlCode(),
FSCTL_GET_REPARSE_POINT,
null,
0,
p,
Expand Down