protected static void touch(VirtualFile file) throws IOException {
   file.setBinaryContent(
       file.contentsToByteArray(), file.getModificationStamp() + 1, file.getTimeStamp() + 1);
   File ioFile = VfsUtil.virtualToIoFile(file);
   assert ioFile.setLastModified(ioFile.lastModified() - 100000);
   file.refresh(false, false);
 }
예제 #2
0
  private static VirtualFile[] copyFiles(
      final VirtualFile[] fromFiles, final VirtualFile toDir, VirtualFile rawProjectRoot)
      throws IOException {
    final VirtualFile[] toFiles = new VirtualFile[fromFiles.length];
    final boolean rootSpecified = rawProjectRoot != null;
    for (int i = 0, n = fromFiles.length; i < n; i++) {
      VirtualFile fromFile = fromFiles[i];
      VirtualFile toP = toDir;
      if (rootSpecified) {
        final List<String> fromParents = new ArrayList<>(4);
        VirtualFile fromP = fromFile.getParent();
        if (!Comparing.equal(fromP, rawProjectRoot)) {
          do {
            fromParents.add(fromP.getName());
          } while (!Comparing.equal((fromP = fromP.getParent()), rawProjectRoot));

          for (int j = fromParents.size() - 1; j >= 0; j--) {
            toP = toP.createChildDirectory(null, fromParents.get(j));
          }
        }
      }
      final VirtualFile toFile = toP.createChildData(null, fromFile.getName());
      toFile.setBinaryContent(fromFile.contentsToByteArray());
      toFiles[i] = toFile;
    }

    return toFiles;
  }
  public void testHardLinks() throws Exception {
    if (!SystemInfo.isWindows && !SystemInfo.isUnix) {
      System.err.println(getName() + " skipped: " + SystemInfo.OS_NAME);
      return;
    }

    final boolean safeWrite = GeneralSettings.getInstance().isUseSafeWrite();
    final File dir = FileUtil.createTempDirectory("hardlinks.", ".dir", false);
    final SafeWriteRequestor requestor = new SafeWriteRequestor() {};
    try {
      GeneralSettings.getInstance().setUseSafeWrite(false);

      final File targetFile = new File(dir, "targetFile");
      assertTrue(targetFile.createNewFile());
      final File hardLinkFile =
          IoTestUtil.createHardLink(targetFile.getAbsolutePath(), "hardLinkFile");

      final VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(targetFile);
      assertNotNull(file);
      file.setBinaryContent("hello".getBytes("UTF-8"), 0, 0, requestor);
      assertTrue(file.getLength() > 0);

      final VirtualFile check =
          LocalFileSystem.getInstance().refreshAndFindFileByIoFile(hardLinkFile);
      assertNotNull(check);
      assertEquals(file.getLength(), check.getLength());
      assertEquals("hello", VfsUtilCore.loadText(check));
    } finally {
      GeneralSettings.getInstance().setUseSafeWrite(safeWrite);
      FileUtil.delete(dir);
    }
  }
예제 #4
0
 public void write() {
   if (!isPopulated()) {
     Flog.warn("Unable to write %s because it's not populated yet.", path);
     return;
   }
   VirtualFile virtualFile = getVirtualFile();
   if (virtualFile == null) {
     virtualFile = createFile();
     if (virtualFile == null) {
       context.errorMessage("The Floobits plugin was unable to write to a file.");
       return;
     }
   }
   Document d = Buf.getDocumentForVirtualFile(virtualFile);
   if (d != null) {
     try {
       Listener.flooDisable();
       d.setReadOnly(false);
       d.setText(buf);
     } finally {
       Listener.flooEnable();
     }
     return;
   }
   Flog.warn("Tried to write to null document: %s", path);
   try {
     virtualFile.setBinaryContent(buf.getBytes());
   } catch (IOException e) {
     Flog.warn(e);
     context.errorMessage("The Floobits plugin was unable to write to a file.");
   }
 }
  public void testContentChangeWhenDirectoryExists() throws Exception {
    VirtualFile f = myRoot.createChildData(this, "foo.txt");
    f.setBinaryContent(new byte[] {1}, -1, 1000);

    getVcs().beginChangeSet();
    f.rename(this, "bar.txt");
    f.setBinaryContent(new byte[] {2}, -1, 2000);
    getVcs().endChangeSet(null);

    myRoot.createChildDirectory(this, "foo.txt");

    revertChange(1, 0, 1);

    assertNull(myRoot.findChild("bar.txt"));
    f = myRoot.findChild("foo.txt");
    assertNotNull(f);
    assertFalse(f.isDirectory());
    assertEquals(1, f.contentsToByteArray()[0]);
    assertEquals(1000, f.getTimeStamp());
  }
  public void testApplyingOnlyLastContent() throws Exception {
    VirtualFile f = root.createChildData(null, "f.txt");
    f.setBinaryContent(new byte[] {1}, -1, 1000);
    f.setBinaryContent(new byte[] {2}, -1, 2000);
    f.setBinaryContent(new byte[] {3}, -1, 3000);
    f.setBinaryContent(new byte[] {4}, -1, 4000);

    LoggingVirtualFileAdapter a = new LoggingVirtualFileAdapter();
    addFileListenerDuring(
        a,
        new RunnableAdapter() {
          @Override
          public void doRun() throws IOException {
            revertChange(root, 2);
          }
        });

    assertEquals("contentChanged ", a.getLog());
    assertEquals(1, f.contentsToByteArray()[0]);
    assertEquals(1000, f.getTimeStamp());
  }
  public void testFileDeletion() throws Exception {
    VirtualFile f = myRoot.createChildData(this, "foo.txt");
    f.setBinaryContent(new byte[] {123}, -1, 4000);
    f.delete(this);

    revertLastChange();

    f = myRoot.findChild("foo.txt");
    assertNotNull(f);
    assertEquals(123, f.contentsToByteArray()[0]);
    assertEquals(4000, f.getTimeStamp());
  }
  public void testApplyingRightContentEvenIfFileWasMoved() throws Exception {
    VirtualFile dir = root.createChildDirectory(null, "dir");
    VirtualFile f = root.createChildData(null, "f.txt");

    f.setBinaryContent(new byte[] {1});
    f.setBinaryContent(new byte[] {2});
    f.move(null, dir);
    f.setBinaryContent(new byte[] {3});

    LoggingVirtualFileAdapter a = new LoggingVirtualFileAdapter();
    addFileListenerDuring(
        a,
        new RunnableAdapter() {
          @Override
          public void doRun() throws IOException {
            revertChange(root, 2);
          }
        });

    assertEquals("fileMoved contentChanged ", a.getLog());
    assertEquals(1, f.contentsToByteArray()[0]);
  }
  public void testDoesNotSetContentIfFileIsToBeDeleted() throws Exception {
    VirtualFile f = root.createChildData(null, "f.txt");
    f.setBinaryContent(new byte[] {1});

    LoggingVirtualFileAdapter a = new LoggingVirtualFileAdapter();
    addFileListenerDuring(
        a,
        new RunnableAdapter() {
          @Override
          public void doRun() throws IOException {
            revertChange(root, 1);
          }
        });

    assertEquals("fileDeleted ", a.getLog());
  }
  public void testParentRename() throws Exception {
    VirtualFile dir = myRoot.createChildDirectory(this, "dir");
    VirtualFile f = dir.createChildData(this, "foo.txt");
    f.setBinaryContent(new byte[] {123}, -1, 4000);

    dir.rename(this, "dir2");

    revertLastChange();

    assertNull(myRoot.findChild("dir2"));
    dir = myRoot.findChild("dir");
    f = dir.findChild("foo.txt");
    assertNotNull(f);
    assertEquals(123, f.contentsToByteArray()[0]);
    assertEquals(4000, f.getTimeStamp());
  }
  public void testRevertingContentChangeFromOldRevisionsWhenDirDoesNotExists() throws Exception {
    VirtualFile dir = myRoot.createChildDirectory(this, "dir");
    VirtualFile f = dir.createChildData(this, "foo.txt");

    f.setBinaryContent(new byte[] {1}, -1, 1000);
    f.setBinaryContent(new byte[] {2}, -1, 2000);

    dir.delete(this);

    revertChange(1);

    dir = myRoot.findChild("dir");
    assertNotNull(dir);
    f = dir.findChild("foo.txt");
    assertNotNull(f);
    assertEquals(1, f.contentsToByteArray()[0]);
    assertEquals(1000, f.getTimeStamp());
  }
  private void restoreCopy(VirtualFile file) {
    try {
      if (file == null) return; // Externally deleted actually.
      if (!file.isWritable()) return; // IDEA was unable to save it as well. So no need to restore.

      final byte[] bytes = mySavedCopies.get(file);
      if (bytes != null) {
        try {
          file.setBinaryContent(bytes, -1, mySavedTimestamps.get(file));
        } catch (IOException e) {
          Messages.showWarningDialog(
              ProjectBundle.message("project.reload.write.failed", file.getPresentableUrl()),
              ProjectBundle.message("project.reload.write.failed.title"));
        }
      }
    } finally {
      mySavedCopies.remove(file);
      mySavedTimestamps.remove(file);
    }
  }
  public void testCopyDir() throws Exception {
    File fromDir = createTempDirectory();
    File toDir = createTempDirectory();

    VirtualFile fromVDir =
        LocalFileSystem.getInstance()
            .findFileByPath(fromDir.getPath().replace(File.separatorChar, '/'));
    VirtualFile toVDir =
        LocalFileSystem.getInstance()
            .findFileByPath(toDir.getPath().replace(File.separatorChar, '/'));
    assertNotNull(fromVDir);
    assertNotNull(toVDir);
    final VirtualFile dirToCopy = fromVDir.createChildDirectory(this, "dir");
    final VirtualFile file = dirToCopy.createChildData(this, "temp_file");
    file.setBinaryContent(new byte[] {0, 1, 2, 3});
    final String newName = "dir";
    final VirtualFile dirCopy = dirToCopy.copy(this, toVDir, newName);
    assertEquals(newName, dirCopy.getName());
    PlatformTestUtil.assertDirectoriesEqual(toVDir, fromVDir);
  }
  public void testCopyFile() throws Exception {
    File fromDir = createTempDirectory();
    File toDir = createTempDirectory();

    VirtualFile fromVDir =
        LocalFileSystem.getInstance()
            .findFileByPath(fromDir.getPath().replace(File.separatorChar, '/'));
    VirtualFile toVDir =
        LocalFileSystem.getInstance()
            .findFileByPath(toDir.getPath().replace(File.separatorChar, '/'));
    assertNotNull(fromVDir);
    assertNotNull(toVDir);
    final VirtualFile fileToCopy = fromVDir.createChildData(this, "temp_file");
    final byte[] byteContent = {0, 1, 2, 3};
    fileToCopy.setBinaryContent(byteContent);
    final String newName = "new_temp_file";
    final VirtualFile copy = fileToCopy.copy(this, toVDir, newName);
    assertEquals(newName, copy.getName());
    assertTrue(Arrays.equals(byteContent, copy.contentsToByteArray()));
  }