public static boolean recursiveDelete(File root) { if (root == null) { return true; } boolean ok = true; if (root.isDirectory()) { final File[] files = root.listFiles(); for (File file : files) { ok &= recursiveDelete(file); } return ok && (root.delete() || !root.exists()); } else { ok &= root.delete() || !root.exists(); } return ok; }
public static boolean recursiveDelete(File root) { if (root == null) { return true; } boolean ok = true; if (root.isDirectory()) { final File[] files = root.listFiles(); for (File file : files) { ok &= recursiveDelete(file); } return ok && (root.delete() || !root.exists()); } else { ok &= root.delete() || !root.exists(); } return ok; }
@Override public void execute(PatchingTaskContext context) throws IOException { // delete the file or directory recursively boolean ok = IoUtils.recursiveDelete(target); for(ContentModification mod : rollback) { // Add the rollback (add actions) // We skip the change - misc files are reused in the processed patch context.recordChange(null, mod); } if(! ok) { throw PatchLogger.ROOT_LOGGER.failedToDelete(target.getAbsolutePath()); } }
@Override public void execute(PatchingTaskContext context) throws IOException { // delete the file or directory recursively boolean ok = IoUtils.recursiveDelete(target); for(ContentModification mod : rollback) { // Add the rollback (add actions) // We skip the change - misc files are reused in the processed patch context.recordChange(null, mod); } if(! ok) { throw PatchLogger.ROOT_LOGGER.failedToDelete(target.getAbsolutePath()); } }
@After public void tearDown() { if (!IoUtils.recursiveDelete(tempDir)) { tempDir.deleteOnExit(); } }
@After public void tearDown() { if (!IoUtils.recursiveDelete(tempDir)) { tempDir.deleteOnExit(); } }
@Override public void operationCancelled(IdentityPatchContext context) { // Cleanup history, bundles and module patch directories final InstalledImage image = structure.getInstalledImage(); IoUtils.recursiveDelete(image.getPatchHistoryDir(patchId)); IoUtils.recursiveDelete(structure.getBundlesPatchDirectory(patchId)); IoUtils.recursiveDelete(structure.getModulePatchDirectory(patchId)); for (final PatchElement element : original.getElements()) { boolean addOn = element.getProvider().isAddOn(); final IdentityPatchContext.PatchEntry entry = context.getEntry(element.getProvider().getName(), addOn); final DirectoryStructure structure = entry.getDirectoryStructure(); IoUtils.recursiveDelete(structure.getBundlesPatchDirectory(element.getId())); IoUtils.recursiveDelete(structure.getModulePatchDirectory(element.getId())); } }
private PatchingResult applyPatch(final File parentWorkDir, final InputStream is, final ContentVerificationPolicy contentPolicy) throws PatchingException { File workDir = null; try { // Create a working dir workDir = parentWorkDir == null ? IdentityPatchRunner.createTempDir() : IdentityPatchRunner.createTempDir(parentWorkDir); try { // Save the content Path cachedContent = workDir.toPath().resolve("content"); Files.copy(is, cachedContent); // Unpack to the work dir ZipUtils.unzip(cachedContent.toFile(), workDir); } catch (IOException e) { throw PatchLogger.ROOT_LOGGER.cannotCopyFilesToTempDir(workDir.getAbsolutePath(), e.getMessage(), e); // add info that temp dir is involved } // Execute return execute(workDir, contentPolicy); } catch (Exception e) { throw rethrowException(e); } finally { if (workDir != null && !IoUtils.recursiveDelete(workDir)) { PatchLogger.ROOT_LOGGER.cannotDeleteFile(workDir.getAbsolutePath()); } } }
@Override public void operationCancelled(IdentityPatchContext context) { // Cleanup history, bundles and module patch directories final InstalledImage image = structure.getInstalledImage(); IoUtils.recursiveDelete(image.getPatchHistoryDir(patchId)); IoUtils.recursiveDelete(structure.getBundlesPatchDirectory(patchId)); IoUtils.recursiveDelete(structure.getModulePatchDirectory(patchId)); for (final PatchElement element : original.getElements()) { boolean addOn = element.getProvider().isAddOn(); final IdentityPatchContext.PatchEntry entry = context.getEntry(element.getProvider().getName(), addOn); final DirectoryStructure structure = entry.getDirectoryStructure(); IoUtils.recursiveDelete(structure.getBundlesPatchDirectory(element.getId())); IoUtils.recursiveDelete(structure.getModulePatchDirectory(element.getId())); } }
private PatchingResult applyPatch(final File parentWorkDir, final InputStream is, final ContentVerificationPolicy contentPolicy) throws PatchingException { File workDir = null; try { // Create a working dir workDir = parentWorkDir == null ? IdentityPatchRunner.createTempDir() : IdentityPatchRunner.createTempDir(parentWorkDir); try { // Save the content Path cachedContent = workDir.toPath().resolve("content"); Files.copy(is, cachedContent); // Unpack to the work dir ZipUtils.unzip(cachedContent.toFile(), workDir); } catch (IOException e) { throw PatchLogger.ROOT_LOGGER.cannotCopyFilesToTempDir(workDir.getAbsolutePath(), e.getMessage(), e); // add info that temp dir is involved } // Execute return execute(workDir, contentPolicy); } catch (Exception e) { throw rethrowException(e); } finally { if (workDir != null && !IoUtils.recursiveDelete(workDir)) { PatchLogger.ROOT_LOGGER.cannotDeleteFile(workDir.getAbsolutePath()); } } }
/** * Cleanup the history directories for all recorded rolled back patches. */ protected void cleanupRollbackPatchHistory() { final DirectoryStructure structure = getDirectoryStructure(); for (final String rollback : rollbacks) { if (!IoUtils.recursiveDelete(structure.getBundlesPatchDirectory(rollback))) { failedToCleanupDir(structure.getBundlesPatchDirectory(rollback)); } if (!IoUtils.recursiveDelete(structure.getModulePatchDirectory(rollback))) { failedToCleanupDir(structure.getModulePatchDirectory(rollback)); } } } }
/** * Cleanup the history directories for all recorded rolled back patches. */ protected void cleanupRollbackPatchHistory() { final DirectoryStructure structure = getDirectoryStructure(); for (final String rollback : rollbacks) { if (!IoUtils.recursiveDelete(structure.getBundlesPatchDirectory(rollback))) { failedToCleanupDir(structure.getBundlesPatchDirectory(rollback)); } if (!IoUtils.recursiveDelete(structure.getModulePatchDirectory(rollback))) { failedToCleanupDir(structure.getModulePatchDirectory(rollback)); } } } }
IoUtils.recursiveDelete(workDir); return merged;
IoUtils.recursiveDelete(workDir); return merged;
@Override public void completed(IdentityPatchContext context) { final InstalledImage installedImage = directoryStructure.getInstalledImage(); final File history = installedImage.getPatchHistoryDir(patch.getPatchId()); if (!recursiveDelete(history)) { context.failedToCleanupDir(history); } // Cleanup all the recorded rollbacks cleanupEntry(context.getLayers()); cleanupEntry(context.getAddOns()); cleanupEntry(Collections.singleton(context.getIdentityEntry())); }
@Test public void testSimpleMissingMiscFiles() throws Exception { final PatchingTestBuilder builder = installOneOff(); // Delete misc final File history = getHistory(builder, ONE_OFF_1_ID); final File misc = new File(history, Constants.MISC); IoUtils.recursiveDelete(misc); cannotRollbackPatch(ONE_OFF_1_ID); }
@Override public void completed(IdentityPatchContext context) { final InstalledImage installedImage = directoryStructure.getInstalledImage(); final File history = installedImage.getPatchHistoryDir(patch.getPatchId()); if (!recursiveDelete(history)) { context.failedToCleanupDir(history); } // Cleanup all the recorded rollbacks cleanupEntry(context.getLayers()); cleanupEntry(context.getAddOns()); cleanupEntry(Collections.singleton(context.getIdentityEntry())); }
@Test public void testSimpleMissingHistory() throws Exception { final PatchingTestBuilder builder = installOneOff(); // Delete complete history final File history = getHistory(builder, ONE_OFF_1_ID); IoUtils.recursiveDelete(history); cannotRollbackPatch(ONE_OFF_1_ID); }
@Test public void testMissingOverlay() throws Exception { final PatchingTestBuilder builder = installOneOffCpOneOff(); // Can rollback incl. the first one off validateRollbackState(ONE_OFF_1_ID, updateInstallationManager().getDefaultIdentity()); final File overlays = getOverlays(builder, "base", "base-" + ONE_OFF_1_ID); IoUtils.recursiveDelete(overlays); cannotRollbackPatch(ONE_OFF_1_ID); cannotRollbackPatch(CP_1_ID); }
@Test public void testMissingHistoryOneOff() throws Exception { final PatchingTestBuilder builder = installOneOffCpOneOff(); // Can rollback incl. the first one off validateRollbackState(ONE_OFF_1_ID, updateInstallationManager().getDefaultIdentity()); // Remove one off history final File oneOffHistory = getHistory(builder, ONE_OFF_1_ID); IoUtils.recursiveDelete(oneOffHistory); cannotRollbackPatch(ONE_OFF_1_ID); // Can rollback CP1 validateRollbackState(CP_1_ID, updateInstallationManager().getDefaultIdentity()); // Remove cp1 history final File cpHistory = getHistory(builder, CP_1_ID); IoUtils.recursiveDelete(cpHistory); cannotRollbackPatch(CP_1_ID); // Could still rollback 2nd one off validateRollbackState(ONE_OFF_2_ID, updateInstallationManager().getDefaultIdentity()); }