view mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/Cleanup.java @ 977:38adef4f1b8f Indore-2017.2.x

Update copyright
author pavel.sher
date Mon, 22 Jan 2018 11:40:45 +0100
parents 31a1aca3305c
children 10dc26b32c35
line wrap: on
line source
/*
 * Copyright 2000-2018 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jetbrains.buildServer.buildTriggers.vcs.mercurial;

import com.intellij.openapi.diagnostic.Logger;
import jetbrains.buildServer.util.FileUtil;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileFilter;
import java.util.*;

import static java.util.Arrays.asList;

/**
 * @author dmitry.neverov
 */
public class Cleanup implements Runnable {

  private static Logger LOG = Logger.getInstance(Cleanup.class.getName());

  private final MirrorManager myMirrorManager;
  private final ServerPluginConfig myConfig;

  public Cleanup(@NotNull final MirrorManager mirrorManager,
                 @NotNull final ServerPluginConfig config) {
    myMirrorManager = mirrorManager;
    myConfig = config;
  }

  public void run() {
    delete(unusedDirs());
  }

  private Collection<File> unusedDirs() {
    List<File> existingDirs = existingDirs();
    List<File> mirrorsInUse = mirrorDirsOfRootsInUse(existingDirs);
    existingDirs.removeAll(mirrorsInUse);
    return existingDirs;
  }

  private List<File> existingDirs() {
    File[] files = listDirs();
    if (files != null)
      return new ArrayList<File>(asList(files));
    if (myConfig.getCachesDir().isDirectory())
      LOG.warn("Cannot list files in " + myConfig.getCachesDir());
    return Collections.emptyList();
  }

  private File[] listDirs() {
    return myConfig.getCachesDir().listFiles(new FileFilter() {
      public boolean accept(File f) {
        return f.isDirectory();
      }
    });
  }

  private List<File> mirrorDirsOfRootsInUse(@NotNull List<File> mirrors) {
    List<File> result = new ArrayList<File>();
    long now = new Date().getTime();
    for (File mirror : mirrors) {
      File dotHg = new File(mirror, ".hg");
      File timestamp = new File(dotHg, "timestamp");
      if (!timestamp.exists())
        continue;
      long lastUsedTime = myMirrorManager.getLastUsedTime(mirror);
      if (now - lastUsedTime < myConfig.getMirrorExpirationTimeoutMillis())
        result.add(mirror);
    }
    return result;
  }

  private void delete(Collection<File> dirs) {
    logUnusedLocalClones(dirs);
    for (File dir : dirs) {
      myMirrorManager.lockDir(dir);
      try {
        myMirrorManager.forgetDir(dir);
        FileUtil.delete(dir);
      } finally {
        myMirrorManager.unlockDir(dir);
      }
    }
  }

  private void logUnusedLocalClones(@NotNull Collection<File> dirs) {
    if (dirs.isEmpty())
      return;
    StringBuilder sb = new StringBuilder();
    sb.append("Unused local clones: ");
    Iterator<File> iter = dirs.iterator();
    while (iter.hasNext()) {
      sb.append(iter.next().getAbsolutePath());
      if (iter.hasNext())
        sb.append(", ");
    }
    LOG.info(sb.toString());
  }
}