Reputation: 12444
I am trying to use this Github library for iOS: https://github.com/PeqNP/FTPKit
The problem is, there is no cancel function for a request which is what I need. I am trying to implement my own cancel function however it doesn't work as it is crashing with a: "write: Bad file descriptor" EXC_BAD_ACCESS
In this file specifically: https://github.com/PeqNP/FTPKit/blob/master/Libraries/include/ftplib/src/ftplib.c
I added this method all the way at the bottom, to use the "ABOR" command that FTP servers can use. However I am not sure if this was the right way of implementing it.
GLOBALDEF void FtpAbort(netbuf *nControl)
{
if (nControl->dir != FTPLIB_CONTROL)
return;
FtpSendCmd("ABOR",'2', nControl);
net_close(nControl->handle);
free(nControl->buf);
free(nControl);
}
Then in the FTPClient.m I just made a method which calls this FtpAbort method on a netbuf * object (which can be a list, download, upload connection, etc..)
Does anyone see what I am doing wrong here?
Upvotes: 0
Views: 212
Reputation: 109
I'm also using FTPKit and also have a need for a cancel function. I think the main problem with your approach is that your are closing and deallocating the control buffers in your FtpAbort() function. If you look at the FtpXfer() function in libFtp.c, it will try to access the same buffers that you have freed.
My approach was to simply add a method to FTPClient.m as follows:
- (BOOL)cancelOperation {
if (self.conn == NULL)
return NO;
NSString *command = [NSString stringWithFormat:@"ABOR"];
BOOL success = [self sendCommand:command conn:self.conn];
NSString *response = [NSString stringWithCString:FtpLastResponse(self.conn) encoding:NSUTF8StringEncoding];
if (!success) {
self.lastError = [NSError FTPKitErrorWithResponse:response];
return NO;
}
return YES;
}
The goal here is to tell the FTP server to close the data connection which should be detected by ftplib - which will, in theory, exit properly. This will allow the downloadHandle() method in FTPClient to finish up by sending the QUIT command which should avoid crashing.
I say "in theory" because in practice it appears that the FtpXfer() doesn't spot this until my FTP server times out and closes the entire connection. My server has a very quick timeout (5 seconds) so this is "good enough". Although granted, it would be nice to have a better solution.
I took your suggestion from the comment you made on FTPKit's GitHub about making netfbuf *conn a property of the FTPClient class.
Upvotes: 1