view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgFileUtil.java @ 949:32bfdf49827f

disable failing test The tested logic is not used
author Dmitry Neverov <dmitry.neverov@gmail.com>
date Thu, 17 Mar 2016 11:51:14 +0100
parents 98657a5c5cb1
children 7bf4d943d5bb
line wrap: on
line source
/*
 * Copyright 2000-2014 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 com.intellij.openapi.util.SystemInfo;
import jetbrains.buildServer.util.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;

/**
 * @author dmitry.neverov
 */
public final class HgFileUtil {

  private final static String TEMP_DIR_PREFIX = "hg";
  private final static Object ourTmpDirLock = new Object();

  private HgFileUtil() {
  }

  /**
   * Create a temp dir with short name
   * @return created dir
   * @throws IOException in case of I/O error
   */
  @NotNull
  public static File createTempDir() throws IOException {
    File parentDir = new File(FileUtil.getTempDirectory());
    return createTempDir(parentDir);
  }

  @NotNull
  public static File createTempDir(@NotNull final File parentDir) throws IOException {
    //noinspection ResultOfMethodCallIgnored
    parentDir.mkdirs();

    int suffix = 0;
    File dir;
    while (true) {
      suffix++;
      String tmpDirName = TEMP_DIR_PREFIX + suffix;
      dir = new File(parentDir, tmpDirName);
      if (dir.exists())
        continue;

      synchronized (ourTmpDirLock) {
        try {
          if (!dir.createNewFile())
            continue;
        } catch (IOException e) {
          //workaround for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6325169
          if (SystemInfo.isWindows && "Access is denied".equals(e.getMessage()))
            continue;
          throw e;
        }
        if (!dir.delete())
          continue;
        if (!dir.mkdir())
          continue;
      }
      return dir;
    }
  }


  public static void deleteDir(@Nullable final File dir, @NotNull final Logger logger) {
    if (dir == null)
      return;
    FileUtil.symlinkAwareDelete(dir);
    if (dir.exists())
      logger.warn("Cannot delete directory " + dir.getAbsolutePath());
  }
}