/** * Delete inactive contents. */ public void deleteInactiveContent() throws PatchingException { List<File> dirs = getInactiveHistory(); if (!dirs.isEmpty()) { for (File dir : dirs) { deleteDir(dir, ALL); } } dirs = getInactiveOverlays(); if (!dirs.isEmpty()) { for (File dir : dirs) { deleteDir(dir, ALL); } } }
/** * Get the garbage locator. * * @param installedIdentity the installed identity * @return the garbage locator */ public static PatchingGarbageLocator getIninitialized(InstalledIdentity installedIdentity) { return new PatchingGarbageLocator(installedIdentity); }
@Test public void testRemovedLastOneOffRollbackXml() throws Exception { installOneOffCpOneOff(); removeRollbackXml(ONE_OFF_2_ID); PatchingGarbageLocator garbageLocator = PatchingGarbageLocator.getIninitialized(updateInstallationManager().getDefaultIdentity()); final List<File> inactiveHistory = garbageLocator.getInactiveHistory(); List<String> inactivePaths = Arrays.asList(new String[] { getExpectedHistoryDir(ONE_OFF_1_ID) }); assertEqualPaths(inactivePaths, inactiveHistory); final List<File> inactiveOverlays = garbageLocator.getInactiveOverlays(); inactivePaths = Arrays.asList(new String[]{getExpectedOverlayDir("base", ONE_OFF_1_ID)}); assertEqualPaths(inactivePaths, inactiveOverlays); // test cleaning garbageLocator.deleteInactiveContent(); garbageLocator.reset(); assertTrue(garbageLocator.getInactiveHistory().isEmpty()); assertTrue(garbageLocator.getInactiveOverlays().isEmpty()); assertTrue(new File(getExpectedOverlayDir("base", ONE_OFF_2_ID)).exists()); }
protected void assertNoGarbage() throws Exception { final PatchingGarbageLocator garbageLocator = PatchingGarbageLocator.getIninitialized(loadInstallationManager().getDefaultIdentity()); List<File> inactiveHistory = garbageLocator.getInactiveHistory(); assertTrue(inactiveHistory.toString(), inactiveHistory.isEmpty()); assertTrue(garbageLocator.getInactiveOverlays().toString(), garbageLocator.getInactiveOverlays().isEmpty()); }
if(cleanup) { try { final PatchingGarbageLocator garbageLocator = PatchingGarbageLocator.getIninitialized(installedIdentity); garbageLocator.deleteInactiveContent(); } catch (Exception e) { PatchLogger.ROOT_LOGGER.debugf(e, "failed to garbage collect changes");
/** * Get the inactive history directories. * * @return the inactive history */ public List<File> getInactiveHistory() throws PatchingException { if (validHistory == null) { walk(); } final File[] inactiveDirs = installedIdentity.getInstalledImage().getPatchesDir().listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory() && !validHistory.contains(pathname.getName()); } }); return inactiveDirs == null ? Collections.<File>emptyList() : Arrays.asList(inactiveDirs); }
protected static boolean deleteDir(File dir, FilenameFilter filter) { boolean success = true; final File[] files = dir.listFiles(filter); if (files != null) { for (int i = 0; i < files.length; i++) { File f = files[i]; if (f.isDirectory()) { // delete the directory and all of its contents. if (!deleteDir(f, filter)) { success = false; log.debugf("Failed to delete dir: %s", f.getAbsolutePath()); } } // delete each file in the directory else if (!f.delete()) { success = false; log.debugf("Failed to delete file: %s", f.getAbsolutePath()); } } } // finally delete the directory if (!dir.delete()) { success = false; log.debugf("Failed to delete dir: %s", dir.getAbsolutePath()); } return success; }
@Test public void testUnpatchedWitGarbage() throws Exception { createDefaultBuilder("layer2", "layer1", "base"); final List<String> historyGarbage = Arrays.asList(new String[]{getExpectedHistoryDir("CP1"), getExpectedHistoryDir("CP2")}); for(int i = 0; i < historyGarbage.size(); ++i) { new File(historyGarbage.get(i)).mkdirs(); } final List<String> overlayGarbage = Arrays.asList(new String[]{getExpectedOverlayDir("layer2", "CP2"), getExpectedOverlayDir("layer1", "CP2"), getExpectedOverlayDir("base", "CP1")}); for(int i = 0; i < overlayGarbage.size(); ++i) { new File(overlayGarbage.get(i)).mkdirs(); } PatchingGarbageLocator garbageLocator = PatchingGarbageLocator.getIninitialized(loadInstallationManager().getDefaultIdentity()); final List<File> inactiveHistory = garbageLocator.getInactiveHistory(); assertEqualPaths(historyGarbage, inactiveHistory); final List<File> inactiveOverlays = garbageLocator.getInactiveOverlays(); assertEqualPaths(overlayGarbage, inactiveOverlays); garbageLocator.deleteInactiveContent(); garbageLocator.reset(); assertTrue(garbageLocator.getInactiveHistory().isEmpty()); assertTrue(garbageLocator.getInactiveOverlays().isEmpty()); }
@Test public void testRemovedCPRollbackXml() throws Exception { installOneOffCpOneOff(); removeRollbackXml(CP_1_ID); final PatchingGarbageLocator garbageLocator = PatchingGarbageLocator.getIninitialized(updateInstallationManager().getDefaultIdentity()); final List<File> inactiveHistory = garbageLocator.getInactiveHistory(); assertEquals(1, inactiveHistory.size()); assertEquals(getExpectedHistoryDir(ONE_OFF_1_ID), inactiveHistory.get(0).getAbsolutePath()); final List<File> inactiveOverlays = garbageLocator.getInactiveOverlays(); assertEquals(1, inactiveOverlays.size()); assertEquals(getExpectedOverlayDir("base", ONE_OFF_1_ID), inactiveOverlays.get(0).getAbsolutePath()); }
if(cleanup) { try { final PatchingGarbageLocator garbageLocator = PatchingGarbageLocator.getIninitialized(installedIdentity); garbageLocator.deleteInactiveContent(); } catch (Exception e) { PatchLogger.ROOT_LOGGER.debugf(e, "failed to garbage collect changes");
/** * Get the inactive history directories. * * @return the inactive history */ public List<File> getInactiveHistory() throws PatchingException { if (validHistory == null) { walk(); } final File[] inactiveDirs = installedIdentity.getInstalledImage().getPatchesDir().listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory() && !validHistory.contains(pathname.getName()); } }); return inactiveDirs == null ? Collections.<File>emptyList() : Arrays.asList(inactiveDirs); }
protected static boolean deleteDir(File dir, FilenameFilter filter) { boolean success = true; final File[] files = dir.listFiles(filter); if (files != null) { for (int i = 0; i < files.length; i++) { File f = files[i]; if (f.isDirectory()) { // delete the directory and all of its contents. if (!deleteDir(f, filter)) { success = false; log.debugf("Failed to delete dir: %s", f.getAbsolutePath()); } } // delete each file in the directory else if (!f.delete()) { success = false; log.debugf("Failed to delete file: %s", f.getAbsolutePath()); } } } // finally delete the directory if (!dir.delete()) { success = false; log.debugf("Failed to delete dir: %s", dir.getAbsolutePath()); } return success; }
PatchingGarbageLocator garbageLocator = PatchingGarbageLocator.getIninitialized(updateInstallationManager().getDefaultIdentity()); final List<File> inactiveHistory = garbageLocator.getInactiveHistory(); List<String> inactivePaths = Arrays.asList(new String[]{getExpectedHistoryDir("CP1"), getExpectedHistoryDir("CP2")}); assertEqualPaths(inactivePaths, inactiveHistory); final List<File> inactiveOverlays = garbageLocator.getInactiveOverlays(); inactivePaths = Arrays.asList(new String[]{getExpectedOverlayDir("layer2", "CP2"), getExpectedOverlayDir( "layer1", "CP2"), getExpectedOverlayDir("base", "CP1")}); garbageLocator.deleteInactiveContent(); garbageLocator.reset(); assertTrue(garbageLocator.getInactiveHistory().isEmpty()); assertTrue(garbageLocator.getInactiveOverlays().isEmpty());
/** * Delete inactive contents. */ public void deleteInactiveContent() throws PatchingException { List<File> dirs = getInactiveHistory(); if (!dirs.isEmpty()) { for (File dir : dirs) { deleteDir(dir, ALL); } } dirs = getInactiveOverlays(); if (!dirs.isEmpty()) { for (File dir : dirs) { deleteDir(dir, ALL); } } }
/** * Get the inactive overlay directories. * * @return the inactive overlay directories */ public List<File> getInactiveOverlays() throws PatchingException { if (referencedOverlayDirectories == null) { walk(); } List<File> inactiveDirs = null; for (Layer layer : installedIdentity.getLayers()) { final File overlaysDir = new File(layer.getDirectoryStructure().getModuleRoot(), Constants.OVERLAYS); final File[] inactiveLayerDirs = overlaysDir.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory() && !referencedOverlayDirectories.contains(pathname); } }); if (inactiveLayerDirs != null && inactiveLayerDirs.length > 0) { if (inactiveDirs == null) { inactiveDirs = new ArrayList<File>(); } inactiveDirs.addAll(Arrays.asList(inactiveLayerDirs)); } } return inactiveDirs == null ? Collections.<File>emptyList() : inactiveDirs; }
/** * Get the garbage locator. * * @param installedIdentity the installed identity * @return the garbage locator */ public static PatchingGarbageLocator getIninitialized(InstalledIdentity installedIdentity) { return new PatchingGarbageLocator(installedIdentity); }
/** * Get the inactive overlay directories. * * @return the inactive overlay directories */ public List<File> getInactiveOverlays() throws PatchingException { if (referencedOverlayDirectories == null) { walk(); } List<File> inactiveDirs = null; for (Layer layer : installedIdentity.getLayers()) { final File overlaysDir = new File(layer.getDirectoryStructure().getModuleRoot(), Constants.OVERLAYS); final File[] inactiveLayerDirs = overlaysDir.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory() && !referencedOverlayDirectories.contains(pathname); } }); if (inactiveLayerDirs != null && inactiveLayerDirs.length > 0) { if (inactiveDirs == null) { inactiveDirs = new ArrayList<File>(); } inactiveDirs.addAll(Arrays.asList(inactiveLayerDirs)); } } return inactiveDirs == null ? Collections.<File>emptyList() : inactiveDirs; }