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 byte[] copy(File source, File target) throws IOException { try (final FileInputStream is = new FileInputStream(source)){ return copy(is, target); } }
/** * Copy input stream to output stream and close them both * * @param is input stream * @param os output stream * * @throws IOException for any error */ public static void copyStreamAndClose(InputStream is, OutputStream os) throws IOException { try { copyStream(is, os, DEFAULT_BUFFER_SIZE); // throw an exception if the close fails since some data might be lost is.close(); os.close(); } finally { // ...but still guarantee that they're both closed safeClose(is); safeClose(os); } }
public static void writeProperties(final File file, final Properties properties) throws IOException { final OutputStream os = new FileOutputStream(file); try { final Writer writer = new OutputStreamWriter(os, StandardCharsets.UTF_8); properties.store(writer, "read only"); writer.close(); } finally { safeClose(os); } }
File patchDir = mkdir(tempDir, patchID); File baseModuleDir = newFile(env.getInstalledImage().getModulesDir(), SYSTEM, LAYERS, BASE); String moduleName = "module-test"; File moduleDir = createModule0(baseModuleDir, moduleName); final File patchBundleDir = mkdir(tempDir, "bundle"); IoUtils.copy(zippedOneOff, new File(patchBundleDir, zippedOneOff.getName())); IoUtils.copy(zippedCP, new File(patchBundleDir, zippedCP.getName())); final List<BundledPatch.BundledPatchEntry> bundledPatches = new ArrayList<BundledPatch.BundledPatchEntry>(); bundledPatches.add(new BundledPatch.BundledPatchEntry(patchID, zippedOneOff.getName())); assertFalse(reader.ready()); } finally { IoUtils.safeClose(bis); assertFalse(reader.ready()); } finally { IoUtils.safeClose(bis);
@Override byte[] apply(PatchingTaskContext context, PatchContentLoader loader) throws IOException { final MiscContentItem item = contentItem; if(item.isDirectory()) { if(! target.mkdirs() && ! target.isDirectory()) { throw PatchLogger.ROOT_LOGGER.cannotCreateDirectory(target.getAbsolutePath()); } return NO_CONTENT; } else { if (!target.exists()) { createParentDirectories(target, item.getPath(), item.getPath().length, context); } final InputStream is = loader.openContentStream(item); try { // Replace the file return copy(is, target); } finally { safeClose(is); } } }
private static void installAddOn(File baseDir, String... addOns) throws Exception { for (String addOn : addOns) { IoUtils.mkdir(baseDir, "system", ADD_ONS, addOn); } } }
private void prepareCP3(final InstalledIdentity installedIdentity) throws IOException, Exception { final String cp3ID = "CP3"; final File cp3Dir = mkdir(tempDir, cp3ID); cp3StandaloneModified = ContentModificationUtils.modifyMisc(cp3Dir, cp3ID, "updated by cp3", standaloneSh, "bin", standaloneSh.getName()); cp3BaseModuleModified = ContentModificationUtils.modifyModule(cp3Dir, baseCP3ID, moduleName, cp2BaseModuleModified.getItem().getContentHash(), "cp3 content"); // cp3BaseModule2Added = ContentModificationUtils.addModule(cp3Dir, baseCP3ID, moduleName + "2"); the patchgen tool // would generate an update instead final File absentModuleXml = IoUtils.newFile(installedIdentity.getLayer("base").loadTargetInfo() .getDirectoryStructure().getModulePatchDirectory(baseCP2ID), "org", "jboss", "test2", "main", "module.xml"); cp3BaseModule2Added = ContentModificationUtils.modifyModule(cp3Dir, baseCP3ID, moduleName + "2", HashUtils.hashFile(absentModuleXml), "cp3 content"); final ContentModification cp3AddedByCP1RemovedByCP3Removed = ContentModificationUtils.removeMisc(addedByCP1RemovedByCP3, "bin", addedByCP1RemovedByCP3.getName()); cp3 = PatchBuilder.create() .setPatchId(cp3ID) .setDescription(randomString()) .upgradeIdentity(installedIdentity.getIdentity().getName(), productConfig.getProductVersion() + "_CP2", productConfig.getProductVersion() + "_CP3") .getParent() .upgradeElement(baseCP3ID, BASE, false) .addContentModification(cp3BaseModuleModified) .addContentModification(cp3BaseModule2Added) .getParent() .addContentModification(cp3StandaloneModified) .addContentModification(cp3AddedByCP1RemovedByCP3Removed) .build(); createPatchXMLFile(cp3Dir, cp3); cp3Zip = createZippedPatchFile(cp3Dir, cp3ID); }
/** * Copy input stream to output stream without closing streams. Flushes output stream when done. * * @param is input stream * @param os output stream * * @throws IOException for any error */ public static void copyStream(InputStream is, OutputStream os) throws IOException { copyStream(is, os, DEFAULT_BUFFER_SIZE); }
@Override public File getPatchHistoryDir(String patchId) { return newFile(getInstallationMetadata(), PATCHES, patchId); }
public static void copyFile(File sourceFile, File targetFile) throws IOException { if (sourceFile.isDirectory()) { copyDir(sourceFile, targetFile); } else { File parent = targetFile.getParentFile(); if (!parent.exists()) { if (!parent.mkdirs()) { throw PatchLogger.ROOT_LOGGER.cannotCreateDirectory(parent.getAbsolutePath()); } } try { Files.copy(sourceFile.toPath(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { throw PatchLogger.ROOT_LOGGER.cannotCopyFiles(sourceFile.getAbsolutePath(), targetFile.getAbsolutePath(), e.getMessage(), e); } } }
public static void writeProperties(final File file, final Properties properties) throws IOException { final OutputStream os = new FileOutputStream(file); try { final Writer writer = new OutputStreamWriter(os, StandardCharsets.UTF_8); properties.store(writer, "read only"); writer.close(); } finally { safeClose(os); } }
/** * Copy input stream to output stream and close them both * * @param is input stream * @param os output stream * * @throws IOException for any error */ public static void copyStreamAndClose(InputStream is, OutputStream os) throws IOException { try { copyStream(is, os, DEFAULT_BUFFER_SIZE); // throw an exception if the close fails since some data might be lost is.close(); os.close(); } finally { // ...but still guarantee that they're both closed safeClose(is); safeClose(os); } }
@Override byte[] apply(PatchingTaskContext context, PatchContentLoader loader) throws IOException { final MiscContentItem item = contentItem; if(item.isDirectory()) { if(! target.mkdirs() && ! target.isDirectory()) { throw PatchLogger.ROOT_LOGGER.cannotCreateDirectory(target.getAbsolutePath()); } return NO_CONTENT; } else { if (!target.exists()) { createParentDirectories(target, item.getPath(), item.getPath().length, context); } final InputStream is = loader.openContentStream(item); try { // Replace the file return copy(is, target); } finally { safeClose(is); } } }
@Before public void setUp() throws IOException { tempDir = mkdir(new File(SYSTEM_TEMP_DIR), randomString()); final File jbossHome = mkdir(tempDir, JBOSS_INSTALLATION); mkdir(jbossHome, MODULES, "system", LAYERS, BASE); mkdir(jbossHome, BUNDLES, "system", LAYERS, BASE); productConfig = new ProductConfig(PRODUCT_NAME, PRODUCT_VERSION, "main"); }
private void prepareCP2(final InstalledIdentity installedIdentity) throws Exception { final File cp2Dir = mkdir(tempDir, cp2ID); final File baseModule2Dir = IoUtils.newFile( installedIdentity.getLayer("base").loadTargetInfo().getDirectoryStructure().getModulePatchDirectory(baseCP1ID), "org", "jboss", "test2"); cp2BaseModule2Removed = ContentModificationUtils.removeModule(baseModule2Dir, moduleName + 2); final File layer2ModuleDir = IoUtils.newFile( installedIdentity.getLayer("layer2").loadTargetInfo().getDirectoryStructure().getModulePatchDirectory(layer2CP1ID), "org", "jboss", "test");