/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2006-2008 Sun Microsystems, Inc.
* Portions copyright 2012-2013 ForgeRock AS.
*/
/**
* Utility class for use by applications containing methods for managing file
* system files. This class handles application notifications for interesting
* events.
*/
public class FileManager
{
/**
* Describes the approach taken to deleting a file or directory.
*/
public static enum DeletionPolicy
{
/**
* Delete the file or directory immediately.
*/
/**
* Mark the file or directory for deletion after the JVM has exited.
*/
/**
* First try to delete the file immediately. If the deletion was
* unsuccessful mark the file for deleteion when the JVM has existed.
*/
}
/**
* Upgrade's Log.
*/
private FileManager()
{
// do nothing;
}
/**
* Deletes everything below the specified file.
*
* @param file
* the path to be deleted.
* @throws IOException
* if something goes wrong.
*/
{
}
{
}
/**
* Copies everything below the specified file.
*
* @param objectFile
* the file to be copied.
* @param destDir
* the directory to copy the file to
* @param overwrite
* overwrite destination files.
* @return File representing the destination
* @throws IOException
* if something goes wrong.
*/
throws IOException
{
return co.getDestination();
}
throws IOException
{
{
{
{
{
}
}
else
{
}
}
else
{
{
{
}
}
{
{
}
}
else
{
}
}
}
else
{
}
}
/**
* 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
* for success and if unsuccessful, this method throws an exception.
*
* @param fileToRename
* The file to rename.
* @param target
* The file to which <code>fileToRename</code> will be moved.
* @throws IOException
* If a problem occurs while attempting to rename the file. On the
* Windows platform, this typically indicates that the file is in
* use by this or another application.
*/
{
{
synchronized (target)
{
{
{
}
}
}
{
.toString());
}
}
}
/**
* A file operation.
*/
private static abstract class FileOperation
{
/**
* Creates a new file operation.
*
* @param objectFile
* to be operated on
*/
{
this.objectFile = objectFile;
}
/**
* Gets the file to be operated on.
*
* @return File to be operated on
*/
{
return objectFile;
}
/**
* Make a copy of this class for the child file.
*
* @param child
* to act as the new file object
* @return FileOperation as the same type as this class
*/
/**
* Execute this operation.
*
* @throws IOException
* if there is a problem.
*/
}
/**
* A copy operation.
*/
{
private boolean overwrite;
/**
* Create a new copy operation.
*
* @param objectFile
* to copy
* @param destDir
* to copy to
* @param overwrite
* if true copy should overwrite any existing file
*/
{
super(objectFile);
}
/**
* {@inheritDoc}
*/
{
}
/**
* Returns the destination file that is the result of copying
* <code>objectFile</code> to <code>destDir</code>.
*
* @return The destination file.
*/
{
return this.destination;
}
/**
* {@inheritDoc}
*/
{
if (objectFile.isDirectory())
{
if (!destination.exists())
{
}
}
else
{
// If overwriting and the destination exists then kill it
{
}
if (!destination.exists())
{
{
try
{
byte[] buf = new byte[1024];
int i;
{
}
if (destination.exists())
{
// TODO: set the file's permissions. This is made easier in
// Java 1.6 but until then use the TestUtilities methods
if (UpgradeUtils.isUnix())
{
}
}
}
catch (Exception e)
{
}
finally
{
}
}
else
{
}
}
else
{
"Ignoring file '%s' since '%s' already exists",
}
}
}
}
/**
* Returns the file permission on the selected file.
*
* @param file
* The file of which we want to extract the permissions.
* @return A file permission about the concerned file.
* @throws DirectoryException
* If the provided string is not a valid three-digit UNIX mode.
*/
throws DirectoryException
{
{
{
}
}
{
}
{
}
{
}
}
/**
* Creates the parent directory if it does not already exist.
*
* @param f
* File for which parentage will be insured
* @return boolean indicating whether or not the input <code>f</code> has a
* parent after this method is invoked.
*/
{
if (!b)
{
}
return b;
}
/**
* A delete operation.
*/
{
/**
* Creates a delete operation.
*
* @param objectFile
* to delete
* @param deletionPolicy
* describing how files will be deleted is to take place after this
* program exists. This is useful for cleaning up files that are
* currently in use.
*/
{
super(objectFile);
this.deletionPolicy = deletionPolicy;
}
/**
* {@inheritDoc}
*/
{
}
/**
* {@inheritDoc}
*/
{
+ file.getAbsolutePath());
boolean delete = false;
/*
* Sometimes the server keeps some locks on the files. TODO: remove this
* code once stop-ds returns properly when server is stopped.
*/
int nTries = 5;
{
{
file.deleteOnExit();
delete = true;
}
else
{
if (!delete
{
file.deleteOnExit();
delete = true;
}
}
if (!delete)
{
try
{
}
{
// do nothing;
}
}
}
if (!delete)
{
if (isFile)
{
}
else
{
}
}
}
}
}