package com.intellij.jps.cache.loader;

import com.intellij.compiler.server.BuildManager;
import com.intellij.jps.cache.client.JpsServerClient;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;

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

class JpsCacheLoader implements JpsOutputLoader {
  private static final Logger LOG = Logger.getInstance("com.intellij.jps.loader.JpsCacheLoader");
  private static final String TIMESTAMPS_FOLDER_NAME = "timestamps";
  private final BuildManager myBuildManager;
  private final JpsServerClient myClient;
  private final Project myProject;
  private File myTmpCacheFolder;

  JpsCacheLoader(JpsServerClient client, @NotNull Project project) {
    myBuildManager = BuildManager.getInstance();
    myClient = client;
    myProject = project;
  }

  @Override
  public LoaderStatus load(@NotNull String commitId) {
    LOG.debug("Loading JPS caches for commit: " + commitId);
    myTmpCacheFolder = null;

    File targetDir = myBuildManager.getBuildSystemDirectory().toFile();
    Pair<Boolean, File> downloadResultPair = myClient.downloadCacheById(myProject, commitId, targetDir);
    myTmpCacheFolder = downloadResultPair.second;
    if (!downloadResultPair.first) return LoaderStatus.FAILED;
    return LoaderStatus.COMPLETE;
  }

  @Override
  public void rollback() {
    if (myTmpCacheFolder != null && myTmpCacheFolder.exists()) {
      FileUtil.delete(myTmpCacheFolder);
      LOG.debug("JPS cache loader rolled back");
    }
  }

  @Override
  public void apply() {
    if (myTmpCacheFolder == null) {
      LOG.warn("Nothing to apply, download results are empty");
      return;
    }
    File currentDirForBuildCache = myBuildManager.getProjectSystemDirectory(myProject);
    if (currentDirForBuildCache != null) {
      File newTimestampFolder = new File(myTmpCacheFolder, TIMESTAMPS_FOLDER_NAME);
      if (newTimestampFolder.exists()) FileUtil.delete(newTimestampFolder);

      File timestamps = new File(currentDirForBuildCache, TIMESTAMPS_FOLDER_NAME);
      if (timestamps.exists()) {
        try {
          newTimestampFolder.mkdirs();
          FileUtil.copyDir(timestamps, newTimestampFolder);
        }
        catch (IOException e) {
          LOG.warn("Couldn't copy timestamps from old JPS cache", e);
        }
      }
      FileUtil.delete(currentDirForBuildCache);
      try {
        FileUtil.rename(myTmpCacheFolder, currentDirForBuildCache);
        LOG.debug("JPS cache downloads finished");
      }
      catch (IOException e) {
        LOG.warn("Couldn't replace JPS cache", e);
      }
    }
  }
}
@@ -624,16 +624,78 @@
 tamps%22;%0A
+  private static final String FS_STATE_FILE = %22fs_state.dat%22;%0A
   privat
@@ -2153,16 +2153,68 @@
 lder);%0A%0A
+      // Copy timestamp old folder to new cache dir%0A
       Fi
@@ -2559,24 +2559,348 @@
    %7D%0A      %7D
+%0A%0A      // Create new empty fsStateFile%0A      File fsStateFile = new File(myTmpCacheFolder, FS_STATE_FILE);%0A      fsStateFile.delete();%0A      try %7B%0A        fsStateFile.createNewFile();%0A      %7D%0A      catch (IOException e) %7B%0A        LOG.warn(%22Couldn't create new empty FsState file%22, e);%0A      %7D%0A%0A      // Remove old cache dir
 %0A      FileU

Add dummy FS_STATE file