1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, QOS.ch. All rights reserved.
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
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.rolling.helper;
15  
16  import java.io.File;
17  import java.time.Instant;
18  import java.util.Arrays;
19  import java.util.Comparator;
20  import java.util.Date;
21  import java.util.regex.Matcher;
22  import java.util.regex.Pattern;
23  
24  public class SizeAndTimeBasedArchiveRemover extends TimeBasedArchiveRemover {
25  
26      protected static final int NO_INDEX = -1;
27  
28      public SizeAndTimeBasedArchiveRemover(FileNamePattern fileNamePattern, RollingCalendar rc) {
29          super(fileNamePattern, rc);
30      }
31  
32      @Override
33      protected File[] getFilesInPeriod(Instant instantOfPeriodToClean) {
34          File archive0 = new File(fileNamePattern.convertMultipleArguments(instantOfPeriodToClean, 0));
35          File parentDir = getParentDir(archive0);
36          String stemRegex = createStemRegex(instantOfPeriodToClean);
37          File[] matchingFileArray = FileFilterUtil.filesInFolderMatchingStemRegex(parentDir, stemRegex);
38          return matchingFileArray;
39      }
40  
41      @Override
42      protected void descendingSort(File[] matchingFileArray, Instant instant) {
43  
44          String regexForIndexExtreaction = createStemRegex(instant);
45          final Pattern pattern = Pattern.compile(regexForIndexExtreaction);
46  
47          Arrays.sort(matchingFileArray, new Comparator<File>() {
48              @Override
49              public int compare(final File f1, final File f2) {
50  
51                  int index1 = extractIndex(pattern, f1);
52                  int index2 = extractIndex(pattern, f2);
53  
54                  if (index1 == index2)
55                      return 0;
56                  // descending sort, i.e. newest files first
57                  if (index2 < index1)
58                      return -1;
59                  else
60                      return 1;
61              }
62  
63              private int extractIndex(Pattern pattern, File f1) {
64                  Matcher matcher = pattern.matcher(f1.getName());
65                  if (matcher.find()) {
66                      String indexAsStr = matcher.group(1);
67  
68                      if (indexAsStr == null || indexAsStr.isEmpty())
69                          return NO_INDEX; // unreachable code?
70                      else
71                          return Integer.parseInt(indexAsStr);
72                  } else
73                      return NO_INDEX;
74              }
75          });
76      }
77  
78      private String createStemRegex(final Instant instantOfPeriodToClean) {
79          String regex = fileNamePattern.toRegexForFixedDate(instantOfPeriodToClean);
80          return FileFilterUtil.afterLastSlash(regex);
81      }
82  
83  }