1
1
/**
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.
4
4
*
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
8
7
*
9
- * or (per the licensee's choosing)
8
+ * or (per the licensee's choosing)
10
9
*
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.
13
11
*/
14
12
package ch .qos .logback .core .rolling .helper ;
15
13
@@ -86,8 +84,7 @@ public void cleanPeriod(Instant instantOfPeriodToClean) {
86
84
File [] matchingFileArray = getFilesInPeriod (instantOfPeriodToClean );
87
85
88
86
for (File f : matchingFileArray ) {
89
- addInfo ("deleting " + f );
90
- f .delete ();
87
+ checkAndDeleteFile (f );
91
88
}
92
89
93
90
if (parentClean && matchingFileArray .length > 0 ) {
@@ -96,6 +93,23 @@ public void cleanPeriod(Instant instantOfPeriodToClean) {
96
93
}
97
94
}
98
95
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
+
99
113
void capTotalSize (Instant now ) {
100
114
long totalSize = 0 ;
101
115
long totalRemoved = 0 ;
@@ -107,8 +121,10 @@ void capTotalSize(Instant now) {
107
121
long size = f .length ();
108
122
if (totalSize + size > totalSizeCap ) {
109
123
addInfo ("Deleting [" + f + "]" + " of size " + new FileSize (size ));
124
+ // assume that deletion attempt will succeed.
110
125
totalRemoved += size ;
111
- f .delete ();
126
+
127
+ checkAndDeleteFile (f );
112
128
}
113
129
totalSize += size ;
114
130
}
@@ -141,7 +157,7 @@ int computeElapsedPeriodsSinceLastClean(long nowInMillis) {
141
157
142
158
/**
143
159
* Computes whether the fileNamePattern may create sub-folders.
144
- *
160
+ *
145
161
* @param fileNamePattern
146
162
* @return
147
163
*/
@@ -183,9 +199,8 @@ void removeFolderIfEmpty(File dir) {
183
199
}
184
200
185
201
/**
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.
189
204
*
190
205
* @param dir
191
206
* @param depth
@@ -197,7 +212,7 @@ private void removeFolderIfEmpty(File dir, int depth) {
197
212
}
198
213
if (dir .isDirectory () && FileFilterUtil .isEmptyDirectory (dir )) {
199
214
addInfo ("deleting folder [" + dir + "]" );
200
- dir . delete ( );
215
+ checkAndDeleteFile ( dir );
201
216
removeFolderIfEmpty (dir .getParentFile (), depth + 1 );
202
217
}
203
218
}
0 commit comments