@NotNull
 public static Module doCreateRealModuleIn(
     @NotNull String moduleName,
     @NotNull final Project project,
     @NotNull final ModuleType moduleType) {
   final VirtualFile baseDir = project.getBaseDir();
   assertNotNull(baseDir);
   final File moduleFile =
       new File(
           baseDir.getPath().replace('/', File.separatorChar),
           moduleName + ModuleFileType.DOT_DEFAULT_EXTENSION);
   FileUtil.createIfDoesntExist(moduleFile);
   myFilesToDelete.add(moduleFile);
   return new WriteAction<Module>() {
     @Override
     protected void run(@NotNull Result<Module> result) throws Throwable {
       final VirtualFile virtualFile =
           LocalFileSystem.getInstance().refreshAndFindFileByIoFile(moduleFile);
       assert virtualFile != null;
       Module module =
           ModuleManager.getInstance(project).newModule(virtualFile.getPath(), moduleType.getId());
       module.getModuleFile();
       result.setResult(module);
     }
   }.execute().getResultObject();
 }
Example #2
0
  private void tryInit(String storageFilePath, PagePool pool, int retryCount) throws IOException {
    convertFromOldExtensions(storageFilePath);

    final File recordsFile = new File(storageFilePath + INDEX_EXTENSION);
    final File dataFile = new File(storageFilePath + DATA_EXTENSION);

    if (recordsFile.exists() != dataFile.exists()) {
      deleteFiles(storageFilePath);
    }

    FileUtil.createIfDoesntExist(recordsFile);
    FileUtil.createIfDoesntExist(dataFile);

    AbstractRecordsTable recordsTable = null;
    DataTable dataTable;
    try {
      recordsTable = createRecordsTable(pool, recordsFile);
      dataTable = new DataTable(dataFile, pool);
    } catch (IOException e) {
      LOG.info(e.getMessage());
      if (recordsTable != null) {
        recordsTable.dispose();
      }

      boolean deleted = deleteFiles(storageFilePath);
      if (!deleted) {
        throw new IOException("Can't delete caches at: " + storageFilePath);
      }
      if (retryCount >= 5) {
        throw new IOException("Can't create storage at: " + storageFilePath);
      }

      tryInit(storageFilePath, pool, retryCount + 1);
      return;
    }

    myRecordsTable = recordsTable;
    myDataTable = dataTable;
    myPool = pool;

    if (myDataTable.isCompactNecessary()) {
      compact(storageFilePath);
    }
  }
 private static void invalidateIndex() {
   LOG.info("Marking VFS as corrupted");
   final File indexRoot = PathManager.getIndexRoot();
   if (indexRoot.exists()) {
     final String[] children = indexRoot.list();
     if (children != null && children.length > 0) {
       // create index corruption marker only if index directory exists and is non-empty
       // It is incorrect to consider non-existing indices "corrupted"
       FileUtil.createIfDoesntExist(new File(PathManager.getIndexRoot(), "corruption.marker"));
     }
   }
 }
Example #4
0
  private void compact(final String path) {
    synchronized (myLock) {
      LOG.info(
          "Space waste in " + path + " is " + myDataTable.getWaste() + " bytes. Compacting now.");
      long start = System.currentTimeMillis();

      try {
        File newDataFile = new File(path + ".storageData.backup");
        FileUtil.delete(newDataFile);
        FileUtil.createIfDoesntExist(newDataFile);

        File oldDataFile = new File(path + DATA_EXTENSION);
        DataTable newDataTable = new DataTable(newDataFile, myPool);

        final int count = myRecordsTable.getRecordsCount();
        for (int i = 1; i <= count; i++) {
          final long addr = myRecordsTable.getAddress(i);
          final int size = myRecordsTable.getSize(i);

          if (size > 0) {
            assert addr > 0;

            final int capacity = myCapacityAllocationPolicy.calculateCapacity(size);
            final long newaddr = newDataTable.allocateSpace(capacity);
            final byte[] bytes = new byte[size];
            myDataTable.readBytes(addr, bytes);
            newDataTable.writeBytes(newaddr, bytes);
            myRecordsTable.setAddress(i, newaddr);
            myRecordsTable.setCapacity(i, capacity);
          }
        }

        myDataTable.dispose();
        newDataTable.dispose();

        if (!FileUtil.delete(oldDataFile)) {
          throw new IOException("Can't delete file: " + oldDataFile);
        }

        newDataFile.renameTo(oldDataFile);
        myDataTable = new DataTable(oldDataFile, myPool);
      } catch (IOException e) {
        LOG.info("Compact failed: " + e.getMessage());
      }

      long timedelta = System.currentTimeMillis() - start;
      LOG.info("Done compacting in " + timedelta + "msec.");
    }
  }
 public void saveVersion() {
   final Boolean differs = myVersionDiffers;
   if (differs == null || differs) {
     try {
       FileUtil.createIfDoesntExist(myVersionFile);
       final DataOutputStream os = new DataOutputStream(new FileOutputStream(myVersionFile));
       try {
         os.writeInt(VERSION);
         myVersionDiffers = Boolean.FALSE;
       } finally {
         os.close();
       }
     } catch (IOException ignored) {
     }
   }
 }
  private static void saveOnDisk(BufferExposingByteArrayOutputStream bytes, final File file)
      throws IOException {
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
      FileUtil.createIfDoesntExist(file);
    }

    if (fos == null) {
      fos = new FileOutputStream(file);
    }
    try {
      fos.write(bytes.getInternalBuffer(), 0, bytes.size());
    } finally {
      fos.close();
    }
  }
 private static void ensureLogConfigExists(File workDirectory) {
   final File logConfig = new File(workDirectory, LOGGER_CONFIG);
   if (!logConfig.exists()) {
     FileUtil.createIfDoesntExist(logConfig);
     try {
       final InputStream in = Server.class.getResourceAsStream("/" + DEFAULT_LOGGER_CONFIG);
       if (in != null) {
         try {
           final FileOutputStream out = new FileOutputStream(logConfig);
           try {
             FileUtil.copy(in, out);
           } finally {
             out.close();
           }
         } finally {
           in.close();
         }
       }
     } catch (IOException e) {
       LOG.error(e);
     }
   }
 }