11/**
2- * Logback: the reliable, generic, fast and flexible logging framework.
3- * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
2+ * Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2015, QOS.ch. All rights
3+ * reserved.
44 *
5- * This program and the accompanying materials are dual-licensed under
6- * either the terms of the Eclipse Public License v1.0 as published by
7- * the Eclipse Foundation
5+ * This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License
6+ * v1.0 as published by the Eclipse Foundation
87 *
9- * or (per the licensee's choosing)
8+ * or (per the licensee's choosing)
109 *
11- * under the terms of the GNU Lesser General Public License version 2.1
12- * as published by the Free Software Foundation.
10+ * under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
1311 */
1412package ch .qos .logback .core .rolling .helper ;
1513
@@ -86,8 +84,7 @@ public void cleanPeriod(Instant instantOfPeriodToClean) {
8684 File [] matchingFileArray = getFilesInPeriod (instantOfPeriodToClean );
8785
8886 for (File f : matchingFileArray ) {
89- addInfo ("deleting " + f );
90- f .delete ();
87+ checkAndDeleteFile (f );
9188 }
9289
9390 if (parentClean && matchingFileArray .length > 0 ) {
@@ -96,6 +93,23 @@ public void cleanPeriod(Instant instantOfPeriodToClean) {
9693 }
9794 }
9895
96+ private boolean checkAndDeleteFile (File f ) {
97+ addInfo ("deleting " + f );
98+ if (f == null ) {
99+ addWarn ("Cannot delete empty file" );
100+ return false ;
101+ } else if (!f .exists ()) {
102+ addWarn ("Cannot delete non existent file" );
103+ return false ;
104+ }
105+
106+ boolean result = f .delete ();
107+ if (!result ) {
108+ addWarn ("Failed to delete file " + f .toString ());
109+ }
110+ return result ;
111+ }
112+
99113 void capTotalSize (Instant now ) {
100114 long totalSize = 0 ;
101115 long totalRemoved = 0 ;
@@ -107,8 +121,10 @@ void capTotalSize(Instant now) {
107121 long size = f .length ();
108122 if (totalSize + size > totalSizeCap ) {
109123 addInfo ("Deleting [" + f + "]" + " of size " + new FileSize (size ));
124+ // assume that deletion attempt will succeed.
110125 totalRemoved += size ;
111- f .delete ();
126+
127+ checkAndDeleteFile (f );
112128 }
113129 totalSize += size ;
114130 }
@@ -141,7 +157,7 @@ int computeElapsedPeriodsSinceLastClean(long nowInMillis) {
141157
142158 /**
143159 * Computes whether the fileNamePattern may create sub-folders.
144- *
160+ *
145161 * @param fileNamePattern
146162 * @return
147163 */
@@ -183,9 +199,8 @@ void removeFolderIfEmpty(File dir) {
183199 }
184200
185201 /**
186- * Will remove the directory passed as parameter if empty. After that, if the
187- * parent is also becomes empty, remove the parent dir as well but at most 3
188- * times.
202+ * Will remove the directory passed as parameter if empty. After that, if the parent is also becomes empty, remove
203+ * the parent dir as well but at most 3 times.
189204 *
190205 * @param dir
191206 * @param depth
@@ -197,7 +212,7 @@ private void removeFolderIfEmpty(File dir, int depth) {
197212 }
198213 if (dir .isDirectory () && FileFilterUtil .isEmptyDirectory (dir )) {
199214 addInfo ("deleting folder [" + dir + "]" );
200- dir . delete ( );
215+ checkAndDeleteFile ( dir );
201216 removeFolderIfEmpty (dir .getParentFile (), depth + 1 );
202217 }
203218 }
0 commit comments