/* (non-Javadoc)
  * @see com.aptana.ide.core.ftp.BaseFTPConnectionFileManager#listDirectory(org.eclipse.core.runtime.IPath, org.eclipse.core.runtime.IProgressMonitor)
  */
 @Override
 protected String[] listDirectory(IPath path, IProgressMonitor monitor)
     throws CoreException, FileNotFoundException {
   try {
     FTPFile[] ftpFiles = listFiles(path, monitor);
     List<String> list = new ArrayList<String>();
     for (FTPFile ftpFile : ftpFiles) {
       String name = ftpFile.getName();
       if (".".equals(name) || "..".equals(name)) { // $NON-NLS-1$ //$NON-NLS-2$
         continue;
       }
       ftpFileCache.put(path.append(ftpFile.getName()), ftpFile);
       list.add(name);
     }
     return list.toArray(new String[list.size()]);
   } catch (FileNotFoundException e) {
     throw e;
   } catch (OperationCanceledException e) {
     throw e;
   } catch (Exception e) {
     throw new CoreException(
         new Status(
             Status.ERROR,
             FTPPlugin.PLUGIN_ID,
             Messages.FTPConnectionFileManager_listing_directory_failed,
             e));
   } finally {
     monitor.done();
   }
 }
 private void recursiveDeleteTree(IPath path, IProgressMonitor monitor, MultiStatus status)
     throws IOException, ParseException {
   try {
     changeCurrentDir(path);
     FTPFile[] ftpFiles = listFiles(path, monitor);
     List<String> dirs = new ArrayList<String>();
     for (FTPFile ftpFile : ftpFiles) {
       String name = ftpFile.getName();
       if (".".equals(name) || "..".equals(name)) { // $NON-NLS-1$ //$NON-NLS-2$
         continue;
       }
       if (ftpFile.isDir()) {
         dirs.add(name);
         continue;
       }
       Policy.checkCanceled(monitor);
       monitor.subTask(path.append(name).toPortableString());
       try {
         ftpClient.delete(name);
       } catch (FTPException e) {
         status.add(
             new Status(
                 IStatus.ERROR,
                 FTPPlugin.PLUGIN_ID,
                 StringUtils.format(
                     Messages.FTPConnectionFileManager_deleting_failed,
                     path.append(name).toPortableString()),
                 e));
       }
       monitor.worked(1);
     }
     for (String name : dirs) {
       monitor.subTask(path.append(name).toPortableString());
       recursiveDeleteTree(path.append(name), monitor, status);
       Policy.checkCanceled(monitor);
       changeCurrentDir(path);
       Policy.checkCanceled(monitor);
       ftpClient.rmdir(name);
       monitor.worked(1);
     }
   } catch (IOException e) {
     throw e;
   } catch (Exception e) {
     status.add(
         new Status(
             IStatus.ERROR,
             FTPPlugin.PLUGIN_ID,
             StringUtils.format(
                 Messages.FTPConnectionFileManager_deleting_failed, path.toPortableString()),
             e));
   }
 }
 private static void fillFileInfo(ExtendedFileInfo fileInfo, FTPFile ftpFile) {
   fileInfo.setExists(true);
   fileInfo.setName(ftpFile.getName());
   fileInfo.setDirectory(ftpFile.isDir());
   fileInfo.setLength(ftpFile.size());
   fileInfo.setLastModified(ftpFile.lastModified() != null ? ftpFile.lastModified().getTime() : 0);
   fileInfo.setOwner(ftpFile.getOwner());
   fileInfo.setGroup(ftpFile.getGroup());
   fileInfo.setPermissions(Policy.permissionsFromString(ftpFile.getPermissions()));
   if (ftpFile.isLink()) {
     fileInfo.setAttribute(EFS.ATTRIBUTE_SYMLINK, true);
     fileInfo.setStringAttribute(EFS.ATTRIBUTE_LINK_TARGET, ftpFile.getLinkedName().trim());
   }
 }
Ejemplo n.º 4
0
  public static void main(String[] args) throws FTPException, IOException, ParseException {
    FtpUtil ftp = new FtpUtil();
    ftp.setRemoteHost("192.168.230.51");
    ftp.setUserName("digivox");
    ftp.setPassword("digivox");

    ftp.connect();

    String[] descriptions = ftp.directoryNameList("uravox", true);

    for (String d : descriptions) {
      System.out.println(d);
    }

    FTPFile[] files = ftp.directoryList("sdfdsf");

    for (FTPFile f : files) {
      System.out.println(f.getName());
    }
    ftp.changeDirectory("uravox");
  }
 private FTPFile[] listFiles(IPath dirPath, IProgressMonitor monitor)
     throws IOException, ParseException, FTPException {
   FTPFile[] ftpFiles = null;
   if (statSupported != Boolean.FALSE) {
     try {
       ftpFiles = ftpSTAT(dirPath.addTrailingSeparator().toPortableString());
     } catch (MalformedReplyException e) {
       statSupported = Boolean.FALSE;
     } catch (FTPException e) {
       if (e.getReplyCode() == 501 || e.getReplyCode() == 502 || e.getReplyCode() == 504) {
         statSupported = null;
       } else if (e.getReplyCode() != 500) {
         throwFileNotFound(e, dirPath);
       }
     }
     if (ftpFiles == null || ftpFiles.length == 0) {
       if (statSupported == null) {
         statSupported = Boolean.FALSE;
       }
       return ftpLIST(dirPath, monitor);
     } else if (ftpFiles[0].isLink()) {
       return ftpLIST(dirPath, monitor);
     } else if (statSupported == null) {
       statSupported = Boolean.TRUE;
     }
   } else {
     ftpFiles = ftpLIST(dirPath, monitor);
   }
   if (fileFactory.getSystem().toUpperCase().startsWith(WINDOWS_STR) && ftpFiles != null) {
     for (FTPFile ftpFile : ftpFiles) {
       if (ftpFile.getPermissions() == null) {
         ftpFile.setPermissions("-rw-r-----"); // $NON-NLS-1$
       }
     }
   }
   return ftpFiles;
 }
Ejemplo n.º 6
0
 /** Gets the name of this entry. */
 public String getName() {
   return ftpFile.getName();
 }
Ejemplo n.º 7
0
 /**
  * Gets the last modification time of this entry.
  *
  * @return the last modification time, in milliseconds since January 1, 1970 UTC.
  * @throws java.io.IOException
  */
 public long getLastModified() throws IOException {
   return ftpFile.lastModified().getTime();
   // return ftpFile.getTimestamp().getTimeInMillis();
 }
Ejemplo n.º 8
0
 /** Is this entry refering to a file? */
 public boolean isFile() {
   return !ftpFile.isDir() && !ftpFile.isLink();
 }
Ejemplo n.º 9
0
 /** Is this entry refering to a (sub-)directory? */
 public boolean isDirectory() {
   return ftpFile.isDir();
 }
  /* (non-Javadoc)
   * @see com.aptana.ide.core.ftp.BaseFTPConnectionFileManager#fetchFiles(org.eclipse.core.runtime.IPath, int, org.eclipse.core.runtime.IProgressMonitor)
   */
  @SuppressWarnings("deprecation")
  @Override
  protected ExtendedFileInfo[] fetchFiles(IPath path, int options, IProgressMonitor monitor)
      throws CoreException, FileNotFoundException {
    monitor = Policy.subMonitorFor(monitor, 1);
    try {
      FTPFile[] ftpFiles = listFiles(path, monitor);
      monitor.beginTask(Messages.FTPConnectionFileManager_gethering_file_details, ftpFiles.length);
      List<ExtendedFileInfo> list = new ArrayList<ExtendedFileInfo>();
      for (FTPFile ftpFile : ftpFiles) {
        if (".".equals(ftpFile.getName())
            || "..".equals(ftpFile.getName())) { // $NON-NLS-1$ //$NON-NLS-2$
          monitor.worked(1);
          continue;
        }
        Date lastModifiedServerInLocalTZ = ftpFile.lastModified();
        if (serverTimeZoneShift != 0 && lastModifiedServerInLocalTZ != null) {
          ftpFile.setLastModified(
              new Date(lastModifiedServerInLocalTZ.getTime() + serverTimeZoneShift));
        }
        if ((options & IExtendedFileStore.DETAILED) != 0) {
          if (!ftpFile.isDir() && ftpFile.lastModified().getSeconds() == 0) {
            if (serverSupportsFeature("MDTM")) { // $NON-NLS-1$
              changeCurrentDir(path);
              Policy.checkCanceled(monitor);
              try {
                Date lastModifiedLocalTZ = ftpClient.modtime(ftpFile.getName());
                if (lastModifiedLocalTZ != null) {
                  ftpFile.setLastModified(lastModifiedLocalTZ);
                }
              } catch (FTPException e) {
              }
            }
          }
        }
        IPath filePath = path.append(ftpFile.getName());
        ftpFileCache.put(filePath, ftpFile);

        ExtendedFileInfo fileInfo = createFileInfo(ftpFile);
        list.add(fileInfo);
        monitor.worked(1);
      }
      return list.toArray(new ExtendedFileInfo[list.size()]);
    } catch (FileNotFoundException e) {
      throw e;
    } catch (OperationCanceledException e) {
      throw e;
    } catch (Exception e) {
      // forces one connection retry
      if (connectionRetryCount < 1) {
        connectionRetryCount++;
        testOrConnect(monitor);
        try {
          return fetchFiles(path, options, monitor);
        } finally {
          connectionRetryCount = 0;
        }
      } else {
        connectionRetryCount = 0;
        throw new CoreException(
            new Status(
                Status.ERROR,
                FTPPlugin.PLUGIN_ID,
                Messages.FTPConnectionFileManager_fetching_directory_failed,
                e));
      }
    } finally {
      monitor.done();
    }
  }
 /* (non-Javadoc)
  * @see com.aptana.ide.core.ftp.BaseFTPConnectionFileManager#fetchFile(org.eclipse.core.runtime.IPath, int, org.eclipse.core.runtime.IProgressMonitor)
  */
 @SuppressWarnings("deprecation")
 @Override
 protected ExtendedFileInfo fetchFile(IPath path, int options, IProgressMonitor monitor)
     throws CoreException, FileNotFoundException {
   try {
     IPath dirPath = path.removeLastSegments(1);
     String name = path.lastSegment();
     FTPFile result = ftpFileCache.get(path);
     if (result == null) {
       FTPFile[] ftpFiles = listFiles(dirPath, monitor);
       for (FTPFile ftpFile : ftpFiles) {
         Date lastModifiedServerInLocalTZ = ftpFile.lastModified();
         if (serverTimeZoneShift != 0 && lastModifiedServerInLocalTZ != null) {
           ftpFile.setLastModified(
               new Date(lastModifiedServerInLocalTZ.getTime() + serverTimeZoneShift));
         }
         if (".".equals(ftpFile.getName())
             || "..".equals(ftpFile.getName())) { // $NON-NLS-1$ //$NON-NLS-2$
           if (Path.ROOT.equals(path) && ".".equals(ftpFile.getName())) { // $NON-NLS-1$
             ftpFile.setName(path.toPortableString());
             ftpFileCache.put(path, ftpFile);
             result = ftpFile;
           }
           continue;
         }
         ftpFileCache.put(dirPath.append(ftpFile.getName()), ftpFile);
         if (name != null && name.equalsIgnoreCase(ftpFile.getName())) {
           result = ftpFile;
         }
       }
     }
     if ((options & IExtendedFileStore.DETAILED) != 0) {
       if (result != null
           && !result.isDir()
           && name != null
           && result.lastModified().getSeconds() == 0) {
         if (serverSupportsFeature("MDTM")) { // $NON-NLS-1$
           changeCurrentDir(dirPath);
           Policy.checkCanceled(monitor);
           try {
             Date lastModifiedLocalTZ = ftpClient.modtime(name);
             if (lastModifiedLocalTZ != null) {
               result.setLastModified(lastModifiedLocalTZ);
             }
           } catch (FTPException e) {
           }
         }
       }
     }
     if (result == null && Path.ROOT.equals(path)) {
       result = new FTPFile(StringUtils.EMPTY, path.toPortableString(), 0, true, new Date(0));
     }
     if (result != null) {
       return createFileInfo(result);
     }
   } catch (FileNotFoundException e) {
     throw e;
   } catch (OperationCanceledException e) {
     throw e;
   } catch (Exception e) {
     // forces one connection retry
     if (connectionRetryCount < 1) {
       connectionRetryCount++;
       testOrConnect(monitor);
       try {
         return fetchFile(path, options, monitor);
       } finally {
         connectionRetryCount = 0;
       }
     } else {
       connectionRetryCount = 0;
       throw new CoreException(
           new Status(
               Status.ERROR,
               FTPPlugin.PLUGIN_ID,
               Messages.FTPConnectionFileManager_fetch_failed,
               e));
     }
   }
   ExtendedFileInfo fileInfo = new ExtendedFileInfo(path.lastSegment());
   fileInfo.setExists(false);
   return fileInfo;
 }
 private static ExtendedFileInfo createFileInfo(FTPFile ftpFile) {
   ExtendedFileInfo fileInfo = new ExtendedFileInfo(ftpFile.getName());
   fillFileInfo(fileInfo, ftpFile);
   return fileInfo;
 }
  @SuppressWarnings("deprecation")
  protected void getherServerInfo(ConnectionContext context, IProgressMonitor monitor) {
    Policy.checkCanceled(monitor);
    monitor.subTask(Messages.FTPConnectionFileManager_gethering_server_info);
    serverFeatures = null;
    try {
      String[] features = ftpClient.features();
      if (features != null && features.length > 0) {
        serverFeatures = new ArrayList<String>();
        for (int i = 0; i < features.length; ++i) {
          String feature = features[i].trim();
          if (feature.indexOf(' ') > 0) {
            feature = feature.substring(0, feature.indexOf(' '));
          }
          serverFeatures.add(feature);
        }
      }
    } catch (Exception e) {
    }
    try {
      String[] validCodes = {"214"}; // $NON-NLS-1$
      FTPReply reply = ftpClient.sendCommand("SITE HELP"); // $NON-NLS-1$
      ftpClient.validateReply(reply, validCodes);
      if (serverFeatures == null) {
        serverFeatures = new ArrayList<String>();
      }
      String[] data = reply.getReplyData();
      for (int i = 0; i < data.length; ++i) {
        String cmd = data[i].trim();
        if (cmd.startsWith("214")) { // $NON-NLS-1$
          continue;
        }
        serverFeatures.add(MessageFormat.format("SITE {0}", cmd)); // $NON-NLS-1$
      }
    } catch (Exception e) {
      e.getCause();
    }

    Policy.checkCanceled(monitor);
    FTPFile[] rootFiles = null;
    try {
      rootFiles = listFiles(Path.ROOT, monitor);
    } catch (Exception e) {
    }

    if (context != null && context.getBoolean(ConnectionContext.DETECT_TIMEZONE)) {
      serverTimeZoneShift = Integer.MIN_VALUE;
    } else if (timezone != null) {
      TimeZone tz = TimeZone.getTimeZone(timezone);
      if (tz != null) {
        long time = new Date().getTime();
        serverTimeZoneShift = TimeZone.getDefault().getOffset(time) - tz.getOffset(time);
      }
    }
    if (serverTimeZoneShift == Integer.MIN_VALUE) {
      Policy.checkCanceled(monitor);
      try {
        changeCurrentDir(Path.ROOT);
        FTPFile file = null;
        if (rootFiles != null) {
          for (FTPFile ftpFile : rootFiles) {
            if (ftpFile.isFile()
                && !ftpFile.getName().startsWith(".ht")
                && !(ftpFile.lastModified().getHours() == 0
                    && ftpFile.lastModified().getMinutes() == 0
                    && ftpFile.lastModified().getSeconds() == 0)) {
              file = ftpFile;
              break;
            }
          }
        }
        if (file == null && !Path.ROOT.equals(basePath)) {
          FTPFile[] ftpFiles = listFiles(basePath, monitor);
          for (FTPFile ftpFile : ftpFiles) {
            if (ftpFile.isFile()
                && !ftpFile.getName().startsWith(".ht")
                && !(ftpFile.lastModified().getHours() == 0
                    && ftpFile.lastModified().getMinutes() == 0
                    && ftpFile.lastModified().getSeconds() == 0)) {
              file = ftpFile;
              break;
            }
          }
        }
        Date lastModifiedLocal = null;
        if (file == null) {
          changeCurrentDir(basePath);
          lastModifiedLocal = new Date();
          ftpClient.put(new ByteArrayInputStream(new byte[] {}), TMP_TIMEZONE_CHECK);
          for (FTPFile ftpFile : listFiles(basePath, monitor)) {
            if (TMP_TIMEZONE_CHECK.equals(ftpFile.getName())) {
              file = ftpFile;
              break;
            }
          }
        }
        if (file != null) {
          Date lastModifiedServerInLocalTZ = file.lastModified();
          if (serverSupportsFeature("MDTM")) { // $NON-NLS-1$
            Date lastModifiedLocalTZ = ftpClient.modtime(file.getName());
            if (lastModifiedLocalTZ != null) {
              // align to minutes
              serverTimeZoneShift =
                  (lastModifiedLocalTZ.getTime() - lastModifiedLocalTZ.getTime() % 60000)
                      - (lastModifiedServerInLocalTZ.getTime()
                          - lastModifiedServerInLocalTZ.getTime() % 60000);
            }
          }
          if (serverTimeZoneShift == Integer.MIN_VALUE) {
            serverTimeZoneShift =
                (lastModifiedLocal.getTime() - lastModifiedLocal.getTime() % 60000)
                    - (lastModifiedServerInLocalTZ.getTime()
                        - lastModifiedServerInLocalTZ.getTime() % 60000);
            // align to 1/4 hour
            long rem = serverTimeZoneShift % 900000;
            if (rem < 450000) {
              serverTimeZoneShift -= rem;
            } else {
              serverTimeZoneShift += (900000 - rem);
            }
          }
          if (TMP_TIMEZONE_CHECK.equals(file.getName())) {
            ftpClient.delete(file.getName());
          }
          if (context != null) {
            Calendar cal = Calendar.getInstance();
            int rawOffset =
                (int)
                    (cal.get(Calendar.ZONE_OFFSET)
                        + cal.get(Calendar.DST_OFFSET)
                        - serverTimeZoneShift);
            context.put(ConnectionContext.SERVER_TIMEZONE, TimeZone.getAvailableIDs(rawOffset));
          }
        }
      } catch (OperationCanceledException e) {
        throw e;
      } catch (Exception e) {
        IdeLog.logImportant(
            FTPPlugin.getDefault(), Messages.FTPConnectionFileManager_server_tz_check, e);
      }
      if (serverTimeZoneShift == Integer.MIN_VALUE) {
        Calendar cal = Calendar.getInstance();
        serverTimeZoneShift = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
      }
    }

    hasServerInfo = true;
  }