| | |
| | | * information: "Portions Copyright [year] [name of copyright owner]". |
| | | * |
| | | * Copyright 2006-2008 Sun Microsystems, Inc. |
| | | * Portions Copyright 2012-2015 ForgeRock AS. |
| | | * Portions Copyright 2012-2016 ForgeRock AS. |
| | | */ |
| | | |
| | | package org.opends.server.tools.upgrade; |
| | | |
| | | |
| | | import static com.forgerock.opendj.util.OperatingSystem.*; |
| | | |
| | | import static org.opends.messages.ToolMessages.*; |
| | | import static com.forgerock.opendj.util.OperatingSystem.isUnix; |
| | | |
| | | import org.forgerock.i18n.LocalizableMessage; |
| | | import org.opends.server.types.DirectoryException; |
| | | import org.opends.server.types.FilePermission; |
| | | import org.opends.server.util.StaticUtils; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileFilter; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | |
| | | import org.forgerock.i18n.LocalizableMessage; |
| | | import org.forgerock.i18n.slf4j.LocalizedLogger; |
| | | |
| | | |
| | | |
| | | import org.opends.server.types.DirectoryException; |
| | | import org.opends.server.types.FilePermission; |
| | | import org.opends.server.util.StaticUtils; |
| | | |
| | | /** |
| | | * Utility class for use by applications containing methods for managing file |
| | |
| | | */ |
| | | class FileManager |
| | | { |
| | | |
| | | /** |
| | | * Describes the approach taken to deleting a file or directory. |
| | | */ |
| | | /** Describes the approach taken to deleting a file or directory. */ |
| | | public static enum DeletionPolicy |
| | | { |
| | | |
| | | /** |
| | | * Delete the file or directory immediately. |
| | | */ |
| | | /** Delete the file or directory immediately. */ |
| | | DELETE_IMMEDIATELY, |
| | | |
| | | /** |
| | | * Mark the file or directory for deletion after the JVM has exited. |
| | | */ |
| | | /** Mark the file or directory for deletion after the JVM has exited. */ |
| | | DELETE_ON_EXIT, |
| | | |
| | | /** |
| | | * First try to delete the file immediately. If the deletion was |
| | | * unsuccessful mark the file for deleteion when the JVM has existed. |
| | | */ |
| | | DELETE_ON_EXIT_IF_UNSUCCESSFUL |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Upgrade's Log. |
| | | */ |
| | | /** Upgrade's Log. */ |
| | | private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); |
| | | |
| | | |
| | | |
| | | private FileManager() |
| | | { |
| | | // do nothing; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Deletes everything below the specified file. |
| | | * |
| | |
| | | deleteRecursively(file, null, DeletionPolicy.DELETE_IMMEDIATELY); |
| | | } |
| | | |
| | | |
| | | |
| | | private static void deleteRecursively(File file, FileFilter filter, |
| | | DeletionPolicy deletePolicy) throws IOException |
| | | { |
| | | operateRecursively(new DeleteOperation(file, deletePolicy), filter); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Copies everything below the specified file. |
| | | * |
| | |
| | | return co.getDestination(); |
| | | } |
| | | |
| | | |
| | | |
| | | private static void operateRecursively(FileOperation op, FileFilter filter) |
| | | throws IOException |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Renames the source file to the target file. If the target file exists it is |
| | | * first deleted. The rename and delete operation return values are checked |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * A file operation. |
| | | */ |
| | | /** A file operation. */ |
| | | private static abstract class FileOperation |
| | | { |
| | | |
| | | private File objectFile; |
| | | |
| | | |
| | | |
| | | /** |
| | | * Creates a new file operation. |
| | | * |
| | |
| | | this.objectFile = objectFile; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Gets the file to be operated on. |
| | | * |
| | |
| | | return objectFile; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Make a copy of this class for the child file. |
| | | * |
| | |
| | | */ |
| | | public abstract FileOperation copyForChild(File child); |
| | | |
| | | |
| | | |
| | | /** |
| | | * Execute this operation. |
| | | * |
| | |
| | | * if there is a problem. |
| | | */ |
| | | public abstract void apply() throws IOException; |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * A copy operation. |
| | | */ |
| | | /** A copy operation. */ |
| | | private static class CopyOperation extends FileOperation |
| | | { |
| | | |
| | | private File destination; |
| | | |
| | | private boolean overwrite; |
| | | |
| | | |
| | | |
| | | /** |
| | | * Create a new copy operation. |
| | | * |
| | |
| | | this.overwrite = overwrite; |
| | | } |
| | | |
| | | |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public FileOperation copyForChild(File child) |
| | | { |
| | | return new CopyOperation(child, destination, overwrite); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Returns the destination file that is the result of copying |
| | | * <code>objectFile</code> to <code>destDir</code>. |
| | |
| | | return this.destination; |
| | | } |
| | | |
| | | |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public void apply() throws IOException |
| | | { |
| | | final File objectFile = getObjectFile(); |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Returns the file permission on the selected file. |
| | | * |
| | |
| | | return FilePermission.decodeUNIXMode("644"); |
| | | } |
| | | return FilePermission.decodeUNIXMode("755"); |
| | | |
| | | } |
| | | else if (name.endsWith(".sh")) |
| | | { |
| | |
| | | return FilePermission.decodeUNIXMode("644"); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Creates the parent directory if it does not already exist. |
| | | * |
| | |
| | | return b; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * A delete operation. |
| | | */ |
| | | /** A delete operation. */ |
| | | private static class DeleteOperation extends FileOperation |
| | | { |
| | | |
| | | private DeletionPolicy deletionPolicy; |
| | | |
| | | |
| | | |
| | | /** |
| | | * Creates a delete operation. |
| | | * |
| | |
| | | this.deletionPolicy = deletionPolicy; |
| | | } |
| | | |
| | | |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public FileOperation copyForChild(File child) |
| | | { |
| | | return new DeleteOperation(child, deletionPolicy); |
| | | } |
| | | |
| | | |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public void apply() throws IOException |
| | | { |
| | | File file = getObjectFile(); |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |