From e369a6b253c027c0626ecdf6c0915e344bdf41f9 Mon Sep 17 00:00:00 2001
From: Dmitry Trofimov <dmitry.trofimov@jetbrains.com>
Date: Wed, 13 Sep 2017 22:53:03 +0200
Subject: [PATCH] Copy prebuilt indices to the writable index folder (PY-26075)

---
 .../psi/impl/stubs/PyPrebuiltStubsProvider.java     | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/python/src/com/jetbrains/python/psi/impl/stubs/PyPrebuiltStubsProvider.java b/python/src/com/jetbrains/python/psi/impl/stubs/PyPrebuiltStubsProvider.java
index 90259f9724b8..41ca95fa645f 100644
--- a/python/src/com/jetbrains/python/psi/impl/stubs/PyPrebuiltStubsProvider.java
+++ b/python/src/com/jetbrains/python/psi/impl/stubs/PyPrebuiltStubsProvider.java
@@ -24,6 +24,7 @@ import com.intellij.openapi.util.Disposer;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.psi.stubs.*;
 import com.intellij.util.indexing.FileContent;
+import com.intellij.util.indexing.IndexInfrastructure;
 import com.intellij.util.io.PersistentHashMap;
 import com.jetbrains.python.psi.PyFileElementType;
 import org.jetbrains.annotations.NotNull;
@@ -55,6 +56,10 @@ public class PyPrebuiltStubsProvider implements PrebuiltStubsProvider, Disposabl
     File indexesRoot = findPrebuiltIndexesRoot();
     try {
       if (indexesRoot != null) {
+        // we should copy prebuilt indexes to a writable folder
+        indexesRoot = copyPrebuiltIndexesToIndexRoot(indexesRoot);
+        // otherwise we can get access denied error, because persistent hash map opens file for read and write
+
         String stubVersion = FileUtil.loadFile(new File(indexesRoot, SDK_STUBS_STORAGE_NAME + ".version"));
 
         int currentVersion = PyFileElementType.INSTANCE.getStubVersion();
@@ -86,6 +91,14 @@ public class PyPrebuiltStubsProvider implements PrebuiltStubsProvider, Disposabl
     }
   }
 
+  private static File copyPrebuiltIndexesToIndexRoot(File prebuiltIndicesRoot) throws IOException {
+    File indexRoot = new File(IndexInfrastructure.getPersistentIndexRoot(), "prebuilt");
+
+    FileUtil.copyDir(prebuiltIndicesRoot, indexRoot);
+
+    return indexRoot;
+  }
+
 
   @Nullable
   @Override
-- 
GitLab