@NotNull
  private VirtualFilePointer create(
      @Nullable("null means the pointer will be created from the (not null) url") VirtualFile file,
      @Nullable("null means url has to be computed from the (not-null) file path") String url,
      @NotNull Disposable parentDisposable,
      @Nullable VirtualFilePointerListener listener) {
    VirtualFileSystem fileSystem;
    String protocol;
    String path;
    if (file == null) {
      int protocolEnd = url.indexOf(URLUtil.SCHEME_SEPARATOR);
      if (protocolEnd == -1) {
        protocol = null;
        fileSystem = null;
      } else {
        protocol = url.substring(0, protocolEnd);
        fileSystem = myVirtualFileManager.getFileSystem(protocol);
      }
      path = url.substring(protocolEnd + URLUtil.SCHEME_SEPARATOR.length());
    } else {
      fileSystem = file.getFileSystem();
      protocol = fileSystem.getProtocol();
      path = file.getPath();
      url = VirtualFileManager.constructUrl(protocol, path);
    }

    if (fileSystem == TEMP_FILE_SYSTEM) {
      // for tests, recreate always
      VirtualFile found = file == null ? VirtualFileManager.getInstance().findFileByUrl(url) : file;
      return new IdentityVirtualFilePointer(found, url);
    }

    boolean isJar = fileSystem == JAR_FILE_SYSTEM;
    if (fileSystem != LOCAL_FILE_SYSTEM && !isJar) {
      // we are unable to track alien file systems for now
      VirtualFile found =
          fileSystem == null
              ? null
              : file != null ? file : VirtualFileManager.getInstance().findFileByUrl(url);
      // if file is null, this pointer will never be alive
      return getOrCreateIdentity(url, found);
    }

    if (file == null) {
      String cleanPath = cleanupPath(path, isJar);
      // if newly created path is the same as substringed from url one then the url did not change,
      // we can reuse it
      //noinspection StringEquality
      if (cleanPath != path) {
        url = VirtualFileManager.constructUrl(protocol, cleanPath);
        path = cleanPath;
      }
    }
    // else url has come from VirtualFile.getPath() and is good enough

    VirtualFilePointerImpl pointer =
        getOrCreate(parentDisposable, listener, path, Pair.create(file, url));
    DelegatingDisposable.registerDisposable(parentDisposable, pointer);
    return pointer;
  }
Example #2
0
 @Override
 public String[] listAll() throws IOException {
   final Transaction txn = env.getAndCheckCurrentTransaction();
   final ArrayList<String> allFiles = new ArrayList<>((int) vfs.getNumberOfFiles(txn));
   for (final File file : vfs.getFiles(txn)) {
     allFiles.add(file.getPath());
   }
   return allFiles.toArray(new String[allFiles.size()]);
 }
Example #3
0
 File openExistingFile(@NotNull final String name, final boolean throwFileNotFound) {
   final File result = vfs.openFile(env.getAndCheckCurrentTransaction(), name, false);
   if (throwFileNotFound && result == null) {
     throw new FileNotFoundException(name);
   }
   return result;
 }
Example #4
0
 private static boolean namesEqual(@NotNull VirtualFileSystem fs, @NotNull String n1, String n2) {
   return fs.isCaseSensitive() ? n1.equals(n2) : n1.equalsIgnoreCase(n2);
 }
Example #5
0
 @Override
 public long fileLength(String name) throws IOException {
   return vfs.getFileLength(env.getAndCheckCurrentTransaction(), openExistingFile(name, true));
 }
Example #6
0
 @Override
 public void deleteFile(String name) throws IOException {
   vfs.deleteFile(env.getAndCheckCurrentTransaction(), name);
 }
Example #7
0
 @Override
 public void touchFile(String name) throws IOException {
   vfs.touchFile(env.getAndCheckCurrentTransaction(), openExistingFile(name, true));
 }
Example #8
0
 @Override
 public void close() throws IOException {
   vfs.shutdown();
 }