Exemplo n.º 1
0
  // {{{ loadDirectory() method
  public void loadDirectory(
      final Object node, String path, final boolean addToHistory, final Runnable delayedAWTTask) {
    path = MiscUtilities.constructPath(browser.getDirectory(), path);
    VFS vfs = VFSManager.getVFSForPath(path);

    Object session = vfs.createVFSSession(path, this);
    if (session == null) {
      if (delayedAWTTask != null) ThreadUtilities.runInDispatchThread(delayedAWTTask);
      return;
    }

    if (node == null) {
      parentDirectories.setListData(new Object[] {new LoadingPlaceholder()});
    }

    final Object[] loadInfo = new Object[2];
    Runnable awtRunnable =
        new Runnable() {
          public void run() {
            browser.directoryLoaded(node, loadInfo, addToHistory);
            if (delayedAWTTask != null) delayedAWTTask.run();
          }
        };
    ThreadUtilities.runInBackground(
        new ListDirectoryBrowserTask(browser, session, vfs, path, loadInfo, awtRunnable));
  } // }}}
Exemplo n.º 2
0
  // {{{ getTargetName() method
  private String getTargetName(Object session, VFS vfs, String path, String baseName)
      throws IOException {
    if (behavior == Behavior.OVERWRITE) {
      // We want to overwrite, no need to check anything
      return baseName;
    }

    String s = MiscUtilities.constructPath(target, baseName);
    VFSFile file = vfs._getFile(session, s, comp);
    if (file == null) {
      // The target file do not exist, perfect
      return baseName;
    }
    if (behavior == Behavior.SKIP) return null;

    String extension = MiscUtilities.getFileExtension(baseName);
    String nameNoExtension = MiscUtilities.getBaseName(baseName);
    for (int i = 1; i < 1000; i++) {
      String name = nameNoExtension + "-copy-" + i;
      if (extension != null) name += extension;
      s = MiscUtilities.constructPath(path, name);
      file = vfs._getFile(session, s, comp);
      if (file == null) return name;
    }
    return null;
  } // }}}
Exemplo n.º 3
0
 public static void registerVFS(String protocol, VFS vfs) {
   Log.log(
       Log.DEBUG,
       VFSManager.class,
       "Registered " + vfs.getName() + " filesystem for " + protocol + " protocol");
   vfsHash.put(vfs.getName(), vfs);
   protocolHash.put(protocol, vfs);
 }
Exemplo n.º 4
0
  private String getPrevOrNextFile(View view, String path, Direction direction) {
    if (files == null) files = _getFiles(view);

    if (files == null || files.length == 0) return null;

    if (path == null) {
      path = view.getBuffer().getSymlinkPath();
      VFS vfs = VFSManager.getVFSForPath(path);
      boolean ignoreCase = ((vfs.getCapabilities() & VFS.CASE_INSENSITIVE_CAP) != 0);

      for (int i = 0; i < files.length; i++) {
        if (StandardUtilities.compareStrings(files[i], path, ignoreCase) == 0) {
          return path;
        }
      }

      if (direction == Direction.NEXT) {
        return getFirstFile(view);
      } else {
        return getLastFile(view);
      }
    } else {
      // -1 so that the last isn't checked
      VFS vfs = VFSManager.getVFSForPath(path);
      boolean ignoreCase = ((vfs.getCapabilities() & VFS.CASE_INSENSITIVE_CAP) != 0);

      if (direction == Direction.NEXT
          && StandardUtilities.compareStrings(files[files.length - 1], path, ignoreCase) == 0) {
        // Going forward and already at the last file
        return null;
      } else if (direction == Direction.PREV
          && StandardUtilities.compareStrings(files[0], path, ignoreCase) == 0) {
        // Going backward and already at the first file
        return null;
      }

      for (int i = 0; i < files.length - 1; i++) {
        if (StandardUtilities.compareStrings(files[i], path, ignoreCase) == 0) {
          if (direction == Direction.NEXT) return files[i + 1];
          else {
            if (i == 0) return files[files.length - 1];
            return files[i - 1];
          }
        }
      }

      return null;
    }
  } // }}}
Exemplo n.º 5
0
  public static void sendVFSUpdate(VFS vfs, String path, boolean parent) {
    if (parent) {
      sendVFSUpdate(vfs, vfs.getParentOfPath(path), false);
      sendVFSUpdate(vfs, path, false);
    } else {

      if (path.length() != 1 && (path.endsWith("/") || path.endsWith(java.io.File.separator)))
        path = path.substring(0, path.length() - 1);

      synchronized (vfsUpdateLock) {
        for (int i = 0; i < vfsUpdates.size(); i++) {
          VFSUpdate msg = (VFSUpdate) vfsUpdates.get(i);
          if (msg.getPath().equals(path)) {

            return;
          }
        }

        vfsUpdates.add(new VFSUpdate(path));

        if (vfsUpdates.size() == 1) {

          VFSManager.runInAWTThread(new SendVFSUpdatesSafely());
        }
      }
    }
  }
Exemplo n.º 6
0
  /**
   * loads a pluginSet xml file and updates the model to reflect certain checked selections
   *
   * @since jEdit 4.3pre10
   * @author Alan Ezust
   */
  boolean loadPluginSet(String path) {
    pluginSet.clear();
    pluginModel.restoreSelection(new HashSet<String>(), new HashSet<String>());

    VFS vfs = VFSManager.getVFSForPath(path);
    Object session = vfs.createVFSSession(path, InstallPanel.this);
    try {
      InputStream is = vfs._createInputStream(session, path, false, InstallPanel.this);
      XMLUtilities.parseXML(is, new StringMapHandler());
    } catch (Exception e) {
      Log.log(Log.WARNING, this, "Loading Pluginset failed:" + e.getMessage());
      return false;
    }
    pluginModel.update();
    return true;
  } // }}}
Exemplo n.º 7
0
 // {{{ DirectoryEntry constructor
 public DirectoryEntry(
     String name, String path, String deletePath, int type, long length, boolean hidden) {
   this.name = name;
   this.path = path;
   this.deletePath = deletePath;
   this.symlinkPath = path;
   this.type = type;
   this.length = length;
   this.hidden = hidden;
   if (path != null) {
     // maintain backwards compatibility
     VFS vfs = VFSManager.getVFSForPath(path);
     canRead = ((vfs.getCapabilities() & READ_CAP) != 0);
     canWrite = ((vfs.getCapabilities() & WRITE_CAP) != 0);
   }
 } // }}}
Exemplo n.º 8
0
 public static Buffer createFeatureBuffer() {
   View view = jEdit.getActiveView();
   String parent = null;
   if (view != null) {
     Buffer buffer = view.getBuffer();
     parent = buffer.getDirectory();
   }
   if (parent == null) {
     parent = System.getProperty("user.home");
   }
   VFS vfs = VFSManager.getVFSForPath(parent);
   if ((vfs.getCapabilities() & VFS.WRITE_CAP) == 0) {
     // cannot write on that VFS, creating untitled buffer in home directory
     parent = System.getProperty("user.home");
   }
   Buffer buffer = jEdit.openTemporary(view, tempPath(), getNextFeatureTemp(), true, null);
   jEdit.commitTemporary(buffer);
   return buffer;
 }
Exemplo n.º 9
0
  // {{{ copyFileList() method
  private void copyFileList() {
    VFS vfs = VFSManager.getVFSForPath(target);
    Object targetSession = null;
    try {
      targetSession = vfs.createVFSSession(target, comp);
      if (targetSession == null) {
        Log.log(Log.ERROR, this, "Target VFS path cannot be reached");
        return;
      }
      VFSFile targetFile = vfs._getFile(targetSession, target, comp);
      if (targetFile == null) {
        Log.log(Log.ERROR, this, "Target is unreachable or do not exist");
        return;
      }

      if (targetFile.getType() != VFSFile.DIRECTORY) {
        Log.log(Log.ERROR, this, "Target is not a directory");
        return;
      }
      if (sources != null) {
        setMaximum(sources.size());
        for (int i = 0; i < sources.size(); i++) {
          setValue(i);
          String sourcePath = sources.get(i);
          String sourceName = MiscUtilities.getFileName(sourcePath);
          setLabel(sourceName);
          copy(targetSession, vfs, sourcePath, sourceName, target);
        }
      }
    } catch (IOException e) {
      Log.log(Log.ERROR, this, e);
    } catch (InterruptedException e) {
      Log.log(Log.WARNING, this, "Copy was interrupted");
    } finally {
      VFSManager.sendVFSUpdate(vfs, target, true);
      try {
        if (targetSession != null) vfs._endVFSSession(targetSession, comp);
      } catch (IOException e) {
      }
    }
  } // }}}
Exemplo n.º 10
0
  /**
   * Copy a file to another using VFS.
   *
   * @param progress the progress observer. It could be null if you don't want to monitor progress.
   *     If not null you should probably launch this command in a WorkThread
   * @param sourceVFS the source VFS
   * @param sourceSession the VFS session
   * @param sourcePath the source path
   * @param targetVFS the target VFS
   * @param targetSession the target session
   * @param targetPath the target path
   * @param comp comp The component that will parent error dialog boxes
   * @param canStop could this copy be stopped ?
   * @return true if the copy was successful
   * @throws IOException IOException If an I/O error occurs
   * @since jEdit 4.3pre3
   */
  public static boolean copy(
      ProgressObserver progress,
      VFS sourceVFS,
      Object sourceSession,
      String sourcePath,
      VFS targetVFS,
      Object targetSession,
      String targetPath,
      Component comp,
      boolean canStop)
      throws IOException {
    if (progress != null) progress.setStatus("Initializing");

    InputStream in = null;
    OutputStream out = null;
    try {
      VFSFile sourceVFSFile = sourceVFS._getFile(sourceSession, sourcePath, comp);
      if (sourceVFSFile == null) throw new FileNotFoundException(sourcePath);
      if (progress != null) {
        progress.setMaximum(sourceVFSFile.getLength());
      }
      VFSFile targetVFSFile = targetVFS._getFile(targetSession, targetPath, comp);
      if (targetVFSFile.getType() == VFSFile.DIRECTORY) {
        if (targetVFSFile.getPath().equals(sourceVFSFile.getPath())) return false;
        targetPath = MiscUtilities.constructPath(targetPath, sourceVFSFile.getName());
      }
      in =
          new BufferedInputStream(
              sourceVFS._createInputStream(sourceSession, sourcePath, false, comp));
      out =
          new BufferedOutputStream(targetVFS._createOutputStream(targetSession, targetPath, comp));
      boolean copyResult = IOUtilities.copyStream(IOBUFSIZE, progress, in, out, canStop);
      VFSManager.sendVFSUpdate(targetVFS, targetPath, true);
      return copyResult;
    } finally {
      IOUtilities.closeQuietly(in);
      IOUtilities.closeQuietly(out);
    }
  }
Exemplo n.º 11
0
 /**
  * Copy a file to another using VFS.
  *
  * @param progress the progress observer. It could be null if you don't want to monitor progress.
  *     If not null you should probably launch this command in a WorkThread
  * @param sourcePath the source path
  * @param targetPath the target path
  * @param comp comp The component that will parent error dialog boxes
  * @param canStop if true the copy can be stopped
  * @return true if the copy was successful
  * @throws IOException IOException If an I/O error occurs
  * @since jEdit 4.3pre3
  */
 public static boolean copy(
     ProgressObserver progress,
     String sourcePath,
     String targetPath,
     Component comp,
     boolean canStop)
     throws IOException {
   VFS sourceVFS = VFSManager.getVFSForPath(sourcePath);
   Object sourceSession = sourceVFS.createVFSSession(sourcePath, comp);
   if (sourceSession == null) {
     Log.log(
         Log.WARNING,
         VFS.class,
         "Unable to get a valid session from " + sourceVFS + " for path " + sourcePath);
     return false;
   }
   VFS targetVFS = VFSManager.getVFSForPath(targetPath);
   Object targetSession = targetVFS.createVFSSession(targetPath, comp);
   if (targetSession == null) {
     Log.log(
         Log.WARNING,
         VFS.class,
         "Unable to get a valid session from " + targetVFS + " for path " + targetPath);
     return false;
   }
   return copy(
       progress,
       sourceVFS,
       sourceSession,
       sourcePath,
       targetVFS,
       targetSession,
       targetPath,
       comp,
       canStop);
 } // }}}
Exemplo n.º 12
0
 // {{{ _run() method
 @Override
 public void _run() {
   Log.log(Log.DEBUG, this, this + ".run()");
   if (source != null) {
     // single file copy
     try {
       VFS.copy(this, source, target, comp, false, false);
     } catch (IOException e) {
       Log.log(Log.ERROR, this, e, e);
     } finally {
       if (latch != null) latch.countDown();
     }
   } else {
     // List file copy
     copyFileList();
   }
 } // }}}
Exemplo n.º 13
0
  /**
   * Rebuild the parent view after a directory has been loaded.
   *
   * @param node
   * @param path
   * @param directory
   */
  public void directoryLoaded(Object node, String path, java.util.List<VFSFile> directory) {
    // {{{ If reloading root, update parent directory list
    if (node == null) {
      DefaultListModel parentList = new DefaultListModel();

      String parent = path;

      for (; ; ) {
        VFS _vfs = VFSManager.getVFSForPath(parent);
        VFSFile file = null;
        if (_vfs instanceof FileVFS) {
          Object session = _vfs.createVFSSession(path, browser);
          try {
            file = _vfs._getFile(session, parent, browser);
            if (file != null) {
              file.setName(_vfs.getFileName(parent));
            }
          } catch (IOException e) {
            Log.log(Log.ERROR, this, e, e);
          }
        }
        if (file == null) {
          // create a DirectoryEntry manually
          // instead of using _vfs._getFile()
          // since so many VFS's have broken
          // implementations of this method
          file =
              new VFSFile(_vfs.getFileName(parent), parent, parent, VFSFile.DIRECTORY, 0L, false);
        }

        /*parentList.insertElementAt(new VFSFile(
        _vfs.getFileName(parent),
        parent,parent,
        VFSFile.DIRECTORY,
        0L,false),0);*/
        parentList.insertElementAt(file, 0);
        String newParent = _vfs.getParentOfPath(parent);

        if (newParent == null || MiscUtilities.pathsEqual(parent, newParent)) break;
        else parent = newParent;
      }

      parentDirectories.setModel(parentList);
      int index = parentList.getSize() - 1;
      parentDirectories.setSelectedIndex(index);
      parentDirectories.ensureIndexIsVisible(index);
    } // }}}

    table.setDirectory(VFSManager.getVFSForPath(path), node, directory, tmpExpanded);
  } // }}}
Exemplo n.º 14
0
  /**
   * Recursively encrypts or decrypts all files given to the method and all files in directories
   * given to the method.
   *
   * @param relativePath The relative from the initial directory up to the current one, for
   *     encryption or decryption into another directory to be able to rebuild the directory
   *     structure
   * @param files The files and directories to encrypt or decrypt
   * @return Whether all encryption or decryption was fine
   */
  @CheckReturnValue(explanation = "If false is returned, something went wrong")
  private boolean cryptFiles(@NonNull String relativePath, @NonNull VFSFile... files) {
    for (VFSFile file : files) {
      VFS vfs = file.getVFS();
      VFS newVfs = null;
      String path = file.getPath();
      Object session = vfs.createVFSSession(path, this);
      Object sessionNew = null;
      Object sessionNewParent = null;
      InputStream in = null;
      ByteArrayInputStream bais = null;
      OutputStream out = null;
      ByteArrayOutputStream baos = null;
      try {
        if (FILE == file.getType()) {
          in = vfs._createInputStream(session, path, false, this);
          baos = new ByteArrayOutputStream();
          if (!IOUtilities.copyStream(null, in, baos, false)) {
            GUIUtilities.error(
                this,
                encrypt
                    ? "cipher.error.error-while-encrypting-file"
                    : "cipher.error.error-while-decrypting-file",
                new Object[] {path});
            continue;
          }
          baos.flush();
          byte[] cryptResult;
          synchronized (cipher) {
            if (encrypt) {
              cipher.setRawData(baos.toByteArray());
            } else {
              cipher.setEncryptedData(baos.toByteArray());
            }
            cipher.setEntropy(password);
            cipher.setAdditionalInformation(additionalInformation);
            if (encrypt) {
              cryptResult = cipher.encryptToByteArray();
            } else {
              cryptResult = cipher.decryptToByteArray();
            }
          }
          if (null == cryptResult) {
            GUIUtilities.error(
                this,
                encrypt
                    ? "cipher.error.error-while-encrypting-file"
                    : "cipher.error.error-while-decrypting-file",
                new Object[] {path});
            continue;
          }
          bais = new ByteArrayInputStream(cryptResult);
          String newPath;
          switch (newFileHandling) {
            case OVERWRITE:
              newPath = path;
              newVfs = vfs;
              break;

            case OTHER_DIRECTORY:
              if (0 < relativePath.length()) {
                newPath = MiscUtilities.constructPath(directoryTextField.getText(), relativePath);
              } else {
                newPath = directoryTextField.getText();
              }
              newPath = MiscUtilities.constructPath(newPath, file.getName());
              newVfs = VFSManager.getVFSForPath(newPath);
              break;

            case SUFFIX:
              newPath = path + suffixTextField.getText();
              newVfs = vfs;
              break;

            default:
              throw new InternalError(
                  "missing case branch for NewFileHandling: " + newFileHandling);
          }
          String newPathParent = MiscUtilities.getParentOfPath(newPath);
          sessionNewParent = newVfs.createVFSSession(newPathParent, this);
          newVfs._mkdir(sessionNewParent, newPathParent, this);
          sessionNew = newVfs.createVFSSession(newPath, this);
          out = newVfs._createOutputStream(sessionNew, newPath, this);
          if (!IOUtilities.copyStream(null, bais, out, false)) {
            GUIUtilities.error(
                this,
                encrypt
                    ? "cipher.error.error-while-encrypting-file"
                    : "cipher.error.error-while-decrypting-file",
                new Object[] {path});
            continue;
          }
          VFSManager.sendVFSUpdate(newVfs, newPath, true);
        } else {
          String newRelativePath;
          if (0 < relativePath.length()) {
            newRelativePath = MiscUtilities.concatPath(relativePath, file.getName());
          } else {
            newRelativePath = file.getName();
          }
          if (!cryptFiles(newRelativePath, vfs._listFiles(session, path, this))) {
            return false;
          }
        }
      } catch (IOException ioe) {
        Log.log(ERROR, this, ioe);
        new TextAreaDialog(
            this,
            encrypt
                ? "cipher.error.error-while-encrypting-files"
                : "cipher.error.error-while-decrypting-files",
            ioe);
        return false;
      } finally {
        try {
          vfs._endVFSSession(session, this);
        } catch (IOException ioe) {
          // just ignore it, we are not interested
        }
        try {
          if (null != newVfs) {
            newVfs._endVFSSession(sessionNew, this);
          }
        } catch (IOException ioe) {
          // just ignore it, we are not interested
        }
        try {
          if (null != newVfs) {
            newVfs._endVFSSession(sessionNewParent, this);
          }
        } catch (IOException ioe) {
          // just ignore it, we are not interested
        }
        try {
          if (null != out) {
            out.flush();
          }
        } catch (IOException ioe) {
          // just ignore it, we are not interested
        }
        IOUtilities.closeQuietly(in);
        IOUtilities.closeQuietly(bais);
        IOUtilities.closeQuietly(out);
        IOUtilities.closeQuietly(baos);
      }
    }
    return true;
  }