static void createParentDirectories(final File target, String[] path, int depth, final PatchingTaskContext context) throws IOException { if (depth > 0) { final File parent = target.getParentFile(); if (! parent.exists()) { createParentDirectories(parent, path, depth - 1, context); } if(! parent.mkdir() && ! parent.isDirectory()) { throw PatchLogger.ROOT_LOGGER.cannotCreateDirectory(target.getAbsolutePath()); } // TODO record changes // final String[] newPath = Arrays.copyOf(path, depth - 1); // final MiscContentItem item = new MiscContentItem(parent.getName(), newPath, NO_CONTENT); // context.recordChange(new ContentModification(item, NO_CONTENT, ModificationType.ADD), new ContentModification(item, NO_CONTENT, ModificationType.REMOVE)); } }
@Override byte[] backup(PatchingTaskContext context) throws IOException { if(target.isFile()) { // Backup the original in the history directory final byte[] backupHash = IoUtils.copy(target, backup); return backupHash; } else if (contentItem.isDirectory() && target.isDirectory()) { // Completely ignore the apply step if the directory already exists // This will basically skip the creation of the rollback item setIgnoreApply(); } return NO_CONTENT; }
@Override ContentModification createRollbackEntry(ContentModification original, byte[] targetHash, byte[] itemHash) { final MiscContentItem item = new MiscContentItem(contentItem.getName(), contentItem.getPath(), itemHash, contentItem.isDirectory(), contentItem.isAffectsRuntime()); return createRollbackEntry(original, item, targetHash); }
protected ContentModification getOriginalModification(byte[] targetHash, byte[] itemHash) { final ContentModification original = super.getOriginalModification(targetHash, itemHash); final ModificationType type; if (Arrays.equals(NO_CONTENT, itemHash) && !backup.exists()) { type = ModificationType.ADD; } else { type = ModificationType.MODIFY; } return new ContentModification(original.getItem(), original.getTargetHash(), type, original.getCondition()); } }
@Override ContentModification createRollbackEntry(ContentModification original, byte[] targetHash, byte[] itemHash) { final MiscContentItem item = new MiscContentItem(contentItem.getName(), contentItem.getPath(), itemHash, contentItem.isDirectory(), contentItem.isAffectsRuntime()); return createRollbackEntry(original, item, targetHash); }
protected ContentModification getOriginalModification(byte[] targetHash, byte[] itemHash) { final ContentModification original = super.getOriginalModification(targetHash, itemHash); final ModificationType type; if (Arrays.equals(NO_CONTENT, itemHash) && !backup.exists()) { type = ModificationType.ADD; } else { type = ModificationType.MODIFY; } return new ContentModification(original.getItem(), original.getTargetHash(), type, original.getCondition()); } }
static void createParentDirectories(final File target, String[] path, int depth, final PatchingTaskContext context) throws IOException { if (depth > 0) { final File parent = target.getParentFile(); if (! parent.exists()) { createParentDirectories(parent, path, depth - 1, context); } if(! parent.mkdir() && ! parent.isDirectory()) { throw PatchLogger.ROOT_LOGGER.cannotCreateDirectory(target.getAbsolutePath()); } // TODO record changes // final String[] newPath = Arrays.copyOf(path, depth - 1); // final MiscContentItem item = new MiscContentItem(parent.getName(), newPath, NO_CONTENT); // context.recordChange(new ContentModification(item, NO_CONTENT, ModificationType.ADD), new ContentModification(item, NO_CONTENT, ModificationType.REMOVE)); } }
@Override byte[] backup(PatchingTaskContext context) throws IOException { if(target.isFile()) { // Backup the original in the history directory final byte[] backupHash = IoUtils.copy(target, backup); return backupHash; } else if (contentItem.isDirectory() && target.isDirectory()) { // Completely ignore the apply step if the directory already exists // This will basically skip the creation of the rollback item setIgnoreApply(); } return NO_CONTENT; }
@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); } } }
@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); } } }