/
FTPHelper.m
745 lines (625 loc) · 30.1 KB
/
FTPHelper.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*
Erica Sadun, http://ericasadun.com
iPhone Developer's Cookbook, 3.0 Edition
BSD License, Use at your own risk
*/
#import "FTPHelper.h"
#define DELEGATE_CALLBACK(X, Y) if (sharedInstance.delegate && [sharedInstance.delegate respondsToSelector:@selector(X)]) [sharedInstance.delegate performSelector:@selector(X) withObject:Y];
#define COMPLAIN_AND_BAIL(X) {NSLog(X); return;}
#define NUMBER(X) [NSNumber numberWithFloat:X]
#define kMyBufferSize 32768
typedef struct MyStreamInfo {
CFWriteStreamRef writeStream;
CFReadStreamRef readStream;
CFDictionaryRef proxyDict;
SInt64 fileSize;
UInt32 totalBytesWritten;
UInt32 leftOverByteCount;
UInt8 buffer[kMyBufferSize];
} MyStreamInfo;
static const CFOptionFlags kNetworkEvents =
kCFStreamEventOpenCompleted
| kCFStreamEventHasBytesAvailable
| kCFStreamEventEndEncountered
| kCFStreamEventCanAcceptBytes
| kCFStreamEventErrorOccurred;
static FTPHelper *sharedInstance = nil;
@implementation FTPHelper
@synthesize delegate;
@synthesize urlString;
@synthesize isBusy;
@synthesize uname;
@synthesize pword;
@synthesize fileListings;
@synthesize filePath;
#pragma mark ***** Happy Happy Apple Stuff
// MyStreamInfoCreate creates a MyStreamInfo 'object' with the specified read and write stream.
static void MyStreamInfoCreate(MyStreamInfo **info, CFReadStreamRef readStream, CFWriteStreamRef writeStream)
{
MyStreamInfo * streamInfo;
assert(info != NULL);
assert(readStream != NULL);
// writeStream may be NULL (this is the case for the directory list operation)
streamInfo = malloc(sizeof(MyStreamInfo));
assert(streamInfo != NULL);
streamInfo->readStream = readStream;
streamInfo->writeStream = writeStream;
streamInfo->proxyDict = NULL; // see discussion of <rdar://problem/3745574> below
streamInfo->fileSize = 0;
streamInfo->totalBytesWritten = 0;
streamInfo->leftOverByteCount = 0;
*info = streamInfo;
}
/* MyStreamInfoDestroy destroys a MyStreamInfo 'object', cleaning up any resources that it owns. */
static void MyStreamInfoDestroy(MyStreamInfo * info)
{
assert(info != NULL);
if (info->readStream) {
CFReadStreamUnscheduleFromRunLoop(info->readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
(void) CFReadStreamSetClient(info->readStream, kCFStreamEventNone, NULL, NULL);
/* CFReadStreamClose terminates the stream. */
CFReadStreamClose(info->readStream);
CFRelease(info->readStream);
}
if (info->writeStream) {
CFWriteStreamUnscheduleFromRunLoop(info->writeStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
(void) CFWriteStreamSetClient(info->writeStream, kCFStreamEventNone, NULL, NULL);
/* CFWriteStreamClose terminates the stream. */
CFWriteStreamClose(info->writeStream);
CFRelease(info->writeStream);
}
if (info->proxyDict) {
CFRelease(info->proxyDict); // see discussion of <rdar://problem/3745574> below
}
free(info);
}
static void MyCFStreamSetUsernamePassword(CFTypeRef stream, CFStringRef username, CFStringRef password)
{
Boolean success;
assert(stream != NULL);
assert( (username != NULL) || (password == NULL) );
if (username && CFStringGetLength(username) > 0) {
if (CFGetTypeID(stream) == CFReadStreamGetTypeID()) {
success = CFReadStreamSetProperty((CFReadStreamRef)stream, kCFStreamPropertyFTPUserName, username);
assert(success);
if (password) {
success = CFReadStreamSetProperty((CFReadStreamRef)stream, kCFStreamPropertyFTPPassword, password);
assert(success);
}
} else if (CFGetTypeID(stream) == CFWriteStreamGetTypeID()) {
success = CFWriteStreamSetProperty((CFWriteStreamRef)stream, kCFStreamPropertyFTPUserName, username);
assert(success);
if (password) {
success = CFWriteStreamSetProperty((CFWriteStreamRef)stream, kCFStreamPropertyFTPPassword, password);
assert(success);
}
} else {
assert(false);
}
}
}
#pragma mark ***** List Command
static void MyDirectoryListingCallBack(CFReadStreamRef readStream, CFStreamEventType type, void * clientCallBackInfo)
{
MyStreamInfo *info = (MyStreamInfo *)clientCallBackInfo;
CFIndex bytesRead;
CFStreamError error;
CFDictionaryRef parsedDict;
assert(readStream != NULL);
assert(info != NULL);
assert(info->readStream == readStream);
switch (type) {
case kCFStreamEventOpenCompleted:
break;
case kCFStreamEventHasBytesAvailable:
bytesRead = CFReadStreamRead(info->readStream, info->buffer + info->leftOverByteCount, kMyBufferSize - info->leftOverByteCount);
if (bytesRead > 0) {
const UInt8 * nextByte;
CFIndex bytesRemaining;
CFIndex bytesConsumedThisTime;
nextByte = info->buffer;
bytesRemaining = bytesRead + info->leftOverByteCount;
do
{
bytesConsumedThisTime = CFFTPCreateParsedResourceListing(NULL, nextByte, bytesRemaining, &parsedDict);
if (bytesConsumedThisTime > 0) {
if (parsedDict != NULL) {
[[FTPHelper sharedInstance].fileListings addObject:(NSDictionary *)parsedDict];
CFRelease(parsedDict);
}
nextByte += bytesConsumedThisTime;
bytesRemaining -= bytesConsumedThisTime;
} else if (bytesConsumedThisTime == 0) {
} else if (bytesConsumedThisTime == -1) {
fprintf(stderr, "CFFTPCreateParsedResourceListing parse failure\n");
goto exit;
}
} while ( (bytesRemaining > 0) && (bytesConsumedThisTime > 0) );
if (bytesRemaining > 0) {
memmove(info->buffer, nextByte, bytesRemaining);
}
info->leftOverByteCount = bytesRemaining;
} else {
}
break;
case kCFStreamEventErrorOccurred:
error = CFReadStreamGetError(info->readStream);
fprintf(stderr, "CFReadStreamGetError returned (%d, %ld)\n", error.domain, error.error);
DELEGATE_CALLBACK(listingFailed, nil);
goto exit;
case kCFStreamEventEndEncountered:
DELEGATE_CALLBACK(receivedListing:, sharedInstance.fileListings);
goto exit;
default:
fprintf(stderr, "Received unexpected CFStream event (%d)", type);
break;
}
return;
exit:
MyStreamInfoDestroy(info);
CFRunLoopStop(CFRunLoopGetCurrent());
return;
}
static Boolean MySimpleDirectoryListing(CFStringRef urlString, CFStringRef username, CFStringRef password)
{
CFReadStreamRef readStream;
CFStreamClientContext context = { 0, NULL, NULL, NULL, NULL };
CFURLRef downloadURL;
Boolean success = true;
MyStreamInfo *streamInfo;
assert(urlString != NULL);
downloadURL = CFURLCreateWithString(kCFAllocatorDefault, urlString, NULL);
assert(downloadURL != NULL);
/* Create an FTP read stream for downloading operation from an FTP URL. */
for (int i = 0; i < 10; i++)
{
readStream = CFReadStreamCreateWithFTPURL(kCFAllocatorDefault, downloadURL);
if (readStream != NULL) continue;
}
assert(readStream != NULL);
CFRelease(downloadURL);
/* Initialize our MyStreamInfo structure, which we use to store some information about the stream. */
MyStreamInfoCreate(&streamInfo, readStream, NULL);
context.info = (void *)streamInfo;
/* CFReadStreamSetClient registers a callback to hear about interesting events that occur on a stream. */
success = CFReadStreamSetClient(readStream, kNetworkEvents, MyDirectoryListingCallBack, &context);
if (success) {
/* Schedule a run loop on which the client can be notified about stream events. The client
callback will be triggered via the run loop. It's the caller's responsibility to ensure that
the run loop is running. */
CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
MyCFStreamSetUsernamePassword(readStream, username, password);
// MyCFStreamSetFTPProxy(readStream, &streamInfo->proxyDict); no proxies
/* CFReadStreamOpen will return success/failure. Opening a stream causes it to reserve all the
system resources it requires. If the stream can open non-blocking, this will always return TRUE;
listen to the run loop source to find out when the open completes and whether it was successful. */
success = CFReadStreamOpen(readStream);
if (success == false) {
fprintf(stderr, "CFReadStreamOpen failed\n");
MyStreamInfoDestroy(streamInfo);
}
} else {
fprintf(stderr, "CFReadStreamSetClient failed\n");
MyStreamInfoDestroy(streamInfo);
}
return success;
}
#define DTTOIF(dirtype) ((dirtype) << 12)
+ (NSString *) textForDirectoryListing: (CFDictionaryRef) dictionary
{
CFDateRef cfModDate;
CFNumberRef cfType, cfMode, cfSize;
CFStringRef cfOwner, cfName, cfLink, cfGroup;
char owner[256], group[256], name[256];
char permString[12], link[1024];
SInt64 size;
SInt32 mode, type;
NSMutableString *outstring = [NSMutableString string];
assert(dictionary != NULL);
/* You should not assume that the directory entry dictionary will contain all the possible keys.
Most of the time it will, however, depending on the FTP server, some of the keys may be missing. */
cfType = CFDictionaryGetValue(dictionary, kCFFTPResourceType);
if (cfType) {
assert(CFGetTypeID(cfType) == CFNumberGetTypeID());
CFNumberGetValue(cfType, kCFNumberSInt32Type, &type);
cfMode = CFDictionaryGetValue(dictionary, kCFFTPResourceMode);
if (cfMode) {
assert(CFGetTypeID(cfMode) == CFNumberGetTypeID());
CFNumberGetValue(cfMode, kCFNumberSInt32Type, &mode);
/* Converts inode status information into a symbolic string */
strmode(mode + DTTOIF(type), permString);
[outstring appendFormat:@"%s ", permString];
}
}
cfOwner = CFDictionaryGetValue(dictionary, kCFFTPResourceOwner);
if (cfOwner) {
assert(CFGetTypeID(cfOwner) == CFStringGetTypeID());
CFStringGetCString(cfOwner, owner, sizeof(owner), kCFStringEncodingASCII);
[outstring appendFormat:@"%9s", owner];
}
cfGroup = CFDictionaryGetValue(dictionary, kCFFTPResourceGroup);
if (cfGroup) {
assert(CFGetTypeID(cfGroup) == CFStringGetTypeID());
CFStringGetCString(cfGroup, group, sizeof(group), kCFStringEncodingASCII);
[outstring appendFormat:@"%9s", group];
}
cfSize = CFDictionaryGetValue(dictionary, kCFFTPResourceSize);
if (cfSize) {
assert(CFGetTypeID(cfSize) == CFNumberGetTypeID());
CFNumberGetValue(cfSize, kCFNumberSInt64Type, &size);
[outstring appendFormat:@"%9lld", size];
}
cfModDate = CFDictionaryGetValue(dictionary, kCFFTPResourceModDate);
if (cfModDate) {
CFLocaleRef locale;
CFDateFormatterRef formatDate;
CFDateFormatterRef formatTime;
CFStringRef cfDate;
CFStringRef cfTime;
char date[256];
char time[256];
assert(CFGetTypeID(cfModDate) == CFDateGetTypeID());
locale = CFLocaleCopyCurrent();
assert(locale != NULL);
formatDate = CFDateFormatterCreate(kCFAllocatorDefault, locale, kCFDateFormatterShortStyle, kCFDateFormatterNoStyle );
assert(formatDate != NULL);
formatTime = CFDateFormatterCreate(kCFAllocatorDefault, locale, kCFDateFormatterNoStyle, kCFDateFormatterShortStyle);
assert(formatTime != NULL);
cfDate = CFDateFormatterCreateStringWithDate(kCFAllocatorDefault, formatDate, cfModDate);
assert(cfDate != NULL);
cfTime = CFDateFormatterCreateStringWithDate(kCFAllocatorDefault, formatTime, cfModDate);
assert(cfTime != NULL);
CFStringGetCString(cfDate, date, sizeof(date), kCFStringEncodingUTF8);
CFStringGetCString(cfTime, time, sizeof(time), kCFStringEncodingUTF8);
[outstring appendFormat:@"%10s %10s ", date, time];
CFRelease(cfTime);
CFRelease(cfDate);
CFRelease(formatTime);
CFRelease(formatDate);
CFRelease(locale);
}
/* Note that this sample assumes UTF-8 since that's what the Mac OS X
FTP server returns, however, some servers may use a different encoding. */
cfName = CFDictionaryGetValue(dictionary, kCFFTPResourceName);
if (cfName) {
assert(CFGetTypeID(cfName) == CFStringGetTypeID());
CFStringGetCString(cfName, name, sizeof(name), kCFStringEncodingUTF8);
[outstring appendFormat:@"%s", name];
cfLink = CFDictionaryGetValue(dictionary, kCFFTPResourceLink);
if (cfLink) {
assert(CFGetTypeID(cfLink) == CFStringGetTypeID());
CFStringGetCString(cfLink, link, sizeof(link), kCFStringEncodingUTF8);
if (strlen(link) > 0)
[outstring appendFormat:@" -> %s", link];
}
}
// CFShow(outstring);
return outstring;
}
- (void) list
{
if (!self.uname || !self.pword) COMPLAIN_AND_BAIL(@"Please set user name and password first");
if (!self.urlString) COMPLAIN_AND_BAIL(@"Please set URL string first");
self.fileListings = [NSMutableArray array];
MySimpleDirectoryListing((CFStringRef)self.urlString, (CFStringRef) uname, (CFStringRef) pword);
}
+ (void) list: (NSString *) aURLString
{
sharedInstance.urlString = aURLString;
[sharedInstance list];
}
#pragma mark ***** Uploads
static void MyUploadCallBack(CFWriteStreamRef writeStream, CFStreamEventType type, void * clientCallBackInfo)
{
MyStreamInfo *info = (MyStreamInfo *)clientCallBackInfo;
CFIndex bytesRead;
CFIndex bytesAvailable;
CFIndex bytesWritten;
CFStreamError error;
assert(writeStream != NULL);
assert(info != NULL);
assert(info->writeStream == writeStream);
switch (type) {
case kCFStreamEventOpenCompleted: // open complete
break;
case kCFStreamEventCanAcceptBytes:
if (info->leftOverByteCount > 0) {
bytesRead = 0;
bytesAvailable = info->leftOverByteCount;
} else {
bytesRead = CFReadStreamRead(info->readStream, info->buffer, kMyBufferSize);
if (bytesRead < 0) {
fprintf(stderr, "CFReadStreamRead returned %ld\n", bytesRead);
goto exit;
}
bytesAvailable = bytesRead;
}
bytesWritten = 0;
if (bytesAvailable == 0) {
DELEGATE_CALLBACK(dataUploadFinished:, NUMBER(info->totalBytesWritten));
goto exit;
} else {
bytesWritten = CFWriteStreamWrite(info->writeStream, info->buffer, bytesAvailable);
if (bytesWritten > 0) {
info->totalBytesWritten += bytesWritten;
if (bytesWritten < bytesAvailable) {
info->leftOverByteCount = bytesAvailable - bytesWritten;
memmove(info->buffer, info->buffer + bytesWritten, info->leftOverByteCount);
} else {
info->leftOverByteCount = 0;
}
} else if (bytesWritten < 0) {
fprintf(stderr, "CFWriteStreamWrite returned %ld\n", bytesWritten);
}
}
break;
case kCFStreamEventErrorOccurred:
error = CFWriteStreamGetError(info->writeStream);
fprintf(stderr, "CFReadStreamGetError returned (%d, %ld)\n", error.domain, error.error);
NSString *reason = [NSString stringWithFormat:@"CFReadStreamGetError returned (%d, %ld)\n", error.domain, error.error];
DELEGATE_CALLBACK(dataUploadFailed:, reason);
goto exit;
case kCFStreamEventEndEncountered:
fprintf(stderr, "\nUpload complete\n");
goto exit;
default:
fprintf(stderr, "Received unexpected CFStream event (%d)", type);
break;
}
return;
exit:
MyStreamInfoDestroy(info);
CFRunLoopStop(CFRunLoopGetCurrent());
return;
}
static Boolean MySimpleUpload(CFStringRef uploadDirectory, CFURLRef fileURL, CFStringRef username, CFStringRef password)
{
CFWriteStreamRef writeStream;
CFReadStreamRef readStream;
CFStreamClientContext context = { 0, NULL, NULL, NULL, NULL };
CFURLRef uploadURL, destinationURL;
CFStringRef fileName;
Boolean success = true;
MyStreamInfo *streamInfo;
assert(uploadDirectory != NULL);
assert(fileURL != NULL);
assert( (username != NULL) || (password == NULL) );
/* Create a CFURL from the upload directory string */
destinationURL = CFURLCreateWithString(kCFAllocatorDefault, uploadDirectory, NULL);
assert(destinationURL != NULL);
/* Copy the end of the file path and use it as the file name. */
fileName = CFURLCopyLastPathComponent(fileURL);
assert(fileName != NULL);
/* Create the destination URL by taking the upload directory and appending the file name. */
uploadURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault, destinationURL, fileName, false);
assert(uploadURL != NULL);
CFRelease(destinationURL);
CFRelease(fileName);
/* Create a CFReadStream from the local file being uploaded. */
readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault, fileURL);
assert(readStream != NULL);
/* Create an FTP write stream for uploading operation to a FTP URL. If the URL specifies a
directory, the open will be followed by a close event/state and the directory will have been
created. Intermediary directory structure is not created. */
writeStream = CFWriteStreamCreateWithFTPURL(kCFAllocatorDefault, uploadURL);
assert(writeStream != NULL);
CFRelease(uploadURL);
/* Initialize our MyStreamInfo structure, which we use to store some information about the stream. */
MyStreamInfoCreate(&streamInfo, readStream, writeStream);
context.info = (void *)streamInfo;
/* CFReadStreamOpen will return success/failure. Opening a stream causes it to reserve all the
system resources it requires. If the stream can open non-blocking, this will always return TRUE;
listen to the run loop source to find out when the open completes and whether it was successful. */
success = CFReadStreamOpen(readStream);
if (success) {
/* CFWriteStreamSetClient registers a callback to hear about interesting events that occur on a stream. */
success = CFWriteStreamSetClient(writeStream, kNetworkEvents, MyUploadCallBack, &context);
if (success) {
/* Schedule a run loop on which the client can be notified about stream events. The client
callback will be triggered via the run loop. It's the caller's responsibility to ensure that
the run loop is running. */
CFWriteStreamScheduleWithRunLoop(writeStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
MyCFStreamSetUsernamePassword(writeStream, username, password);
// MyCFStreamSetFTPProxy(writeStream, &streamInfo->proxyDict); // no proxies!
/* CFWriteStreamOpen will return success/failure. Opening a stream causes it to reserve all the
system resources it requires. If the stream can open non-blocking, this will always return TRUE;
listen to the run loop source to find out when the open completes and whether it was successful. */
success = CFWriteStreamOpen(writeStream);
if (success == false) {
fprintf(stderr, "CFWriteStreamOpen failed\n");
MyStreamInfoDestroy(streamInfo);
}
} else {
fprintf(stderr, "CFWriteStreamSetClient failed\n");
MyStreamInfoDestroy(streamInfo);
}
} else {
fprintf(stderr, "CFReadStreamOpen failed\n");
MyStreamInfoDestroy(streamInfo);
}
return success;
}
- (void) put: (NSString *) fileLocation
{
if (!self.uname || !self.pword) COMPLAIN_AND_BAIL(@"Please set user name and password first");
if (!self.urlString) COMPLAIN_AND_BAIL(@"Please set URL string first");
NSString *readPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:fileLocation];
CFURLRef readURL = CFURLCreateFromFileSystemRepresentation (NULL, (const UInt8 *) [readPath UTF8String], [readPath length], NO);
MySimpleUpload((CFStringRef)self.urlString, readURL, (CFStringRef)uname, (CFStringRef)pword);
CFRelease(readURL);
}
+ (void) upload: (NSString *) anItem
{
[sharedInstance put: anItem];
}
#pragma mark ***** Downloads
static void MyDownloadCallBack(CFReadStreamRef readStream, CFStreamEventType type, void * clientCallBackInfo)
{
MyStreamInfo *info = (MyStreamInfo *)clientCallBackInfo;
CFIndex bytesRead = 0, bytesWritten = 0;
CFStreamError error;
CFNumberRef cfSize;
SInt64 size;
float progress;
assert(readStream != NULL);
assert(info != NULL);
assert(info->readStream == readStream);
switch (type) {
case kCFStreamEventOpenCompleted:
cfSize = CFReadStreamCopyProperty(info->readStream, kCFStreamPropertyFTPResourceSize);
if (cfSize) {
if (CFNumberGetValue(cfSize, kCFNumberSInt64Type, &size)) {
fprintf(stderr, "File size is %" PRId64 "\n", size);
info->fileSize = size;
}
CFRelease(cfSize);
} else {
fprintf(stderr, "File size is unknown. Uh oh.\n");
assert(info->fileSize == 0);
}
break;
case kCFStreamEventHasBytesAvailable:
/* CFReadStreamRead will return the number of bytes read, or -1 if an error occurs
preventing any bytes from being read, or 0 if the stream's end was encountered. */
bytesRead = CFReadStreamRead(info->readStream, info->buffer, kMyBufferSize);
if (bytesRead > 0) {
bytesWritten = 0;
while (bytesWritten < bytesRead) {
CFIndex result;
result = CFWriteStreamWrite(info->writeStream, info->buffer + bytesWritten, bytesRead - bytesWritten);
if (result <= 0) {
fprintf(stderr, "CFWriteStreamWrite returned %ld\n", result);
goto exit;
}
bytesWritten += result;
}
info->totalBytesWritten += bytesWritten;
} else {
/* If bytesRead < 0, we've hit an error. If bytesRead == 0, we've hit the end of the file.
In either case, we do nothing, and rely on CF to call us with kCFStreamEventErrorOccurred
or kCFStreamEventEndEncountered in order for us to do our clean up. */
}
if (info->fileSize > 0) {
progress = ((float)info->totalBytesWritten/(float)info->fileSize);
DELEGATE_CALLBACK(progressAtPercent:, NUMBER(progress));
}
break;
case kCFStreamEventErrorOccurred:
error = CFReadStreamGetError(info->readStream);
NSString *reason = [NSString stringWithFormat:@"CFReadStreamGetError returned (%d, %ld)\n", error.domain, error.error];
fprintf(stderr, "CFReadStreamGetError returned (%d, %ld)\n", error.domain, error.error);
DELEGATE_CALLBACK(dataDownloadFailed:, reason);
goto exit;
case kCFStreamEventEndEncountered:
DELEGATE_CALLBACK(downloadFinished, nil);
fprintf(stderr, "\nDownload complete\n");
goto exit;
default:
fprintf(stderr, "Received unexpected CFStream event (%d)", type);
break;
}
return;
exit:
MyStreamInfoDestroy(info);
CFRunLoopStop(CFRunLoopGetCurrent());
return;
}
static Boolean MySimpleDownload(CFStringRef urlString, CFURLRef destinationFolder, CFStringRef username, CFStringRef password)
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamClientContext context = { 0, NULL, NULL, NULL, NULL };
CFURLRef downloadPath, downloadURL;
CFStringRef fileName;
Boolean success = true;
BOOL dirPath;
MyStreamInfo *streamInfo;
assert(urlString != NULL);
assert(destinationFolder != NULL);
assert( (username != NULL) || (password == NULL) );
[[NSFileManager defaultManager] fileExistsAtPath:[(NSURL *)destinationFolder path] isDirectory: &dirPath];
if (!dirPath) {
fprintf(stderr, "Download destination must be a directory.\n");
return false;
}
/* Create a CFURL from the urlString. */
downloadURL = CFURLCreateWithString(kCFAllocatorDefault, urlString, NULL);
assert(downloadURL != NULL);
/* Copy the end of the file path and use it as the file name. */
fileName = CFURLCopyLastPathComponent(downloadURL);
assert(fileName != NULL);
/* Create the downloadPath by taking the destination folder and appending the file name. */
downloadPath = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault, destinationFolder, fileName, false);
assert(downloadPath != NULL);
CFRelease(fileName);
/* Create a CFWriteStream for the file being downloaded. */
writeStream = CFWriteStreamCreateWithFile(kCFAllocatorDefault, downloadPath);
assert(writeStream != NULL);
CFRelease(downloadPath);
/* CFReadStreamCreateWithFTPURL creates an FTP read stream for downloading from an FTP URL. */
readStream = CFReadStreamCreateWithFTPURL(kCFAllocatorDefault, downloadURL);
assert(readStream != NULL);
CFRelease(downloadURL);
/* Initialize our MyStreamInfo structure, which we use to store some information about the stream. */
MyStreamInfoCreate(&streamInfo, readStream, writeStream);
context.info = (void *)streamInfo;
/* CFWriteStreamOpen will return success/failure. Opening a stream causes it to reserve all the
system resources it requires. If the stream can open non-blocking, this will always return TRUE;
listen to the run loop source to find out when the open completes and whether it was successful. */
success = CFWriteStreamOpen(writeStream);
if (success) {
/* CFReadStreamSetClient registers a callback to hear about interesting events that occur on a stream. */
success = CFReadStreamSetClient(readStream, kNetworkEvents, MyDownloadCallBack, &context);
if (success) {
/* Schedule a run loop on which the client can be notified about stream events. The client
callback will be triggered via the run loop. It's the caller's responsibility to ensure that
the run loop is running. */
CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
MyCFStreamSetUsernamePassword(readStream, username, password);
// MyCFStreamSetFTPProxy(readStream, &streamInfo->proxyDict);
/* Setting the kCFStreamPropertyFTPFetchResourceInfo property will instruct the FTP stream
to fetch the file size before downloading the file. Note that fetching the file size adds
some time to the length of the download. Fetching the file size allows you to potentially
provide a progress dialog during the download operation. You will retrieve the actual file
size after your CFReadStream Callback gets called with a kCFStreamEventOpenCompleted event. */
CFReadStreamSetProperty(readStream, kCFStreamPropertyFTPFetchResourceInfo, kCFBooleanTrue);
/* CFReadStreamOpen will return success/failure. Opening a stream causes it to reserve all the
system resources it requires. If the stream can open non-blocking, this will always return TRUE;
listen to the run loop source to find out when the open completes and whether it was successful. */
success = CFReadStreamOpen(readStream);
if (success == false) {
fprintf(stderr, "CFReadStreamOpen failed\n");
MyStreamInfoDestroy(streamInfo);
}
} else {
fprintf(stderr, "CFReadStreamSetClient failed\n");
MyStreamInfoDestroy(streamInfo);
}
} else {
fprintf(stderr, "CFWriteStreamOpen failed\n");
MyStreamInfoDestroy(streamInfo);
}
return success;
}
- (void) fetch: (NSString *) anItem
{
if (!self.uname || !self.pword) COMPLAIN_AND_BAIL(@"Please set user name and password first");
if (!self.urlString) COMPLAIN_AND_BAIL(@"Please set URL string first");
NSString *ftpRequest = [NSString stringWithFormat:@"%@/%@", self.urlString, [anItem stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
CFShow(ftpRequest);
NSString *writepath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
self.filePath = [writepath stringByAppendingPathComponent:anItem];
CFURLRef writeURL = CFURLCreateFromFileSystemRepresentation (NULL, (const UInt8 *) [writepath UTF8String], [writepath length], NO);
MySimpleDownload((CFStringRef)ftpRequest, writeURL, (CFStringRef) self.uname, (CFStringRef)self.pword);
}
+ (void) download:(NSString *) anItem
{
[sharedInstance fetch:anItem];
}
#pragma mark Class
+ (FTPHelper *) sharedInstance
{
if(!sharedInstance) sharedInstance = [[self alloc] init];
return sharedInstance;
}
@end