Java Code Examples for com.intellij.openapi.util.io.FileUtil#delete()

The following examples show how to use com.intellij.openapi.util.io.FileUtil#delete() . 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: SharedProxyConfig.java    From consulo with Apache License 2.0 7 votes vote down vote up
public static boolean store(@Nonnull ProxyParameters params) {
  if (params.host != null) {
    try {
      final Properties props = new Properties();
      props.setProperty(HOST, params.host);
      props.setProperty(PORT, String.valueOf(params.port));
      if (params.login != null) {
        props.setProperty(LOGIN, params.login);
        props.setProperty(PASSWORD, new String(params.password));
      }
      ourEncryptionSupport.store(props, "Proxy Configuration", CONFIG_FILE);
      return true;
    }
    catch (Exception ignored) {
    }
  }
  else {
    FileUtil.delete(CONFIG_FILE);
  }
  return false;
}
 
Example 2
Source File: TestFileSystemItem.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void assertFileEqual(File file, String relativePath) {
  try {
    Assert.assertEquals("in " + relativePath, myName, file.getName());
    if (myArchive) {
      final File dirForExtracted = FileUtil.createTempDirectory("extracted_archive", null,false);
      ZipUtil.extract(file, dirForExtracted, null);
      assertDirectoryEqual(dirForExtracted, relativePath);
      FileUtil.delete(dirForExtracted);
    }
    else if (myDirectory) {
      Assert.assertTrue(relativePath + file.getName() + " is not a directory", file.isDirectory());
      assertDirectoryEqual(file, relativePath);
    }
    else if (myContent != null) {
      final String content = FileUtil.loadFile(file);
      Assert.assertEquals("content mismatch for " + relativePath, myContent, content);
    }
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 3
Source File: FileListeningTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testCreationAndDeletionOfFileUnderUnversionedDir() throws IOException {
  addExcludedDir(myRoot.getPath() + "/dir");

  Module m = createModule("foo");
  addContentRoot(m, myRoot.getPath() + "/dir/subDir");

  createFileExternally("dir/subDir/file.txt");
  LocalFileSystem.getInstance().refresh(false);

  FileUtil.delete(new File(myRoot.getPath() + "/dir/subDir"));
  LocalFileSystem.getInstance().refresh(false);

  createFileExternally("dir/subDir/file.txt");
  LocalFileSystem.getInstance().refresh(false);

  List<Revision> revs = getRevisionsFor(myRoot);
  assertEquals(4, revs.size());
  assertNotNull(revs.get(0).findEntry().findEntry("dir/subDir/file.txt"));
  assertNull(revs.get(1).findEntry().findEntry("dir/subDir"));
  assertNotNull(revs.get(2).findEntry().findEntry("dir/subDir/file.txt"));
  assertNull(revs.get(3).findEntry().findEntry("dir/subDir"));
}
 
Example 4
Source File: UsageNodeTreeBuilderTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testFilesWithTheSameNameButDifferentPathsEndUpInDifferentGroups() throws IOException {
  File ioDir = FileUtil.createTempDirectory("t", null, false);
  VirtualFile dir = null;
  try {
    dir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioDir);
    PsiFile f1 = getPsiManager().findFile(VfsTestUtil.createFile(dir, "/x/X.java", "class X{}"));
    PsiFile f2 = getPsiManager().findFile(VfsTestUtil.createFile(dir, "/y/X.java", "class X{}"));
    PsiElement class1 = ArrayUtil.getLastElement(f1.getChildren());
    PsiElement class2 = ArrayUtil.getLastElement(f2.getChildren());
    FileGroupingRule fileGroupingRule = new FileGroupingRule(getProject());
    UsageGroup group1 = fileGroupingRule.getParentGroupFor(new UsageInfo2UsageAdapter(new UsageInfo(class1)), UsageTarget.EMPTY_ARRAY);
    UsageGroup group2 = fileGroupingRule.getParentGroupFor(new UsageInfo2UsageAdapter(new UsageInfo(class2)), UsageTarget.EMPTY_ARRAY);
    int compareTo = group1.compareTo(group2);
    assertTrue(String.valueOf(compareTo), compareTo < 0);
  }
  finally {
    if (dir != null) {
      VfsTestUtil.deleteFile(dir);
    }
    FileUtil.delete(ioDir);
  }
}
 
Example 5
Source File: PerformanceWatcher.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void deleteOldThreadDumps() {
  File allLogsDir = new File(myContainerPathManager.getLogPath());
  if (allLogsDir.isDirectory()) {
    final String[] dirs = allLogsDir.list(new FilenameFilter() {
      @Override
      public boolean accept(@Nonnull final File dir, @Nonnull final String name) {
        return name.startsWith("threadDumps-");
      }
    });
    if (dirs != null) {
      Arrays.sort(dirs);
      for (int i = 0; i < dirs.length - 11; i++) {
        FileUtil.delete(new File(allLogsDir, dirs[i]));
      }
    }
  }
}
 
Example 6
Source File: ProjectImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void saveAsyncImpl(@Nonnull UIAccess uiAccess) {
  if (myApplication.isDoNotSave()) {
    // no need to save
    return;
  }

  if (!mySavingInProgress.compareAndSet(false, true)) {
    return;
  }

  //HeavyProcessLatch.INSTANCE.prioritizeUiActivity();

  try {
    if (!isDefault()) {
      String projectBasePath = getStateStore().getProjectBasePath();
      if (projectBasePath != null) {
        File projectDir = new File(projectBasePath);
        File nameFile = new File(projectDir, DIRECTORY_STORE_FOLDER + "/" + NAME_FILE);
        if (!projectDir.getName().equals(getName())) {
          try {
            FileUtil.writeToFile(nameFile, getName());
          }
          catch (IOException e) {
            LOG.error("Unable to store project name", e);
          }
        }
        else {
          FileUtil.delete(nameFile);
        }
      }
    }

    StoreUtil.saveAsync(getStateStore(), uiAccess, this);
  }
  finally {
    mySavingInProgress.set(false);
    myApplication.getMessageBus().syncPublisher(ProjectSaved.TOPIC).saved(this);
  }
}
 
Example 7
Source File: TempFiles.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void deleteAll() {
  for (File file : myFilesToDelete) {
    if (!FileUtil.delete(file)) {
      file.deleteOnExit();
    }
  }
}
 
Example 8
Source File: GenericCompilerCache.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void createMap() throws IOException {
  try {
    myPersistentMap = new PersistentHashMap<KeyAndTargetData<Key>, PersistentStateData<SourceState,OutputState>>(myCacheFile, new SourceItemDataDescriptor(myCompiler.getItemKeyDescriptor()),
                                                                                                                 new PersistentStateDataExternalizer(myCompiler));
  }
  catch (PersistentEnumerator.CorruptedException e) {
    FileUtil.delete(myCacheFile);
    throw e;
  }
}
 
Example 9
Source File: BTreeEnumeratorTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void tearDown() throws Exception {
  myEnumerator.close();
  FileUtil.delete(myFile);
  FileUtil.delete(new File(myFile.getParentFile(), myFile.getName() + ".len"));
  assertTrue(!myFile.exists());
  super.tearDown();
}
 
Example 10
Source File: OSProcessHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void deleteTempFiles(Set<File> tempFiles) {
  if (tempFiles != null) {
    try {
      for (File file : tempFiles) {
        FileUtil.delete(file);
      }
    }
    catch (Throwable t) {
      LOG.error("failed to delete temp. files", t);
    }
  }
}
 
Example 11
Source File: MetricsIntegrationTest.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void tearDown() throws Exception {
  System.clearProperty(PantsMetrics.SYSTEM_PROPERTY_METRICS_ENABLE);
  if (tempDir != null) {
    FileUtil.delete(tempDir);
  }
  super.tearDown();
}
 
Example 12
Source File: IoDirectoryBasedStorage.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void deleteFiles(@Nonnull File dir) {
  for (File file : dir.listFiles()) {
    if (removedFileNames.contains(file.getName())) {
      FileUtil.delete(file);
    }
  }
}
 
Example 13
Source File: PropertiesEncryptionSupport.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void store(@Nonnull Properties props, @Nonnull String comments, @Nonnull File file) throws Exception {
  if (props.isEmpty()) {
    FileUtil.delete(file);
    return;
  }

  final ByteArrayOutputStream out = new ByteArrayOutputStream();
  props.store(out, comments);
  out.close();
  FileUtil.writeToFile(file, encrypt(out.toByteArray()));
}
 
Example 14
Source File: DownloadUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Downloads content of {@code url} to {@code outputFile} atomically.<br/>
 * {@code outputFile} isn't modified if an I/O error occurs or {@code contentChecker} is provided and returns false on the downloaded content.
 * More formally, the steps are:
 * <ol>
 *   <li>Download {@code url} to {@code tempFile}. Stop in case of any I/O errors.</li>
 *   <li>Stop if {@code contentChecker} is provided, and it returns false on the downloaded content.</li>
 *   <li>Move {@code tempFile} to {@code outputFile}. On most OS this operation is done atomically.</li>
 * </ol>
 *
 * Motivation: some web filtering products return pure HTML with HTTP 200 OK status instead of
 * the asked content.
 *
 * @param indicator   progress indicator
 * @param url         url to download
 * @param outputFile  output file
 * @param tempFile    temporary file to download to. This file is deleted on method exit.
 * @param contentChecker checks whether the downloaded content is OK or not
 * @returns true if no {@code contentChecker} is provided or the provided one returned true
 * @throws IOException if an I/O error occurs
 */
public static boolean downloadAtomically(@Nullable ProgressIndicator indicator,
                                         @Nonnull String url,
                                         @Nonnull File outputFile,
                                         @Nonnull File tempFile,
                                         @Nullable Predicate<String> contentChecker) throws IOException
{
  try {
    downloadContentToFile(indicator, url, tempFile);
    if (contentChecker != null) {
      String content = FileUtil.loadFile(tempFile);
      if (!contentChecker.test(content)) {
        return false;
      }
    }
    FileUtil.rename(tempFile, outputFile);
    return true;
  } finally {
    FileUtil.delete(tempFile);
  }
}
 
Example 15
Source File: StorageUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static void deleteFile(@Nonnull File file) throws IOException {
  if (file.exists()) {
    FileUtil.delete(file);
  }
}
 
Example 16
Source File: CompositePrintable.java    From consulo with Apache License 2.0 4 votes vote down vote up
public synchronized void dispose() {
  if (myFile != null) FileUtil.delete(myFile);
}
 
Example 17
Source File: UpdateableZipTest.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected void tearDown() throws Exception {
  FileUtil.delete(zipFile);
  super.tearDown();
}
 
Example 18
Source File: FileWatcherTest.java    From consulo with Apache License 2.0 4 votes vote down vote up
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.getPath());
  VfsRootAccess.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);
    myAcceptedDirectories.add(substDir.getPath());

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

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

      myAccept = true;
      FileUtil.writeToFile(file, "re-creation");
      assertEvent(VFileCreateEvent.class, substFile.getPath());
    }
    finally {
      unwatch(request);
    }
  }
  finally {
    delete(targetDir);
    deleteSubst(rootFile.getPath());
    if (vfsRoot != null) {
      ((NewVirtualFile)vfsRoot).markDirty();
      myFileSystem.refresh(false);
    }
    VfsRootAccess.disallowRootAccess(rootFile.getPath());
  }
}
 
Example 19
Source File: FileWatcherTest.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void testDirectoryOverlapping() throws Exception {
  File topDir = createTestDir("top");
  File fileInTopDir = createTestFile(topDir, "file1.txt");
  File subDir = createTestDir(topDir, "sub");
  File fileInSubDir = createTestFile(subDir, "file2.txt");
  File sideDir = createTestDir("side");
  File fileInSideDir = createTestFile(sideDir, "file3.txt");
  refresh(topDir);
  refresh(sideDir);

  LocalFileSystem.WatchRequest requestForSubDir = watch(subDir);
  LocalFileSystem.WatchRequest requestForSideDir = watch(sideDir);
  try {
    myAccept = true;
    FileUtil.writeToFile(fileInTopDir, "new content");
    FileUtil.writeToFile(fileInSubDir, "new content");
    FileUtil.writeToFile(fileInSideDir, "new content");
    assertEvent(VFileContentChangeEvent.class, fileInSubDir.getPath(), fileInSideDir.getPath());

    LocalFileSystem.WatchRequest requestForTopDir = watch(topDir);
    try {
      myAccept = true;
      FileUtil.writeToFile(fileInTopDir, "newer content");
      FileUtil.writeToFile(fileInSubDir, "newer content");
      FileUtil.writeToFile(fileInSideDir, "newer content");
      assertEvent(VFileContentChangeEvent.class, fileInTopDir.getPath(), fileInSubDir.getPath(), fileInSideDir.getPath());
    }
    finally {
      unwatch(requestForTopDir);
    }

    myAccept = true;
    FileUtil.writeToFile(fileInTopDir, "newest content");
    FileUtil.writeToFile(fileInSubDir, "newest content");
    FileUtil.writeToFile(fileInSideDir, "newest content");
    assertEvent(VFileContentChangeEvent.class, fileInSubDir.getPath(), fileInSideDir.getPath());

    myAccept = true;
    FileUtil.delete(fileInTopDir);
    FileUtil.delete(fileInSubDir);
    FileUtil.delete(fileInSideDir);
    assertEvent(VFileDeleteEvent.class, fileInTopDir.getPath(), fileInSubDir.getPath(), fileInSideDir.getPath());
  }
  finally {
    unwatch(requestForSubDir, requestForSideDir);
    delete(topDir);
  }
}
 
Example 20
Source File: CacheManager.java    From r2m-plugin-android with Apache License 2.0 2 votes vote down vote up
/**
 * Clear the file definition for a particular controller method.
 * @param methodName method name
 */
public void clearControllerMethodCache(String methodName) {
    FileUtil.delete(getControllerMethodExample(methodName));
}