@Test public void testDeleteParentDirectoriesIfEmpty() throws Exception { Path root = new Path(temp.getRoot().toURI() + "/"); File subFolder = temp.newFolder("subFolder"); File subSubFolder = new File(subFolder, "subSubFolder"); Path subSubFolderPath = new Path(subSubFolder.toURI()); FileSystem fs = LocalFileSystem.getInstance(); boolean pathDeleted = FileSystemUtil.deleteParentDirectoriesIfEmpty(fs, root, subSubFolderPath); assertTrue(pathDeleted); assertFalse(subFolder.exists()); assertTrue(temp.getRoot().exists()); }
static boolean deleteParentDirectoriesIfEmpty(FileSystem fs, Path root, Path path) throws IOException { boolean deleted = false; try { for (Path current = path.getParent(); !current.equals(root) && !(current.getParent() == null); current = current.getParent()) { final FileStatus[] stats = fs.listStatus(current); if (stats == null || stats.length == 0) { LOG.debug("Deleting empty path {}", current); deleted = fs.delete(current, true) || deleted; } else { break; } } } catch (FileNotFoundException e) { LOG.debug("Path does not exist it may have been deleted by another process.", e); } return deleted; }
FileSystemUtil { static boolean deleteParentDirectoriesIfEmpty(FileSystem fs, Path root, Path path) throws IOException { boolean deleted = false; try { for (Path current = path.getParent(); !current.equals(root) && !(current.getParent() == null); current = current.getParent()) { final FileStatus[] stats = fs.listStatus(current); if (stats == null || stats.length == 0) { LOG.debug("Deleting empty path {}", current); deleted = fs.delete(current, true) || deleted; } else { break; } } } catch (FileNotFoundException e) { LOG.debug("Path does not exist it may have been deleted by another process.", e); } return deleted; } }
FileSystemUtil { static boolean deleteParentDirectoriesIfEmpty(FileSystem fs, Path root, Path path) throws IOException { boolean deleted = false; try { for (Path current = path.getParent(); !current.equals(root) && !(current.getParent() == null); current = current.getParent()) { final FileStatus[] stats = fs.listStatus(current); if (stats == null || stats.length == 0) { LOG.debug("Deleting empty path {}", current); deleted = fs.delete(current, true) || deleted; } else { break; } } } catch (FileNotFoundException e) { LOG.debug("Path does not exist it may have been deleted by another process.", e); } return deleted; }  }
FileSystemUtil { static boolean deleteParentDirectoriesIfEmpty(FileSystem fs, Path root, Path path) throws IOException { boolean deleted = false; try { for (Path current = path.getParent(); !current.equals(root) && !(current.getParent() == null); current = current.getParent()) { final FileStatus[] stats = fs.listStatus(current); if (stats == null || stats.length == 0) { LOG.debug("Deleting empty path {}", current); deleted = fs.delete(current, true) || deleted; } else { break; } } } catch (FileNotFoundException e) { LOG.debug("Path does not exist it may have been deleted by another process.", e); } return deleted; }  static void ensureLocationExists(
      DatasetDescriptor descriptor, Configuration conf); static Schema schema(String name, FileSystem fs, Path location); static PartitionStrategy strategy(FileSystem fs, Path location); static Format format(FileSystem fs, Path location); static Collection<DatasetDescriptor> findPotentialDatasets(
      FileSystem fs, Path path); static boolean supportsRename(URI fsUri, Configuration conf); }
FileSystemUtil { static boolean deleteParentDirectoriesIfEmpty(FileSystem fs, Path root, Path path) throws IOException { boolean deleted = false; try { for (Path current = path.getParent(); !current.equals(root) && !(current.getParent() == null); current = current.getParent()) { final FileStatus[] stats = fs.listStatus(current); if (stats == null || stats.length == 0) { LOG.debug("Deleting empty path {}", current); deleted = fs.delete(current, true) || deleted; } else { break; } } } catch (FileNotFoundException e) { LOG.debug("Path does not exist it may have been deleted by another process.", e); } return deleted; }  static void ensureLocationExists(
      DatasetDescriptor descriptor, Configuration conf); static Schema schema(String name, FileSystem fs, Path location); static PartitionStrategy strategy(FileSystem fs, Path location); static Format format(FileSystem fs, Path location); static Collection<DatasetDescriptor> findPotentialDatasets(
      FileSystem fs, Path path); static boolean supportsRename(URI fsUri, Configuration conf);  }