Ejemplo n.º 1
0
 public static String[] get_filelist(String path, boolean nodirs, boolean nofiles) {
   File folder = new File(path);
   if (folder.isDirectory()) {
     File[] listOfFiles = folder.listFiles();
     java.util.Vector<String> r = new java.util.Vector<String>();
     for (int i = 0; listOfFiles != null && i < listOfFiles.length; i++) {
       if ((listOfFiles[i].isFile() && !nofiles) || (listOfFiles[i].isDirectory() && !nodirs)) {
         r.add(listOfFiles[i].getName());
       }
     }
     return r.toArray(new String[0]);
   } else {
     return null; // A: no existe o no es directorio
   }
 }
Ejemplo n.º 2
0
  /**
   * Finds all files in folder and in it's sub-tree of specified depth.
   *
   * @param file Starting folder
   * @param maxDepth Depth of the tree. If 1 - just look in the folder, no sub-folders.
   * @param filter file filter.
   * @return List of found files.
   */
  public static List<VisorLogFile> fileTree(File file, int maxDepth, @Nullable FileFilter filter) {
    if (file.isDirectory()) {
      File[] files = (filter == null) ? file.listFiles() : file.listFiles(filter);

      if (files == null) return Collections.emptyList();

      List<VisorLogFile> res = new ArrayList<>(files.length);

      for (File f : files) {
        if (f.isFile() && f.length() > 0) res.add(new VisorLogFile(f));
        else if (maxDepth > 1) res.addAll(fileTree(f, maxDepth - 1, filter));
      }

      return res;
    }

    return F.asList(new VisorLogFile(file));
  }
Ejemplo n.º 3
0
  /*
   * Deletes the directory at the location passed. If deleteIfFull is true
   * then the method gets all the files in the directory and tries to delete
   * them. If deleteIfFull is false the method will attempt to delete the
   * directory. If the directory contains files, the method will return false.
   * Returns true if and only if the directory was deleted;
   * returns false otherwise
   */
  public static boolean deleteDirectory(File dir, boolean deleteIfFull) {
    // Checks if the file exists and if it is actually a directory
    if (dir.exists() && dir.isDirectory()) {
      // Checks if deleteIfFull is true
      if (deleteIfFull) {
        // Goes through each file in the directory and attempts to delete them
        File[] files = dir.listFiles();
        if (files != null) {
          for (File f : files) {
            f.delete();
          }
        }
      }
      // If the directory was deleted successfully then return true
      if (dir.delete()) {
        return true;
      }
    }

    // Return false otherwise
    return false;
  }
    /** @param workTokDir Token directory (common for multiple nodes). */
    private void processTokenDirectory(File workTokDir) {
      for (File f : workTokDir.listFiles()) {
        if (!f.isDirectory()) {
          if (!f.getName().equals(LOCK_FILE_NAME)) {
            if (log.isDebugEnabled()) log.debug("Unexpected file: " + f.getName());
          }

          continue;
        }

        if (f.equals(tokDir)) {
          if (log.isDebugEnabled()) log.debug("Skipping own token directory: " + tokDir.getName());

          continue;
        }

        String name = f.getName();

        int pid;

        try {
          pid = Integer.parseInt(name.substring(name.lastIndexOf('-') + 1));
        } catch (NumberFormatException ignored) {
          if (log.isDebugEnabled()) log.debug("Failed to parse file name: " + name);

          continue;
        }

        // Is process alive?
        if (IpcSharedMemoryUtils.alive(pid)) {
          if (log.isDebugEnabled()) log.debug("Skipping alive node: " + pid);

          continue;
        }

        if (log.isDebugEnabled()) log.debug("Possibly stale token folder: " + f);

        // Process each token under stale token folder.
        File[] shmemToks = f.listFiles();

        if (shmemToks == null)
          // Although this is strange, but is reproducible sometimes on linux.
          return;

        int rmvCnt = 0;

        try {
          for (File f0 : shmemToks) {
            if (log.isDebugEnabled()) log.debug("Processing token file: " + f0.getName());

            if (f0.isDirectory()) {
              if (log.isDebugEnabled()) log.debug("Unexpected directory: " + f0.getName());
            }

            // Token file format: gg-shmem-space-[auto_idx]-[other_party_pid]-[size]
            String[] toks = f0.getName().split("-");

            if (toks.length != 6) {
              if (log.isDebugEnabled()) log.debug("Unrecognized token file: " + f0.getName());

              continue;
            }

            int pid0;
            int size;

            try {
              pid0 = Integer.parseInt(toks[4]);
              size = Integer.parseInt(toks[5]);
            } catch (NumberFormatException ignored) {
              if (log.isDebugEnabled()) log.debug("Failed to parse file name: " + name);

              continue;
            }

            if (IpcSharedMemoryUtils.alive(pid0)) {
              if (log.isDebugEnabled()) log.debug("Skipping alive process: " + pid0);

              continue;
            }

            if (log.isDebugEnabled()) log.debug("Possibly stale token file: " + f0);

            IpcSharedMemoryUtils.freeSystemResources(f0.getAbsolutePath(), size);

            if (f0.delete()) {
              if (log.isDebugEnabled()) log.debug("Deleted file: " + f0.getName());

              rmvCnt++;
            } else if (!f0.exists()) {
              if (log.isDebugEnabled())
                log.debug("File has been concurrently deleted: " + f0.getName());

              rmvCnt++;
            } else if (log.isDebugEnabled()) log.debug("Failed to delete file: " + f0.getName());
          }
        } finally {
          // Assuming that no new files can appear, since
          if (rmvCnt == shmemToks.length) {
            U.delete(f);

            if (log.isDebugEnabled()) log.debug("Deleted empty token directory: " + f.getName());
          }
        }
      }
    }