Skip to content

Commit

Permalink
updating native methods to current dokany standard, adding classes fo…
Browse files Browse the repository at this point in the history
…r DOKAN_CONTROL support
  • Loading branch information
infeo committed Sep 20, 2019
1 parent 4b7602f commit 0716547
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 10 deletions.
21 changes: 21 additions & 0 deletions src/main/java/dev/dokan/dokan_java/DokanyUtils.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package dev.dokan.dokan_java;

import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinBase.FILETIME;
import dev.dokan.dokan_java.structure.DokanControl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.sun.jna.platform.win32.WinError.*;
import static dev.dokan.dokan_java.constants.microsoft.NtStatuses.*;
Expand Down Expand Up @@ -55,6 +60,22 @@ public static boolean canHandleShutdownHooks() {
}
}

public static List<DokanControl> getDokanControlList(Pointer start, long length) {
List<DokanControl> list = new ArrayList<>();
if(length == 0){
return list;
}else if(length<0) {
//TODO length is actually an unsigned long! -> java always treats them as signed
return list;
}else {
list.add(new DokanControl(start));
for(int i=1; i<length; i++){
list.add(new DokanControl(start,i*list.get(0).size()));
}
return list;
}
}

/**
* Maps Win32 error codes to their respective NTStatus accordingly to ntstatus.i
*
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/dev/dokan/dokan_java/NativeMethods.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package dev.dokan.dokan_java;

import dev.dokan.dokan_java.constants.dokany.MountError;
import dev.dokan.dokan_java.structure.DokanOptions;
import dev.dokan.dokan_java.structure.DokanFileInfo;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.WString;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;
import dev.dokan.dokan_java.constants.dokany.MountError;
import dev.dokan.dokan_java.structure.DokanControl;
import dev.dokan.dokan_java.structure.DokanFileInfo;
import dev.dokan.dokan_java.structure.DokanOptions;

/**
* Native API to the kernel Dokany driver. This is an internal class and should not be used directly by code outside com.dokany.java.
Expand Down Expand Up @@ -161,15 +164,20 @@ static native void DokanMapKernelToUserCreateFileFlags(
static native void DokanDebugMode(boolean status);

/**
* Get active Dokany mount points.
* Get active Dokany mount points. Returned array need to be released by calling {@link #DokanReleaseMountPointList}.
*
* @param uncOnly - Get only instances that have UNC Name.
* @param nbRead - Number of instances successfully retrieved
* @return a pointer to the start of the allocated array of {@link DokanControl} elemets. The actual list can be retrieved with {@link DokanyUtils#getDokanControlList(Pointer, long)}.
*/
static native Pointer DokanGetMountPointList(boolean uncOnly, LongByReference nbRead);

/**
* Release Mount point list resources from {@link #DokanGetMountPointList}.
*
* @param fileAttributes - Allocate array of DOKAN_CONTROL
* @param length - Number of DOKAN_CONTROL instance in list.
* @param uncOnly - Get only instances that have UNC Name.
* @param nbRead - Number of instances successfully retrieved
* @return List retrieved or not.
* @param startOfList Pointer to the start of the {@link DokanControl} list.
*/
static native boolean DokanGetMountPointList(long fileAttributes, long length, boolean uncOnly, long nbRead);
static native void DokanReleaseMountPointList(Pointer startOfList);

/**
* Convert Win32 error to NtStatus
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/dev/dokan/dokan_java/structure/DokanControl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package dev.dokan.dokan_java.structure;

import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.ptr.LongByReference;

import java.util.Arrays;
import java.util.List;

/**
* Structure containing information for a single dokany mount.
* <p>
* Used by {@link dev.dokan.dokan_java.NativeMethods#DokanGetMountPointList(boolean, LongByReference)} and {@link dev.dokan.dokan_java.NativeMethods#DokanReleaseMountPointList(Pointer)}.
*/
public class DokanControl extends Structure implements Structure.ByReference {

/**
* File System Type
*/
public long Type;

/**
* Mount point. Can be "M:\" (drive letter) or "C:\mount\dokan" (path in NTFS)
*/
public char[] MountPoint = new char[256];

/**
* UNC name used for network volume
*/
public char[] UNCName = new char[64];

/**
* Disk Device Name
*/
public char[] DeviceName = new char[64];

/**
* Volume Device Object
*/
public Pointer DeviceObject;

/**
* Session ID of calling process
*/
public long SessionId;

public DokanControl(Pointer p) {
this(p, 0);
}

public DokanControl(Pointer p, long offset) {
super(p);
long currentOffset = offset;
this.Type = p.getLong(currentOffset);
currentOffset += NativeLong.SIZE;
this.MountPoint = p.getCharArray(currentOffset, WinNT.MAX_PATH);
currentOffset += WinNT.MAX_PATH * 2;
this.UNCName = p.getCharArray(currentOffset, 64);
currentOffset += 64 * 2;
this.DeviceName = p.getCharArray(currentOffset, 64);
currentOffset += 64 * 2;
this.DeviceObject = new Pointer(p.getLong(currentOffset));
currentOffset += NativeLong.SIZE;
this.SessionId = p.getLong(currentOffset);
}


@Override
protected List<String> getFieldOrder() {
return Arrays.asList("Type", "MountPoint", "UNCName", "DeviceName", "DeviceObject", "SessionId");
}
}

0 comments on commit 0716547

Please sign in to comment.