@Override
  FileTypeDetector getFileTypeDetector() {
    String userHome = GetPropertyAction.privilegedGetProperty("user.home");
    Path userMimeTypes = Paths.get(userHome, ".mime.types");
    Path etcMimeTypes = Paths.get("/etc/mime.types");

    return chain(
        new MimeTypesFileTypeDetector(userMimeTypes), new MimeTypesFileTypeDetector(etcMimeTypes));
  }
  public static void main(String[] args) throws Exception {
    Path dir = Paths.get(args[0]);

    Files.walkFileTree(
        dir,
        new SimpleFileVisitor<Path>() {
          @Override
          public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
            check(dir);
            if (skip(dir)) return FileVisitResult.SKIP_SIBLINGS;
            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            check(file);
            if (skip(file)) return FileVisitResult.SKIP_SIBLINGS;
            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult postVisitDirectory(Path dir, IOException x) {
            if (x != null) throw new RuntimeException(x);
            check(dir);
            return FileVisitResult.CONTINUE;
          }
        });
  }
Beispiel #3
0
  public static void main(String[] args) throws Exception {
    boolean followLinks = false;
    boolean printCycles = false;
    int i = 0;
    while (i < (args.length - 1)) {
      switch (args[i]) {
        case "-follow":
          followLinks = true;
          break;
        case "-printCycles":
          printCycles = true;
          break;
        default:
          throw new RuntimeException(args[i] + " not recognized");
      }
      i++;
    }
    Path dir = Paths.get(args[i]);

    Set<FileVisitOption> options = new HashSet<FileVisitOption>();
    if (followLinks) options.add(FileVisitOption.FOLLOW_LINKS);

    final boolean follow = followLinks;
    final boolean reportCycles = printCycles;
    Files.walkFileTree(
        dir,
        options,
        Integer.MAX_VALUE,
        new FileVisitor<Path>() {
          @Override
          public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
            System.out.println(dir);
            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            System.out.println(file);
            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            if (exc != null) throw exc;
            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
            if (follow && (exc instanceof FileSystemLoopException)) {
              if (reportCycles) System.out.println(file);
              return FileVisitResult.CONTINUE;
            } else {
              throw exc;
            }
          }
        });
  }
Beispiel #4
0
 /** Puts library to temp dir and loads to memory */
 private static void loadLib(String name) {
   Path sourcePath = Paths.get(LIBPATH, name);
   try {
     Path libPath = Files.copy(sourcePath, tempPath, StandardCopyOption.REPLACE_EXISTING);
     System.load(libPath.toString());
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Beispiel #5
0
 public static void main(String[] args) throws IOException {
   System.out.format("%-20s %12s %12s %12s\n", "Filesystem", "kbytes", "used", "avail");
   if (args.length == 0) {
     FileSystem fs = FileSystems.getDefault();
     for (FileStore store : fs.getFileStores()) {
       printFileStore(store);
     }
   } else {
     for (String file : args) {
       FileStore store = Files.getFileStore(Paths.get(file));
       printFileStore(store);
     }
   }
 }
  /**
   * Makes home folder and the configuration file readable and writable only to the owner.
   *
   * @param cs the <tt>ConfigurationService</tt> instance to check for home folder and configuration
   *     file.
   */
  private static void fixPermissions(ConfigurationService cs) {
    if (!OSUtils.IS_LINUX && !OSUtils.IS_MAC) return;

    try {
      // let's check config file and config folder
      File homeFolder = new File(cs.getScHomeDirLocation(), cs.getScHomeDirName());
      Set<PosixFilePermission> perms =
          new HashSet<PosixFilePermission>() {
            {
              add(PosixFilePermission.OWNER_READ);
              add(PosixFilePermission.OWNER_WRITE);
              add(PosixFilePermission.OWNER_EXECUTE);
            }
          };
      Files.setPosixFilePermissions(Paths.get(homeFolder.getAbsolutePath()), perms);

      String fileName = cs.getConfigurationFilename();
      if (fileName != null) {
        File cf = new File(homeFolder, fileName);
        if (cf.exists()) {
          perms =
              new HashSet<PosixFilePermission>() {
                {
                  add(PosixFilePermission.OWNER_READ);
                  add(PosixFilePermission.OWNER_WRITE);
                }
              };
          Files.setPosixFilePermissions(Paths.get(cf.getAbsolutePath()), perms);
        }
      }
    } catch (Throwable t) {
      logger.error("Error creating c lib instance for fixing file permissions", t);

      if (t instanceof InterruptedException) Thread.currentThread().interrupt();
      else if (t instanceof ThreadDeath) throw (ThreadDeath) t;
    }
  }
Beispiel #7
0
  public void backup(String wildCardPath) {
    try {
      if (wildCardPath.contains("*") || wildCardPath.contains("?")) {
        // Extract the path so we know where to start and get the name which contains the
        // wildcard so we know what to store:

        File wildCardFile = new File(wildCardPath);
        String parentDirectory = wildCardFile.getParent();
        String wildCardPattern = wildCardFile.getName();
        _pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + wildCardPattern);
        Files.walkFileTree(Paths.get(parentDirectory), this);
      } else storeFile(new File(wildCardPath));
    } catch (FileNotFoundException exception) {
      Console.printError("File/Directory not found: " + wildCardPath);
    } catch (IOException exception) {
      Console.printError(
          "File/Directory '%s' not stored because: '%s'", wildCardPath, exception.getMessage());
    }
  }
Beispiel #8
0
 /** @throws IOException */
 public static void start() throws IOException {
   // флаг для рекурсивного просмотра каталогов
   Path dir = Paths.get(IN_PATH);
   new WatchDir(dir).processEvents();
 }
Beispiel #9
0
  /** Tests all possible ways to invoke copy to copy a file to a file */
  static void testCopyFileToFile(Path dir1, Path dir2, boolean supportsLinks) throws IOException {
    Path source, target, link, entry;

    // -- regular file --

    /** Test: move regular file, target does not exist */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    copyAndVerify(source, target);
    delete(source);
    delete(target);

    /** Test: copy regular file, target exists */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    try {
      copyAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(target);
    createDirectory(target);
    try {
      copyAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(source);
    delete(target);

    /** Test: copy regular file, target does not exist */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    copyAndVerify(source, target, REPLACE_EXISTING);
    delete(source);
    delete(target);

    /** Test: copy regular file, target exists */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    copyAndVerify(source, target, REPLACE_EXISTING);
    delete(source);
    delete(target);

    /** Test: copy regular file, target exists and is empty directory */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    copyAndVerify(source, target, REPLACE_EXISTING);
    delete(source);
    delete(target);

    /** Test: copy regular file, target exists and is non-empty directory */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    entry = target.resolve("foo");
    createFile(entry);
    try {
      copyAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(entry);
    delete(source);
    delete(target);

    /** Test: copy regular file + attributes */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    copyAndVerify(source, target, COPY_ATTRIBUTES);
    delete(source);
    delete(target);

    // -- directory --

    /*
     * Test: copy directory, target does not exist
     */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    copyAndVerify(source, target);
    delete(source);
    delete(target);

    /** Test: copy directory, target exists */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    try {
      copyAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(target);
    createDirectory(target);
    try {
      copyAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(source);
    delete(target);

    /** Test: copy directory, target does not exist */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    copyAndVerify(source, target, REPLACE_EXISTING);
    delete(source);
    delete(target);

    /** Test: copy directory, target exists */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    copyAndVerify(source, target, REPLACE_EXISTING);
    delete(source);
    delete(target);

    /** Test: copy directory, target exists and is empty directory */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    copyAndVerify(source, target, REPLACE_EXISTING);
    delete(source);
    delete(target);

    /** Test: copy directory, target exists and is non-empty directory */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    entry = target.resolve("foo");
    createFile(entry);
    try {
      copyAndVerify(source, target, REPLACE_EXISTING);
      throw new RuntimeException("DirectoryNotEmptyException expected");
    } catch (DirectoryNotEmptyException x) {
    }
    delete(entry);
    delete(source);
    delete(target);

    /*
     * Test: copy directory + attributes
     */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    copyAndVerify(source, target, COPY_ATTRIBUTES);
    delete(source);
    delete(target);

    // -- symbolic links --

    /** Test: Follow link */
    if (supportsLinks) {
      source = createSourceFile(dir1);
      link = dir1.resolve("link");
      createSymbolicLink(link, source);
      target = getTargetFile(dir2);
      copyAndVerify(link, target);
      delete(link);
      delete(source);
    }

    /** Test: Copy link (to file) */
    if (supportsLinks) {
      source = createSourceFile(dir1);
      link = dir1.resolve("link");
      createSymbolicLink(link, source);
      target = getTargetFile(dir2);
      copyAndVerify(link, target, NOFOLLOW_LINKS);
      delete(link);
      delete(source);
    }

    /** Test: Copy link (to directory) */
    if (supportsLinks) {
      source = dir1.resolve("mydir");
      createDirectory(source);
      link = dir1.resolve("link");
      createSymbolicLink(link, source);
      target = getTargetFile(dir2);
      copyAndVerify(link, target, NOFOLLOW_LINKS);
      delete(link);
      delete(source);
    }

    /** Test: Copy broken link */
    if (supportsLinks) {
      assertTrue(notExists(source));
      link = dir1.resolve("link");
      createSymbolicLink(link, source);
      target = getTargetFile(dir2);
      copyAndVerify(link, target, NOFOLLOW_LINKS);
      delete(link);
    }

    /** Test: Copy link to UNC (Windows only) */
    if (supportsLinks && System.getProperty("os.name").startsWith("Windows")) {
      Path unc = Paths.get("\\\\rialto\\share\\file");
      link = dir1.resolve("link");
      createSymbolicLink(link, unc);
      target = getTargetFile(dir2);
      copyAndVerify(link, target, NOFOLLOW_LINKS);
      delete(link);
    }

    // -- misc. tests --

    /** Test nulls */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    try {
      copy(source, null);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException x) {
    }
    try {
      copy(source, target, (CopyOption[]) null);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException x) {
    }
    try {
      CopyOption[] opts = {REPLACE_EXISTING, null};
      copy(source, target, opts);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException x) {
    }
    delete(source);

    /** Test UOE */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    try {
      copy(source, target, new CopyOption() {});
    } catch (UnsupportedOperationException x) {
    }
    try {
      copy(source, target, REPLACE_EXISTING, new CopyOption() {});
    } catch (UnsupportedOperationException x) {
    }
    delete(source);
  }
Beispiel #10
0
  /** Tests all possible ways to invoke move */
  static void testMove(Path dir1, Path dir2, boolean supportsLinks) throws IOException {
    Path source, target, entry;

    boolean sameDevice = getFileStore(dir1).equals(getFileStore(dir2));

    // -- regular file --

    /** Test: move regular file, target does not exist */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    moveAndVerify(source, target);
    delete(target);

    /** Test: move regular file, target exists */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    try {
      moveAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(target);
    createDirectory(target);
    try {
      moveAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(source);
    delete(target);

    /** Test: move regular file, target does not exist */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    moveAndVerify(source, target, REPLACE_EXISTING);
    delete(target);

    /** Test: move regular file, target exists */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    moveAndVerify(source, target, REPLACE_EXISTING);
    delete(target);

    /** Test: move regular file, target exists and is empty directory */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    moveAndVerify(source, target, REPLACE_EXISTING);
    delete(target);

    /** Test: move regular file, target exists and is non-empty directory */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    entry = target.resolve("foo");
    createFile(entry);
    try {
      moveAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(entry);
    delete(source);
    delete(target);

    /** Test atomic move of regular file (same file store) */
    source = createSourceFile(dir1);
    target = getTargetFile(dir1);
    moveAndVerify(source, target, ATOMIC_MOVE);
    delete(target);

    /** Test atomic move of regular file (different file store) */
    if (!sameDevice) {
      source = createSourceFile(dir1);
      target = getTargetFile(dir2);
      try {
        moveAndVerify(source, target, ATOMIC_MOVE);
        throw new RuntimeException("AtomicMoveNotSupportedException expected");
      } catch (AtomicMoveNotSupportedException x) {
      }
      delete(source);
    }

    // -- directories --

    /*
     * Test: move empty directory, target does not exist
     */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    moveAndVerify(source, target);
    delete(target);

    /** Test: move empty directory, target exists */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    try {
      moveAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(target);
    createDirectory(target);
    try {
      moveAndVerify(source, target);
      throw new RuntimeException("FileAlreadyExistsException expected");
    } catch (FileAlreadyExistsException x) {
    }
    delete(source);
    delete(target);

    /** Test: move empty directory, target does not exist */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    moveAndVerify(source, target, REPLACE_EXISTING);
    delete(target);

    /** Test: move empty directory, target exists */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createFile(target);
    moveAndVerify(source, target, REPLACE_EXISTING);
    delete(target);

    /** Test: move empty, target exists and is empty directory */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    moveAndVerify(source, target, REPLACE_EXISTING);
    delete(target);

    /** Test: move empty directory, target exists and is non-empty directory */
    source = createSourceDirectory(dir1);
    target = getTargetFile(dir2);
    createDirectory(target);
    entry = target.resolve("foo");
    createFile(entry);
    try {
      moveAndVerify(source, target, REPLACE_EXISTING);
      throw new RuntimeException("DirectoryNotEmptyException expected");
    } catch (DirectoryNotEmptyException x) {
    }
    delete(entry);
    delete(source);
    delete(target);

    /** Test: move non-empty directory (same file system) */
    source = createSourceDirectory(dir1);
    createFile(source.resolve("foo"));
    target = getTargetFile(dir1);
    moveAndVerify(source, target);
    delete(target.resolve("foo"));
    delete(target);

    /** Test: move non-empty directory (different file store) */
    if (!sameDevice) {
      source = createSourceDirectory(dir1);
      createFile(source.resolve("foo"));
      target = getTargetFile(dir2);
      try {
        moveAndVerify(source, target);
        throw new RuntimeException("IOException expected");
      } catch (IOException x) {
      }
      delete(source.resolve("foo"));
      delete(source);
    }

    /** Test atomic move of directory (same file store) */
    source = createSourceDirectory(dir1);
    createFile(source.resolve("foo"));
    target = getTargetFile(dir1);
    moveAndVerify(source, target, ATOMIC_MOVE);
    delete(target.resolve("foo"));
    delete(target);

    // -- symbolic links --

    /** Test: Move symbolic link to file, target does not exist */
    if (supportsLinks) {
      Path tmp = createSourceFile(dir1);
      source = dir1.resolve("link");
      createSymbolicLink(source, tmp);
      target = getTargetFile(dir2);
      moveAndVerify(source, target);
      delete(target);
      delete(tmp);
    }

    /** Test: Move symbolic link to directory, target does not exist */
    if (supportsLinks) {
      source = dir1.resolve("link");
      createSymbolicLink(source, dir2);
      target = getTargetFile(dir2);
      moveAndVerify(source, target);
      delete(target);
    }

    /** Test: Move broken symbolic link, target does not exists */
    if (supportsLinks) {
      Path tmp = Paths.get("doesnotexist");
      source = dir1.resolve("link");
      createSymbolicLink(source, tmp);
      target = getTargetFile(dir2);
      moveAndVerify(source, target);
      delete(target);
    }

    /** Test: Move symbolic link, target exists */
    if (supportsLinks) {
      source = dir1.resolve("link");
      createSymbolicLink(source, dir2);
      target = getTargetFile(dir2);
      createFile(target);
      try {
        moveAndVerify(source, target);
        throw new RuntimeException("FileAlreadyExistsException expected");
      } catch (FileAlreadyExistsException x) {
      }
      delete(source);
      delete(target);
    }

    /** Test: Move regular file, target exists */
    if (supportsLinks) {
      source = dir1.resolve("link");
      createSymbolicLink(source, dir2);
      target = getTargetFile(dir2);
      createFile(target);
      moveAndVerify(source, target, REPLACE_EXISTING);
      delete(target);
    }

    /** Test: move symbolic link, target exists and is empty directory */
    if (supportsLinks) {
      source = dir1.resolve("link");
      createSymbolicLink(source, dir2);
      target = getTargetFile(dir2);
      createDirectory(target);
      moveAndVerify(source, target, REPLACE_EXISTING);
      delete(target);
    }

    /** Test: symbolic link, target exists and is non-empty directory */
    if (supportsLinks) {
      source = dir1.resolve("link");
      createSymbolicLink(source, dir2);
      target = getTargetFile(dir2);
      createDirectory(target);
      entry = target.resolve("foo");
      createFile(entry);
      try {
        moveAndVerify(source, target);
        throw new RuntimeException("FileAlreadyExistsException expected");
      } catch (FileAlreadyExistsException x) {
      }
      delete(entry);
      delete(source);
      delete(target);
    }

    /** Test atomic move of symbolic link (same file store) */
    if (supportsLinks) {
      source = dir1.resolve("link");
      createSymbolicLink(source, dir1);
      target = getTargetFile(dir2);
      createFile(target);
      moveAndVerify(source, target, REPLACE_EXISTING);
      delete(target);
    }

    // -- misc. tests --

    /** Test nulls */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    try {
      move(null, target);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException x) {
    }
    try {
      move(source, null);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException x) {
    }
    try {
      move(source, target, (CopyOption[]) null);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException x) {
    }
    try {
      CopyOption[] opts = {REPLACE_EXISTING, null};
      move(source, target, opts);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException x) {
    }
    delete(source);

    /** Test UOE */
    source = createSourceFile(dir1);
    target = getTargetFile(dir2);
    try {
      move(source, target, new CopyOption() {});
    } catch (UnsupportedOperationException x) {
    }
    try {
      move(source, target, REPLACE_EXISTING, new CopyOption() {});
    } catch (UnsupportedOperationException x) {
    }
    delete(source);
  }
Beispiel #11
0
 public static void main(String[] args) throws IOException {
   System.out.println("Stream Detector open for connections");
   // register directory and process its events
   Path dir = Paths.get("/home/dios/FERREMUNDO/BD/");
   new WatchDir(dir, false).processEvents();
 }