private void queueUnresolvedFilesSinceLastRestart() { PersistentFS fs = PersistentFS.getInstance(); int maxId = FSRecords.getMaxId(); TIntArrayList list = new TIntArrayList(); for (int id= fileIsResolved.nextClearBit(1); id >= 0 && id < maxId; id = fileIsResolved.nextClearBit(id + 1)) { int nextSetBit = fileIsResolved.nextSetBit(id); int endOfRun = Math.min(maxId, nextSetBit == -1 ? maxId : nextSetBit); do { VirtualFile virtualFile = fs.findFileById(id); if (queueIfNeeded(virtualFile, myProject)) { list.add(id); } else { fileIsResolved.set(id); } } while (++id < endOfRun); } log("Initially added to resolve " + toVfString(list.toNativeArray())); }
@Override public void tearDown() throws Exception { Project project = getProject(); CodeStyleSettingsManager.getInstance(project).dropTemporarySettings(); CodeStyleSettings oldCodeStyleSettings = myOldCodeStyleSettings; myOldCodeStyleSettings = null; List<Throwable> exceptions = new SmartList<Throwable>(); try { UsefulTestCase.doCheckForSettingsDamage(oldCodeStyleSettings, getCurrentCodeStyleSettings(), exceptions); LightPlatformTestCase.doTearDown(project, LightPlatformTestCase.getApplication(), true, exceptions); super.tearDown(); InjectedLanguageManagerImpl.checkInjectorsAreDisposed(project); PersistentFS.getInstance().clearIdCache(); PlatformTestCase.cleanupApplicationCaches(project); } finally { CompoundRuntimeException.throwIfNotEmpty(exceptions); } }
protected void fireEventsInWriteAction() { final VirtualFileManagerEx manager = (VirtualFileManagerEx)VirtualFileManager.getInstance(); manager.fireBeforeRefreshStart(myIsAsync); try { while (!myWorkQueue.isEmpty() || !myEvents.isEmpty()) { PersistentFS.getInstance().processEvents(mergeEventsAndReset()); scan(); } } finally { try { manager.fireAfterRefreshFinish(myIsAsync); } finally { if (myFinishRunnable != null) { myFinishRunnable.run(); } } } }
@Override @NotNull public Iterable<VirtualFile> iterInDbChildren() { if (!ourPersistence.wereChildrenAccessed(this)) { return Collections.emptyList(); } if (!ourPersistence.areChildrenLoaded(this)) { final String[] names = ourPersistence.listPersisted(this); final NewVirtualFileSystem delegate = PersistentFS.replaceWithNativeFS(getFileSystem()); for (String name : names) { findChild(name, false, false, delegate); } } return getCachedChildren(); }
@Override public <Key, Psi extends PsiElement> boolean processElements(@NotNull final StubIndexKey<Key, Psi> indexKey, @NotNull final Key key, @NotNull final Project project, @Nullable final GlobalSearchScope scope, @Nullable IdFilter idFilter, @NotNull final Class<Psi> requiredClass, @NotNull final Processor<? super Psi> processor) { return doProcessStubs(indexKey, key, project, scope, new StubIdListContainerAction(idFilter, project) { final PersistentFS fs = (PersistentFS)ManagingFS.getInstance(); @Override protected boolean process(int id, StubIdList value) { final VirtualFile file = IndexInfrastructure.findFileByIdIfCached(fs, id); if (file == null || scope != null && !scope.contains(file)) { return true; } return myStubProcessingHelper.processStubsInFile(project, file, value, processor, requiredClass); } }); }
private static boolean processVirtualFiles(@NotNull TIntHashSet ids, @NotNull final GlobalSearchScope filter, @NotNull final Processor<VirtualFile> processor) { final PersistentFS fs = (PersistentFS)ManagingFS.getInstance(); return ids.forEach(new TIntProcedure() { @Override public boolean execute(int id) { ProgressManager.checkCanceled(); VirtualFile file = IndexInfrastructure.findFileByIdIfCached(fs, id); if (file != null && filter.accept(file)) { return processor.process(file); } return true; } }); }
@Nullable public static VirtualFile findFileById(@NotNull PersistentFS fs, final int id) { if (ourUnitTestMode) { final VirtualFile testFile = findTestFile(id); if (testFile != null) { return testFile; } } return fs.findFileById(id); /* final boolean isDirectory = fs.isDirectory(id); final DirectoryInfo directoryInfo = isDirectory ? dirIndex.getInfoForDirectoryId(id) : dirIndex.getInfoForDirectoryId(fs.getParent(id)); if (directoryInfo != null && (directoryInfo.contentRoot != null || directoryInfo.sourceRoot != null || directoryInfo.libraryClassRoot != null)) { return isDirectory? directoryInfo.directory : directoryInfo.directory.findChild(fs.getName(id)); } return null; */ }
@Before public void setUp() { MockitoAnnotations.initMocks(this); PowerMockito.mockStatic(CommandUtils.class, TFSVcs.class, PersistentFS.class, RenameUtil.class); when(mockPsiFile.getVirtualFile()).thenReturn(mockVirtualFile); when(mockPsiFile.getProject()).thenReturn(mockProject); when(mockPsiDirectory.getVirtualFile()).thenReturn(mockVirtualFile); when(mockPsiDirectory.getProject()).thenReturn(mockProject); when(mockVirtualParent.getPath()).thenReturn(PARENT_PATH); when(mockVirtualFile.getParent()).thenReturn(mockVirtualParent); when(mockTFSVcs.getServerContext(anyBoolean())).thenReturn(mockServerContext); when(TFSVcs.getInstance(mockProject)).thenReturn(mockTFSVcs); when(PersistentFS.getInstance()).thenReturn(mockPersistentFS); }
@Test public void testExecute_RenameFileNoChanges() { when(mockVirtualFile.getPath()).thenReturn(CURRENT_FILE_PATH); when(CommandUtils.getStatusForFiles(mockServerContext, ImmutableList.of(CURRENT_FILE_PATH))) .thenReturn(Collections.EMPTY_LIST); RenameFileDirectory.execute(mockPsiFile, NEW_FILE_NAME, usageInfos, mockListener); verifyStatic(times(1)); CommandUtils.renameFile(eq(mockServerContext), eq(CURRENT_FILE_PATH), eq(NEW_FILE_PATH)); PersistentFS.getInstance().processEvents(any(List.class)); verify(mockListener).elementRenamed(mockPsiFile); verifyStatic(never()); RenameUtil.doRenameGenericNamedElement(any(PsiElement.class), any(String.class), any(UsageInfo[].class), any(RefactoringElementListener.class)); }
@Test public void testExecute_RenameDirectoryNoChanges() { String dirName = Path.combine("/path/to/the", "directory"); when(mockVirtualFile.getPath()).thenReturn(dirName); when(CommandUtils.getStatusForFiles(mockServerContext, ImmutableList.of(dirName))) .thenReturn(Collections.EMPTY_LIST); RenameFileDirectory.execute(mockPsiFile, NEW_DIRECTORY_NAME, usageInfos, mockListener); verifyStatic(times(1)); CommandUtils.renameFile(eq(mockServerContext), eq(dirName), eq(Path.combine("/path/to/the", "newDirectory"))); PersistentFS.getInstance().processEvents(any(List.class)); verify(mockListener).elementRenamed(mockPsiFile); verifyStatic(never()); RenameUtil.doRenameGenericNamedElement(any(PsiElement.class), any(String.class), any(UsageInfo[].class), any(RefactoringElementListener.class)); }
@Test public void testExecute_RenameFileEditChanges() { when(mockPendingChange.getChangeTypes()).thenReturn(ImmutableList.of(ServerStatusType.EDIT)); when(mockVirtualFile.getPath()).thenReturn(CURRENT_FILE_PATH); when(CommandUtils.getStatusForFiles(mockServerContext, ImmutableList.of(CURRENT_FILE_PATH))) .thenReturn(ImmutableList.of(mockPendingChange)); RenameFileDirectory.execute(mockPsiFile, NEW_FILE_NAME, usageInfos, mockListener); verifyStatic(times(1)); CommandUtils.renameFile(eq(mockServerContext), eq(CURRENT_FILE_PATH), eq(NEW_FILE_PATH)); PersistentFS.getInstance().processEvents(any(List.class)); verify(mockListener).elementRenamed(mockPsiFile); verifyStatic(never()); RenameUtil.doRenameGenericNamedElement(any(PsiElement.class), any(String.class), any(UsageInfo[].class), any(RefactoringElementListener.class)); }
@Test public void testExecute_RenameFileEditRenameChanges() { when(mockPendingChange.getChangeTypes()).thenReturn(ImmutableList.of(ServerStatusType.EDIT, ServerStatusType.RENAME)); when(mockVirtualFile.getPath()).thenReturn(CURRENT_FILE_PATH); when(CommandUtils.getStatusForFiles(mockServerContext, ImmutableList.of(CURRENT_FILE_PATH))) .thenReturn(ImmutableList.of(mockPendingChange)); RenameFileDirectory.execute(mockPsiFile, NEW_FILE_NAME, usageInfos, mockListener); verifyStatic(times(1)); CommandUtils.renameFile(eq(mockServerContext), eq(CURRENT_FILE_PATH), eq(NEW_FILE_PATH)); PersistentFS.getInstance().processEvents(any(List.class)); verify(mockListener).elementRenamed(mockPsiFile); verifyStatic(never()); RenameUtil.doRenameGenericNamedElement(any(PsiElement.class), any(String.class), any(UsageInfo[].class), any(RefactoringElementListener.class)); }
@Test public void testExecute_RenameFileUnversionedChange() { when(mockPendingChange.getChangeTypes()).thenReturn(ImmutableList.of(ServerStatusType.ADD)); when(mockVirtualFile.getPath()).thenReturn(CURRENT_FILE_PATH); when(CommandUtils.getStatusForFiles(mockServerContext, ImmutableList.of(CURRENT_FILE_PATH))) .thenReturn(ImmutableList.of(mockPendingChange)); RenameFileDirectory.execute(mockPsiFile, NEW_FILE_NAME, usageInfos, mockListener); verifyStatic(times(1)); RenameUtil.doRenameGenericNamedElement(mockPsiFile, NEW_FILE_NAME, usageInfos, mockListener); verify(mockListener).elementRenamed(mockPsiFile); verifyStatic(never()); CommandUtils.renameFile(any(ServerContext.class), any(String.class), any(String.class)); PersistentFS.getInstance().processEvents(any(List.class)); }
@NotNull public VirtualFileSystemEntry createChild(@NotNull String name, int id, @NotNull NewVirtualFileSystem delegate) { VirtualFileSystemEntry child; final int attributes = ourPersistence.getFileAttributes(id); if (PersistentFS.isDirectory(attributes)) { child = new VirtualDirectoryImpl(name, this, delegate, id, attributes); } else { child = new VirtualFileImpl(name, this, id, attributes); //noinspection TestOnlyProblems assertAccessInTests(child, delegate); } if (delegate.markNewFilesAsDirty()) { child.markDirty(); } return child; }
public VirtualFileSystemEntry(@NotNull String name, VirtualDirectoryImpl parent, int id, @PersistentFS.Attributes int attributes) { myParent = parent; myId = id; storeName(name); if (parent != null && parent != VirtualDirectoryImpl.NULL_VIRTUAL_FILE) { setFlagInt(IS_SYMLINK_FLAG, PersistentFS.isSymLink(attributes)); setFlagInt(IS_SPECIAL_FLAG, PersistentFS.isSpecialFile(attributes)); updateLinkStatus(); } setFlagInt(IS_WRITABLE_FLAG, PersistentFS.isWritable(attributes)); setFlagInt(IS_HIDDEN_FLAG, PersistentFS.isHidden(attributes)); setModificationStamp(LocalTimeCounter.currentTime()); }
public void testListChildrenOfTheRootOfTheRoot() { PersistentFS fs = PersistentFS.getInstance(); NewVirtualFile fakeRoot = fs.findRoot("", LocalFileSystem.getInstance()); assertNotNull(fakeRoot); int users = fs.getId(fakeRoot, "Users", LocalFileSystem.getInstance()); assertEquals(0, users); users = fs.getId(fakeRoot, "usr", LocalFileSystem.getInstance()); assertEquals(0, users); int win = fs.getId(fakeRoot, "Windows", LocalFileSystem.getInstance()); assertEquals(0, win); VirtualFile[] roots = fs.getRoots(LocalFileSystem.getInstance()); for (VirtualFile root : roots) { int rid = fs.getId(fakeRoot, root.getName(), LocalFileSystem.getInstance()); assertTrue(root.getPath()+"; Roots:"+ Arrays.toString(roots), 0 != rid); } NewVirtualFile c = fakeRoot.refreshAndFindChild("Users"); assertNull(c); c = fakeRoot.refreshAndFindChild("Users"); assertNull(c); c = fakeRoot.refreshAndFindChild("Windows"); assertNull(c); c = fakeRoot.refreshAndFindChild("Windows"); assertNull(c); }
public void testFindRootShouldNotBeFooledByRelativePath() throws IOException { File tmp = createTempDirectory(); File x = new File(tmp, "x.jar"); x.createNewFile(); LocalFileSystem lfs = LocalFileSystem.getInstance(); VirtualFile vx = lfs.refreshAndFindFileByIoFile(x); assertNotNull(vx); JarFileSystem jfs = JarFileSystem.getInstance(); VirtualFile root = jfs.getJarRootForLocalFile(vx); PersistentFS fs = PersistentFS.getInstance(); String path = vx.getPath() + "/../" + vx.getName() + JarFileSystem.JAR_SEPARATOR; NewVirtualFile root1 = fs.findRoot(path, jfs); assertSame(root1, root); }
public void testDeleteSubstRoots() throws IOException, InterruptedException { if (!SystemInfo.isWindows) return; File tempDirectory = FileUtil.createTempDirectory(getTestName(false), null); File substRoot = IoTestUtil.createSubst(tempDirectory.getPath()); VirtualFile subst = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(substRoot); assertNotNull(subst); try { final File[] children = substRoot.listFiles(); assertNotNull(children); } finally { IoTestUtil.deleteSubst(substRoot.getPath()); } subst.refresh(false, true); PersistentFS fs = PersistentFS.getInstance(); VirtualFile[] roots = fs.getRoots(LocalFileSystem.getInstance()); for (VirtualFile root : roots) { String rootPath = root.getPath(); String prefix = StringUtil.commonPrefix(rootPath, substRoot.getPath()); assertEmpty(prefix); } }
@Nullable public static VirtualFile findFileById(final PersistentFS fs, final int id) { if (ourUnitTestMode) { final VirtualFile testFile = findTestFile(id); if (testFile != null) { return testFile; } } return fs.findFileById(id); /* final boolean isDirectory = fs.isDirectory(id); final DirectoryInfo directoryInfo = isDirectory ? dirIndex.getInfoForDirectoryId(id) : dirIndex.getInfoForDirectoryId(fs.getParent(id)); if (directoryInfo != null && (directoryInfo.contentRoot != null || directoryInfo.sourceRoot != null || directoryInfo.libraryClassRoot != null)) { return isDirectory? directoryInfo.directory : directoryInfo.directory.findChild(fs.getName(id)); } return null; */ }
public void testFindRootShouldNotBeFooledByRelativePath() throws IOException { File tmp = createTempDirectory(); File x = new File(tmp, "x.jar"); x.createNewFile(); LocalFileSystem lfs = LocalFileSystem.getInstance(); VirtualFile vx = lfs.refreshAndFindFileByIoFile(x); assertNotNull(vx); ArchiveFileSystem jfs = (ArchiveFileSystem)StandardFileSystems.jar(); VirtualFile root = ArchiveVfsUtil.getArchiveRootForLocalFile(vx); PersistentFS fs = PersistentFS.getInstance(); String path = vx.getPath() + "/../" + vx.getName() + ArchiveFileSystem.ARCHIVE_SEPARATOR; NewVirtualFile root1 = fs.findRoot(path, (NewVirtualFileSystem)jfs); assertSame(root1, root); }
@Override @Nonnull public Iterable<VirtualFile> iterInDbChildren() { if (!ourPersistence.wereChildrenAccessed(this)) { return Collections.emptyList(); } if (!ourPersistence.areChildrenLoaded(this)) { final String[] names = ourPersistence.listPersisted(this); final NewVirtualFileSystem delegate = PersistentFS.replaceWithNativeFS(getFileSystem()); for (String name : names) { findChild(name, false, false, delegate); } } return getCachedChildren(); }
@Override public <Key, Psi extends PsiElement> boolean processElements(@Nonnull final StubIndexKey<Key, Psi> indexKey, @Nonnull final Key key, @Nonnull final Project project, @Nullable final GlobalSearchScope scope, @Nullable IdFilter idFilter, @Nonnull final Class<Psi> requiredClass, @Nonnull final Processor<? super Psi> processor) { return doProcessStubs(indexKey, key, project, scope, new StubIdListContainerAction(idFilter, project) { final PersistentFS fs = (PersistentFS)ManagingFS.getInstance(); @Override protected boolean process(int id, StubIdList value) { final VirtualFile file = IndexInfrastructure.findFileByIdIfCached(fs, id); if (file == null || scope != null && !scope.contains(file)) { return true; } return myStubProcessingHelper.processStubsInFile(project, file, value, processor, requiredClass); } }); }
@Nonnull @Override public InputDataDiffBuilder<Key, Value> getDiffBuilder(int inputId) throws IOException { if (SharedIndicesData.ourFileSharedIndicesEnabled) { Collection<Key> keys = SharedIndicesData.recallFileData(inputId, (ID<Key, ?>)myIndexId, mySnapshotIndexExternalizer); if (myUnderlying != null) { Collection<Key> keysFromInputsIndex = myUnderlying.getInputsIndex().get(inputId); if (keys == null && keysFromInputsIndex != null || !DebugAssertions.equals(keysFromInputsIndex, keys, myKeyDescriptor) ) { SharedIndicesData.associateFileData(inputId, (ID<Key, ?>)myIndexId, keysFromInputsIndex, mySnapshotIndexExternalizer); if (keys != null) { DebugAssertions.error( "Unexpected indexing diff " + myIndexId + ", file:" + IndexInfrastructure.findFileById(PersistentFS.getInstance(), inputId) + "," + keysFromInputsIndex + "," + keys); } keys = keysFromInputsIndex; } } return new CollectionInputDataDiffBuilder<>(inputId, keys); } return new CollectionInputDataDiffBuilder<>(inputId, myUnderlying.getInputsIndex().get(inputId)); }
@Nullable public static VirtualFile findFileById(@Nonnull PersistentFS fs, final int id) { if (ourUnitTestMode) { final VirtualFile testFile = findTestFile(id); if (testFile != null) { return testFile; } } return fs.findFileById(id); /* final boolean isDirectory = fs.isDirectory(id); final DirectoryInfo directoryInfo = isDirectory ? dirIndex.getInfoForDirectoryId(id) : dirIndex.getInfoForDirectoryId(fs.getParent(id)); if (directoryInfo != null && (directoryInfo.contentRoot != null || directoryInfo.sourceRoot != null || directoryInfo.libraryClassRoot != null)) { return isDirectory? directoryInfo.directory : directoryInfo.directory.findChild(fs.getName(id)); } return null; */ }
@Override public Data getFileData(@Nonnull Project project, @Nonnull VirtualFile file) { ApplicationManager.getApplication().assertReadAccessAllowed(); ProgressManager.checkCanceled(); if (!(file instanceof VirtualFileWithId)) return myCalculator.calcData(project, file); int stamp = PersistentFS.getInstance().getModificationCount(file) + ((GistManagerImpl)GistManager.getInstance()).getReindexCount(); try (DataInputStream stream = getFileAttribute(project).readAttribute(file)) { if (stream != null && DataInputOutputUtil.readINT(stream) == stamp) { return stream.readBoolean() ? myExternalizer.read(stream) : null; } } catch (IOException e) { LOG.error(e); } Data result = myCalculator.calcData(project, file); cacheResult(stamp, result, project, file); return result; }
@NotNull private static List<VirtualFile> toVf(@NotNull int[] ids) { List<VirtualFile> res = new ArrayList<VirtualFile>(); for (int id : ids) { VirtualFile file = PersistentFS.getInstance().findFileById(id); if (file != null) { res.add(file); } } return res; }
public void addChild(@NotNull VirtualFileSystemEntry child) { final String childName = child.getName(); final boolean ignoreCase = !getFileSystem().isCaseSensitive(); synchronized (myData) { int indexInReal = findIndex(myData.myChildrenIds, childName, ignoreCase); myData.removeAdoptedName(childName); if (indexInReal < 0) { insertChildAt(child, indexInReal); ((PersistentFSImpl)PersistentFS.getInstance()).incStructuralModificationCount(); } // else already stored assertConsistency(ignoreCase, child); } }
public void setNewName(@NotNull String newName) { if (!isValidName(newName)) { throw new IllegalArgumentException(VfsBundle.message("file.invalid.name.error", newName)); } VirtualDirectoryImpl parent = getParent(); parent.removeChild(this); mySegment.setNameId(myId, FileNameCache.storeName(newName)); ((PersistentFSImpl)PersistentFS.getInstance()).incStructuralModificationCount(); parent.addChild(this); }
public void testFileLength() throws Exception { File file = FileUtil.createTempFile("test", "txt"); FileUtil.writeToFile(file, "hello"); VirtualFile virtualFile = myFS.refreshAndFindFileByIoFile(file); assertNotNull(virtualFile); String s = VfsUtilCore.loadText(virtualFile); assertEquals("hello", s); assertEquals(5, virtualFile.getLength()); FileUtil.writeToFile(file, "new content"); ((PersistentFSImpl)PersistentFS.getInstance()).cleanPersistedContents(); s = VfsUtilCore.loadText(virtualFile); assertEquals("new content", s); assertEquals(11, virtualFile.getLength()); }
public void testNoMoreFakeRoots() throws Exception { try { PersistentFS.getInstance().findRoot("", myFS); fail("should fail by assertion in PersistentFsImpl.findRoot()"); } catch (Throwable t) { String message = t.getMessage(); assertTrue(message, message.startsWith("Invalid root")); } }
@Override public void actionPerformed(AnActionEvent e) { final ApplicationEx application = ApplicationManagerEx.getApplicationEx(); String m = "Started loading content"; LOG.info(m); System.out.println(m); long start = System.currentTimeMillis(); count.set(0); totalSize.set(0); ApplicationManagerEx.getApplicationEx().runProcessWithProgressSynchronously(new Runnable() { @Override public void run() { PersistentFS vfs = (PersistentFS)application.getComponent(ManagingFS.class); VirtualFile[] roots = vfs.getRoots(); for (VirtualFile root : roots) { iterateCached(root); } } }, "Loading", false, null); long end = System.currentTimeMillis(); String message = "Finished loading content of " + count + " files. " + "Total size=" + StringUtil.formatFileSize(totalSize.get()) + ". " + "Elapsed=" + ((end - start) / 1000) + "sec."; LOG.info(message); System.out.println(message); }
@Nullable public static VirtualFile findFileByIdIfCached(@NotNull PersistentFS fs, final int id) { if (ourUnitTestMode) { final VirtualFile testFile = findTestFile(id); if (testFile != null) { return testFile; } } return fs.findFileByIdIfCached(id); }
@Override public void tearDown() throws Exception { Project project = getProject(); CodeStyleSettingsManager.getInstance(project).dropTemporarySettings(); CodeStyleSettings oldCodeStyleSettings = myOldCodeStyleSettings; myOldCodeStyleSettings = null; UsefulTestCase.doCheckForSettingsDamage(oldCodeStyleSettings, getCurrentCodeStyleSettings()); LightPlatformTestCase.doTearDown(project, LightPlatformTestCase.getApplication(), true); super.tearDown(); InjectedLanguageManagerImpl.checkInjectorsAreDisposed(project); PersistentFS.getInstance().clearIdCache(); ((DirectoryIndexImpl)DirectoryIndex.getInstance(project)).assertAncestorConsistent(); }
public VirtualDirectoryImpl(@NonNls @NotNull final String name, @Nullable final VirtualDirectoryImpl parent, @NotNull final NewVirtualFileSystem fs, final int id, @PersistentFS.Attributes final int attributes) { super(name, parent, id, attributes); myFS = fs; }
@TestOnly public void cleanupForNextTest() { ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { FileDocumentManager.getInstance().saveAllDocuments(); } }); PersistentFS.getInstance().clearIdCache(); myRootsToWatch.clear(); }