merge with branch issue-154

This commit is contained in:
Sebastian Sdorra
2012-06-01 15:52:25 +02:00
3 changed files with 181 additions and 5 deletions

View File

@@ -181,6 +181,8 @@ public abstract class AbstractSimpleRepositoryHandler<C extends SimpleRepository
if (directory.exists())
{
fileSystem.destroy(directory);
cleanupEmptyDirectories(config.getRepositoryDirectory(),
directory.getParentFile());
}
else if (logger.isWarnEnabled())
{
@@ -436,6 +438,47 @@ public abstract class AbstractSimpleRepositoryHandler<C extends SimpleRepository
}
}
/**
* Method description
*
*
* @param baseDirectory
* @param directory
*/
private void cleanupEmptyDirectories(File baseDirectory, File directory)
{
if (IOUtil.isChild(baseDirectory, directory))
{
if (IOUtil.isEmpty(directory))
{
// TODO use filesystem
if (directory.delete())
{
if (logger.isInfoEnabled())
{
logger.info("successfully deleted directory {}", directory);
}
cleanupEmptyDirectories(baseDirectory, directory.getParentFile());
}
else if (logger.isWarnEnabled())
{
logger.warn("could not delete directory {}", directory);
}
}
else if (logger.isDebugEnabled())
{
logger.debug("could not remove non empty directory {}", directory);
}
}
else if (logger.isWarnEnabled())
{
logger.warn("directory {} is not a child of {}", directory,
baseDirectory);
}
}
//~--- fields ---------------------------------------------------------------
/** Field description */

View File

@@ -677,26 +677,50 @@ public class IOUtil
}
/**
* Method description
* Returns true if the second file parameter is a child of the first one.
*
*
* @param parent
* @param child
* @param parent parent file
* @param child chile file
* @since 1.9
*
* @return
* @return true if the second file parameter is a child of the first one
*
*/
public static boolean isChild(File parent, File child)
{
boolean ischild = false;
try
{
return child.getCanonicalPath().startsWith(parent.getCanonicalPath());
String path = child.getCanonicalPath();
String parentPath = parent.getCanonicalPath();
if (!parentPath.equals(path))
{
ischild = path.startsWith(parentPath);
}
}
catch (IOException ex)
{
throw new RuntimeException(ex);
}
return ischild;
}
/**
* Returns true if the given directory is empty.
*
*
* @param directory directory to check
*
* @return true if the directory is empty
* @since 1.16
*/
public static boolean isEmpty(File directory)
{
return Util.isEmpty(directory.listFiles());
}
//~--- methods --------------------------------------------------------------