@Test public void testDeleteParentDirectoriesIfEmptyCatchesFileNotFoundException() throws Exception { FileSystem fs = mock(FileSystem.class); Path root = mock(Path.class); Path path = mock(Path.class); Path firstParent = mock(Path.class); when(path.getParent()).thenReturn(firstParent); when(firstParent.getParent()).thenReturn(root); when(fs.listStatus(firstParent)).thenThrow(FileNotFoundException.class); boolean pathDeleted = FileSystemUtil.deleteParentDirectoriesIfEmpty(fs, root, path); verify(fs).listStatus(firstParent); assertFalse(pathDeleted); }
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);  }