Example #1
0
 @Nullable
 public static VirtualFile findFileByIoFile(@NotNull File file, boolean refreshIfNeeded) {
   LocalFileSystem fileSystem = LocalFileSystem.getInstance();
   VirtualFile virtualFile = fileSystem.findFileByIoFile(file);
   if (virtualFile == null && refreshIfNeeded) {
     virtualFile = fileSystem.refreshAndFindFileByIoFile(file);
   }
   return virtualFile;
 }
Example #2
0
 @Nullable
 public static VirtualFile getVirtualFileWithRefresh(final File file) {
   if (file == null) return null;
   final LocalFileSystem lfs = LocalFileSystem.getInstance();
   VirtualFile result = lfs.findFileByIoFile(file);
   if (result == null) {
     result = lfs.refreshAndFindFileByIoFile(file);
   }
   return result;
 }
  public void testSubst() throws Exception {
    if (!SystemInfo.isWindows) {
      System.err.println("Ignored: Windows required");
      return;
    }

    File targetDir = createTestDir("top");
    File subDir = createTestDir(targetDir, "sub");
    File file = createTestFile(subDir, "test.txt");
    File rootFile = createSubst(targetDir.getAbsolutePath());
    VirtualDirectoryImpl.allowRootAccess(rootFile.getPath());
    VirtualFile vfsRoot = myFileSystem.findFileByIoFile(rootFile);

    try {
      assertNotNull(rootFile.getPath(), vfsRoot);
      File substDir = new File(rootFile, subDir.getName());
      File substFile = new File(substDir, file.getName());
      refresh(targetDir);
      refresh(substDir);

      LocalFileSystem.WatchRequest request = watch(substDir);
      try {
        myAccept = true;
        FileUtil.writeToFile(file, "new content");
        assertEvent(VFileContentChangeEvent.class, substFile.getAbsolutePath());

        LocalFileSystem.WatchRequest request2 = watch(targetDir);
        try {
          myAccept = true;
          FileUtil.delete(file);
          assertEvent(VFileDeleteEvent.class, file.getAbsolutePath(), substFile.getAbsolutePath());
        } finally {
          unwatch(request2);
        }

        myAccept = true;
        FileUtil.writeToFile(file, "re-creation");
        assertEvent(VFileCreateEvent.class, substFile.getAbsolutePath());
      } finally {
        unwatch(request);
      }
    } finally {
      delete(targetDir);
      IoTestUtil.deleteSubst(rootFile.getPath());
      if (vfsRoot != null) {
        ((NewVirtualFile) vfsRoot).markDirty();
        myFileSystem.refresh(false);
      }
      VirtualDirectoryImpl.disallowRootAccess(rootFile.getPath());
    }
  }
 private void delete(File file) throws IOException {
   VirtualFile vFile = myFileSystem.findFileByIoFile(file);
   if (vFile != null) {
     AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(getClass());
     try {
       vFile.delete(this);
     } finally {
       token.finish();
     }
   }
   if (file.exists()) {
     FileUtil.delete(file);
   }
 }
  private static void doAsyncRefreshTest(File temp) throws Exception {
    final int N = 1000;
    final byte[] data = "xxx".getBytes("UTF-8");

    LocalFileSystem fs = LocalFileSystem.getInstance();
    VirtualFile vTemp = fs.findFileByIoFile(temp);
    assertNotNull(vTemp);
    VirtualFile[] children = new VirtualFile[N];

    long[] timestamp = new long[N];
    for (int i = 0; i < N; i++) {
      File file = new File(temp, i + ".txt");
      FileUtil.writeToFile(file, data);
      VirtualFile child = fs.refreshAndFindFileByIoFile(file);
      assertNotNull(child);
      children[i] = child;
      timestamp[i] = file.lastModified();
    }

    vTemp.refresh(false, true);

    for (int i = 0; i < N; i++) {
      File file = new File(temp, i + ".txt");
      assertEquals(timestamp[i], file.lastModified());
      VirtualFile child = fs.findFileByIoFile(file);
      assertNotNull(child);
      IoTestUtil.assertTimestampsEqual(timestamp[i], child.getTimeStamp());
    }

    for (int i = 0; i < N; i++) {
      File file = new File(temp, i + ".txt");
      FileUtil.writeToFile(file, data);
      assertTrue(file.setLastModified(timestamp[i] - 2000));
      long modified = file.lastModified();
      assertTrue("File:" + file.getPath() + "; time:" + modified, timestamp[i] != modified);
      timestamp[i] = modified;
      IoTestUtil.assertTimestampsNotEqual(children[i].getTimeStamp(), modified);
    }

    final CountDownLatch latch = new CountDownLatch(N);
    for (final VirtualFile child : children) {
      child.refresh(
          true,
          true,
          new Runnable() {
            @Override
            public void run() {
              latch.countDown();
            }
          });
      TimeoutUtil.sleep(10);
    }
    while (latch.getCount() > 0) {
      latch.await(100, TimeUnit.MILLISECONDS);
      UIUtil.pump();
    }

    for (int i = 0; i < N; i++) {
      VirtualFile child = children[i];
      IoTestUtil.assertTimestampsEqual(timestamp[i], child.getTimeStamp());
    }
  }