Git Product home page Git Product logo

dlsftpclient's Introduction

DLSFTPClient

SFTP Client library and sample app for iOS, using libssh2

Overview

DLSFTPClient is a an Objective-C wrapper around libssh2, providing simple access to upload and download files, as well as perform directory operations. It requires iOS 5.1.

Status

As of 2013-06-01, DLSFTPClient is considered stable. There are no plans to further change the API, though there may be continued efforts to further leverage Grand Central Dispatch.

Sample Usage

You'll need to initialize a DLSFTPConnection object with the host and credentials. Simple username/password as well as private key authentication are supported.

DLSFTPConnection *connection = [[DLSFTPConnection alloc] initWithHostname:@"foo.bar.com"
                                                                     port:22
                                                                 username:@"username"
                                                                 password:@"password"];

To establish a connection, use connectWithSuccessBlock:failureBlock:

DLSFTPClientSuccessBlock successBlock = ^{ ... };
DLSFTPClientFailureBlock failureBlock = ^(NSError *error) { ... };
[connection connectWithSuccessBlock:successBlock
                       failureBlock:failureBlock];

To disconnect, use disconnect

[connection disconnect];

Your success and failure blocks are dispatched to the global concurrent queue. Be sure to dispatch back to the main queue if you need to drive UI updates.

Operations such as listing directory contents, downloading files, and moving/renaming are carried out through subclasses of DLSFTPRequest:

DLSFTPClientArraySuccessBlock successBlock = ^(NSArray *files) {
    for (DLSFTPFile *sftpFile in files) {
        NSLog(@"File: %@", sftpFile.filename);
    }
};

DLSFTPClientFailureBlock failureBlock = ^(NSError *error) {
    NSLog(@"Error listing files: %@", error);
};

DLSFTPRequest *request = [[DLSFTPListFilesRequest alloc] initWithDirectoryPath:@"/Users/dan/"
                                                                  successBlock:successBlock
                                                                  failureBlock:failureBlock];
[connection submitRequest:request];

The DLSFTPFile class is used to encapsulate file paths and metadata.

When uploading and downloading files, a progress block may be provided. The progress block will be dispatched by the connection as it is transferring the file, and can be used to monitor progress.

Features

  1. Upload and download files via SFTP
  2. List files
  3. Create directories
  4. Rename/Move files/directories
  5. Remove files/directories
  6. Operations can be cancelled

Testing

DLSFTPClient includes two test case classes. To use them, you'll need to copy ConnectionInfo-template.plist to ConnectionInfo.plist and update the values with credentials and file paths for your own server. To test private key authentication, you'll need to add a privatekey.pem file as well.

Remaining issues

The Demo App is incomplete and needs some polish.

Project Dependencies

  1. libssh2 - Provides a shell script to build libssh2.a Static Library and headers

License

DLSFTPClient is open-source under the BSD license. See the LICENSE file for more info.

dlsftpclient's People

Contributors

dleehr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

dlsftpclient's Issues

Fat binary

I'm trying to build a fat binary (i386, armv7 and arm64) to accommodate Apple's requirement that all existing apps be 64-bit before July. I have built openssl with all five architectures, but I can't seem to get anything other than armv7 and arm64 into the DLSFTPClient build. Any thoughts would be much appreciated.

Is This Library multithreaded

Hi Dan,

Can we upload multiple files at at time? Also can multiple simultaneous requests possible - like upload with delete, rename, get directory etc

Thanks,
-Mohammad Sadiq

DLSFTPListFilesRequest buffer is not zeroed

When listing directories, the buffer[cBufferSize] is used and reused without zeroing out. There seem to be some cases where the null terminator isn't caught when converting to NSString. Best to zero out the buffer on each loop or allocate the NSString with a fixed length (from result)

Add cleanup handler to dispatch sources

upload and download use dispatch sources to relay transfer progress. When deploying to iOS 5, these sources are released in the cleanup handler. They should be released in a cancel handler instead.

This causes tests to fail if deployment target is 5.1

Swift implementation

Hi There,

How can we implement below functions in swift ? 

DLSFTPClientArraySuccessBlock successBlock = ^(NSArray *files) {
for (DLSFTPFile *sftpFile in files) {
NSLog(@"File: %@", sftpFile.filename);
}
};

DLSFTPClientFailureBlock failureBlock = ^(NSError *error) {
NSLog(@"Error listing files: %@", error);
};

Thanks,
Chinna

Application crashes while setting deployment target to iOS 6

First of all thanks for putting your effort to make this library. I have tried your sample code in my XCode 5 setting deployment target to iOS 5.1 and application runs just fine.

But when i tried setting deployment target to iOS 6 application generates warnings regarding "dispatch_get_current_queue deprecated" in iOS 6.0 and above.

And after that application started crashing on login, download and upload. I have changed the property of @Property (nonatomic, strong) dispatch_source_t timeoutTimer; in "DLSFTPConnection.m" and login works now.

But as i am not familiar with dispatch_queue and blocks need your help regarding that.

Thanks in advance.

Fix warnings about implicit conversion loses integer precision

$ pod spec lint

 -> DLSFTPClient (1.0.0)
    - NOTE  | [iOS] [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPConnection.m:356:93: warning: implicit conversion loses integer precision: 'unsigned long' to 'unsigned int' [-Wshorten-64-to-32]
    - NOTE  | [iOS] [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPConnection.m:363:33: warning: implicit conversion loses integer precision: 'unsigned long' to 'unsigned int' [-Wshorten-64-to-32]
    - NOTE  | [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPConnection.m:368:33: warning: implicit conversion loses integer precision: 'unsigned long' to 'unsigned int' [-Wshorten-64-to-32]
    - NOTE  | [iOS] [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPConnection.m:373:33: warning: implicit conversion loses integer precision: 'unsigned long' to 'unsigned int' [-Wshorten-64-to-32]
    - NOTE  | [iOS] [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPConnection.m:849:31: warning: implicit conversion loses integer precision: 'unsigned long' to 'unsigned int' [-Wshorten-64-to-32]
    - NOTE  | [iOS] [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPDownloadRequest.m:106:25: warning: implicit conversion loses integer precision: 'unsigned long' to 'unsigned int' [-Wshorten-64-to-32]
    - NOTE  | [iOS] [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPMakeDirectoryRequest.m:76:25: warning: implicit conversion loses integer precision: 'unsigned long' to 'unsigned int' [-Wshorten-64-to-32]
    - NOTE  | [iOS] [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPMakeDirectoryRequest.m:99:25: warning: implicit conversion loses integer precision: 'unsigned long' to 'unsigned int' [-Wshorten-64-to-32]
    - NOTE  | [iOS] [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPListFilesRequest.m:77:25: warning: implicit conversion loses integer precision: 'unsigned long' to 'unsigned int' [-Wshorten-64-to-32]
    - NOTE  | [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPMoveRenameRequest.m:75:25: warning: implicit conversion loses integer precision: 'unsigned long' to 'unsigned int' [-Wshorten-64-to-32]
    - NOTE  | [iOS] [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPMoveRenameRequest.m:98:25: warning: implicit conversion loses integer precision: 'unsigned long' to 'unsigned int' [-Wshorten-64-to-32]
    - NOTE  | [iOS] [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPRemoveDirectoryRequest.m:68:25: warning: implicit conversion loses integer precision: 'unsigned long' to 'unsigned int' [-Wshorten-64-to-32]
    - NOTE  | [iOS] [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPRemoveFileRequest.m:68:25: warning: implicit conversion loses integer precision: 'unsigned long' to 'unsigned int' [-Wshorten-64-to-32]
    - NOTE  | [iOS] [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPUploadRequest.m:79:25: warning: implicit conversion loses integer precision: 'unsigned long' to 'unsigned int' [-Wshorten-64-to-32]
    - NOTE  | [iOS] [xcodebuild]  DLSFTPClient/DLSFTPClient/Classes/DLSFTPUploadRequest.m:199:57: warning: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' [-Wshorten-64-to-32]

Improve request queuing

Requesting a download (or upload) will fail if the connection is not connected. Instead, the request should be queued and wait for the socket/session/sftp initialization to complete.

After upgrading to IOS15 Application crashes at the time of File uploading

Issue 1:

*** Incorrect guard value: 0
malloc: *** set a breakpoint in malloc_error_break to debug
malloc: Heap corruption detected, free list is damaged at 0x600003f44b60
*** Incorrect guard value: 0
CoreSimulator 776.3 - Device: iPod touch (7th generation) (06C16--4D-AA-5FC********) - Runtime: iOS 15.0 (19A***) - DeviceType: iPod touch (7th generation)

malloc: Heap corruption detected, free list is damaged at 0x60000059bf70

Issue 2:

Date: 04-10-2021 -------- FileName: Filename_CombinedZip.zip -------- Status: Failed -------- errorMessage: Error Domain=SFTPClientErrorDomain Code=21 "Unable to open file for writing: SFTP Status Code 0 /JP_IN/Filename_CombinedZip.zip" UserInfo={NSLocalizedDescription=Unable to open file for writing: SFTP Status Code 0 /JP_IN/Filename_CombinedZip.zip, [SFTPClientUnderlyingError=0}](url
Screenshot 2021-10-07 at 7 22 05 PM
)

Crash on download cancelation

Crash in DLSFTPDownloadRequest.m on the line 301:
dispatch_async(self.connection.socketQueue, ^{ [weakSelf downloadChunk]; });

after I cancel current download request

How to use this library for mac app. Using swift

Hi Dan,

I want to use one of your library in my mac project. DLSFTPClient to connect to sftp server & file transfer. it works in your iOS demo but not working for the mac demo i prepared. It sometimes connects & sometimes crashes giving following error

[__NSArrayM count]: message sent to deallocated instance 0x11824dfd0
(lldb)

at first line if (index >= [addresses count])` in following code in DLSFTPConnection.m

  • (void)connectToAddressAtIndex:(NSUInteger)index inArray:(NSArray *)addresses {
    if (index >= [addresses count]) {
    [self _disconnect];
    [self failConnectionWithErrorCode:eSFTPClientErrorUnableToConnect
    errorDescription:@"Unable to connect"];
    self.connectionSuccessBlock = nil;
    return;
    }

Can you please suggest some way to find solution to this problem?

Thanks,

Sadiq

Unable to stat file when uploading

When you initiate an upload, the library will return an error about being unable to stat the file even though the file was uploaded successfully.

The part where this happens is in the file DLSFTPUploadRequest.m around line 275

I've tried fixing it but without any luck.

Here's the code I've been playing with:

// stat the remote file after uploading
LIBSSH2_SFTP_ATTRIBUTES attributes;
while ( ((result = libssh2_sftp_fstat(self.handle, &attributes)) == LIBSSH2SFTP_EAGAIN)
&& self.isCancelled == NO){
waitsocket(socketFD, session);
}
if ([self ready] == NO) {
[self.connection requestDidFail:self withError:self.error];
return; }

// result is always LIBSSH2_ERROR_SFTP_PROTOCOL
if (result == LIBSSH2_ERROR_SFTP_PROTOCOL) {
result = libssh2_sftp_last_error([self.connection sftp]);

// usually result at this point is LIBSSH2_FX_FAILURE
if (result != LIBSSH2_FX_OK) {
result = libssh2_sftp_stat([self.connection sftp], [self.remotePath UTF8String], NULL);
}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.