Browse files

Make Util.link work on windows too.

  • Loading branch information...
1 parent a65428d commit 29e2da20850945c2d2190ecf9c04b5782b01af6b @chirino chirino committed Aug 23, 2012
Showing with 20 additions and 4 deletions.
  1. +20 −4 leveldbjni/src/main/java/org/fusesource/leveldbjni/internal/Util.java
View
24 leveldbjni/src/main/java/org/fusesource/leveldbjni/internal/Util.java
@@ -33,13 +33,15 @@
import org.fusesource.hawtjni.runtime.JniArg;
import org.fusesource.hawtjni.runtime.JniClass;
+import org.fusesource.hawtjni.runtime.JniField;
import org.fusesource.hawtjni.runtime.JniMethod;
import java.io.File;
import java.io.IOException;
import static org.fusesource.hawtjni.runtime.ClassFlag.CPP;
-import static org.fusesource.hawtjni.runtime.MethodFlag.CONSTANT_GETTER;
+import static org.fusesource.hawtjni.runtime.FieldFlag.CONSTANT;
+import static org.fusesource.hawtjni.runtime.MethodFlag.*;
import static org.fusesource.hawtjni.runtime.ArgFlag.*;
/**
@@ -54,17 +56,25 @@
static {
NativeDB.LIBRARY.load();
+ init();
}
+ @JniMethod(flags={CONSTANT_INITIALIZER})
+ private static final native void init();
+
+ @JniField(flags={CONSTANT}, accessor="1", conditional="defined(_WIN32) || defined(_WIN64)")
+ static int ON_WINDOWS;
+
+
@JniMethod(conditional="!defined(_WIN32) && !defined(_WIN64)")
static final native int link(
@JniArg(cast="const char*") String source,
@JniArg(cast="const char*") String target);
@JniMethod(conditional="defined(_WIN32) || defined(_WIN64)")
static final native int CreateHardLinkW(
- @JniArg(cast="LPCTSTR", flags={POINTER_ARG, UNICODE}) String source,
@JniArg(cast="LPCTSTR", flags={POINTER_ARG, UNICODE}) String target,
+ @JniArg(cast="LPCTSTR", flags={POINTER_ARG, UNICODE}) String source,
@JniArg(cast="LPSECURITY_ATTRIBUTES", flags={POINTER_ARG}) long lpSecurityAttributes);
@JniMethod(flags={CONSTANT_GETTER})
@@ -85,8 +95,14 @@ public static final native int strlen(
* @return
*/
public static void link(File source, File target) throws IOException {
- if( UtilJNI.link(source.getCanonicalPath(), target.getCanonicalPath()) != 0 ) {
- throw new IOException("link failed: "+strerror());
+ if( UtilJNI.ON_WINDOWS == 1 ) {
+ if( UtilJNI.CreateHardLinkW(target.getCanonicalPath(), source.getCanonicalPath(), 0) == 0) {
+ throw new IOException("link failed");
+ }
+ } else {
+ if( UtilJNI.link(source.getCanonicalPath(), target.getCanonicalPath()) != 0 ) {
+ throw new IOException("link failed: "+strerror());
+ }
}
}

0 comments on commit 29e2da2

Please sign in to comment.