Commits

nskvortsov committed 526b6f7

fix http://bitbucket.org/nskvortsov/deployer/issue/9
more accurate recursive dir creation for FTP

Comments (0)

Files changed (2)

deploy-runner/deploy-runner-agent/src/main/java/jetbrains/buildServer/deployer/agent/ftp/FtpBuildProcessAdapter.java

 package jetbrains.buildServer.deployer.agent.ftp;
 
-import it.sauronsoftware.ftp4j.FTPClient;
-import it.sauronsoftware.ftp4j.FTPException;
-import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
+import it.sauronsoftware.ftp4j.*;
 import jetbrains.buildServer.RunBuildException;
 import jetbrains.buildServer.agent.BuildRunnerContext;
 import jetbrains.buildServer.agent.impl.artifacts.ArtifactsCollection;
     }
 
     private void createPath(@NotNull final FTPClient client,
-                            @NotNull final String path) throws IOException, FTPIllegalReplyException, FTPException {
+                            @NotNull final String path) throws Exception {
+        final String root = client.currentDirectory();
         final String normalisedPath = path.trim().replaceAll("\\\\","/");
         final StringTokenizer pathTokenizer = new StringTokenizer(normalisedPath, "/");
-        final StringBuilder sb = new StringBuilder(pathTokenizer.nextToken());
-        createDirSkipExisting(client, sb.toString());
-        while(pathTokenizer.hasMoreTokens()) {
-            sb.append('/').append(pathTokenizer.nextToken());
-            createDirSkipExisting(client, sb.toString());
+
+        boolean prevDirExisted = true;
+        while (pathTokenizer.hasMoreTokens()) {
+            final String nextDir = pathTokenizer.nextToken();
+            if (prevDirExisted && dirExists(nextDir, client)) {
+                client.changeDirectory(nextDir);
+            } else {
+                prevDirExisted = false;
+                client.createDirectory(nextDir);
+                client.changeDirectory(nextDir);
+            }
+        }
+
+        client.changeDirectory(root);
+    }
+
+    private boolean dirExists(@NotNull final String nextDir,
+                              @NotNull final FTPClient client) throws Exception {
+        final String[] strings = client.listNames();
+        for (String string : strings) {
+            if (string.equals(nextDir)) {
+                return true;
+            }
         }
+        return false;
     }
 
     private void createDirSkipExisting(FTPClient client, final String directoryName) throws IOException, FTPIllegalReplyException, FTPException {
         try {
+            //client.changeDirectory(directoryName);
             client.createDirectory(directoryName);
         } catch (FTPException e) {
             // we can safely ignore if dir already exists

deploy-runner/deploy-runner-agent/src/test/java/jetbrains/buildServer/deployer/agent/ftp/FtpBuildProcessAdapterTest.java

         DeployTestUtils.assertCollectionsTransferred(existingPath, myArtifactsCollections);
     }
 
+    @Test
+    public void testTransferToExistingPath2() throws Exception {
+        final String subPath = "test_path/subdir";
+        myArtifactsCollections.add(DeployTestUtils.buildArtifactsCollection(myTempFiles, "dest1", "dest1"));
+        final BuildProcess process = getProcess("127.0.0.1:" + TEST_PORT);
+        DeployTestUtils.runProcess(process, 5000);
+        DeployTestUtils.assertCollectionsTransferred(myRemoteDir, myArtifactsCollections);
+    }
+
 
     private BuildProcess getProcess(String target) {
         return new FtpBuildProcessAdapter(myContext, target, myUsername, myPassword, myArtifactsCollections);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.