LCOV - code coverage report
Current view: top level - pgm/init - InitCache.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 25 33 75.8 %
Date: 2022-11-19 15:00:39 Functions: 2 2 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2009 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.pgm.init;
      16             : 
      17             : import com.google.gerrit.common.FileUtil;
      18             : import com.google.gerrit.pgm.init.api.ConsoleUI;
      19             : import com.google.gerrit.pgm.init.api.InitFlags;
      20             : import com.google.gerrit.pgm.init.api.InitStep;
      21             : import com.google.gerrit.pgm.init.api.Section;
      22             : import com.google.gerrit.server.config.SitePaths;
      23             : import com.google.inject.Inject;
      24             : import com.google.inject.Singleton;
      25             : import java.io.IOException;
      26             : import java.nio.file.DirectoryStream;
      27             : import java.nio.file.Files;
      28             : import java.nio.file.Path;
      29             : import java.util.ArrayList;
      30             : import java.util.List;
      31             : 
      32             : /** Initialize the {@code cache} configuration section. */
      33             : @Singleton
      34             : class InitCache implements InitStep {
      35             :   private final ConsoleUI ui;
      36             :   private final InitFlags flags;
      37             :   private final SitePaths site;
      38             :   private final Section cache;
      39             : 
      40             :   @Inject
      41             :   InitCache(
      42             :       final ConsoleUI ui,
      43             :       final InitFlags flags,
      44             :       final SitePaths site,
      45          15 :       final Section.Factory sections) {
      46          15 :     this.ui = ui;
      47          15 :     this.flags = flags;
      48          15 :     this.site = site;
      49          15 :     this.cache = sections.get("cache", null);
      50          15 :   }
      51             : 
      52             :   @Override
      53             :   public void run() {
      54          15 :     ui.header("Cache");
      55          15 :     String path = cache.get("directory");
      56             : 
      57          15 :     if (path != null && path.isEmpty()) {
      58             :       // Explicitly set to empty implies the administrator has
      59             :       // disabled the on disk cache and doesn't want it enabled.
      60             :       //
      61           0 :       return;
      62             :     }
      63             : 
      64          15 :     if (path == null) {
      65          15 :       path = "cache";
      66          15 :       cache.set("directory", path);
      67             :     }
      68             : 
      69          15 :     Path loc = site.resolve(path);
      70          15 :     FileUtil.mkdirsOrDie(loc, "cannot create cache.directory");
      71          15 :     List<Path> cacheFiles = new ArrayList<>();
      72          15 :     try (DirectoryStream<Path> stream = Files.newDirectoryStream(loc, "*.{lock,h2,trace}.db")) {
      73          15 :       for (Path entry : stream) {
      74           1 :         cacheFiles.add(entry);
      75           1 :       }
      76           0 :     } catch (IOException e) {
      77           0 :       ui.message("IO error during cache directory scan");
      78           0 :       return;
      79          15 :     }
      80          15 :     if (!cacheFiles.isEmpty()) {
      81           1 :       for (Path entry : cacheFiles) {
      82           1 :         if (flags.deleteCaches || ui.yesno(false, "Delete cache file %s", entry)) {
      83             :           try {
      84           0 :             Files.deleteIfExists(entry);
      85           0 :           } catch (IOException e) {
      86           0 :             ui.message("Could not delete " + entry);
      87           0 :           }
      88             :         }
      89           1 :       }
      90             :     }
      91          15 :   }
      92             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750