void backup(final File root, final File backupLocation, final List<String> path, final List<ContentModification> rollback, final PatchingTaskContext context) throws IOException { if(!root.exists()) { // Perhaps an error condition? } else if(root.isDirectory()) { final File[] files = root.listFiles(); final String rootName = root.getName(); if(files == null || files.length == 0) { // Create empty directory rollback.add(createRollbackItem(rootName, path, NO_CONTENT, true)); } else { final List<String> newPath = new ArrayList<String>(path); newPath.add(rootName); for (File file : files) { final String name = file.getName(); final File newBackupLocation = new File(backupLocation, name); backup(file, newBackupLocation, newPath, rollback, context); } } } else { // Copy and record the backup action final byte[] hash = copy(root, backupLocation); rollback.add(createRollbackItem(root.getName(), path, hash, false)); } }
static PatchingTask createMiscTask(final PatchingTaskDescription description, final MiscContentItem item, final PatchingTaskContext context) { // Create the task final File target = context.getTargetFile(item); final File backup = context.getBackupFile(item); final ModificationType type = description.getModificationType(); switch (type) { case ADD: case MODIFY: return new FileUpdateTask(description, target, backup); case REMOVE: return new FileRemoveTask(description, target, backup); default: throw new IllegalStateException(); } } }
@Override public boolean prepare(PatchingTaskContext context) throws IOException { // we create the backup in any case, since it is possible that the task // will be processed anyhow if the user specified OVERRIDE_ALL policy. // If the task is undone, the patch history will be deleted (including this backup). backup(target, backup, Arrays.asList(item.getPath()), rollback, context); // See if the hash matches the metadata boolean isEmptyDirectory = false; if (target.isDirectory()) { final File[] children = target.listFiles(); if (children == null || children.length == 0) { isEmptyDirectory = true; } } final byte[] expected = description.getModification().getTargetHash(); final byte[] actual = isEmptyDirectory ? NO_CONTENT : HashUtils.hashFile(target); return Arrays.equals(expected, actual); }
static PatchingTask createMiscTask(final PatchingTaskDescription description, final MiscContentItem item, final PatchingTaskContext context) { // Create the task final File target = context.getTargetFile(item); final File backup = context.getBackupFile(item); final ModificationType type = description.getModificationType(); switch (type) { case ADD: case MODIFY: return new FileUpdateTask(description, target, backup); case REMOVE: return new FileRemoveTask(description, target, backup); default: throw new IllegalStateException(); } } }
@Override public boolean prepare(PatchingTaskContext context) throws IOException { // we create the backup in any case, since it is possible that the task // will be processed anyhow if the user specified OVERRIDE_ALL policy. // If the task is undone, the patch history will be deleted (including this backup). backup(target, backup, Arrays.asList(item.getPath()), rollback, context); // See if the hash matches the metadata boolean isEmptyDirectory = false; if (target.isDirectory()) { final File[] children = target.listFiles(); if (children == null || children.length == 0) { isEmptyDirectory = true; } } final byte[] expected = description.getModification().getTargetHash(); final byte[] actual = isEmptyDirectory ? NO_CONTENT : HashUtils.hashFile(target); return Arrays.equals(expected, actual); }
void backup(final File root, final File backupLocation, final List<String> path, final List<ContentModification> rollback, final PatchingTaskContext context) throws IOException { if(!root.exists()) { // Perhaps an error condition? } else if(root.isDirectory()) { final File[] files = root.listFiles(); final String rootName = root.getName(); if(files == null || files.length == 0) { // Create empty directory rollback.add(createRollbackItem(rootName, path, NO_CONTENT, true)); } else { final List<String> newPath = new ArrayList<String>(path); newPath.add(rootName); for (File file : files) { final String name = file.getName(); final File newBackupLocation = new File(backupLocation, name); backup(file, newBackupLocation, newPath, rollback, context); } } } else { // Copy and record the backup action final byte[] hash = copy(root, backupLocation); rollback.add(createRollbackItem(root.getName(), path, hash, false)); } }