Java Code Examples for com.intellij.openapi.vfs.VirtualFile#getTimeStamp()

The following examples show how to use com.intellij.openapi.vfs.VirtualFile#getTimeStamp() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StubTreeLoaderImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void diagnoseLengthMismatch(VirtualFile vFile, boolean wasIndexedAlready, @Nullable Document document, boolean saved, @Nullable PsiFile cachedPsi) {
  String message = "Outdated stub in index: " +
                   vFile +
                   " " +
                   getIndexingStampInfo(vFile) +
                   ", doc=" +
                   document +
                   ", docSaved=" +
                   saved +
                   ", wasIndexedAlready=" +
                   wasIndexedAlready +
                   ", queried at " +
                   vFile.getTimeStamp();
  message += "\ndoc length=" + (document == null ? -1 : document.getTextLength()) + "\nfile length=" + vFile.getLength();
  if (cachedPsi != null) {
    message += "\ncached PSI " + cachedPsi.getClass();
    if (cachedPsi instanceof PsiFileImpl && ((PsiFileImpl)cachedPsi).isContentsLoaded()) {
      message += "\nPSI length=" + cachedPsi.getTextLength();
    }
    List<Project> projects = ContainerUtil.findAll(ProjectManager.getInstance().getOpenProjects(), p -> PsiManagerEx.getInstanceEx(p).getFileManager().findCachedViewProvider(vFile) != null);
    message += "\nprojects with file: " + (LOG.isDebugEnabled() ? projects.toString() : projects.size());
  }

  processError(vFile, message, new Exception());
}
 
Example 2
Source File: ServerConnectionManager.java    From aem-ide-tooling-4-intellij with Apache License 2.0 6 votes vote down vote up
private long getParentLastModificationTimestamp(
    Module module,
    String basePath,
    VirtualFile file,
    Set<String> handledPaths
) {
    long ret = -1;
    VirtualFile parentFile = file.getParent();
    messageManager.sendDebugNotification("debug.last.modification.time.parent.resource.file", parentFile);
    if(parentFile.getPath().equals(basePath)) {
        return ret;
    }
    // already published by us, a parent of another resource that was published in this execution
    if (handledPaths.contains(parentFile.getPath())) {
        return ret;
    }
    long parentLastModificationTimestamp = getParentLastModificationTimestamp(module, basePath, parentFile, handledPaths);
    ret = Math.max(parentLastModificationTimestamp, ret);
    long timestamp = file.getTimeStamp();
    long fileTimestamp = Util.getModificationStamp(file);
    if(fileTimestamp > 0) {
        ret = Math.max(timestamp, ret);
    }
    return ret;
}
 
Example 3
Source File: VcsTestUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void editFileInCommand(@Nonnull Project project, @Nonnull final VirtualFile file, @Nonnull final String newContent) {
  assertTrue(file.isValid());
  file.getTimeStamp();
  new WriteCommandAction.Simple(project) {
    @Override
    protected void run() throws Throwable {
      try {
        final long newTs = Math.max(System.currentTimeMillis(), file.getTimeStamp() + 1100);
        file.setBinaryContent(newContent.getBytes(), -1, newTs);
        final File file1 = new File(file.getPath());
        FileUtil.writeToFile(file1, newContent.getBytes());
        file.refresh(false, false);
        assertTrue(file1 + " / " + newTs, file1.setLastModified(newTs));
      }
      catch(IOException ex) {
        throw new RuntimeException(ex);
      }
    }
  }.execute();
}
 
Example 4
Source File: AbstractVcsTestCase.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void editFileInCommand(final Project project, final VirtualFile file, final String newContent) {
  assertTrue(file.isValid());
  file.getTimeStamp();
  new WriteCommandAction.Simple(project) {
    @Override
    protected void run() throws Throwable {
      try {
        final long newTs = Math.max(System.currentTimeMillis(), file.getTimeStamp() + 1100);
        file.setBinaryContent(newContent.getBytes(), -1, newTs);
        final File file1 = new File(file.getPath());
        FileUtil.writeToFile(file1, newContent.getBytes());
        file.refresh(false, false);
        assertTrue(file1 + " / " + newTs, file1.setLastModified(newTs));
      }
      catch(IOException ex) {
        throw new RuntimeException(ex);
      }
    }
  }.execute();
}
 
Example 5
Source File: LastUnchangedContentTracker.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void updateLastUnchangedContent(@Nonnull VirtualFile file) {
  if (isTouched(file)) {
    return;
  }

  Long lastTs = getLastSavedStamp(file);
  final long stamp = file.getTimeStamp();
  if (lastTs != null && stamp == lastTs) {
    return;
  }

  Integer oldContentId = getSavedContentId(file);
  if (oldContentId != null && oldContentId > 0) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("releasing content for " + file + ", id = " + oldContentId);
    }
    getFS().releaseContent(oldContentId);
  }

  saveContentReference(file, getFS().acquireContent(file));
  markTouched(file);
}
 
Example 6
Source File: FileChangedNotificationProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
@Nullable
@Override
public EditorNotificationPanel createNotificationPanel(@Nonnull VirtualFile file, @Nonnull FileEditor fileEditor) {
  if (!myProject.isDisposed() && !GeneralSettings.getInstance().isSyncOnFrameActivation()) {
    VirtualFileSystem fs = file.getFileSystem();
    if (fs instanceof LocalFileSystem) {
      FileAttributes attributes = ((LocalFileSystem)fs).getAttributes(file);
      if (attributes == null || file.getTimeStamp() != attributes.lastModified || file.getLength() != attributes.length) {
        LogUtil.debug(LOG, "%s: (%s,%s) -> %s", file, file.getTimeStamp(), file.getLength(), attributes);
        return createPanel(file);
      }
    }
  }

  return null;
}
 
Example 7
Source File: ArtifactCompilerCompileItem.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isOutputUpToDate(@Nonnull ArtifactPackagingItemOutputState state) {
  final SmartList<Pair<String, Long>> cachedDestinations = state.myDestinations;
  if (cachedDestinations.size() != myDestinations.size()) {
    return false;
  }

  for (DestinationInfo info : myDestinations) {
    final VirtualFile outputFile = info.getOutputFile();
    long timestamp = outputFile != null ? outputFile.getTimeStamp() : -1;
    final String path = info.getOutputPath();
    boolean found = false;
    //todo[nik] use map if list contains many items
    for (Pair<String, Long> cachedDestination : cachedDestinations) {
      if (cachedDestination.first.equals(path)) {
        if (cachedDestination.second != timestamp) return false;
        found = true;
        break;
      }
    }
    if (!found) return false;
  }

  return true;
}
 
Example 8
Source File: ArtifactCompilerCompileItem.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public ArtifactPackagingItemOutputState computeOutputState() {
  final SmartList<Pair<String, Long>> pairs = new SmartList<Pair<String, Long>>();
  for (DestinationInfo destination : myDestinations) {
    destination.update();
    final VirtualFile outputFile = destination.getOutputFile();
    long timestamp = outputFile != null ? outputFile.getTimeStamp() : -1;
    pairs.add(Pair.create(destination.getOutputPath(), timestamp));
  }
  return new ArtifactPackagingItemOutputState(pairs);
}
 
Example 9
Source File: FileBasedIndexImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void indexFileContent(@Nullable Project project, @Nonnull com.intellij.ide.caches.FileContent content) {
  VirtualFile file = content.getVirtualFile();
  final int fileId = Math.abs(getIdMaskingNonIdBasedFile(file));

  boolean setIndexedStatus = true;
  try {
    // if file was scheduled for update due to vfs events then it is present in myFilesToUpdate
    // in this case we consider that current indexing (out of roots backed CacheUpdater) will cover its content
    if (file.isValid() && content.getTimeStamp() != file.getTimeStamp()) {
      content = new com.intellij.ide.caches.FileContent(file);
    }
    if (!file.isValid() || isTooLarge(file)) {
      removeDataFromIndicesForFile(fileId, file);
      if (file instanceof DeletedVirtualFileStub && ((DeletedVirtualFileStub)file).isResurrected()) {
        doIndexFileContent(project, new com.intellij.ide.caches.FileContent(((DeletedVirtualFileStub)file).getOriginalFile()));
      }
    }
    else {
      setIndexedStatus = doIndexFileContent(project, content);
    }
  }
  finally {
    IndexingStamp.flushCache(fileId);
  }

  getChangedFilesCollector().removeFileIdFromFilesScheduledForUpdate(fileId);
  if (file instanceof VirtualFileSystemEntry && setIndexedStatus) ((VirtualFileSystemEntry)file).setFileIndexed(true);
}
 
Example 10
Source File: ImagePreviewComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"AutoUnboxing"})
private static boolean refresh(@Nonnull VirtualFile file) throws IOException {
  Long loadedTimeStamp = file.getUserData(TIMESTAMP_KEY);
  SoftReference<BufferedImage> imageRef = file.getUserData(BUFFERED_IMAGE_REF_KEY);
  if (loadedTimeStamp == null || loadedTimeStamp < file.getTimeStamp() || SoftReference.dereference(imageRef) == null) {
    try {
      final byte[] content = file.contentsToByteArray();
      InputStream inputStream = new ByteArrayInputStream(content, 0, content.length);
      ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream);
      try {
        Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
        if (imageReaders.hasNext()) {
          ImageReader imageReader = imageReaders.next();
          try {
            file.putUserData(FORMAT_KEY, imageReader.getFormatName());
            ImageReadParam param = imageReader.getDefaultReadParam();
            imageReader.setInput(imageInputStream, true, true);
            int minIndex = imageReader.getMinIndex();
            BufferedImage image = imageReader.read(minIndex, param);
            file.putUserData(BUFFERED_IMAGE_REF_KEY, new SoftReference<BufferedImage>(image));
            return true;
          }
          finally {
            imageReader.dispose();
          }
        }
      }
      finally {
        imageInputStream.close();
      }
    }
    finally {
      // We perform loading no more needed
      file.putUserData(TIMESTAMP_KEY, System.currentTimeMillis());
    }
  }
  return false;
}
 
Example 11
Source File: LastUnchangedContentTracker.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void saveContentReference(VirtualFile file, int contentId) {
  if (contentId == 0) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("saveContentReference without content for file = " + file);
    }
    return; // content not loaded yet, nothing to save
  }

  LOG.assertTrue(contentId > 0, contentId);

  if (LOG.isDebugEnabled()) {
    LOG.debug("saveContentReference file = " + file + ", id = " + contentId);
  }

  long stamp = file.getTimeStamp();
  try {
    final DataOutputStream contentStream = ACQUIRED_CONTENT_ATTR.writeAttribute(file);
    try {
      contentStream.writeInt(contentId);
    }
    finally {
      contentStream.close();
    }

    final DataOutputStream tsStream = LAST_TS_ATTR.writeAttribute(file);
    try {
      tsStream.writeLong(stamp);
    }
    finally {
      tsStream.close();
    }

    file.putUserData(LAST_TS_KEY, stamp);
  }
  catch (IOException e) {
    LOG.info(e);
  }
}
 
Example 12
Source File: IndexingStampInfo.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean isUpToDate(@Nullable Document document, @Nonnull VirtualFile file, @Nonnull PsiFile psi) {
  if (document == null || FileDocumentManager.getInstance().isDocumentUnsaved(document) || !PsiDocumentManager.getInstance(psi.getProject()).isCommitted(document)) {
    return false;
  }

  return indexingFileStamp == file.getTimeStamp() && contentLengthMatches(file.getLength(), document.getTextLength());
}
 
Example 13
Source File: ExternalSystemUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static long getTimeStamp(@Nonnull ExternalProjectSettings externalProjectSettings, @Nonnull ProjectSystemId externalSystemId) {
  long timeStamp = 0;
  for (ExternalSystemConfigLocator locator : ExternalSystemConfigLocator.EP_NAME.getExtensionList()) {
    if (!externalSystemId.equals(locator.getTargetExternalSystemId())) {
      continue;
    }
    for (VirtualFile virtualFile : locator.findAll(externalProjectSettings)) {
      timeStamp += virtualFile.getTimeStamp();
    }
  }
  return timeStamp;
}
 
Example 14
Source File: VirtualFileWithDependenciesState.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean isUpToDate(@Nonnull VirtualFile sourceFile) {
  if (sourceFile.getTimeStamp() != mySourceTimestamp) {
    return false;
  }

  VirtualFileManager manager = VirtualFileManager.getInstance();
  for (Map.Entry<String, Long> entry : myDependencies.entrySet()) {
    final VirtualFile file = manager.findFileByUrl(entry.getKey());
    if (file == null || file.getTimeStamp() != entry.getValue()) {
      return false;
    }
  }
  return true;
}
 
Example 15
Source File: TestFileSystem.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
public long getFileModifiedTime(File file) {
  VirtualFile vf = getVirtualFile(file);
  if (vf == null) {
    return super.getFileModifiedTime(file);
  }
  return vf.getTimeStamp();
}
 
Example 16
Source File: FlutterApp.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @return whether the latest of the version of the file is running.
 */
public boolean isLatestVersionRunning(VirtualFile file) {
  return file != null && file.getTimeStamp() <= maxFileTimestamp;
}
 
Example 17
Source File: DifferenceReverter.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void revertContentChange(Entry l, VirtualFile file) throws IOException {
  if (l.isDirectory()) return;
  if (file.getTimeStamp() != l.getTimestamp()) {
    setContent(l, file);
  }
}
 
Example 18
Source File: Util.java    From aem-ide-tooling-4-intellij with Apache License 2.0 4 votes vote down vote up
public static boolean isOutdated(VirtualFile file) {
    long savedModificationTimeStamp = getModificationStamp(file);
    long actualModificationTimeStamp = file.getTimeStamp();
    return savedModificationTimeStamp < actualModificationTimeStamp;
}
 
Example 19
Source File: BlazeAndroidModel.java    From intellij with Apache License 2.0 4 votes vote down vote up
public static boolean testIsClassFileOutOfDate(
    @NotNull Project project, @NotNull String fqcn, @NotNull VirtualFile classFile) {
  VirtualFile sourceFile =
      ApplicationManager.getApplication()
          .runReadAction(
              (Computable<VirtualFile>)
                  () -> {
                    PsiClass psiClass =
                        JavaPsiFacade.getInstance(project)
                            .findClass(fqcn, GlobalSearchScope.projectScope(project));
                    if (psiClass == null) {
                      return null;
                    }
                    PsiFile psiFile = psiClass.getContainingFile();
                    if (psiFile == null) {
                      return null;
                    }
                    return psiFile.getVirtualFile();
                  });
  if (sourceFile == null) {
    return false;
  }

  // Edited but not yet saved?
  if (FileDocumentManager.getInstance().isFileModified(sourceFile)) {
    return true;
  }

  long sourceTimeStamp = sourceFile.getTimeStamp();
  long buildTimeStamp = classFile.getTimeStamp();

  if (classFile.getFileSystem() instanceof JarFileSystem) {
    JarFileSystem jarFileSystem = (JarFileSystem) classFile.getFileSystem();
    VirtualFile jarFile = jarFileSystem.getVirtualFileForJar(classFile);
    if (jarFile != null) {
      if (jarFile.getFileSystem() instanceof LocalFileSystem) {
        // The virtual file timestamp could be stale since we don't watch this file.
        buildTimeStamp = VfsUtilCore.virtualToIoFile(jarFile).lastModified();
      } else {
        buildTimeStamp = jarFile.getTimeStamp();
      }
    }
  }

  if (sourceTimeStamp > buildTimeStamp) {
    // It's possible that the source file's timestamp has been updated, but the content remains
    // same. In this case, blaze will not try to rebuild the jar, we have to also check whether
    // the user recently clicked the build button. So they can at least manually get rid of the
    // error.
    Long projectBuildTimeStamp = BlazeBuildService.getLastBuildTimeStamp(project);
    return projectBuildTimeStamp == null || sourceTimeStamp > projectBuildTimeStamp;
  }

  return false;
}
 
Example 20
Source File: FlutterApp.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @return whether the latest of the version of the file is running.
 */
public boolean isLatestVersionRunning(VirtualFile file) {
  return file != null && file.getTimeStamp() <= maxFileTimestamp;
}