package org.deepfs.jfuse;

import org.catacombae.jfuse.JNILoader;
import org.catacombae.jfuse.types.system.FileModeFlags;
import org.catacombae.jfuse.util.FUSEUtil;

/**
 * This class assembles all references to the external jfuse library.
 * 
 * In order to keep BaseX independent from external libraries this class
 * assembles all calls to the Java FUSE bindings and provides default values. In
 * case jfuse.jar is removed from the build path this class and the actual FUSE
 * implementation should be the only classes to be modified or removed. In fact,
 * it is intended to just exclude DeepFSImpl from the build path and to toggle
 * the (un)commented code here.
 * 
 * @author Alexander Holupirek <alex@holupirek.de>
 * 
 */
public final class JFUSEAdapter implements FileModeFlags {

//  /** Directory bit. */
//  private static final int DFS_S_IFDIR = 0040000;
//  /** Regular file bit. */
//  private static final int DFS_S_IFREG = 0100000;
//  /** File bit mask. */
//  private static final int DFS_S_IFMT = 0170000;

  /** Private utility constructor. */
  private JFUSEAdapter() {
    throw new UnsupportedOperationException();
  }

  /**
   * Check (and load) native Java FUSE bindings.
   * @return true in case of success.
   */
  public static boolean loadJFUSELibrary() {
    try {
      JNILoader.ensureLoaded();
      return true;
    } catch(final UnsatisfiedLinkError ex) {
      return false;
    }
    // return false;
  }

  /**
   * Get (native) user id or default value.
   * @return user id
   */
  public static long getUID() {
    if(loadJFUSELibrary()) {
      return FUSEUtil.getProcessUid();
    }
    return 0;
  }

  /**
   * Get (native) group id or default value.
   * @return group id
   */
  public static long getGID() {
    if(loadJFUSELibrary()) {
      return FUSEUtil.getProcessGid();
    }
    return 0;
  }

  /**
   * Returns directory bit.
   * 
   * @return bitmask indicating a directory
   */
  public static int getSIFDIR() {
    return S_IFDIR;
    // return DFS_S_IFDIR;
  }

  /**
   * Returns regular file bit.
   * 
   * @return bitmask indicating a directory
   */
  public static int getSIFREG() {
    return S_IFREG;
    // return DFS_S_IFREG;
  }

  /**
   * Test mode for regular file flag.
   * @param mode of file
   * @return true if mode is regular file
   */
  public static boolean isReg(final int mode) {
    return (mode & S_IFMT) == S_IFREG;
    // return (mode & DFS_S_IFMT) == DFS_S_IFREG;
  }
}

@@ -786,18 +786,16 @@
 lags %7B%0A%0A
-//
   /** Di
@@ -802,34 +802,32 @@
 rectory bit. */%0A
-//
   private static
@@ -856,26 +856,24 @@
  = 0040000;%0A
-//
   /** Regula
@@ -883,26 +883,24 @@
 ile bit. */%0A
-//
   private st
@@ -937,18 +937,16 @@
 100000;%0A
-//
   /** Fi
@@ -961,18 +961,16 @@
 ask. */%0A
-//
   privat
@@ -1975,30 +1975,38 @@
 urn 
-S_IFDIR;%0A    // return
+loadJFUSELibrary() ? S_IFDIR :
  DFS
@@ -2158,38 +2158,46 @@
  return 
-S_IFREG;%0A    // return
+loadJFUSELibrary() ? S_IFREG :
  DFS_S_I
@@ -2368,24 +2368,54 @@
 int mode) %7B%0A
+    if (loadJFUSELibrary())%0A  
     return (
@@ -2440,27 +2440,24 @@
 S_IFREG;%0A   
- //
  return (mod

fix creation of deepfs instances on windows.