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 {
try {
return runnable.run();
} catch (Throwable e) {
throw e;
} finally {
if (ftpUser != null && ftpUser.user != null) {
LOG.trace("Switching back from {}", ftpUser.user.getUsername());
......
......@@ -42,7 +42,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;
......@@ -241,9 +240,25 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ
public boolean changeWorkingDirectory(final String dir) {
final Path normalized = Paths.get(getAbsolutePath()).resolve(dir).normalize().toAbsolutePath();
LOG.info("CWD this={} dir={} normalized={}", getAbsolutePath(), dir, normalized);
this.cwd(normalized);
// TODO Check if such path exists?
return true;
if ("/".equals(normalized.toString())) {
this.cwd(normalized);
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
try {
return isDirectory(path) ?
// directory
directory(path, this)
// or file
: file(FtpRunAs.asFtpUser(user, () -> repositoryService.getFile(path.getParent(), path.getFileName().toString())), this);
// directory
directory("/".equals(path.toString()) ? path : FtpRunAs.asFtpUser(user, () -> repositoryService.getFolder(path).getFolderPath()), this)
// or file
: file(FtpRunAs.asFtpUser(user, () -> repositoryService.getFile(path.getParent(), path.getFileName().toString())), this);
} catch (final AuthenticationException e) {
LOG.warn("Authentication problem {}", e.getMessage(), e);
......@@ -282,7 +297,7 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ
} catch (InvalidRepositoryPathException e) {
throw new FtpException(e.getMessage());
} 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()) {
......@@ -290,8 +305,8 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ
public boolean mkdir() {
LOG.debug("MKDIR {}", path);
try {
FtpRunAs.asFtpUser(user, () -> repositoryService.ensureFolder(path));
return true;
RepositoryFolder repositoryFolder = FtpRunAs.asFtpUser(user, () -> repositoryService.ensureFolder(path));
return repositoryFolder != null;
} catch (InvalidRepositoryPathException e) {
LOG.warn("{}", e.getMessage(), e);
return false;
......@@ -300,7 +315,7 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ
@Override
public boolean delete() {
return false;
return true;
}
@Override
......@@ -309,7 +324,10 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ
if (path.getParent() != null) {
LOG.debug("MKDIR {}", path.getParent());
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);
} catch (InvalidRepositoryPathException e) {
LOG.warn("{}", e.getMessage(), e);
......
......@@ -89,13 +89,12 @@ public class TemporaryBytesManager {
FtpRunAs.asFtpUser(user, () -> {
final byte[] bytes = readTempFileToBytes(tempFile);
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) {
LOG.warn("Error synchronizing new file parent={} filename={} with repository: {}", parent, filename, e.getMessage());
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 {
try (InputStream local = new ByteArrayInputStream(TEST_FILE_CONTENTS.getBytes())) {
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" }) {
assertThat(ftp.changeWorkingDirectory(dir), is(true));
debugListFiles(ftp);
......@@ -484,6 +485,10 @@ public class FtpServerTest {
ftp.setFileTransferMode(FTP.ASCII_FILE_TYPE);
ftp.cwd("folder");
assertThat(ftp.printWorkingDirectory(), is("/"));
ftp.makeDirectory("folder");
ftp.cwd("folder");
assertThat(ftp.printWorkingDirectory(), is("/folder"));
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