Commit c9d5e0cc authored by Matija Obreza's avatar Matija Obreza

FTP: CWD only to existing folders

parent 887342e4
Pipeline #8978 passed with stage
in 5 minutes and 2 seconds
...@@ -69,6 +69,8 @@ public class FtpRunAs { ...@@ -69,6 +69,8 @@ public class FtpRunAs {
try { try {
return runnable.run(); return runnable.run();
} catch (Throwable e) {
throw e;
} finally { } finally {
if (ftpUser != null && ftpUser.user != null) { if (ftpUser != null && ftpUser.user != null) {
LOG.trace("Switching back from {}", ftpUser.user.getUsername()); LOG.trace("Switching back from {}", ftpUser.user.getUsername());
......
...@@ -42,7 +42,6 @@ import org.slf4j.Logger; ...@@ -42,7 +42,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -241,9 +240,25 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ ...@@ -241,9 +240,25 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ
public boolean changeWorkingDirectory(final String dir) { public boolean changeWorkingDirectory(final String dir) {
final Path normalized = Paths.get(getAbsolutePath()).resolve(dir).normalize().toAbsolutePath(); final Path normalized = Paths.get(getAbsolutePath()).resolve(dir).normalize().toAbsolutePath();
LOG.info("CWD this={} dir={} normalized={}", getAbsolutePath(), dir, normalized); LOG.info("CWD this={} dir={} normalized={}", getAbsolutePath(), dir, normalized);
this.cwd(normalized); if ("/".equals(normalized.toString())) {
// TODO Check if such path exists? this.cwd(normalized);
return true; return true;
}
// Check if such path exists?
try {
RepositoryFolder folder = repositoryService.getFolder(normalized);
if (folder != null) {
this.cwd(normalized);
return true;
} else {
LOG.warn("CWD to non-existent folder {} normalized={}", dir, normalized.toString());
return false;
}
} catch (InvalidRepositoryPathException e) {
LOG.warn("CWD to non-existent folder {} normalized={}", dir, normalized.toString(), e);
return false;
}
} }
}; };
...@@ -271,10 +286,10 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ ...@@ -271,10 +286,10 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ
try { try {
return isDirectory(path) ? return isDirectory(path) ?
// directory // directory
directory(path, this) directory("/".equals(path.toString()) ? path : FtpRunAs.asFtpUser(user, () -> repositoryService.getFolder(path).getFolderPath()), this)
// or file // or file
: file(FtpRunAs.asFtpUser(user, () -> repositoryService.getFile(path.getParent(), path.getFileName().toString())), this); : file(FtpRunAs.asFtpUser(user, () -> repositoryService.getFile(path.getParent(), path.getFileName().toString())), this);
} catch (final AuthenticationException e) { } catch (final AuthenticationException e) {
LOG.warn("Authentication problem {}", e.getMessage(), e); LOG.warn("Authentication problem {}", e.getMessage(), e);
...@@ -282,7 +297,7 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ ...@@ -282,7 +297,7 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ
} catch (InvalidRepositoryPathException e) { } catch (InvalidRepositoryPathException e) {
throw new FtpException(e.getMessage()); throw new FtpException(e.getMessage());
} catch (NoSuchRepositoryFileException e) { } catch (NoSuchRepositoryFileException e) {
LOG.warn("No such file {}", file); LOG.debug("No such file {}: {}", file, e.getMessage());
return new CanBeAnythingFile(path.getParent(), path.getFileName().toString()) { return new CanBeAnythingFile(path.getParent(), path.getFileName().toString()) {
...@@ -290,8 +305,8 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ ...@@ -290,8 +305,8 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ
public boolean mkdir() { public boolean mkdir() {
LOG.debug("MKDIR {}", path); LOG.debug("MKDIR {}", path);
try { try {
FtpRunAs.asFtpUser(user, () -> repositoryService.ensureFolder(path)); RepositoryFolder repositoryFolder = FtpRunAs.asFtpUser(user, () -> repositoryService.ensureFolder(path));
return true; return repositoryFolder != null;
} catch (InvalidRepositoryPathException e) { } catch (InvalidRepositoryPathException e) {
LOG.warn("{}", e.getMessage(), e); LOG.warn("{}", e.getMessage(), e);
return false; return false;
...@@ -300,7 +315,7 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ ...@@ -300,7 +315,7 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ
@Override @Override
public boolean delete() { public boolean delete() {
return false; return true;
} }
@Override @Override
...@@ -309,7 +324,10 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ ...@@ -309,7 +324,10 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ
if (path.getParent() != null) { if (path.getParent() != null) {
LOG.debug("MKDIR {}", path.getParent()); LOG.debug("MKDIR {}", path.getParent());
try { try {
FtpRunAs.asFtpUser(user, () -> repositoryService.ensureFolder(path.getParent())); RepositoryFolder repositoryFolder = FtpRunAs.asFtpUser(user, () -> repositoryService.ensureFolder(path.getParent()));
if (repositoryFolder == null) {
throw new InvalidRepositoryPathException("Folder not created " + path.getParent());
}
return bytesManager.newFile(user, path); return bytesManager.newFile(user, path);
} catch (InvalidRepositoryPathException e) { } catch (InvalidRepositoryPathException e) {
LOG.warn("{}", e.getMessage(), e); LOG.warn("{}", e.getMessage(), e);
......
...@@ -89,13 +89,12 @@ public class TemporaryBytesManager { ...@@ -89,13 +89,12 @@ public class TemporaryBytesManager {
FtpRunAs.asFtpUser(user, () -> { FtpRunAs.asFtpUser(user, () -> {
final byte[] bytes = readTempFileToBytes(tempFile); final byte[] bytes = readTempFileToBytes(tempFile);
try { try {
repositoryService.addFile(parent, filename, null, bytes, null); LOG.info("Synchronizing file={} with repository path={} originalFilename={}", tempFile.getAbsolutePath(), parent, filename);
return repositoryService.addFile(parent, filename, null, bytes, null);
} catch (InvalidRepositoryPathException | InvalidRepositoryFileDataException e) { } catch (InvalidRepositoryPathException | InvalidRepositoryFileDataException e) {
LOG.warn("Error synchronizing new file parent={} filename={} with repository: {}", parent, filename, e.getMessage()); LOG.warn("Error synchronizing new file parent={} filename={} with repository: {}", parent, filename, e.getMessage());
throw new IOException(e); throw new IOException(e);
} }
LOG.info("Synchronized file={} with repository path={} originalFilename={}", tempFile.getAbsolutePath(), parent, filename);
return null;
}); });
} }
}; };
......
...@@ -387,7 +387,8 @@ public class FtpServerTest { ...@@ -387,7 +387,8 @@ public class FtpServerTest {
try (InputStream local = new ByteArrayInputStream(TEST_FILE_CONTENTS.getBytes())) { try (InputStream local = new ByteArrayInputStream(TEST_FILE_CONTENTS.getBytes())) {
assertThat(ftp.storeFile("/a/b/c/d/e/f/g", local), is(true)); assertThat(ftp.storeFile("/a/b/c/d/e/f/g", local), is(true));
} }
debugListFiles(ftp);
for (final String dir : new String[] { "/", "/a", "/a/b", "/a/b/c", "/a/b/c/d", "/a/b/c/d/e", "/a/b/c/d/e", "/a/b/c/d/e/f" }) { for (final String dir : new String[] { "/", "/a", "/a/b", "/a/b/c", "/a/b/c/d", "/a/b/c/d/e", "/a/b/c/d/e", "/a/b/c/d/e/f" }) {
assertThat(ftp.changeWorkingDirectory(dir), is(true)); assertThat(ftp.changeWorkingDirectory(dir), is(true));
debugListFiles(ftp); debugListFiles(ftp);
...@@ -484,6 +485,10 @@ public class FtpServerTest { ...@@ -484,6 +485,10 @@ public class FtpServerTest {
ftp.setFileTransferMode(FTP.ASCII_FILE_TYPE); ftp.setFileTransferMode(FTP.ASCII_FILE_TYPE);
ftp.cwd("folder"); ftp.cwd("folder");
assertThat(ftp.printWorkingDirectory(), is("/"));
ftp.makeDirectory("folder");
ftp.cwd("folder");
assertThat(ftp.printWorkingDirectory(), is("/folder")); assertThat(ftp.printWorkingDirectory(), is("/folder"));
try (InputStream local = new ByteArrayInputStream(TEST_FILE_CONTENTS.getBytes())) { try (InputStream local = new ByteArrayInputStream(TEST_FILE_CONTENTS.getBytes())) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment