OSX Preference Pane and AFS Backgrounder
[openafs.git] / src / platform / DARWIN / AFSPreference / AFSPropertyManager.m
1 //
2 //  AFSPropertyManager.m
3 //  AFSCommander
4 //
5 //  Created by Claudio Bisegni on 21/05/07.
6 //  Copyright 2007 INFN - National Institute of Nuclear Physics. All rights reserved.
7 //
8
9 #import <Kerberos/Kerberos.h>
10 #import "AFSPropertyManager.h"
11 #import "TaskUtil.h"
12
13 #define kKerberosAuthError                                      NSLocalizedStringFromTableInBundle(@"KerberosAuthError",nil,[NSBundle bundleForClass:[self class]],@"KerberosAuthError")
14 #define kPathNotEmpty                                           NSLocalizedStringFromTableInBundle(@"PathNotEmpty",nil,[NSBundle bundleForClass:[self class]],@"PathNotEmpty")
15 #define kPathDontContainAfsInstallation         NSLocalizedStringFromTableInBundle(@"PathDontContainAfsInstallation",nil,[NSBundle bundleForClass:[self class]],@"PathDontContainAfsInstallation")
16 #define kThisCellFOError                                        NSLocalizedStringFromTableInBundle(@"ThisCellFOError",nil,[NSBundle bundleForClass:[self class]],@"ThisCellFOError")
17 #define kConfFileNotExits                                       NSLocalizedStringFromTableInBundle(@"ConfFileNotExits",nil,[NSBundle bundleForClass:[self class]],@"ConfFileNotExits")
18 #define kUserNotAuth                                            NSLocalizedStringFromTableInBundle(@"UserNotAuth",nil,[NSBundle bundleForClass:[self class]],@"UserNotAuth")
19 #define kBadAfsPath                                                     NSLocalizedStringFromTableInBundle(@"BadAfsPath",nil,[NSBundle bundleForClass:[self class]],@"BadAfsPath")
20
21 #define AFS_TOOLS_DIRECTORY /Library/Openafs/
22 #define AFS_TOOLS_BIN(x) "/Library/Openafs/bin/"#x
23 #define AFS_CACHE_PATH "/var/db/openafs/cache"
24
25 #define AFSD_TMP_OLD_PREFERENCE_FILE @"/tmp/afsd.options"  
26 #define AFSD_TMP_NEW_PREFERENCE_FILE @"/tmp/afs.conf"
27 #define AFSD_OLD_PREFERENCE_FILE @"/etc/config/afsd.options" 
28 #define AFSD_NEW_PREFERENCE_FILE @"/etc/config/afs.conf"
29
30
31 //-afsdb -stat 2000 -dcache 800 -daemons 3 -volumes 70 -dynroot -fakestat-all
32 #define AFSD_OPTION_AFSDB_KEY   "-afsdb"
33 #define AFSD_OPTION_VERBOSE_KEY "-verbose"
34 #define AFSD_OPTION_STAT_KEY    "-stat"
35 #define AFSD_OPTION_DCACHE_KEY  "-dcache"
36 #define AFSD_OPTION_DAEMONS_KEY "-daemons"
37 #define AFSD_OPTION_VOLUMES_KEY "-volumes"
38 #define AFSD_OPTION_DYNROOT_KEY "-dynroot"
39 #define AFSD_OPTION_FKESTAT_ALL "-fakestat-all"
40
41 #define AFSD_CONF_VERBOSE               @"VERBOSE"
42 #define AFSD_CONF_OPTION                @"OPTIONS"
43 #define AFSD_CONF_SYSNAME               @"AFS_SYSNAME"
44 #define AFSD_CONF_POST_INI              @"AFS_POST_INIT"
45 #define AFSD_CONF_PRE_SHUTDOWN  @"AFS_PRE_SHUTDOWN"
46
47 #define AFS_CONF_CHANGE_FROM_Mj_VERSION 1
48 #define AFS_CONF_CHANGE_FROM_Mi_VERSION 4
49 #define AFS_DEV_CONF_CHANGE_FROM_Mi_VERSION 5
50 #define AFS_CONF_CHANGE_FROM_Pa_VERSION 7
51 #define AFS_DEV_CONF_CHANGE_FROM_Pa_VERSION 31
52
53 @implementation AFSPropertyManager
54
55
56 // -------------------------------------------------------------------------------
57 //  init:
58 // -------------------------------------------------------------------------------
59 -(id) init
60 {
61         [super init];
62         cellList = [[NSMutableArray alloc] init];
63         cellName = nil;
64         userDefaultCellArray = nil;
65         useAfsdConfVersion = NO;
66         dynRoot = NO;
67         afsDB = NO;
68         verbose = NO;
69         futil = [[FileUtil alloc] init];
70         return self;
71 }
72
73 // -------------------------------------------------------------------------------
74 //  initWithAfsPath:
75 // -------------------------------------------------------------------------------
76 - (id)initWithAfsPath:(NSString*)path
77 {
78         // avvio la creazione standard dell'oggetto
79         if([self init]){
80                 // imposto il path
81                 [self setPath:path];
82         }
83         return self;
84 }
85
86 // -------------------------------------------------------------------------------
87 //  dealloc:
88 // -------------------------------------------------------------------------------
89 -(void) dealloc
90 {
91         if(installationPath){ [installationPath release];}
92         if(cellList) {[cellList removeAllObjects];[cellList release];}
93         if(cellName) {[cellName release];}
94         if(futil) {
95                 [futil endAutorization];
96                 [futil release];
97                 futil = nil; 
98         }
99         [super dealloc];
100 }
101
102 // -------------------------------------------------------------------------------
103 //  setPath:
104 // -------------------------------------------------------------------------------
105 - (void) setPath:(NSString*)path {
106         if(installationPath)[installationPath release];
107         
108         if(path)
109                 installationPath = [path retain];
110 }
111
112 // -------------------------------------------------------------------------------
113 //  path:
114 // -------------------------------------------------------------------------------
115 - (NSString*) path{
116         return installationPath;
117 }
118
119 // -------------------------------------------------------------------------------
120 //  statCacheEntry:
121 // -------------------------------------------------------------------------------
122 -(int) statCacheEntry
123 {
124         return statCacheEntry;
125 }
126
127 // -------------------------------------------------------------------------------
128 //  setStatCacheEntry:
129 // -------------------------------------------------------------------------------
130 -(void) setStatCacheEntry:(int)statEntry
131 {
132         statCacheEntry = statEntry;
133 }
134
135
136 // -------------------------------------------------------------------------------
137 //  dCacheDim:
138 // -------------------------------------------------------------------------------
139 -(int) dCacheDim
140 {
141         return dCacheDim;
142 }
143
144 // -------------------------------------------------------------------------------
145 //  setDCacheDim:
146 // -------------------------------------------------------------------------------
147 -(void) setDCacheDim:(int)dcDim
148 {
149         dCacheDim = dcDim;
150 }
151
152 // -------------------------------------------------------------------------------
153 //  cacheDimension:
154 // -------------------------------------------------------------------------------
155 -(int) cacheDimension
156 {
157         return cacheDimension;
158 }
159
160 // -------------------------------------------------------------------------------
161 //  setCacheDimension:
162 // -------------------------------------------------------------------------------
163 -(void) setCacheDimension:(int)cacheDim
164 {
165         cacheDimension = cacheDim;
166 }
167
168 // -------------------------------------------------------------------------------
169 //  daemonNumber:
170 // -------------------------------------------------------------------------------
171 -(int) daemonNumber
172 {
173         return daemonNumber;
174 }
175
176 // -------------------------------------------------------------------------------
177 //  setDaemonNumber:
178 // -------------------------------------------------------------------------------
179 -(void) setDaemonNumber:(int)dNumber
180 {
181         daemonNumber = dNumber;
182 }
183
184 // -------------------------------------------------------------------------------
185 //  afsRootMountPoint:
186 // -------------------------------------------------------------------------------
187 -(NSString*) afsRootMountPoint
188 {
189         return afsRootMountPoint;
190 }
191
192 // -------------------------------------------------------------------------------
193 //  setAfsRootMountPoint:
194 // -------------------------------------------------------------------------------
195 -(void) setAfsRootMountPoint:(NSString*)mountPoint
196 {
197         if(afsRootMountPoint)[afsRootMountPoint release];
198         
199         if(mountPoint)
200                 afsRootMountPoint = [mountPoint retain];
201 }
202
203 // -------------------------------------------------------------------------------
204 //  nVolEntry:
205 // -------------------------------------------------------------------------------
206 -(int) nVolEntry
207 {
208         return nVolEntry;
209 }
210
211 // -------------------------------------------------------------------------------
212 //  setNVolEntry:
213 // -------------------------------------------------------------------------------
214 -(void) setNVolEntry:(int)entry
215 {
216         nVolEntry = entry;
217 }
218
219 // -------------------------------------------------------------------------------
220 //  dynRoot:
221 // -------------------------------------------------------------------------------
222 -(bool) dynRoot
223 {
224         return dynRoot;
225 }
226
227 // -------------------------------------------------------------------------------
228 //  setDynRoot:
229 // -------------------------------------------------------------------------------
230 -(void) setDynRoot:(bool)flag
231 {
232         dynRoot = flag;
233 }
234
235 // -------------------------------------------------------------------------------
236 //  dynRoot:
237 // -------------------------------------------------------------------------------
238 -(bool) afsDB
239 {
240         return afsDB;
241 }
242
243 // -------------------------------------------------------------------------------
244 //  setDynRoot:
245 // -------------------------------------------------------------------------------
246 -(void) setAfsDB:(bool)flag
247 {
248         afsDB = flag;
249 }
250
251 // -------------------------------------------------------------------------------
252 //  setDynRoot:
253 // -------------------------------------------------------------------------------
254 -(bool) verbose
255 {
256         return verbose;
257 }
258
259 // -------------------------------------------------------------------------------
260 //  setDynRoot:
261 // -------------------------------------------------------------------------------
262 -(void) setVerbose:(bool)flag
263 {
264         verbose = flag;
265 }
266
267 // -------------------------------------------------------------------------------
268 //  useAfsdConfVersion:
269 // -------------------------------------------------------------------------------
270 -(BOOL) useAfsdConfConfigFile
271 {
272         return useAfsdConfVersion;
273 }
274
275 // -------------------------------------------------------------------------------
276 //  exceptionOnInvalidPath:
277 // -------------------------------------------------------------------------------
278 - (void) exceptionOnInvalidPath
279 {
280         if(!installationPath || ([installationPath length] == 0)) {
281                 @throw [NSException exceptionWithName:@"AFSPropertyManager:exceptionOnInvalidPath" 
282                                                                            reason:kPathNotEmpty 
283                                                                          userInfo:nil];                 
284         }
285         if(![[NSFileManager defaultManager] fileExistsAtPath:installationPath]){
286                 @throw [NSException exceptionWithName:@"AFSPropertyManager:exceptionOnInvalidPath" 
287                                                                            reason:kPathDontContainAfsInstallation
288                                                                          userInfo:nil];                 
289         }
290         
291 }
292
293 // -------------------------------------------------------------------------------
294 //  loadConfiguration:
295 // -------------------------------------------------------------------------------
296 - (void) loadConfiguration {
297         int mjVersion = [self getAfsMajorVersionVersion];
298         int miVersion = [self getAfsMinorVersionVersion];
299         int paVersion = [self getAfsPatchVersionVersion];
300         
301         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
302         @try{
303                 
304                 [self exceptionOnInvalidPath];
305                 [self clearConfiguration];
306                 
307                 //chech the afs version for chioce wich afsd conf file usage
308                 useAfsdConfVersion = mjVersion >= 1 && miVersion>=4 && paVersion>=7;
309                 useAfsdConfVersion = useAfsdConfVersion || (mjVersion >= 1 && miVersion>=6 && paVersion>=31);
310                 
311                 // read thiscell config file
312                 [filePath setString:installationPath];
313                 [filePath appendString: @"/etc/ThisCell"];
314
315                 [self readCellInfo:filePath];
316                 if(!cellName){
317                         @throw [NSException exceptionWithName:@"readCellInfo" 
318                                                                         reason:kThisCellFOError 
319                                                                   userInfo:nil];
320                 }
321                 //read TheseCell file
322                 [filePath setString: installationPath];
323                 [filePath appendString: @"/etc/TheseCells"];
324                 userDefaultCellArray = [self readTheseCell:filePath];
325                 
326                 //read cell serv db
327                 [filePath setString: installationPath];
328                 [filePath appendString: @"/etc/CellServDB"];
329                 [self readCellDB:filePath];
330                 
331                 //Read cacheinfo
332                 [filePath setString: installationPath];
333                 [filePath appendString: @"/etc/cacheinfo"];
334                 [self readCacheInfo:filePath];
335                 
336                 //Read config/afsd.options
337                 [filePath setString: installationPath];
338                 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
339                 [self readAfsdOption:filePath];
340         } @catch(NSException * e){
341                 @throw e;
342         } @finally {
343                 [filePath release];
344         }
345 }
346
347 // -------------------------------------------------------------------------------
348 //  readCacheInfo:
349 //  file template "/afs:/var/db/openafs/cache:30000"
350 // -------------------------------------------------------------------------------
351 -(void) readCacheInfo:(NSString*)filePath
352 {
353         int cicle = 0;
354         NSString *tmpString = nil;
355         NSCharacterSet *fullStopCS = [NSCharacterSet characterSetWithCharactersInString:@":"];
356         NSMutableCharacterSet *chunkStartCS = [[NSMutableCharacterSet alloc] init];
357         [chunkStartCS formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
358         [chunkStartCS formUnionWithCharacterSet:[NSMutableCharacterSet characterSetWithCharactersInString:@"/"]];
359         
360         NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
361         NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:filePath];
362         NSData *fileHData = [fileH readDataToEndOfFile];
363         NSString *cacheInfoStrData = [[NSString alloc] initWithData:fileHData
364                                                                                                            encoding:NSASCIIStringEncoding];
365         NSScanner *cacheInfoS = [NSScanner  scannerWithString:cacheInfoStrData];
366         
367         @try{
368                 do {
369                         cicle++;
370                         switch(cicle){
371                                 case 1:
372                                         // afs mount path
373                                         [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
374                                         [self setAfsRootMountPoint:tmpString];
375                                         [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
376                                         break;
377                                         
378                                 case 2:
379                                         //cache path default '/var/db/openafs/cache'
380                                         [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
381                                         [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
382                                         break;
383                                 
384                                 case 3:
385                                         // cache dimension
386                                         [cacheInfoS scanUpToCharactersFromSet:returnCS intoString:&tmpString];
387                                         [self setCacheDimension:[tmpString intValue]];
388                                         break;
389                         }
390                 }while(cicle < 3 && ![cacheInfoS isAtEnd]);
391         }@catch(NSException *e){
392                 @throw e;
393         }@finally{
394                 //if(cacheInfoStrData) [cacheInfoStrData release];
395                 if(chunkStartCS) [chunkStartCS release];
396         }
397         
398 }
399
400
401 // -------------------------------------------------------------------------------
402 //  writeCacheInfo:
403 // -------------------------------------------------------------------------------
404 -(int) writeCacheInfo:(NSString*)filePath
405 {
406         NSNumber *tmpNum = nil;
407         NSMutableString *cacheInfoContent = [[NSMutableString alloc] init];
408         if(!cacheInfoContent) return -1;
409         
410         //Afs root mount point
411         if([[self afsRootMountPoint] rangeOfString:@"/"].location == NSNotFound || [[self afsRootMountPoint] length] == 0) 
412                 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"  
413                                                                            reason:@"Bad afs path" 
414                                                                          userInfo:nil];
415         [cacheInfoContent appendString:[self afsRootMountPoint]];
416         
417         //standard cache path
418         
419         [cacheInfoContent appendString:@":"]; [cacheInfoContent appendString:@AFS_CACHE_PATH]; [cacheInfoContent appendString:@":"];
420         
421         //cache dimension
422         tmpNum = [NSNumber numberWithInt:[self cacheDimension]];
423         if([tmpNum intValue] == 0)
424                 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"  
425                                                                            reason:@"Bad cache dimension" 
426                                                                          userInfo:nil];
427         [cacheInfoContent appendString:[tmpNum stringValue]];
428         
429         [cacheInfoContent writeToFile: [filePath stringByExpandingTildeInPath]
430                                            atomically: YES 
431                                                  encoding: NSASCIIStringEncoding 
432                                                         error: nil];
433         
434         [cacheInfoContent release];
435         return noErr;
436 }
437
438 // -------------------------------------------------------------------------------
439 //  readCacheInfo:
440 //  file template "/afs:/var/db/openafs/cache:30000"
441 // -------------------------------------------------------------------------------
442 -(void) readAfsdOption:(NSString*)filePath
443 {
444         @try{
445                 if(useAfsdConfVersion) {
446                         [self readNewAfsdOption:filePath];
447                 } else {
448                         [self readOldAfsdOption:filePath];
449                 }
450         
451         }@catch(NSException *e){
452                 @throw e;
453         }@finally{
454                 
455         }
456         
457 }
458
459 // -------------------------------------------------------------------------------
460 //  readOldAfsdOption:
461 // -------------------------------------------------------------------------------
462 -(void) readOldAfsdOption:(NSString*)filePath
463 {
464
465         /*NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:filePath];
466         NSData *fileHData = [fileH readDataToEndOfFile];
467         NSString *afsdOptionStrData = [[NSString alloc] initWithData:fileHData
468                                                                                                                 encoding:NSASCIIStringEncoding];*/
469         if(!filePath) return;
470         [self readAFSDParamLineContent:[[NSString stringWithContentsOfFile:filePath] stringByStandardizingPath]];
471 }
472
473 // -------------------------------------------------------------------------------
474 //  readAFSDParamLineContent:
475 // -------------------------------------------------------------------------------
476 -(void) readAFSDParamLineContent:(NSString*) paramLine{
477         NSString *tmpString = nil;
478         NSCharacterSet *space = [NSCharacterSet characterSetWithCharactersInString:@" "];
479         NSScanner *afsdOptionS = [NSScanner  scannerWithString:paramLine];
480         
481         do{
482                 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
483                 if(!tmpString) continue;
484                 //check parameter type
485                 if([tmpString isEqualToString:@AFSD_OPTION_DAEMONS_KEY])
486                 {
487                         // get number of daemon
488                         [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
489                         [self setDaemonNumber:[tmpString intValue]];
490                 } else                          
491                         //check parameter type
492                         if([tmpString isEqualToString:@AFSD_OPTION_DCACHE_KEY])
493                         {
494                                 //get dcache dimension
495                                 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
496                                 [self setDCacheDim:[tmpString intValue]];
497                         } else 
498                                 //check parameter type
499                                 if([tmpString isEqualToString:@AFSD_OPTION_DYNROOT_KEY])
500                                 {
501                                         [self setDynRoot:true];
502                                 } else 
503                                         if([tmpString isEqualToString:@AFSD_OPTION_VERBOSE_KEY])
504                                         {
505                                                 [self setVerbose:true];
506                                         } else if([tmpString isEqualToString:@AFSD_OPTION_AFSDB_KEY])
507                                         {
508                                                 [self setAfsDB:true];
509                                         } else
510                                                 //check parameter type
511                                                 if([tmpString isEqualToString:@AFSD_OPTION_STAT_KEY])
512                                                 {
513                                                         // get fstat entry num
514                                                         [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
515                                                         [self setStatCacheEntry:[tmpString intValue]];
516                                                 } else
517                                                         
518                                                         //check parameter type
519                                                         if([tmpString isEqualToString:@AFSD_OPTION_VOLUMES_KEY])
520                                                         {
521                                                                 // get fstat entry num
522                                                                 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
523                                                                 [self setNVolEntry:[tmpString intValue]];
524                                                         }
525                 
526                 
527                 
528         }while(![afsdOptionS isAtEnd]);
529         
530 }
531
532 // -------------------------------------------------------------------------------
533 //  readNewAfsdOption:
534 // -------------------------------------------------------------------------------
535 -(void) readNewAfsdOption:(NSString*)filePath
536 {
537         if(!filePath) return;
538         NSString *currentLines = nil;
539         NSString *paramValue = nil;
540         NSScanner *lineScanner = nil;
541         
542         //Get file content
543         NSString *newAFSDConfContent = [NSString stringWithContentsOfFile:filePath];
544         
545         //get lines in array
546         NSArray *confLines = [newAFSDConfContent componentsSeparatedByString:@"\n"];
547         
548         //Get the lines enumerator
549         NSEnumerator *lineEnumerator = [confLines objectEnumerator];
550         
551         //scann all lines
552         while(currentLines = [lineEnumerator nextObject]) {
553                 if([currentLines rangeOfString:@"#"].location != NSNotFound) continue;
554                         
555                 if([currentLines rangeOfString:AFSD_CONF_OPTION].location != NSNotFound) {
556                         lineScanner = [NSScanner scannerWithString:currentLines];
557                         if(!lineScanner) continue;
558                         
559                         //scann the line
560                         
561                         [lineScanner scanUpToString:@"\"" intoString:&paramValue];[lineScanner scanUpToString:@"-" intoString:&paramValue];
562                         [lineScanner scanUpToString:@"\"" intoString:&paramValue];
563                         
564                         // read the asfd option param line
565                         [self readAFSDParamLineContent:paramValue];
566                 } else if([currentLines rangeOfString:AFSD_CONF_SYSNAME].location != NSNotFound) {
567                         
568                 } else if([currentLines rangeOfString:AFSD_CONF_POST_INI].location != NSNotFound) {
569                         
570                 } else if([currentLines rangeOfString:AFSD_CONF_PRE_SHUTDOWN].location != NSNotFound) {
571                         
572                 }
573         }
574 }
575
576
577 // -------------------------------------------------------------------------------
578 //  writeCacheInfo:
579 // -------------------------------------------------------------------------------
580 -(int) writeAfsdOption:(NSString*)filePath
581 {
582         int result = 0;
583         @try{
584                 if(useAfsdConfVersion) {
585                         result = [self writeNewAfsdOption:filePath];                    
586                 } else {
587                         result = [self writeOldAfsdOption:filePath];
588                 }
589                 
590         }@catch(NSException *e){
591                 @throw e;
592         }@finally{
593                 
594         }
595         return result;
596 }
597
598 // -------------------------------------------------------------------------------
599 //  writeOldAfsdOption:
600 // -------------------------------------------------------------------------------
601 -(int) writeOldAfsdOption:(NSString*)filePath;
602 {
603         NSMutableString *oldConfFileContent = [[[NSMutableString alloc] init] autorelease];
604         //add afsd param
605         [oldConfFileContent appendString:[self makeChaceParamString]];
606         
607         //add cariage return at end of file
608         [oldConfFileContent appendString:@"\n"];
609         
610         //write content on file
611         [oldConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
612                                                  atomically: YES 
613                                                    encoding: NSASCIIStringEncoding 
614                                                           error: nil];
615         return noErr;
616         
617 }
618
619 // -------------------------------------------------------------------------------
620 //  writeNewAfsdOption:
621 //  OPTIONS=
622 //  AFS_SYSNAME=
623 //  AFS_POST_INIT=afs_server_prefs
624 //  AFS_PRE_SHUTDOWN=
625 // -------------------------------------------------------------------------------
626 -(int) writeNewAfsdOption:(NSString*)filePath
627 {
628         NSMutableString *newConfFileContent = [[[NSMutableString alloc] init] autorelease];
629         
630         //Verbose
631         [newConfFileContent appendString:AFSD_CONF_VERBOSE]; [newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
632         
633         //AFSD Option
634         [newConfFileContent appendString:AFSD_CONF_OPTION];[newConfFileContent appendString:@"=\""]; [newConfFileContent appendString:[self makeChaceParamString]]; [newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"]; 
635         
636         //AFS_SYSNAME
637         [newConfFileContent appendString:AFSD_CONF_SYSNAME];[newConfFileContent appendString:@"=\""];[newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"];
638         
639         //AFS_POST_INIT
640         [newConfFileContent appendString:AFSD_CONF_POST_INI];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"]; 
641         
642         //AFS_PRE_SHUTDOWN
643         [newConfFileContent appendString:AFSD_CONF_PRE_SHUTDOWN];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
644         
645         //add content on file
646         [newConfFileContent appendString:@"\n"];
647         
648         //Write to file
649         [newConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
650                                                  atomically: YES 
651                                                    encoding: NSASCIIStringEncoding 
652                                                           error: nil];
653         return noErr;
654 }
655
656 // -------------------------------------------------------------------------------
657 //  getAfsVersion:
658 // -------------------------------------------------------------------------------
659 -(NSString*) getAfsVersion
660 {
661         NSString *tmpString = nil;
662         NSString *result = [TaskUtil executeTaskSearchingPath:@"fs" args:[NSArray arrayWithObjects:@"-version", nil]];
663         NSCharacterSet *endVersionCS = [NSCharacterSet characterSetWithCharactersInString:@"qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREWQ"];
664         NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" "];
665         NSScanner *versionS = [NSScanner  scannerWithString:result];
666         //go to  start of version
667         [versionS scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
668         
669         //get the total version string
670         [versionS scanUpToCharactersFromSet:endVersionCS intoString:&tmpString];
671
672         return tmpString;
673 }
674
675 // -------------------------------------------------------------------------------
676 //  getAfsMajorVersionVersion:
677 // -------------------------------------------------------------------------------
678 -(int) getAfsMajorVersionVersion
679 {
680         NSString *tmpString = nil;
681         NSString *totalVersion = [self getAfsVersion];
682         NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
683         NSScanner *versionMS = [NSScanner  scannerWithString:totalVersion];
684         [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
685         return [tmpString intValue];
686 }
687
688 // -------------------------------------------------------------------------------
689 //  getAfsMinorVersionVersion:
690 // -------------------------------------------------------------------------------
691 -(int) getAfsMinorVersionVersion
692 {
693         NSString *tmpString = nil;
694         NSString *totalVersion = [self getAfsVersion];
695         NSCharacterSet *numCS = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
696         NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
697         NSScanner *versionMS = [NSScanner  scannerWithString:totalVersion];
698         [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];[versionMS scanUpToCharactersFromSet:numCS intoString:&tmpString];[versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
699         return [tmpString intValue];
700 }
701
702 // -------------------------------------------------------------------------------
703 //  getAfsPatchVersionVersion:
704 // -------------------------------------------------------------------------------
705 -(int) getAfsPatchVersionVersion
706 {
707         NSString *totalVersion = [self getAfsVersion];
708         NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
709         int lastPointIndex = [totalVersion rangeOfCharacterFromSet:pointCS 
710                                                                                                            options:NSBackwardsSearch].location;
711         int patchVersion = [[totalVersion substringFromIndex:lastPointIndex+1] intValue];
712         return patchVersion;
713 }
714
715
716 // -------------------------------------------------------------------------------
717 //  clearConfiguration:
718 // -------------------------------------------------------------------------------
719 - (void) clearConfiguration{
720         
721         // clear list of cell
722         [cellList removeAllObjects];
723         
724         userDefaultCellArray= nil;
725         
726         // remove the old cell name
727         if(cellName) {
728                 [cellName release];
729                 cellName = nil;
730         }
731 }
732
733 // -------------------------------------------------------------------------------
734 //  getCellList:
735 // -------------------------------------------------------------------------------
736 -(NSMutableArray*) getCellList
737 {
738         return cellList;
739 }
740
741
742
743 // -------------------------------------------------------------------------------
744 //  getAllCellName:
745 // -------------------------------------------------------------------------------
746 -(NSArray*) getAllCellsName {
747         NSMutableArray *allCelListName = [[[NSMutableArray alloc] init] autorelease];
748         for(int idx = 0; idx < [cellList count]; idx++){
749                 DBCellElement *elemnt = [cellList objectAtIndex:idx];
750                 [allCelListName addObject:[elemnt getCellName]];
751         }
752         return allCelListName;
753 }
754
755 // -------------------------------------------------------------------------------
756 //  getCellList:
757 // -------------------------------------------------------------------------------
758 -(NSArray*) getUserDefaultForTokenCells {
759         return userDefaultCellArray;
760 }
761
762 // -------------------------------------------------------------------------------
763 //  getDefaultCellName:
764 // -------------------------------------------------------------------------------
765 -(NSArray*) getDefaultForTokenCellsName {
766         NSMutableArray *allCelListName = [[[NSMutableArray alloc] init] autorelease];
767         for(int idx = 0; idx < [cellList count]; idx++){
768                 DBCellElement *elemnt = [cellList objectAtIndex:idx];
769                 if([elemnt userDefaultForToken]) [allCelListName addObject:[[elemnt getCellName] retain]];
770         }
771         return allCelListName;
772 }
773
774 // -------------------------------------------------------------------------------
775 //  getCellName:
776 // -------------------------------------------------------------------------------
777 -(NSString*) getDefaultCellName
778 {
779         return cellName;
780 }
781
782 // -------------------------------------------------------------------------------
783 //  setDefaultCellByName:
784 // -------------------------------------------------------------------------------
785 -(void) setDefaultCellByName:(NSString*)name
786 {
787         DBCellElement *elementCell = nil;
788         BOOL cellFound = false;
789         if(!name) return;
790         
791         for(int idx = 0; idx < [cellList count]; idx++) {
792                 // check every cell for delete as old user default cell or selected as neww cell
793                 elementCell = [cellList objectAtIndex:idx];
794                 cellFound = [name  compare:[elementCell getCellName]] == NSOrderedSame;
795                 [elementCell setUserDefaultCell:cellFound];
796                 if(cellFound) {
797                         [elementCell setUserDefaultForToken:YES];
798                         if(cellName)[cellName release];
799                         cellName = [name retain];
800                  }
801         }
802         
803 }
804
805 // -------------------------------------------------------------------------------
806 //  setCellName:
807 // -------------------------------------------------------------------------------
808 - (void) setCellName:(NSString*)cell {
809         [self setDefaultCellByName:cell];
810 }
811
812 // -------------------------------------------------------------------------------
813 //  readCellInfo:
814 // -------------------------------------------------------------------------------
815 -(void) readCellInfo:(NSString*) configFile {
816         NSError *error = nil;
817         NSString *tmpStr = nil;
818         NSString * result = [NSString stringWithContentsOfFile:configFile
819                                                                                                   encoding:NSASCIIStringEncoding
820                                                                                                          error:&error];
821         if(!result && error){
822                 if([error domain] )
823                 @throw [NSException exceptionWithName:@"readCellInfo" 
824                                                                            reason:kConfFileNotExits
825                                                                          userInfo:nil];
826         }
827         NSScanner *scanner = [NSScanner scannerWithString:result];
828         [scanner scanUpToString:@"\n" 
829                                  intoString:&tmpStr];
830         
831         // make a copy of self created string
832         cellName = [tmpStr retain];
833 }
834
835 // -------------------------------------------------------------------------------
836 //  readCellDB:
837 // -------------------------------------------------------------------------------
838 -(void) readCellDB:(NSString*) configFile {     
839         NSString *tmpString = nil;
840         BOOL isInCellDefaultArray = NO; // the cell belong
841         BOOL isDefaultCell = NO;
842         DBCellElement *afsCellDBElement = nil;
843         NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
844         NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
845         
846         NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
847         NSData *dbCellData = [fileH readDataToEndOfFile];
848         NSString *strData = [[NSString alloc] initWithData:dbCellData
849                                                                                           encoding:NSASCIIStringEncoding];
850         NSScanner *cellDBScanner = [NSScanner  scannerWithString:strData];
851         
852         @try{
853         [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
854         while([cellDBScanner isAtEnd] == NO) {
855                 // make new cell
856                 afsCellDBElement = [[DBCellElement alloc] init];
857                 
858                 // get the name of cell
859                 [cellDBScanner scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
860                 [afsCellDBElement setCellName: tmpString];
861                 
862                 //check if this cells is one of user has selected to get token
863                 isInCellDefaultArray = [userDefaultCellArray containsObject:tmpString];
864                 //check if this cell is also the default cell
865                 isDefaultCell = [cellName compare:tmpString]==NSOrderedSame;
866                 
867                 [afsCellDBElement setUserDefaultForToken:isInCellDefaultArray||isDefaultCell];
868                 [afsCellDBElement setUserDefaultCell:isDefaultCell];
869                 
870                 
871         
872                 
873                 
874                 // get the cell comment
875                 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
876                 [cellDBScanner scanUpToCharactersFromSet:returnCS intoString:&tmpString];
877                 [afsCellDBElement setCellComment: tmpString];
878
879                 // get all ip
880                 [cellDBScanner scanUpToString:@">" intoString:&tmpString];
881                 // scann all ip in list
882                 [self scanIpForCell:afsCellDBElement allIP:tmpString];  
883                 
884                 // add cell to list
885                 [cellList addObject:afsCellDBElement];
886                 // release the object becasuse NSMutableArray make a retain on object
887                 [afsCellDBElement release];
888                 //repeat
889                 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
890         }
891         }@catch(NSException *e){
892                 @throw e;
893         }@finally{
894                 //if(strData) [strData release];
895         }
896 }
897
898 // -------------------------------------------------------------------------------
899 //  scanIpForCell:
900 // -------------------------------------------------------------------------------
901 -(void) scanIpForCell:(DBCellElement*) cellElement allIP:(NSString*)allIP {
902         NSScanner *ipScann = [NSScanner scannerWithString:allIP];
903         NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
904         NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
905         NSCharacterSet *startCommentCS = [NSCharacterSet characterSetWithCharactersInString:@"#"];
906         NSString *tmpString = nil;
907         while([ipScann isAtEnd] == NO){
908                 CellIp *cellIpDesc = [[CellIp alloc] init];
909                 
910                 //ip string
911                 [ipScann scanUpToCharactersFromSet:spaceCS 
912                                                                 intoString:&tmpString];
913                 
914                 [cellIpDesc setCellIp:tmpString];
915                 //[tmpString release];
916                 
917                 // go to comment
918                 [ipScann scanUpToCharactersFromSet:startCommentCS intoString:nil];
919                 // skip comment symbol
920                 [ipScann scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
921                 // get comment
922                 [ipScann scanUpToCharactersFromSet:returnCS intoString:&tmpString];
923                 [cellIpDesc setCellComment:tmpString];
924                 //[tmpString release];
925                 
926                 [cellElement addIpToCell:cellIpDesc];
927                 // release the object becasuse NSMutableArray make a retain on object
928                 [cellIpDesc release];
929         }
930 }
931
932
933 // -------------------------------------------------------------------------------
934 //  scanIpForCell:
935 // -------------------------------------------------------------------------------
936 -(NSArray*) readTheseCell:(NSString*) configFile {
937         
938         NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
939         NSData *dbCellData = [fileH readDataToEndOfFile];
940         NSString *strData = [[NSString alloc] initWithData:dbCellData
941                                                                                           encoding:NSASCIIStringEncoding];
942         
943         return [strData componentsSeparatedByString:@"\n"];
944 }
945
946 // -------------------------------------------------------------------------------
947 //  -(void) getTokenList
948 // -------------------------------------------------------------------------------
949 -(NSArray*) getTokenList
950 {
951         int line = 0;
952         NSString *tokenLine = nil;
953         //NSString *tmpValue = nil;
954         NSMutableArray *tokenList = [[NSMutableArray alloc] init];
955         NSString *tokensOutput = [TaskUtil executeTaskSearchingPath:@"tokens" args:[NSArray arrayWithObjects:nil]];
956         
957         // scann the tokens
958         NSScanner *tokenScan = [NSScanner scannerWithString:tokensOutput];
959         NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
960         
961         while([tokenScan isAtEnd] == NO){
962                 line++;
963                 // get the next line
964                 [tokenScan scanUpToCharactersFromSet:returnCS 
965                                                                 intoString:&tokenLine];
966                 
967                 // check for tokens end
968                 if([tokenLine rangeOfString:@"--End of list--"].location != NSNotFound) break;
969                 
970                 
971                 if(line >= 2){
972                         // add enteir row to result
973                         [tokenList addObject:tokenLine];
974                         // create the line scanner for all the row that contains token info
975                 }
976         }
977         //
978         return tokenList;
979 }
980
981 // -------------------------------------------------------------------------------
982 //  +(void) klog:(NSString*)uName uPwd:(NSString*)uPwd
983 // -------------------------------------------------------------------------------
984 -(void) klog:(NSString*)uName uPwd:(NSString*)uPwd cell:(NSString*)theCell
985 {
986         if(uName == @"" ||  uPwd == @"") return;
987         
988         [TaskUtil executeTaskSearchingPath:@"klog" 
989                                                                   args:(theCell==nil?[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, nil]:[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, @"-c", theCell, nil])];
990         
991         
992 }
993
994
995 // -------------------------------------------------------------------------------
996 //  +(void) aklog
997 // -------------------------------------------------------------------------------
998 -(void) aklog:(NSString*)theCell noKerberosCall:(BOOL)krb5CallEnable {
999         KLPrincipal  princ = nil;
1000         KLStatus kstatus = noErr;
1001         char *princName = malloc(255);
1002         @try {
1003                 // trying to ket kerberos ticket
1004                 if(krb5CallEnable) {
1005                         kstatus =  KLAcquireInitialTickets (0L, 0L, &princ,  &princName);
1006                         if(kstatus != noErr && kstatus != klUserCanceledErr) @throw [NSException exceptionWithName:@"aklog" 
1007                                                                                                                                                                                                 reason:kPathNotEmpty 
1008                                                                                                                                                                                           userInfo:nil];
1009                 } else kstatus = klNoErr;
1010                 
1011                  //ok to launch aklog
1012                 if(kstatus == klNoErr) [TaskUtil executeTaskSearchingPath:@"aklog" 
1013                                                                                                                          args:(theCell==nil?[NSArray arrayWithObjects:nil]:[NSArray arrayWithObjects:@"-c", theCell, nil])];
1014                 
1015         }
1016         @catch (NSException * e) {
1017                 @throw e;
1018         }
1019         @finally {
1020                 // destory the kerberos va
1021                 if(princName && princ != nil) 
1022                         KLDisposeString(princName);
1023                 else if(princName) free(princName);
1024         }
1025                 
1026 }
1027
1028
1029 // -------------------------------------------------------------------------------
1030 //  getTokens:
1031 // -------------------------------------------------------------------------------
1032 - (void) getTokens:(BOOL)klogAklogFlag usr:(NSString*)usr pwd:(NSString*)pwd {
1033         
1034         NSString *celStrName = nil;
1035         NSArray *tmpDefaultArray = [self getDefaultForTokenCellsName];
1036         if(tmpDefaultArray && [tmpDefaultArray count] > 1) {
1037                 //there are other cell to autenticate
1038                 for(int idx=0; idx < [tmpDefaultArray count]; idx++){
1039                         celStrName = [tmpDefaultArray objectAtIndex:idx];
1040                         if(klogAklogFlag)
1041                                 [self klog:usr 
1042                                           uPwd:pwd  
1043                                           cell:celStrName];
1044                         else
1045                                 [self aklog:celStrName noKerberosCall:YES];     
1046                 }
1047                 
1048         } else {
1049                 //there is only default cell to autenticate
1050                 if(klogAklogFlag)
1051                         [self klog:usr 
1052                                   uPwd:pwd 
1053                                   cell:nil];
1054                 else
1055                         [self aklog:nil noKerberosCall:YES];
1056         }
1057         
1058 }
1059
1060 // -------------------------------------------------------------------------------
1061 //  +(void) unlog
1062 // -------------------------------------------------------------------------------
1063 -(void) unlog:(NSString*)cell
1064 {
1065         [TaskUtil executeTaskSearchingPath:@"unlog" 
1066                                                                   args:(cell?[NSArray arrayWithObjects:@"-c",cell,nil]:[NSArray arrayWithObjects:nil])];
1067 }
1068
1069 // -------------------------------------------------------------------------------
1070 //  -(void) shutdown
1071 // -------------------------------------------------------------------------------
1072 -(void) shutdown
1073 {
1074         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1075         @try {
1076                 if([[AuthUtil shared] autorize] != noErr)
1077                         return;
1078                 
1079                 /*const char *args0[] = {"stop", 0L};
1080                 [[AuthUtil shared] execUnixCommand:"/Library/StartupItems/OpenAFS/OpenAFS_stop"
1081                                                                           args:args0 
1082                                                                         output:0L];*/
1083                 
1084                 // unmount afs
1085                 const char *args1[] = {"-f", "/afs", 0L};
1086                 [[AuthUtil shared] execUnixCommand:"/sbin/umount"
1087                                                                           args:args1 
1088                                                                         output:0L];
1089                 
1090                 const char *args2[] = {"-shutdown", 0L};
1091                 [[AuthUtil shared] execUnixCommand:"/usr/sbin/afsd"
1092                                                                           args:args2 
1093                                                                         output:0L];
1094                                 
1095                 const char *args3[] = {[filePath UTF8String], 0L};
1096                 [[AuthUtil shared] execUnixCommand:"/sbin/kextunload"
1097                                                                           args:args3 
1098                                                                         output:0L];
1099                 
1100         }
1101         @catch (NSException * e) {
1102                 @throw e;
1103         }
1104         @finally {
1105         
1106         }
1107         
1108         
1109 }
1110
1111 // -------------------------------------------------------------------------------
1112 //  -(void) saveConfigurationFiles
1113 // -------------------------------------------------------------------------------
1114 -(void) saveConfigurationFiles:(BOOL) makeBackup
1115 {
1116         NSError *err;
1117         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1118         NSMutableString *cellServDBString = [[NSMutableString alloc] initWithCapacity:256];
1119         NSMutableString *theseCellString = [[NSMutableString alloc] initWithCapacity:256];
1120         DBCellElement *cellElement = nil;
1121         
1122         // save the configuration file
1123         @try{
1124                 [self exceptionOnInvalidPath];
1125
1126                 // ThisCell
1127                 [filePath setString: @"/tmp/ThisCell"];
1128                 [cellName writeToFile: [filePath stringByExpandingTildeInPath]
1129                                    atomically:YES 
1130                                          encoding: NSASCIIStringEncoding 
1131                                                 error:nil];
1132                 // CellServDB
1133                 
1134                 for(int idx = 0; idx < [cellList count]; idx++){
1135                         cellElement = [cellList objectAtIndex:idx];
1136                         [cellServDBString appendString:[cellElement description]];
1137                         if([cellElement userDefaultForToken]) {
1138                                 [theseCellString  appendString:[cellElement getCellName]];
1139                                 [theseCellString  appendString:@"\n"];
1140                         }
1141                 }
1142                 
1143                 
1144                 [filePath setString: @"/tmp/CellServDB"];
1145                 [cellServDBString writeToFile: [filePath stringByExpandingTildeInPath]
1146                                                    atomically:YES 
1147                                                          encoding:  NSUTF8StringEncoding 
1148                                                                 error:&err];
1149                 
1150                 [filePath setString: @"/tmp/TheseCells"];
1151                 [theseCellString writeToFile: [filePath stringByExpandingTildeInPath]
1152                                                    atomically:YES 
1153                                                          encoding:  NSUTF8StringEncoding 
1154                                                                 error:&err];
1155                 
1156                 // backup original file
1157                 if([futil startAutorization] != noErr){
1158                         @throw [NSException exceptionWithName:@"saveConfigurationFiles:startAutorization" 
1159                                                                                    reason:kUserNotAuth
1160                                                                                  userInfo:nil];
1161                 }
1162
1163                 if(makeBackup) [self backupConfigurationFiles];
1164
1165                 // install ThisCell
1166                 [filePath setString: installationPath];
1167                 [filePath appendString: @"/etc/ThisCell"];
1168                 [self installConfigurationFile:@"/tmp/ThisCell" 
1169                                                           destPath:filePath];           
1170                 
1171                 // install CellServDB
1172                 [filePath setString: installationPath];
1173                 [filePath appendString: @"/etc/CellServDB"];
1174                 [self installConfigurationFile:@"/tmp/CellServDB" 
1175                                                           destPath:filePath];
1176                 
1177                 // install CellServDB
1178                 [filePath setString: installationPath];
1179                 [filePath appendString: @"/etc/TheseCells"];
1180                 [self installConfigurationFile:@"/tmp/TheseCells" 
1181                                                           destPath:filePath];
1182                 
1183         } @catch (NSException *e) {
1184                 @throw e;
1185         }@finally {
1186                 // dispose all variable used
1187                 if(filePath) [filePath release];
1188                 if(cellServDBString) [cellServDBString release];
1189         }
1190         
1191 }
1192
1193 // -------------------------------------------------------------------------------
1194 //  -(void) saveCacheConfigurationFiles
1195 // -------------------------------------------------------------------------------
1196 -(void) saveCacheConfigurationFiles:(BOOL)makeBackup
1197 {
1198         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];     
1199         // save the configuration file
1200         @try{
1201                 [self exceptionOnInvalidPath];
1202                 
1203                 // cacheinfo file creation
1204                 [self writeCacheInfo:@"/tmp/cacheinfo"];
1205                 
1206                 //afsd.option or afs.conf file creation
1207                 [self writeAfsdOption:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE];
1208                 
1209                 // backup original file
1210                 if([futil startAutorization] != noErr){
1211                         @throw [NSException exceptionWithName:@"AFSPropertyManager:saveCacheConfigurationFiles:startAutorization" 
1212                                                                                    reason:kUserNotAuth
1213                                                                                  userInfo:nil];
1214                 }
1215                 
1216                 if(makeBackup) {
1217                         //cacheinfo
1218                         [self backupFile:@"/etc/cacheinfo"];
1219                         
1220                         //afsd.options
1221                         [self backupFile:useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE]; 
1222                 }
1223                 
1224                 // install cacheinfo
1225                 [filePath setString:installationPath];
1226                 [filePath appendString: @"/etc/cacheinfo"];
1227                 [self installConfigurationFile:@"/tmp/cacheinfo" 
1228                                                           destPath:filePath];           
1229                 
1230                 // install afsd.conf or afs.conf configuration file 
1231                 [filePath setString: installationPath];
1232                 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
1233                 [self installConfigurationFile:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE
1234                                                           destPath:filePath];
1235                 
1236         } @catch (NSException *e) {
1237                 @throw e;
1238         }@finally {
1239                 if(filePath) [filePath release];
1240         }
1241         
1242         
1243 }
1244
1245 // -------------------------------------------------------------------------------
1246 //  -(void) installConfigurationFile
1247 // -------------------------------------------------------------------------------
1248 -(void) installConfigurationFile:(NSString*)srcConfFile 
1249                                                 destPath:(NSString*)destPath
1250 {
1251         // delete the file original file
1252
1253         if([futil autorizedDelete:destPath] != noErr){
1254                 @throw [NSException exceptionWithName:@"installConfigurationFile:autorizedDelete" 
1255                                                                            reason:destPath
1256                                                                          userInfo:nil];
1257         }
1258         
1259         // move the file
1260         if([futil autorizedMoveFile:srcConfFile
1261                                                  toPath:destPath] != noErr) {
1262                 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedMoveFile" 
1263                                                                            reason:srcConfFile
1264                                                                          userInfo:nil];
1265         }
1266         
1267         
1268         if([futil autorizedChown:destPath 
1269                                            owner:@"root"
1270                                            group:@"wheel"]!= noErr) {
1271                 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedChown" 
1272                                                                            reason:destPath
1273                                                                          userInfo:nil];
1274         }
1275 }
1276
1277 // -------------------------------------------------------------------------------
1278 //  -(void) backupConfigurationFiles
1279 // -------------------------------------------------------------------------------
1280 -(void) backupConfigurationFiles
1281 {       
1282
1283         @try{
1284                 if([futil startAutorization] != noErr){
1285                         @throw [NSException exceptionWithName:@"backupConfigurationFiles:startAutorization" 
1286                                                                                    reason:kUserNotAuth
1287                                                                                  userInfo:nil];
1288                 }
1289                 //This cell
1290                 [self backupFile:@"/etc/ThisCell"];
1291         
1292                 //CellServDB
1293                 [self backupFile:@"/etc/CellServDB"];
1294                 
1295                 //TheseCell
1296                 [self backupFile:@"/etc/TheseCells"];
1297                 
1298                 //[futil endAutorization];
1299         } @catch (NSException *e) {
1300                 @throw e;
1301         } @finally {
1302         }
1303 }
1304
1305 // -------------------------------------------------------------------------------
1306 //  -(void) backupFile:(NSString*)localAfsFilePath
1307 // -------------------------------------------------------------------------------
1308 -(void) backupFile:(NSString*)localAfsFilePath
1309 {
1310         NSString *srcString = nil;
1311         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1312         OSStatus err = noErr;
1313         @try{
1314                 [filePath setString: installationPath];
1315                 [filePath appendString: localAfsFilePath];
1316                 
1317                 //Check if the file at path exist
1318                 NSFileManager *fileManager = [NSFileManager defaultManager];
1319                 
1320                 //check if th efile exist
1321                 if(![fileManager fileExistsAtPath:[filePath stringByExpandingTildeInPath]]) return;
1322                 
1323                 // store the source path
1324                 srcString  = [filePath stringByExpandingTildeInPath];
1325                 [filePath appendString: @".afscommander_bk"];
1326                 
1327                 // check for presence of bk file
1328                 if(![[NSFileManager defaultManager] fileExistsAtPath:[filePath stringByExpandingTildeInPath]]){
1329                         // backup the file
1330                         err = [futil autorizedCopy:srcString 
1331                                                   toPath:[filePath stringByExpandingTildeInPath]];
1332                 }
1333         } @catch (NSException *e) {
1334                 @throw e;
1335         } @finally {
1336                 if(filePath) [filePath release];
1337         }
1338 }
1339 // -------------------------------------------------------------------------------
1340 //  checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1341 // -------------------------------------------------------------------------------
1342 -(BOOL) checkAfsStatus
1343 {
1344         BOOL result = NO;
1345         NSString *fsResult = [TaskUtil executeTaskSearchingPath:@"fs" args:[NSArray arrayWithObjects:@"checkserver", nil]];
1346         result = (fsResult?([fsResult rangeOfString:@"All servers are running."].location != NSNotFound):NO);
1347         return result;  
1348 }
1349
1350 // -------------------------------------------------------------------------------
1351 //  makeChaceParamString
1352 // -------------------------------------------------------------------------------
1353 -(NSString*) makeChaceParamString
1354 {
1355         NSNumber *tmpNum = nil;
1356         NSMutableString *afsdOption = [[NSMutableString alloc] init];
1357         if(!afsdOption) return @"";
1358         //write the data for afsd config file '-afsdb -stat x -dcache x -daemons x -volumes x -dynroot -fakestat-all'
1359         //afsdb
1360         //dynRoot
1361         if([self afsDB]) {
1362                 [afsdOption appendString:@AFSD_OPTION_AFSDB_KEY];[afsdOption appendString:@" "];
1363         }
1364         
1365         //Verbose
1366         if([self verbose]) {
1367                 [afsdOption appendString:@AFSD_OPTION_VERBOSE_KEY];[afsdOption appendString:@" "];
1368         }
1369         
1370         //stat entry
1371         tmpNum = [NSNumber numberWithInt:[self statCacheEntry]];
1372         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_STAT_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1373         
1374         //dcace
1375         tmpNum = [NSNumber numberWithInt:[self dCacheDim]];
1376         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_DCACHE_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1377         
1378         //daemons
1379         tmpNum = [NSNumber numberWithInt:[self daemonNumber]];
1380         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_DAEMONS_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1381         
1382         //volumes
1383         tmpNum = [NSNumber numberWithInt:[self nVolEntry]];
1384         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_VOLUMES_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1385         
1386         //dynRoot
1387         if([self dynRoot]) {
1388                 [afsdOption appendString:@AFSD_OPTION_DYNROOT_KEY];[afsdOption appendString:@" "];
1389         }
1390         
1391         //fakestat-all
1392         [afsdOption appendString:@AFSD_OPTION_FKESTAT_ALL];[afsdOption appendString:@" "];
1393         
1394         return [afsdOption autorelease];
1395 }
1396
1397 @end
1398