in src/main/java/org/apache/log4j/rolling/FixedWindowRollingPolicy.java [236:322]
private boolean purge(final int lowIndex, final int highIndex) {
int suffixLength = 0;
List renames = new ArrayList();
StringBuffer buf = new StringBuffer();
formatFileName(new Integer(lowIndex), buf);
String lowFilename = buf.toString();
if (lowFilename.endsWith(".gz")) {
suffixLength = 3;
} else if (lowFilename.endsWith(".zip")) {
suffixLength = 4;
}
for (int i = lowIndex; i <= highIndex; i++) {
File toRename = new File(lowFilename);
boolean isBase = false;
if (suffixLength > 0) {
File toRenameBase =
new File(
lowFilename.substring(0, lowFilename.length() - suffixLength));
if (toRename.exists()) {
if (toRenameBase.exists()) {
toRenameBase.delete();
}
} else {
toRename = toRenameBase;
isBase = true;
}
}
if (toRename.exists()) {
//
// if at upper index then
// attempt to delete last file
// if that fails then abandon purge
if (i == highIndex) {
if (!toRename.delete()) {
return false;
}
break;
}
//
// if intermediate index
// add a rename action to the list
buf.setLength(0);
formatFileName(new Integer(i + 1), buf);
String highFilename = buf.toString();
String renameTo = highFilename;
if (isBase) {
renameTo =
highFilename.substring(0, highFilename.length() - suffixLength);
}
renames.add(new FileRenameAction(toRename, new File(renameTo), true));
lowFilename = highFilename;
} else {
break;
}
}
//
// work renames backwards
//
for (int i = renames.size() - 1; i >= 0; i--) {
Action action = (Action) renames.get(i);
try {
if (!action.execute()) {
return false;
}
} catch (Exception ex) {
LogLog.warn("Exception during purge in RollingFileAppender", ex);
return false;
}
}
return true;
}