-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMySVN.m
791 lines (619 loc) · 27 KB
/
MySVN.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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
//
// MySVN.m
//
#import "MySVN.h"
#import "MyApp.h"
#import "CommonUtils.h"
#import "Tasks.h"
#import <unistd.h>
extern UInt32 gSvnVersion;
//----------------------------------------------------------------------------------------
static id
makeTaskInfo (NSString* name, NSString* commmandPath, NSArray* arguments)
{
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
name, @"name",
[NSString stringWithFormat: @"%@ %@",
commmandPath, [arguments componentsJoinedByString: @" "]], @"command",
nil];
}
//----------------------------------------------------------------------------------------
static id
makeSvnInfo (NSString* name, NSArray* arguments)
{
return makeTaskInfo(name, SvnCmdPath(), arguments);
}
//----------------------------------------------------------------------------------------
static NSString*
joinedOptions (NSArray* options1, NSArray* options2)
{
return [NSString stringWithFormat: @"%@ %@", [options1 componentsJoinedByString: @" "],
[options2 componentsJoinedByString: @" "]];
}
//----------------------------------------------------------------------------------------
static NSString*
concatOptions (NSInvocation* generalOptions, NSArray* options)
{
return joinedOptions([MySvn optionsFromSvnOptionsInvocation: generalOptions], options);
}
//----------------------------------------------------------------------------------------
static void
addGeneralOptions (NSMutableArray* arguments, NSInvocation* generalOptions)
{
[arguments addObjectsFromArray: [MySvn optionsFromSvnOptionsInvocation: generalOptions]];
}
//----------------------------------------------------------------------------------------
NSString*
SvnPath ()
{
return GetPreference(@"svnBinariesFolder");
}
//----------------------------------------------------------------------------------------
NSString*
SvnCmdPath ()
{
return [SvnPath() stringByAppendingPathComponent: @"svn"];
}
//----------------------------------------------------------------------------------------
NSString*
ShellScriptPath (NSString* script)
{
return [[NSBundle mainBundle] pathForResource: script ofType: @"sh"];
}
//----------------------------------------------------------------------------------------
NSString*
GetDiffAppName ()
{
// 0: FileMerge, 1: TextWrangler, 2: CodeWarrior, 3: BBEdit,
// 4: Araxis Merge, 5: DiffMerge, 6: Changes, 7: Guiffy, 8: KDiff3
static NSString* const diffAppNames[] = {
@"filemerge", @"textwrangler", @"codewarrior", @"bbedit",
@"araxis", @"diffmerge", @"changes", @"guiffy", @"kdiff3"
};
int diffAppIndex = GetPreferenceInt(@"defaultDiffApplication");
if (diffAppIndex < 0 || diffAppIndex >= sizeof(diffAppNames) / sizeof(diffAppNames[0]))
diffAppIndex = 0;
return diffAppNames[diffAppIndex];
}
//----------------------------------------------------------------------------------------
#pragma mark -
@implementation MySvn
// New-style svn calls (>=0.8)
+ (NSMutableDictionary*) diffItems: (NSArray*) itemsPaths
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
NSMutableArray* arguments = [NSMutableArray arrayWithObjects:
@"diff", @"--diff-cmd", ShellScriptPath(@"svndiff"),
@"--extensions", GetDiffAppName(),
nil];
addGeneralOptions(arguments, generalOptions);
[arguments addObjectsFromArray: options];
[arguments addObjectsFromArray: itemsPaths];
NSString* taskLaunchPath = SvnCmdPath();
id additionalTaskInfo = makeTaskInfo(@"svn diff", taskLaunchPath, arguments);
// <svnCmdPath> diff --diff-cmd <svndiff.sh> --extensions <diffAppName> ...
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: NO];
}
//----------------------------------------------------------------------------------------
+ (NSMutableDictionary*) runScript: (NSString*) scriptName
options: (NSArray*) options
args: (NSArray*) args
name: (NSString*) name
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
dataOnly: (BOOL) dataOnly
{
NSString* taskLaunchPath = ShellScriptPath(scriptName);
NSMutableArray* arguments = [NSMutableArray arrayWithArray: options];
[arguments addObjectsFromArray: args];
id additionalTaskInfo = makeTaskInfo(name, taskLaunchPath, arguments);
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: dataOnly];
}
//----------------------------------------------------------------------------------------
+ (NSMutableDictionary*) genericCommand: (NSString*) command
arguments: (NSArray*) args
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
NSString *taskLaunchPath = SvnCmdPath();
NSMutableArray *arguments = [NSMutableArray arrayWithObject: command];
[arguments addObjectsFromArray: args];
// dprintf("cmd='%@' gSvnVersion=%d", command, gSvnVersion);
if (gSvnVersion >= 1006000 && [command isEqualToString: @"cleanup"])
[arguments addObject: @"--non-interactive"];
else if (![command isEqualToString: @"revert"] && ![command isEqualToString: @"add"] &&
![command isEqualToString: @"move"] && ![command isEqualToString: @"resolved"])
addGeneralOptions(arguments, generalOptions);
[arguments addObjectsFromArray: options];
id additionalTaskInfo = makeTaskInfo([@"svn " stringByAppendingString: command], taskLaunchPath, arguments);
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: NO];
}
+ (NSMutableDictionary*) moveMultiple: (NSArray*) files
destination: (NSString*) destinationPath
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
#pragma unused(generalOptions)
NSString *taskLaunchPath = ShellScriptPath(@"svnmove");
NSMutableArray *arguments = [NSMutableArray arrayWithObject: SvnCmdPath()];
[arguments addObject: [options componentsJoinedByString: @" "]]; // see svnmove.sh
[arguments addObject: destinationPath]; // see svnmove.sh
[arguments addObjectsFromArray: files];
id additionalTaskInfo = makeTaskInfo(@"svn move multiple", taskLaunchPath, arguments);
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: NO];
}
+ (NSMutableDictionary*) copyMultiple: (NSArray*) files
destination: (NSString*) destinationPath
generalOptions:(NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
#pragma unused(generalOptions)
NSString *taskLaunchPath = ShellScriptPath(@"svncopy");
NSMutableArray *arguments = [NSMutableArray arrayWithObject: SvnCmdPath()];
[arguments addObject: [options componentsJoinedByString: @" "]]; // see svncopy.sh
[arguments addObject: destinationPath]; // see svncopy.sh
[arguments addObjectsFromArray: files];
id additionalTaskInfo = makeTaskInfo(@"svn copy multiple", taskLaunchPath, arguments);
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: NO];
}
+ (NSMutableDictionary*) log: (NSString*) path
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
NSString *taskLaunchPath = SvnCmdPath();
NSMutableArray *arguments = [NSMutableArray array];
[arguments addObject: @"log"];
addGeneralOptions(arguments, generalOptions);
[arguments addObjectsFromArray: options];
[arguments addObject: path];
id additionalTaskInfo = makeTaskInfo(@"svn log", taskLaunchPath, arguments);
// [additionalTaskInfo setObject: kNSTrue forKey: @"outputToData"];
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: YES];
}
+ (NSMutableDictionary*) list: (NSString*) path
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
NSString *taskLaunchPath = SvnCmdPath();
NSMutableArray *arguments = [NSMutableArray array];
[arguments addObject: @"list"];
addGeneralOptions(arguments, generalOptions);
[arguments addObjectsFromArray: options];
[arguments addObject: path];
id additionalTaskInfo = makeTaskInfo(@"svn list", taskLaunchPath, arguments);
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: YES];
}
+ (NSMutableDictionary*) statusAtWorkingCopyPath: (NSString*) path
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
NSString *taskLaunchPath = SvnCmdPath();
NSMutableArray *arguments = [NSMutableArray array];
[arguments addObject: @"status"];
addGeneralOptions(arguments, generalOptions);
[arguments addObjectsFromArray: options];
[arguments addObject: path];
id additionalTaskInfo = makeTaskInfo(@"svn status", taskLaunchPath, arguments);
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: YES];
}
+ (NSMutableDictionary*) updateAtWorkingCopyPath: (NSString*) path
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
NSString *taskLaunchPath = SvnCmdPath();
NSMutableArray *arguments = [NSMutableArray array];
[arguments addObject: @"update"];
addGeneralOptions(arguments, generalOptions);
[arguments addObjectsFromArray: options];
[arguments addObject: path];
id additionalTaskInfo = makeTaskInfo(@"svn update", taskLaunchPath, arguments);
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: NO];
}
+ (NSMutableDictionary*) checkout: (NSString*) file
destination: (NSString*) destinationPath
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
NSString *taskLaunchPath = SvnCmdPath();
NSMutableArray *arguments = [NSMutableArray array];
[arguments addObject: @"checkout"];
addGeneralOptions(arguments, generalOptions);
[arguments addObjectsFromArray: options];
[arguments addObject: file];
[arguments addObject: destinationPath];
id additionalTaskInfo = makeTaskInfo(@"svn checkout", taskLaunchPath, arguments);
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: NO];
}
+ (NSMutableDictionary*) extractItems: (NSArray*) items
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
NSString *taskLaunchPath = ShellScriptPath(@"svnextract");
NSMutableArray *arguments = [NSMutableArray array];
[arguments addObject: SvnCmdPath()];
[arguments addObject: concatOptions(generalOptions, options)];
[arguments addObjectsFromArray: items];
id additionalTaskInfo = makeTaskInfo(@"extract", taskLaunchPath, arguments);
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: NO];
}
+ (NSMutableDictionary*) import: (NSString*) file
destination: (NSString*) destinationPath
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
NSString *taskLaunchPath = SvnCmdPath();
NSMutableArray *arguments = [NSMutableArray array];
[arguments addObject: @"import"];
addGeneralOptions(arguments, generalOptions);
[arguments addObjectsFromArray: options];
[arguments addObject: file];
[arguments addObject: destinationPath];
id additionalTaskInfo = makeTaskInfo(@"svn import", taskLaunchPath, arguments);
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: NO];
}
+ (NSMutableDictionary*) copy: (NSString*) file
destination: (NSString*) destinationPath
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
NSString *taskLaunchPath = SvnCmdPath();
NSMutableArray *arguments = [NSMutableArray array];
[arguments addObject: @"copy"];
addGeneralOptions(arguments, generalOptions);
[arguments addObjectsFromArray: options];
[arguments addObject: file];
[arguments addObject: destinationPath];
id additionalTaskInfo = makeTaskInfo(@"svn copy", taskLaunchPath, arguments);
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: NO];
}
+ (NSMutableDictionary*) move: (NSString*) file
destination: (NSString*) destinationPath
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
NSString *taskLaunchPath = SvnCmdPath();
NSMutableArray *arguments = [NSMutableArray array];
[arguments addObject: @"move"];
addGeneralOptions(arguments, generalOptions);
[arguments addObjectsFromArray: options];
[arguments addObject: file];
[arguments addObject: destinationPath];
id additionalTaskInfo = makeTaskInfo(@"svn move", taskLaunchPath, arguments);
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: NO];
}
+ (NSMutableDictionary*) mkdir: (NSArray*) files
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
NSString *taskLaunchPath = SvnCmdPath();
NSMutableArray *arguments = [NSMutableArray array];
[arguments addObject: @"mkdir"];
addGeneralOptions(arguments, generalOptions);
[arguments addObjectsFromArray: options];
[arguments addObjectsFromArray: files];
id additionalTaskInfo = makeTaskInfo(@"svn mkdir", taskLaunchPath, arguments);
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: NO];
}
+ (NSMutableDictionary*) delete: (NSArray*) files
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
NSString *taskLaunchPath = SvnCmdPath();
NSMutableArray *arguments = [NSMutableArray array];
[arguments addObject: @"delete"];
addGeneralOptions(arguments, generalOptions);
[arguments addObjectsFromArray: options];
[arguments addObjectsFromArray: files];
id additionalTaskInfo = makeTaskInfo(@"svn delete", taskLaunchPath, arguments);
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: NO];
}
//----------------------------------------------------------------------------------------
+ (NSMutableDictionary*) blame: (NSArray*) files
revision: (NSString*) revision
generalOptions: (NSInvocation*) generalOptions
options: (NSArray*) options
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
{
NSMutableArray* arguments = [NSMutableArray arrayWithObjects:
SvnCmdPath(), GetDiffAppName(), revision, concatOptions(generalOptions, options), nil];
[arguments addObjectsFromArray: files];
NSString* taskLaunchPath = ShellScriptPath(@"svnblame");
id additionalTaskInfo = makeTaskInfo(@"svn blame", taskLaunchPath, arguments);
// svnblame.sh <svn-tool> <diff-app> <revision> <options> <url...>
return [MySvn launchTask: taskLaunchPath arguments: arguments callback: callback callbackInfo: callbackInfo
taskInfo: taskInfo additionalTaskInfo: additionalTaskInfo outputToData: NO];
}
//----------------------------------------------------------------------------------------
#pragma mark -
#pragma mark Helpers
//----------------------------------------------------------------------------------------
static NSDictionary*
ensureDict (NSDictionary* dictOrNil)
{
return dictOrNil ? dictOrNil : [NSDictionary dictionary];
}
+ (NSArray*) optionsFromSvnOptionsInvocation: (NSInvocation*) invocation
{
NSMutableDictionary *dic;
[invocation invoke];
[invocation getReturnValue: &dic];
NSMutableArray *arr = [NSMutableArray array];
NSString *username = [dic objectForKey: @"user"];
NSString *password = [dic objectForKey: @"pass"];
if ([username length])
{
[arr addObject: @"--username"];
[arr addObject: username];
if ([password length])
{
[arr addObject: @"--password"];
[arr addObject: password];
}
}
[arr addObject: @"--non-interactive"];
return arr;
}
//----------------------------------------------------------------------------------------
+ (NSMutableDictionary*) launchTask: (NSString*) taskLaunchPath
arguments: (NSArray*) arguments
callback: (NSInvocation*) callback
callbackInfo: (id) callbackInfo
taskInfo: (id) taskInfo
additionalTaskInfo: (id) additionalTaskInfo
outputToData: (BOOL) outputToData
{
/* dprintf_("launchTask: `%@ %@`\n [%@ %s] callback=%@",
taskLaunchPath, [arguments componentsJoinedByString: @" "],
[callback target], sel_getName([callback selector]), callback);*/
[callback retainArguments];
NSTask* task = [[NSTask alloc] init];
NSPipe* pipe = [[NSPipe alloc] init];
NSPipe* errorPipe = [[NSPipe alloc] init];
[task setEnvironment: [Task createEnvironment: TRUE]];
[task setLaunchPath: taskLaunchPath];
[task setArguments: arguments];
[task setStandardOutput: pipe];
[task setStandardError: errorPipe];
NSFileHandle* handle = [pipe fileHandleForReading];
NSFileHandle* errorHandle = [errorPipe fileHandleForReading];
// this will be done by Tasks
// [task launch];
NSMutableDictionary *taskObj = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"pending", @"status",
task, @"task",
handle, @"handle",
errorHandle, @"errorHandle",
// [NSNumber numberWithInt: [task processIdentifier]], @"pid",
callback, @"callback",
ensureDict(callbackInfo), @"callbackInfo",
ensureDict(taskInfo), @"taskInfo",
ensureDict(additionalTaskInfo), @"additionalTaskInfo",
NSBool(outputToData), @"outputToData",
nil];
[errorPipe release];
[pipe release];
[task release];
[Tasks addTask: taskObj];
return taskObj;
}
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
+ (void) killTask: (NSDictionary*) taskObj
force: (BOOL) force
{
NSTask* const task = [taskObj objectForKey: @"task"];
// dprintf("task=%@ force=%d taskObj=%@", task, force, [taskObj objectForKey: @"pid"]);
// dprintf("task=%@ force=%d", task, force);
AssertClass(task, NSTask);
if ([task isRunning])
{
if (force) // Use kill -9 to kill.
{
pid_t pid = [task processIdentifier];
if (kill(pid, SIGKILL))
dprintf("kill(%d) => errno=%d", pid, errno);
}
else
{
[task terminate];
}
}
}
//----------------------------------------------------------------------------------------
enum {
kCacheMaxSize = 400,
kCacheMinAge = 3600 * 8, // seconds
kCacheMaxDeletableFileSize = 32 * 1024, // bytes
kCacheDeleteCount = 8
};
//----------------------------------------------------------------------------------------
// Try to delete up to kCacheDeleteCount entries & their files.
// Called if adding an entry to the cache with grow it past kCacheMaxSize.
// Only delete entries older than kCacheMinAge that have no file or whose size is <= kCacheMaxDeletableFileSize.
// Deletes the least recently accessed entries first.
static void
removeOldEntries (NSMutableDictionary* cacheDict, NSString* cacheDir,
NSFileManager* fileMgr, CFAbsoluteTime now)
{
struct Desc
{
CFAbsoluteTime time;
id key;
id path;
};
typedef struct Desc Desc;
int count = 0, i;
Desc descs[kCacheDeleteCount + 1];
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
CFAbsoluteTime time0 = now - kCacheMinAge;
id key, fattrs;
NSEnumerator* enumerator = [cacheDict keyEnumerator];
while ((key = [enumerator nextObject]))
{
NSDictionary* cacheEntry = [cacheDict objectForKey: key];
CFAbsoluteTime time = [[cacheEntry objectForKey: @"time"] doubleValue];
if (time < time0)
{
NSString* path = [cacheDir stringByAppendingPathComponent:
[cacheEntry objectForKey: @"name"]];
if ((fattrs = [fileMgr fileAttributesAtPath: path traverseLink: NO]) == nil ||
[fattrs fileSize] <= kCacheMaxDeletableFileSize)
{
const Desc d = { time, key, fattrs ? path : nil };
descs[count] = d;
for (i = count - 1; i >= 0; --i)
{
if (time >= descs[i].time)
break;
descs[i + 1] = descs[i];
descs[i] = d;
}
if (count < kCacheDeleteCount)
++count;
else
time0 = descs[kCacheDeleteCount - 1].time;
}
}
}
// NSLog(@"removeOldEntries: count=%d", count);
for (i = 0; i < count; ++i)
{
Desc* d = &descs[i];
if (d->path)
if (![fileMgr removeFileAtPath: d->path handler: nil])
continue;
[cacheDict removeObjectForKey: d->key];
// NSLog(@"\n %f '%@'", d->time, d->key);
}
[pool release];
}
//----------------------------------------------------------------------------------------
+ (NSString*) cachePathForKey: (NSString*) key
{
static NSMutableDictionary* cacheDict; // this dictionary contains the cache
static NSString* cacheDir; // the path to the cache directory
static unsigned int nextIndex = 0;
NSFileManager* const fileMgr = [NSFileManager defaultManager];
NSUserDefaults* const prefs = [NSUserDefaults standardUserDefaults];
if (cacheDict == nil)
{
id obj = [prefs dictionaryForKey: @"cacheFilesDict"];
cacheDict = obj ? [obj mutableCopyWithZone: nil] : [[NSMutableDictionary alloc] init];
if ((obj = [prefs objectForKey: @"cacheNextIndex"]))
nextIndex = [obj unsignedIntValue];
}
// Find or create the cache directory
if (cacheDir == nil)
{
NSArray* libraryDirectories = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, TRUE);
NSString* cachesFolder = [libraryDirectories objectAtIndex: 0];
cacheDir = [cachesFolder stringByAppendingPathComponent: [[NSBundle mainBundle] bundleIdentifier]];
[cacheDir retain];
BOOL isDir;
if ([fileMgr fileExistsAtPath: cacheDir isDirectory: &isDir])
{
if (!isDir)
return nil;
}
else // create directory
{
if (![fileMgr createDirectoryAtPath: cacheDir attributes: nil])
return nil;
}
}
NSString* name = nil;
NSDictionary* cacheEntry = [cacheDict objectForKey: key];
if (cacheEntry)
{
name = [cacheEntry objectForKey: @"name"];
}
const CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
if (name == nil)
{
const unichar kCacheFormat = 'A'; // cache format 'A'
do {
name = [NSString stringWithFormat: @"%C%09u.cache", kCacheFormat, nextIndex++];
}
while ([fileMgr fileExistsAtPath: [cacheDir stringByAppendingPathComponent: name]]);
}
if (cacheEntry == nil && [cacheDict count] >= kCacheMaxSize)
removeOldEntries(cacheDict, cacheDir, fileMgr, now);
cacheEntry = [NSDictionary dictionaryWithObjectsAndKeys: name, @"name",
[NSNumber numberWithDouble: now], @"time",
nil];
[cacheDict setValue: cacheEntry forKey: key];
[prefs setObject: cacheDict forKey: @"cacheFilesDict"];
[prefs setObject: [NSNumber numberWithUnsignedInt: nextIndex] forKey: @"cacheNextIndex"];
[prefs synchronize];
NSString* resultString = [cacheDir stringByAppendingPathComponent: name];
// NSLog(@"cacheDict=%@", cacheDict);
// NSLog(@"cachePathForKey('%@') => '%@'", key, name);
return resultString;
}
@end