Example #1
0
  private void uploadFile(String relPath, Path fullPath) throws DropboxException, IOException {
    if (!fullPath.toFile().exists()) return;

    /*if(api.metadata(relPath, 1, null, false, null).rev == MetaHandler.getRev(relPath)){
    	logger.debug("File "+relPath+" not changed");
    	return;
    }*/

    VOSync.debug("Uploading " + relPath);

    String rev = MetaHandler.getRev(relPath);

    InputStream inp = new FileInputStream(fullPath.toFile());
    Entry fileEntry = api.putFile(relPath, inp, fullPath.toFile().length(), rev, null);
    inp.close();

    Path destFilePath =
        FileSystems.getDefault()
            .getPath(fullPath.toFile().getParentFile().getPath(), fileEntry.fileName());

    MetaHandler.setFile(relPath, destFilePath.toFile(), fileEntry.rev);

    logger.debug(relPath + " put to db");

    // if the file was renamed, move the file on disk and download the current from server
    if (!fileEntry.fileName().equals(fullPath.toFile().getName())) {
      logger.error(fileEntry.fileName() + " != " + fullPath.toFile().getName());
      fullPath.toFile().renameTo(destFilePath.toFile());
    }
  }
Example #2
0
 private static FileSystem fileSystem(URI file) {
   try {
     return FileSystems.newFileSystem(Paths.get(file.getPath()), null);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
Example #3
0
  /** Asynchronous close of watcher causes blocked threads to wakeup */
  static void testWakeup(Path dir) throws IOException {
    System.out.println("-- Wakeup Tests --");
    final WatchService watcher = FileSystems.getDefault().newWatchService();
    Runnable r =
        new Runnable() {
          public void run() {
            try {
              Thread.sleep(5000);
              System.out.println("close WatchService...");
              watcher.close();
            } catch (InterruptedException x) {
              x.printStackTrace();
            } catch (IOException x) {
              x.printStackTrace();
            }
          }
        };

    // start thread to close watch service after delay
    new Thread(r).start();

    try {
      System.out.println("take...");
      watcher.take();
      throw new RuntimeException("ClosedWatchServiceException not thrown");
    } catch (InterruptedException x) {
      throw new RuntimeException(x);
    } catch (ClosedWatchServiceException x) {
      System.out.println("ClosedWatchServiceException thrown");
    }

    System.out.println("OKAY");
  }
Example #4
0
  /** Check that a cancelled key will never be queued */
  static void testCancel(Path dir) throws IOException {
    System.out.println("-- Cancel --");

    try (WatchService watcher = FileSystems.getDefault().newWatchService()) {

      System.out.format("register %s for events\n", dir);
      WatchKey myKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_CREATE});
      checkKey(myKey, dir);

      System.out.println("cancel key");
      myKey.cancel();

      // create a file in the directory
      Path file = dir.resolve("mars");
      System.out.format("create: %s\n", file);
      Files.createFile(file);

      // poll for keys - there will be none
      System.out.println("poll...");
      try {
        WatchKey key = watcher.poll(3000, TimeUnit.MILLISECONDS);
        if (key != null) throw new RuntimeException("key should not be queued");
      } catch (InterruptedException x) {
        throw new RuntimeException(x);
      }

      // done
      Files.delete(file);

      System.out.println("OKAY");
    }
  }
Example #5
0
 private ArrayList<Path> holeLaufwerkeUnix() {
   ArrayList<Path> laufwerksRoot = new ArrayList<>();
   for (FileStore store : FileSystems.getDefault().getFileStores()) {
     if (store.name().contains("/dev/sd")) {
       laufwerksRoot.add(
           Paths.get(store.toString().substring(0, store.toString().indexOf(' '))));
     }
   }
   return laufwerksRoot;
 }
Example #6
0
    private ArrayList<Path> holeLaufwerkeWindows() {
      ArrayList<Path> laufwerksRoot = new ArrayList<>();

      Iterable<Path> rootDirectories = FileSystems.getDefault().getRootDirectories();

      for (Path path : rootDirectories) {
        laufwerksRoot.add(path.getRoot());
      }
      return laufwerksRoot;
    }
Example #7
0
 public Object tryArgument(String arg) throws Exception {
   try {
     Path p = FileSystems.getDefault().getPath(arg);
     File f = p.toFile();
     if (mustExist && !f.exists()) {
       throw new InvalidPathException(arg, "file must exist");
     }
     return f;
   } catch (InvalidPathException e) {
     throw e;
   }
 }
Example #8
0
 private void setOwnership(File dir, String group, String owner) {
   try {
     Path path = dir.toPath();
     UserPrincipalLookupService lookupService =
         FileSystems.getDefault().getUserPrincipalLookupService();
     GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(group);
     UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(owner);
     PosixFileAttributeView pfav =
         Files.getFileAttributeView(path, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
     pfav.setGroup(groupPrincipal);
     pfav.setOwner(userPrincipal);
   } catch (Exception ex) {
     cp.appendln("Unable to set the file group and owner for\n   " + dir);
   }
 }
Example #9
0
  /** Creates a WatchService and registers the given directory */
  WatchDir(Path dir, boolean recursive) throws IOException {
    this.watcher = FileSystems.getDefault().newWatchService();
    this.keys = new HashMap<WatchKey, Path>();
    this.recursive = recursive;

    if (recursive) {
      System.out.format("Scanning %s ...\n", dir);
      registerAll(dir);
      System.out.println("Done.");
    } else {
      register(dir);
    }

    // enable trace after initial registration
    this.trace = true;
  }
  private void insertFile(
      Path apkPath, Map<String, String> zip_properties, File insert, String method, Path location)
      throws AndrolibException, IOException {
    // ZipFileSystem only writes at .close()
    // http://mail.openjdk.java.net/pipermail/nio-dev/2012-July/001764.html
    try (FileSystem fs = FileSystems.newFileSystem(apkPath, null)) {
      Path root = fs.getPath("/");

      // in order to get the path relative to the zip, we strip off the absolute path, minus what we
      // already have in the zip. thus /var/files/apktool/apk/unknown/folder/file => /folder/file
      Path dest =
          fs.getPath(root.toString(), insert.getAbsolutePath().replace(location.toString(), ""));
      Path newFile = Paths.get(insert.getAbsolutePath());
      Files.copy(newFile, dest, StandardCopyOption.REPLACE_EXISTING);
      fs.close();
    }
  }
  private void insertFolder(
      Path apkPath, Map<String, String> zip_properties, File insert, String method, Path location)
      throws AndrolibException, IOException {
    try (FileSystem fs = FileSystems.newFileSystem(apkPath, null)) {
      Path root = fs.getPath("/");
      Path dest =
          fs.getPath(root.toString(), insert.getAbsolutePath().replace(location.toString(), ""));
      Path parent = dest.normalize();

      // check for folder existing in apkFileSystem
      if (parent != null && Files.notExists(parent)) {
        if (!Files.isDirectory(parent, LinkOption.NOFOLLOW_LINKS)) {
          Files.createDirectories(parent);
        }
      }
      fs.close();
    }
  }
Example #12
0
  /** Check that deleting a registered directory causes the key to be cancelled and queued. */
  static void testAutomaticCancel(Path dir) throws IOException {
    System.out.println("-- Automatic Cancel --");

    Path subdir = Files.createDirectory(dir.resolve("bar"));

    try (WatchService watcher = FileSystems.getDefault().newWatchService()) {

      System.out.format("register %s for events\n", subdir);
      WatchKey myKey =
          subdir.register(
              watcher, new WatchEvent.Kind<?>[] {ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY});

      System.out.format("delete: %s\n", subdir);
      Files.delete(subdir);
      takeExpectedKey(watcher, myKey);

      System.out.println("reset key");
      if (myKey.reset()) throw new RuntimeException("Key was not cancelled");
      if (myKey.isValid()) throw new RuntimeException("Key is still valid");

      System.out.println("OKAY");
    }
  }
Example #13
0
  public void downloadFile(String relPath) {
    VOSync.debug("Downloading file from storage: " + relPath);
    Path filePath = FileSystems.getDefault().getPath(startDir.toString(), relPath.substring(1));
    try {
      WatchKey key =
          filePath.getParent().register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
      keys.remove(key);
      key.cancel();

      FileOutputStream outp = new FileOutputStream(filePath.toFile());
      DropboxFileInfo info = api.getFile(relPath, null, outp, null);
      outp.close();

      key = filePath.getParent().register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
      keys.put(key, filePath.getParent());

      MetaHandler.setFile(relPath, filePath.toFile(), info.getMetadata().rev);
    } catch (IOException ex) {
      logger.error("Error downloading file " + relPath + ": " + ex.getMessage());
    } catch (DropboxException ex) {
      ex.printStackTrace();
      logger.error("Error downloading file " + relPath + ": " + ex.getMessage());
    }
  }
Example #14
0
 private static void extractAndLoadNativeLib(String nativeLibName, Path target) {
   //		System.err.println("loading "+nativeLibName);
   final Path path = Paths.get(target.toString(), nativeLibName);
   if (!path.toFile().exists()) {
     try (InputStream is =
         CudaEngine.class
             .getClassLoader()
             .getResourceAsStream("/lib/" + nativeLibName)) { // TODO TK property for lib dir
       Files.copy(is, path);
     } catch (IOException e) {
       e.printStackTrace();
     } catch (NullPointerException e) { // TODO find a way to do it instead of eclipse
       final Path eclipsePath = FileSystems.getDefault().getPath("lib", nativeLibName);
       try {
         Files.copy(eclipsePath, path);
       } catch (IOException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
       }
     }
   }
   System.load(path.toString());
   //		System.load(nativeLibName);
 }
Example #15
0
  /** Simple test to check exceptions and other cases */
  @SuppressWarnings("unchecked")
  static void testExceptions(Path dir) throws IOException {
    System.out.println("-- Exceptions and other simple tests --");

    WatchService watcher = FileSystems.getDefault().newWatchService();
    try {

      // Poll tests

      WatchKey key;
      System.out.println("poll...");
      key = watcher.poll();
      if (key != null) throw new RuntimeException("no keys registered");

      System.out.println("poll with timeout...");
      try {
        long start = System.currentTimeMillis();
        key = watcher.poll(3000, TimeUnit.MILLISECONDS);
        if (key != null) throw new RuntimeException("no keys registered");
        long waited = System.currentTimeMillis() - start;
        if (waited < 2900) throw new RuntimeException("poll was too short");
      } catch (InterruptedException x) {
        throw new RuntimeException(x);
      }

      // IllegalArgumentException
      System.out.println("IllegalArgumentException tests...");
      try {
        dir.register(watcher, new WatchEvent.Kind<?>[] {});
        throw new RuntimeException("IllegalArgumentException not thrown");
      } catch (IllegalArgumentException x) {
      }
      try {
        // OVERFLOW is ignored so this is equivalent to the empty set
        dir.register(watcher, new WatchEvent.Kind<?>[] {OVERFLOW});
        throw new RuntimeException("IllegalArgumentException not thrown");
      } catch (IllegalArgumentException x) {
      }

      // UnsupportedOperationException
      try {
        dir.register(
            watcher,
            new WatchEvent.Kind<?>[] {
              new WatchEvent.Kind<Object>() {
                @Override
                public String name() {
                  return "custom";
                }

                @Override
                public Class<Object> type() {
                  return Object.class;
                }
              }
            });
      } catch (UnsupportedOperationException x) {
      }
      try {
        dir.register(
            watcher,
            new WatchEvent.Kind<?>[] {ENTRY_CREATE},
            new WatchEvent.Modifier() {
              @Override
              public String name() {
                return "custom";
              }
            });
        throw new RuntimeException("UnsupportedOperationException not thrown");
      } catch (UnsupportedOperationException x) {
      }

      // NullPointerException
      System.out.println("NullPointerException tests...");
      try {
        dir.register(null, new WatchEvent.Kind<?>[] {ENTRY_CREATE});
        throw new RuntimeException("NullPointerException not thrown");
      } catch (NullPointerException x) {
      }
      try {
        dir.register(watcher, new WatchEvent.Kind<?>[] {null});
        throw new RuntimeException("NullPointerException not thrown");
      } catch (NullPointerException x) {
      }
      try {
        dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_CREATE}, (WatchEvent.Modifier) null);
        throw new RuntimeException("NullPointerException not thrown");
      } catch (NullPointerException x) {
      }
    } finally {
      watcher.close();
    }

    // -- ClosedWatchServiceException --

    System.out.println("ClosedWatchServiceException tests...");

    try {
      watcher.poll();
      throw new RuntimeException("ClosedWatchServiceException not thrown");
    } catch (ClosedWatchServiceException x) {
    }

    // assume that poll throws exception immediately
    long start = System.currentTimeMillis();
    try {
      watcher.poll(10000, TimeUnit.MILLISECONDS);
      throw new RuntimeException("ClosedWatchServiceException not thrown");
    } catch (InterruptedException x) {
      throw new RuntimeException(x);
    } catch (ClosedWatchServiceException x) {
      long waited = System.currentTimeMillis() - start;
      if (waited > 5000) throw new RuntimeException("poll was too long");
    }

    try {
      watcher.take();
      throw new RuntimeException("ClosedWatchServiceException not thrown");
    } catch (InterruptedException x) {
      throw new RuntimeException(x);
    } catch (ClosedWatchServiceException x) {
    }

    try {
      dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_CREATE});
      throw new RuntimeException("ClosedWatchServiceException not thrown");
    } catch (ClosedWatchServiceException x) {
    }

    System.out.println("OKAY");
  }
Example #16
0
 /** Создаем WatchService и регистрируем директорию */
 WatchDir(Path dir) throws IOException {
   this.watcher = FileSystems.getDefault().newWatchService();
   this.keys = new HashMap<WatchKey, Path>();
   register(dir);
 }
Example #17
0
 /**
  * Creates a WatchService and registers the given directory
  *
  * @throws IOException
  */
 DirWatcher2(Path dir, DropboxAPI<WebAuthSession> api) throws IOException {
   this.watcher = FileSystems.getDefault().newWatchService();
   this.keys = new HashMap<WatchKey, Path>();
   this.startDir = dir;
   this.api = api;
 }
Example #18
0
  /**
   * Test that directory can be registered with more than one watch service and that events don't
   * interfere with each other
   */
  static void testTwoWatchers(Path dir) throws IOException {
    System.out.println("-- Two watchers test --");

    FileSystem fs = FileSystems.getDefault();
    WatchService watcher1 = fs.newWatchService();
    WatchService watcher2 = fs.newWatchService();
    try {
      Path name1 = fs.getPath("gus1");
      Path name2 = fs.getPath("gus2");

      // create gus1
      Path file1 = dir.resolve(name1);
      System.out.format("create %s\n", file1);
      Files.createFile(file1);

      // register with both watch services (different events)
      System.out.println("register for different events");
      WatchKey key1 = dir.register(watcher1, new WatchEvent.Kind<?>[] {ENTRY_CREATE});
      WatchKey key2 = dir.register(watcher2, new WatchEvent.Kind<?>[] {ENTRY_DELETE});

      if (key1 == key2) throw new RuntimeException("keys should be different");

      // create gus2
      Path file2 = dir.resolve(name2);
      System.out.format("create %s\n", file2);
      Files.createFile(file2);

      // check that key1 got ENTRY_CREATE
      takeExpectedKey(watcher1, key1);
      checkExpectedEvent(key1.pollEvents(), StandardWatchEventKinds.ENTRY_CREATE, name2);

      // check that key2 got zero events
      WatchKey key = watcher2.poll();
      if (key != null) throw new RuntimeException("key not expected");

      // delete gus1
      Files.delete(file1);

      // check that key2 got ENTRY_DELETE
      takeExpectedKey(watcher2, key2);
      checkExpectedEvent(key2.pollEvents(), StandardWatchEventKinds.ENTRY_DELETE, name1);

      // check that key1 got zero events
      key = watcher1.poll();
      if (key != null) throw new RuntimeException("key not expected");

      // reset for next test
      key1.reset();
      key2.reset();

      // change registration with watcher2 so that they are both
      // registered for the same event
      System.out.println("register for same event");
      key2 = dir.register(watcher2, new WatchEvent.Kind<?>[] {ENTRY_CREATE});

      // create file and key2 should be queued
      System.out.format("create %s\n", file1);
      Files.createFile(file1);
      takeExpectedKey(watcher2, key2);
      checkExpectedEvent(key2.pollEvents(), StandardWatchEventKinds.ENTRY_CREATE, name1);

      System.out.println("OKAY");

    } finally {
      watcher2.close();
      watcher1.close();
    }
  }
Example #19
0
  /** Simple test of each of the standard events */
  static void testEvents(Path dir) throws IOException {
    System.out.println("-- Standard Events --");

    FileSystem fs = FileSystems.getDefault();
    Path name = fs.getPath("foo");

    try (WatchService watcher = fs.newWatchService()) {
      // --- ENTRY_CREATE ---

      // register for event
      System.out.format("register %s for ENTRY_CREATE\n", dir);
      WatchKey myKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_CREATE});
      checkKey(myKey, dir);

      // create file
      Path file = dir.resolve("foo");
      System.out.format("create %s\n", file);
      Files.createFile(file);

      // remove key and check that we got the ENTRY_CREATE event
      takeExpectedKey(watcher, myKey);
      checkExpectedEvent(myKey.pollEvents(), StandardWatchEventKinds.ENTRY_CREATE, name);

      System.out.println("reset key");
      if (!myKey.reset()) throw new RuntimeException("key has been cancalled");

      System.out.println("OKAY");

      // --- ENTRY_DELETE ---

      System.out.format("register %s for ENTRY_DELETE\n", dir);
      WatchKey deleteKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_DELETE});
      if (deleteKey != myKey) throw new RuntimeException("register did not return existing key");
      checkKey(deleteKey, dir);

      System.out.format("delete %s\n", file);
      Files.delete(file);
      takeExpectedKey(watcher, myKey);
      checkExpectedEvent(myKey.pollEvents(), StandardWatchEventKinds.ENTRY_DELETE, name);

      System.out.println("reset key");
      if (!myKey.reset()) throw new RuntimeException("key has been cancalled");

      System.out.println("OKAY");

      // create the file for the next test
      Files.createFile(file);

      // --- ENTRY_MODIFY ---

      System.out.format("register %s for ENTRY_MODIFY\n", dir);
      WatchKey newKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_MODIFY});
      if (newKey != myKey) throw new RuntimeException("register did not return existing key");
      checkKey(newKey, dir);

      System.out.format("update: %s\n", file);
      try (OutputStream out = Files.newOutputStream(file, StandardOpenOption.APPEND)) {
        out.write("I am a small file".getBytes("UTF-8"));
      }

      // remove key and check that we got the ENTRY_MODIFY event
      takeExpectedKey(watcher, myKey);
      checkExpectedEvent(myKey.pollEvents(), StandardWatchEventKinds.ENTRY_MODIFY, name);
      System.out.println("OKAY");

      // done
      Files.delete(file);
    }
  }