Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SFTP connection remaining problem #27

Merged
merged 2 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 36 additions & 29 deletions src/main/java/org/embulk/input/sftp/SftpFileInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public TaskReport commit()
@Override
public void close()
{
super.close();
}

private static StandardFileSystemManager initializeStandardFileSystemManager()
Expand Down Expand Up @@ -189,43 +190,49 @@ public FileList call() throws IOException
{
String lastKey = null;
log.info("Getting to download file list");
StandardFileSystemManager manager = initializeStandardFileSystemManager();
FileSystemOptions fsOptions = initializeFsOptions(task);
StandardFileSystemManager manager = null;
try {
manager = initializeStandardFileSystemManager();
FileSystemOptions fsOptions = initializeFsOptions(task);

if (task.getLastPath().isPresent() && !task.getLastPath().get().isEmpty()) {
lastKey = manager.resolveFile(getSftpFileUri(task, task.getLastPath().get()), fsOptions).toString();
}
if (task.getLastPath().isPresent() && !task.getLastPath().get().isEmpty()) {
lastKey = manager.resolveFile(getSftpFileUri(task, task.getLastPath().get()), fsOptions).toString();
}

FileObject files = manager.resolveFile(getSftpFileUri(task, task.getPathPrefix()), fsOptions);
FileObject files = manager.resolveFile(getSftpFileUri(task, task.getPathPrefix()), fsOptions);

if (files.isFolder()) {
//path_prefix is a folder, we add everything in that folder
FileObject[] children = files.getChildren();
Arrays.sort(children);
for (FileObject f : children) {
if (f.isFile()) {
addFileToList(builder, f.toString(), f.getContent().getSize(), "", lastKey);
if (files.isFolder()) {
//path_prefix is a folder, we add everything in that folder
FileObject[] children = files.getChildren();
Arrays.sort(children);
for (FileObject f : children) {
if (f.isFile()) {
addFileToList(builder, f.toString(), f.getContent().getSize(), "", lastKey);
}
}
} else if (files.isFile()) {
//path_prefix is a file then we just need to add that file
addFileToList(builder, files.toString(), files.getContent().getSize(), "", lastKey);
} else {
// path_prefix is neither file or folder, then we scan the parent folder to file path
// that match the path_prefix basename
FileObject parent = files.getParent();
FileObject[] children = parent.getChildren();
Arrays.sort(children);
String fileName = FilenameUtils.getName(task.getPathPrefix());
for (FileObject f : children) {
if (f.isFile()) {
addFileToList(builder, f.toString(), f.getContent().getSize(), fileName, lastKey);
}
}
}
return builder.build();
}
else if (files.isFile()) {
//path_prefix is a file then we just need to add that file
addFileToList(builder, files.toString(), files.getContent().getSize(), "", lastKey);
}
else {
// path_prefix is neither file or folder, then we scan the parent folder to file path
// that match the path_prefix basename
FileObject parent = files.getParent();
FileObject[] children = parent.getChildren();
Arrays.sort(children);
String fileName = FilenameUtils.getName(task.getPathPrefix());
for (FileObject f : children) {
if (f.isFile()) {
addFileToList(builder, f.toString(), f.getContent().getSize(), fileName, lastKey);
}
finally {
if (manager != null) {
manager.close();
}
}
return builder.build();
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/embulk/input/sftp/SingleFileProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,8 @@ public void onGiveup(Exception firstException, Exception lastException)
@Override
public void close()
{
if (manager != null) {
manager.close();
}
}
}