Skip to content

Add copy implementation from SSH File Transfer Protocol extensions. #14387

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
52 changes: 52 additions & 0 deletions ssh/src/main/java/ch/cyberduck/core/sftp/SFTPCopyFeature.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ch.cyberduck.core.sftp;

/*
* Copyright (c) 2002-2023 iterate GmbH. All rights reserved.
* https://cyberduck.io/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

import ch.cyberduck.core.ConnectionCallback;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.features.Copy;
import ch.cyberduck.core.io.StreamListener;
import ch.cyberduck.core.transfer.TransferStatus;

import java.io.IOException;

import net.schmizz.sshj.sftp.PacketType;
import net.schmizz.sshj.sftp.Request;

public class SFTPCopyFeature implements Copy {

private final SFTPSession session;

public SFTPCopyFeature(final SFTPSession session) {
this.session = session;
}

@Override
public Path copy(final Path source, final Path target, final TransferStatus status, final ConnectionCallback prompt, final StreamListener listener) throws BackgroundException {
try {
final Request request = session.sftp().newExtendedRequest("copy-file")
.putString(source.getAbsolute())
.putString(target.getAbsolute())
.putBoolean(status.isExists());
session.sftp().request(request).retrieve().ensurePacketTypeIs(PacketType.STATUS);
}
catch(IOException e) {
throw new SFTPExceptionMappingService().map("Cannot copy {0}", e, source);
}
return null;
}
}
3 changes: 3 additions & 0 deletions ssh/src/main/java/ch/cyberduck/core/sftp/SFTPSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,9 @@ public <T> T _getFeature(final Class<T> type) {
if(type == Move.class) {
return (T) new SFTPMoveFeature(this);
}
if(type == Copy.class) {
return (T) new SFTPCopyFeature(this);
}
if(type == UnixPermission.class) {
return (T) new SFTPUnixPermissionFeature(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ch.cyberduck.core.sftp;

/*
* Copyright (c) 2002-2023 iterate GmbH. All rights reserved.
* https://cyberduck.io/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

import ch.cyberduck.core.AlphanumericRandomStringService;
import ch.cyberduck.core.DisabledConnectionCallback;
import ch.cyberduck.core.DisabledLoginCallback;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.features.Delete;
import ch.cyberduck.core.io.DisabledStreamListener;
import ch.cyberduck.core.transfer.TransferStatus;

import org.junit.Test;

import java.util.Collections;
import java.util.EnumSet;

import static org.junit.Assert.assertTrue;

public class SFTPCopyFeatureTest extends AbstractSFTPTest {

@Test
public void testCopy() throws Exception {
final Path test = new Path(new SFTPHomeDirectoryService(session).find(), new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
new SFTPTouchFeature(session).touch(test, new TransferStatus());
final Path copy = new Path(new SFTPHomeDirectoryService(session).find(), new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
new SFTPCopyFeature(session).copy(test, copy, new TransferStatus(), new DisabledConnectionCallback(), new DisabledStreamListener());
assertTrue(new SFTPFindFeature(session).find(test));
assertTrue(new SFTPFindFeature(session).find(copy));
new SFTPDeleteFeature(session).delete(Collections.<Path>singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
new SFTPDeleteFeature(session).delete(Collections.<Path>singletonList(copy), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
}