示例#1
0
  @Test
  public void fileStatNanoTime() throws Throwable {
    // Currently only linux file stat support nano time resolution
    jnr.ffi.Platform nativePlatform = jnr.ffi.Platform.getNativePlatform();
    if (nativePlatform.getOS() == jnr.ffi.Platform.OS.LINUX) {
      File f = File.createTempFile("stat", null);
      try {
        FileStat st = posix.stat(f.getAbsolutePath());
        assertNotNull("posix.stat failed", st);

        FileStat stat = posix.allocateStat();
        int result = posix.stat(f.getAbsolutePath(), stat);
        assertNotNull("posix.stat failed", st);
        assertEquals(0, result);

        if (Platform.IS_32_BIT) {
          LinuxFileStat32 fstat32 = (LinuxFileStat32) stat;
          assertTrue(fstat32.cTimeNanoSecs() > 0);
          assertTrue(fstat32.mTimeNanoSecs() > 0);
          assertTrue(fstat32.aTimeNanoSecs() > 0);
          assertEquals(fstat32.cTimeNanoSecs(), fstat32.mTimeNanoSecs());
        } else {
          LinuxFileStat64 fstat64 = (LinuxFileStat64) stat;
          assertTrue(fstat64.cTimeNanoSecs() > 0);
          assertTrue(fstat64.mTimeNanoSecs() > 0);
          assertTrue(fstat64.aTimeNanoSecs() > 0);

          assertEquals(fstat64.cTimeNanoSecs(), fstat64.mTimeNanoSecs());
        }
      } finally {
        f.delete();
      }
    }
  }
示例#2
0
 @Test
 public void filestatInt() throws Throwable {
   // Windows does not store fd in FileDescriptor so this test wll not work
   if (jnr.ffi.Platform.getNativePlatform().isUnix()) {
     File f = File.createTempFile("stat", null);
     try {
       FileInputStream fis = new FileInputStream(f);
       FileDescriptor desc = fis.getFD();
       int fd = -1;
       try {
         Field fdField = FieldAccess.getProtectedField(FileDescriptor.class, "fd");
         fd = fdField.getInt(desc);
       } catch (SecurityException e) {
       } catch (IllegalArgumentException e) {
       } catch (IllegalAccessException e) {
       }
       FileStat stat = posix.allocateStat();
       int result = posix.fstat(fd, stat);
       assertTrue(fd > 2); // should not be stdin, stdout, stderr
       assertEquals(0, result);
     } finally {
       f.delete();
     }
   } else {
     FileStat stat = posix.fstat(0);
     assertTrue(stat != null);
   }
 }
示例#3
0
 private static String libraryName() {
   switch (Platform.getNativePlatform().getOS()) {
     case WINDOWS:
       return "libsodium";
     default:
       return "sodium";
   }
 }
示例#4
0
  private static void init() {
    if (libFuse != null) return;
    initLock.lock();
    try {
      final jnr.ffi.Platform p = jnr.ffi.Platform.getNativePlatform();
      // Need to recheck
      if (libFuse == null) {
        switch (p.getOS()) {
          case FREEBSD:
            platform = PlatformEnum.FREEBSD;
            libFuse = LibraryLoader.create(LibFuse.class).failImmediately().load("fuse");
            break;
          case DARWIN:
            // First, need to load iconv
            final LibDl dl = LibraryLoader.create(LibDl.class).failImmediately().load("iconv");
            dl.dlopen("iconv", LibDl.RTLD_LAZY | LibDl.RTLD_GLOBAL);
            libFuse = null;
            LibFuseProbe probe;
            for (String library : osxFuseLibraries) {
              try {
                // Regular FUSE-compatible fuse library
                platform = PlatformEnum.MAC;
                libFuse = LibraryLoader.create(LibFuse.class).failImmediately().load(library);
                break;
              } catch (Throwable e) {
                // Carry on
              }
              try {
                probe = LibraryLoader.create(LibMacFuseProbe.class).failImmediately().load(library);
                ((LibMacFuseProbe) probe).macfuse_version();
                // MacFUSE-compatible fuse library
                platform = PlatformEnum.MAC_MACFUSE;
                libFuse = LibraryLoader.create(LibFuse.class).failImmediately().load("fuse");
                break;
              } catch (Throwable e) {
                // Carry on
              }
            }
            if (libFuse == null) {
              // Everything failed. Do a last-ditch attempt.
              // Worst-case scenario, this causes an exception
              // which will be more meaningful to the user than a NullPointerException on libFuse.
              libFuse = LibraryLoader.create(LibFuse.class).failImmediately().load("fuse");
            }
            break;

          case LINUX:
            LibraryLoader<LibFuse> libraryLoader = LibraryLoader.create(LibFuse.class);
            if (p.getCPU() == CPU.X86_64) {
              libraryLoader.search("/lib/x86_64-linux-gnu");
              platform = PlatformEnum.LINUX_X86_64;
            } else if (p.getCPU() == CPU.I386) {
              libraryLoader.search("/lib/i386-linux-gnu");
              platform = PlatformEnum.LINUX_I686;
            } else if (p.getCPU() == CPU.ARM) {
              platform = PlatformEnum.LINUX_ARM;
            } else {
              platform = PlatformEnum.LINUX_PPC;
            }

            libFuse = libraryLoader.failImmediately().load("fuse");
            break;

          default:
            throw new RuntimeException("Unsupported Platform");
        }
      }
    } finally {
      initLock.unlock();
    }
  }