osx: prefs pane should properly detect version
[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 "Krb5Util.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==5 && paVersion>=31);
310                 useAfsdConfVersion = useAfsdConfVersion || (mjVersion >= 1 && miVersion>=6);
311                 
312                 // read thiscell config file
313                 [filePath setString:installationPath];
314                 [filePath appendString: @"/etc/ThisCell"];
315
316                 [self readCellInfo:filePath];
317                 if(!cellName){
318                         @throw [NSException exceptionWithName:@"readCellInfo" 
319                                                                         reason:kThisCellFOError 
320                                                                   userInfo:nil];
321                 }
322                 //read TheseCell file
323                 [filePath setString: installationPath];
324                 [filePath appendString: @"/etc/TheseCells"];
325                 userDefaultCellArray = [self readTheseCell:filePath];
326                 
327                 //read cell serv db
328                 [filePath setString: installationPath];
329                 [filePath appendString: @"/etc/CellServDB"];
330                 [self readCellDB:filePath];
331                 
332                 //Read cacheinfo
333                 [filePath setString: installationPath];
334                 [filePath appendString: @"/etc/cacheinfo"];
335                 [self readCacheInfo:filePath];
336                 
337                 //Read config/afsd.options
338                 [filePath setString: installationPath];
339                 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
340                 [self readAfsdOption:filePath];
341         } @catch(NSException * e){
342                 @throw e;
343         } @finally {
344                 [filePath release];
345         }
346 }
347
348 // -------------------------------------------------------------------------------
349 //  readCacheInfo:
350 //  file template "/afs:/var/db/openafs/cache:30000"
351 // -------------------------------------------------------------------------------
352 -(int) readCacheInfo:(NSString*)filePath
353 {
354         int cicle = 0;
355         NSString *tmpString = nil;
356         NSCharacterSet *fullStopCS = [NSCharacterSet characterSetWithCharactersInString:@":"];
357         NSMutableCharacterSet *chunkStartCS = [[NSMutableCharacterSet alloc] init];
358         [chunkStartCS formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
359         [chunkStartCS formUnionWithCharacterSet:[NSMutableCharacterSet characterSetWithCharactersInString:@"/"]];
360         
361         NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
362         NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:filePath];
363         if (!fileH) return nil;
364         NSData *fileHData = [fileH readDataToEndOfFile];
365         NSString *cacheInfoStrData = [[NSString alloc] initWithData:fileHData
366                                                                                                            encoding:NSASCIIStringEncoding];
367         NSScanner *cacheInfoS = [NSScanner  scannerWithString:cacheInfoStrData];
368         
369         @try{
370                 do {
371                         cicle++;
372                         switch(cicle){
373                                 case 1:
374                                         // afs mount path
375                                         [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
376                                         [self setAfsRootMountPoint:tmpString];
377                                         [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
378                                         break;
379                                         
380                                 case 2:
381                                         //cache path default '/var/db/openafs/cache'
382                                         [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
383                                         [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
384                                         break;
385                                 
386                                 case 3:
387                                         // cache dimension
388                                         [cacheInfoS scanUpToCharactersFromSet:returnCS intoString:&tmpString];
389                                         [self setCacheDimension:[tmpString intValue]];
390                                         break;
391                         }
392                 }while(cicle < 3 && ![cacheInfoS isAtEnd]);
393         }@catch(NSException *e){
394                 @throw e;
395         }@finally{
396                 //if(cacheInfoStrData) [cacheInfoStrData release];
397                 if(chunkStartCS) [chunkStartCS release];
398         }
399         return noErr;
400 }
401
402
403 // -------------------------------------------------------------------------------
404 //  writeCacheInfo:
405 // -------------------------------------------------------------------------------
406 -(int) writeCacheInfo:(NSString*)filePath
407 {
408         NSNumber *tmpNum = nil;
409         NSMutableString *cacheInfoContent = [[NSMutableString alloc] init];
410         if(!cacheInfoContent) return -1;
411         
412         //Afs root mount point
413         if([[self afsRootMountPoint] rangeOfString:@"/"].location == NSNotFound || [[self afsRootMountPoint] length] == 0) 
414                 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"  
415                                                                            reason:@"Bad afs path" 
416                                                                          userInfo:nil];
417         [cacheInfoContent appendString:[self afsRootMountPoint]];
418         
419         //standard cache path
420         
421         [cacheInfoContent appendString:@":"]; [cacheInfoContent appendString:@AFS_CACHE_PATH]; [cacheInfoContent appendString:@":"];
422         
423         //cache dimension
424         tmpNum = [NSNumber numberWithInt:[self cacheDimension]];
425         if([tmpNum intValue] == 0)
426                 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"  
427                                                                            reason:@"Bad cache dimension" 
428                                                                          userInfo:nil];
429         [cacheInfoContent appendString:[tmpNum stringValue]];
430         
431         [cacheInfoContent writeToFile: [filePath stringByExpandingTildeInPath]
432                                            atomically: YES 
433                                                  encoding: NSASCIIStringEncoding 
434                                                         error: nil];
435         
436         [cacheInfoContent release];
437         return noErr;
438 }
439
440 // -------------------------------------------------------------------------------
441 //  readCacheInfo:
442 //  file template "/afs:/var/db/openafs/cache:30000"
443 // -------------------------------------------------------------------------------
444 -(void) readAfsdOption:(NSString*)filePath
445 {
446         @try{
447                 if(useAfsdConfVersion) {
448                     if ([self readNewAfsdOption:filePath] != noErr)
449                         [self readOldAfsdOption:filePath];
450                 } else {
451                     if ([self readOldAfsdOption:filePath] != noErr)
452                         [self readNewAfsdOption:filePath];
453                 }
454         
455         }@catch(NSException *e){
456                 @throw e;
457         }@finally{
458                 
459         }
460         
461 }
462
463 // -------------------------------------------------------------------------------
464 //  readOldAfsdOption:
465 // -------------------------------------------------------------------------------
466 -(int) readOldAfsdOption:(NSString*)filePath
467 {
468         if(!filePath) return nil;
469         return [self readAFSDParamLineContent:[[NSString stringWithContentsOfFile:filePath 
470                                                                                                                           encoding:NSUTF8StringEncoding 
471                                                                                                                                  error:nil] stringByStandardizingPath]];
472 }
473
474 // -------------------------------------------------------------------------------
475 //  readAFSDParamLineContent:
476 // -------------------------------------------------------------------------------
477 -(int) readAFSDParamLineContent:(NSString*) paramLine{
478         if (!paramLine) return nil;
479
480         NSString *tmpString = nil;
481         NSCharacterSet *space = [NSCharacterSet characterSetWithCharactersInString:@" "];
482         NSScanner *afsdOptionS = [NSScanner  scannerWithString:paramLine];
483         
484         do{
485                 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
486                 if(!tmpString) continue;
487                 //check parameter type
488                 if([tmpString isEqualToString:@AFSD_OPTION_DAEMONS_KEY])
489                 {
490                         // get number of daemon
491                         [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
492                         [self setDaemonNumber:[tmpString intValue]];
493                 } else                          
494                         //check parameter type
495                         if([tmpString isEqualToString:@AFSD_OPTION_DCACHE_KEY])
496                         {
497                                 //get dcache dimension
498                                 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
499                                 [self setDCacheDim:[tmpString intValue]];
500                         } else 
501                                 //check parameter type
502                                 if([tmpString isEqualToString:@AFSD_OPTION_DYNROOT_KEY])
503                                 {
504                                         [self setDynRoot:true];
505                                 } else 
506                                         if([tmpString isEqualToString:@AFSD_OPTION_VERBOSE_KEY])
507                                         {
508                                                 [self setVerbose:true];
509                                         } else if([tmpString isEqualToString:@AFSD_OPTION_AFSDB_KEY])
510                                         {
511                                                 [self setAfsDB:true];
512                                         } else
513                                                 //check parameter type
514                                                 if([tmpString isEqualToString:@AFSD_OPTION_STAT_KEY])
515                                                 {
516                                                         // get fstat entry num
517                                                         [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
518                                                         [self setStatCacheEntry:[tmpString intValue]];
519                                                 } else
520                                                         
521                                                         //check parameter type
522                                                         if([tmpString isEqualToString:@AFSD_OPTION_VOLUMES_KEY])
523                                                         {
524                                                                 // get fstat entry num
525                                                                 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
526                                                                 [self setNVolEntry:[tmpString intValue]];
527                                                         }
528                 
529                 
530                 
531         }while(![afsdOptionS isAtEnd]);
532         return noErr;
533 }
534
535 // -------------------------------------------------------------------------------
536 //  readNewAfsdOption:
537 // -------------------------------------------------------------------------------
538 -(int) readNewAfsdOption:(NSString*)filePath
539 {
540         if(!filePath) return nil;
541         NSString *currentLines = nil;
542         NSString *paramValue = nil;
543         NSScanner *lineScanner = nil;
544         
545         //Get file content
546         NSString *newAFSDConfContent = [NSString stringWithContentsOfFile:filePath 
547                                                                                                                          encoding:NSUTF8StringEncoding 
548                                                                                                                                 error:nil];
549         if (!newAFSDConfContent) return nil;
550         
551         //get lines in array
552         NSArray *confLines = [newAFSDConfContent componentsSeparatedByString:@"\n"];
553         
554         //Get the lines enumerator
555         NSEnumerator *lineEnumerator = [confLines objectEnumerator];
556         
557         //scann all lines
558         while(currentLines = [lineEnumerator nextObject]) {
559                 if([currentLines rangeOfString:@"#"].location != NSNotFound) continue;
560                         
561                 if([currentLines rangeOfString:AFSD_CONF_OPTION].location != NSNotFound) {
562                         lineScanner = [NSScanner scannerWithString:currentLines];
563                         if(!lineScanner) continue;
564                         
565                         //scann the line
566                         
567                         [lineScanner scanUpToString:@"\"" intoString:&paramValue];[lineScanner scanUpToString:@"-" intoString:&paramValue];
568                         [lineScanner scanUpToString:@"\"" intoString:&paramValue];
569                         
570                         // read the asfd option param line
571                         [self readAFSDParamLineContent:paramValue];
572                 } else if([currentLines rangeOfString:AFSD_CONF_SYSNAME].location != NSNotFound) {
573                         
574                 } else if([currentLines rangeOfString:AFSD_CONF_POST_INI].location != NSNotFound) {
575                         
576                 } else if([currentLines rangeOfString:AFSD_CONF_PRE_SHUTDOWN].location != NSNotFound) {
577                         
578                 }
579         }
580         return noErr;
581 }
582
583
584 // -------------------------------------------------------------------------------
585 //  writeCacheInfo:
586 // -------------------------------------------------------------------------------
587 -(int) writeAfsdOption:(NSString*)filePath
588 {
589         int result = 0;
590         @try{
591                 if(useAfsdConfVersion) {
592                         result = [self writeNewAfsdOption:filePath];                    
593                 } else {
594                         result = [self writeOldAfsdOption:filePath];
595                 }
596                 
597         }@catch(NSException *e){
598                 @throw e;
599         }@finally{
600                 
601         }
602         return result;
603 }
604
605 // -------------------------------------------------------------------------------
606 //  writeOldAfsdOption:
607 // -------------------------------------------------------------------------------
608 -(int) writeOldAfsdOption:(NSString*)filePath;
609 {
610         NSMutableString *oldConfFileContent = [[[NSMutableString alloc] init] autorelease];
611         //add afsd param
612         [oldConfFileContent appendString:[self makeChaceParamString]];
613         
614         //add cariage return at end of file
615         [oldConfFileContent appendString:@"\n"];
616         
617         //write content on file
618         [oldConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
619                                                  atomically: YES 
620                                                    encoding: NSASCIIStringEncoding 
621                                                           error: nil];
622         return noErr;
623         
624 }
625
626 // -------------------------------------------------------------------------------
627 //  writeNewAfsdOption:
628 //  OPTIONS=
629 //  AFS_SYSNAME=
630 //  AFS_POST_INIT=afs_server_prefs
631 //  AFS_PRE_SHUTDOWN=
632 // -------------------------------------------------------------------------------
633 -(int) writeNewAfsdOption:(NSString*)filePath
634 {
635         NSMutableString *newConfFileContent = [[[NSMutableString alloc] init] autorelease];
636         
637         //Verbose
638         [newConfFileContent appendString:AFSD_CONF_VERBOSE]; [newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
639         
640         //AFSD Option
641         [newConfFileContent appendString:AFSD_CONF_OPTION];[newConfFileContent appendString:@"=\""]; [newConfFileContent appendString:[self makeChaceParamString]]; [newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"]; 
642         
643         //AFS_SYSNAME
644         [newConfFileContent appendString:AFSD_CONF_SYSNAME];[newConfFileContent appendString:@"=\""];[newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"];
645         
646         //AFS_POST_INIT
647         [newConfFileContent appendString:AFSD_CONF_POST_INI];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"]; 
648         
649         //AFS_PRE_SHUTDOWN
650         [newConfFileContent appendString:AFSD_CONF_PRE_SHUTDOWN];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
651         
652         //add content on file
653         [newConfFileContent appendString:@"\n"];
654         
655         //Write to file
656         [newConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
657                                                  atomically: YES 
658                                                    encoding: NSASCIIStringEncoding 
659                                                           error: nil];
660         return noErr;
661 }
662
663 // -------------------------------------------------------------------------------
664 //  getAfsVersion:
665 // -------------------------------------------------------------------------------
666 -(NSString*) getAfsVersion
667 {
668         NSString *tmpString = nil;
669         NSString *result = [TaskUtil executeTaskSearchingPath:@"fs" args:[NSArray arrayWithObjects:@"-version", nil]];
670         if (!result) return nil;
671         NSCharacterSet *endVersionCS = [NSCharacterSet characterSetWithCharactersInString:@"qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREWQ"];
672         NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" "];
673         NSScanner *versionS = [NSScanner  scannerWithString:result];
674         //go to  start of version
675         [versionS scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
676         
677         //get the total version string
678         [versionS scanUpToCharactersFromSet:endVersionCS intoString:&tmpString];
679
680         return tmpString;
681 }
682
683 // -------------------------------------------------------------------------------
684 //  getAfsMajorVersionVersion:
685 // -------------------------------------------------------------------------------
686 -(int) getAfsMajorVersionVersion
687 {
688         NSString *tmpString = nil;
689         NSString *totalVersion = [self getAfsVersion];
690         if (!totalVersion) return 0;
691         NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
692         NSScanner *versionMS = [NSScanner  scannerWithString:totalVersion];
693         [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
694         return [tmpString intValue];
695 }
696
697 // -------------------------------------------------------------------------------
698 //  getAfsMinorVersionVersion:
699 // -------------------------------------------------------------------------------
700 -(int) getAfsMinorVersionVersion
701 {
702         NSString *tmpString = nil;
703         NSString *totalVersion = [self getAfsVersion];
704         if (!totalVersion) return 0;
705         NSCharacterSet *numCS = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
706         NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
707         NSScanner *versionMS = [NSScanner  scannerWithString:totalVersion];
708         [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];[versionMS scanUpToCharactersFromSet:numCS intoString:&tmpString];[versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
709         return [tmpString intValue];
710 }
711
712 // -------------------------------------------------------------------------------
713 //  getAfsPatchVersionVersion:
714 // -------------------------------------------------------------------------------
715 -(int) getAfsPatchVersionVersion
716 {
717         NSString *totalVersion = [self getAfsVersion];
718         if (!totalVersion) return 0;
719         NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
720         int lastPointIndex = [totalVersion rangeOfCharacterFromSet:pointCS 
721                                                                                                            options:NSBackwardsSearch].location;
722         int patchVersion = [[totalVersion substringFromIndex:lastPointIndex+1] intValue];
723         return patchVersion;
724 }
725
726
727 // -------------------------------------------------------------------------------
728 //  clearConfiguration:
729 // -------------------------------------------------------------------------------
730 - (void) clearConfiguration{
731         
732         // clear list of cell
733         [cellList removeAllObjects];
734         
735         userDefaultCellArray= nil;
736         
737         // remove the old cell name
738         if(cellName) {
739                 [cellName release];
740                 cellName = nil;
741         }
742 }
743
744 // -------------------------------------------------------------------------------
745 //  getCellList:
746 // -------------------------------------------------------------------------------
747 -(NSMutableArray*) getCellList
748 {
749         return cellList;
750 }
751
752
753
754 // -------------------------------------------------------------------------------
755 //  getAllCellName:
756 // -------------------------------------------------------------------------------
757 -(NSArray*) getAllCellsName {
758         NSMutableArray *allCelListName = [[[NSMutableArray alloc] init] autorelease];
759         for(int idx = 0; idx < [cellList count]; idx++){
760                 DBCellElement *elemnt = [cellList objectAtIndex:idx];
761                 [allCelListName addObject:[elemnt getCellName]];
762         }
763         return allCelListName;
764 }
765
766 // -------------------------------------------------------------------------------
767 //  getCellList:
768 // -------------------------------------------------------------------------------
769 -(NSArray*) getUserDefaultForTokenCells {
770         return userDefaultCellArray;
771 }
772
773 // -------------------------------------------------------------------------------
774 //  getDefaultCellName:
775 // -------------------------------------------------------------------------------
776 -(NSArray*) getDefaultForTokenCellsName {
777         NSMutableArray *allCelListName = [[[NSMutableArray alloc] init] autorelease];
778         for(int idx = 0; idx < [cellList count]; idx++){
779                 DBCellElement *elemnt = [cellList objectAtIndex:idx];
780                 if([elemnt userDefaultForToken]) [allCelListName addObject:[[elemnt getCellName] retain]];
781         }
782         return allCelListName;
783 }
784
785 // -------------------------------------------------------------------------------
786 //  getCellName:
787 // -------------------------------------------------------------------------------
788 -(NSString*) getDefaultCellName
789 {
790         return cellName;
791 }
792
793 // -------------------------------------------------------------------------------
794 //  setDefaultCellByName:
795 // -------------------------------------------------------------------------------
796 -(void) setDefaultCellByName:(NSString*)name
797 {
798         DBCellElement *elementCell = nil;
799         BOOL cellFound = false;
800         if(!name) return;
801         
802         for(int idx = 0; idx < [cellList count]; idx++) {
803                 // check every cell for delete as old user default cell or selected as neww cell
804                 elementCell = [cellList objectAtIndex:idx];
805                 cellFound = [name  compare:[elementCell getCellName]] == NSOrderedSame;
806                 [elementCell setUserDefaultCell:cellFound];
807                 if(cellFound) {
808                         [elementCell setUserDefaultForToken:YES];
809                         if(cellName)[cellName release];
810                         cellName = [name retain];
811                  }
812         }
813         
814 }
815
816 // -------------------------------------------------------------------------------
817 //  setCellName:
818 // -------------------------------------------------------------------------------
819 - (void) setCellName:(NSString*)cell {
820         [self setDefaultCellByName:cell];
821 }
822
823 // -------------------------------------------------------------------------------
824 //  readCellInfo:
825 // -------------------------------------------------------------------------------
826 -(void) readCellInfo:(NSString*) configFile {
827         NSError *error = nil;
828         NSString *tmpStr = nil;
829         NSString * result = [NSString stringWithContentsOfFile:configFile
830                                                                                                   encoding:NSASCIIStringEncoding
831                                                                                                          error:&error];
832         if(!result && error){
833                 if([error domain] )
834                 @throw [NSException exceptionWithName:@"readCellInfo" 
835                                                                            reason:kConfFileNotExits
836                                                                          userInfo:nil];
837         } else if (!result)
838             return;
839         NSScanner *scanner = [NSScanner scannerWithString:result];
840         [scanner scanUpToString:@"\n" 
841                                  intoString:&tmpStr];
842         
843         // make a copy of self created string
844         cellName = [tmpStr retain];
845 }
846
847 // -------------------------------------------------------------------------------
848 //  readCellDB:
849 // -------------------------------------------------------------------------------
850 -(void) readCellDB:(NSString*) configFile {     
851         NSString *tmpString = nil;
852         BOOL isInCellDefaultArray = NO; // the cell belong
853         BOOL isDefaultCell = NO;
854         DBCellElement *afsCellDBElement = nil;
855         NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
856         NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
857         
858         NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
859         if (!fileH) return;
860         NSData *dbCellData = [fileH readDataToEndOfFile];
861         NSString *strData = [[NSString alloc] initWithData:dbCellData
862                                                                                           encoding:NSASCIIStringEncoding];
863         NSScanner *cellDBScanner = [NSScanner  scannerWithString:strData];
864         
865         @try{
866         [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
867         while([cellDBScanner isAtEnd] == NO) {
868                 // make new cell
869                 afsCellDBElement = [[DBCellElement alloc] init];
870                 
871                 // get the name of cell
872                 [cellDBScanner scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
873                 [afsCellDBElement setCellName: tmpString];
874                 
875                 //check if this cells is one of user has selected to get token
876                 isInCellDefaultArray = [userDefaultCellArray containsObject:tmpString];
877                 //check if this cell is also the default cell
878                 isDefaultCell = [cellName compare:tmpString]==NSOrderedSame;
879                 
880                 [afsCellDBElement setUserDefaultForToken:isInCellDefaultArray||isDefaultCell];
881                 [afsCellDBElement setUserDefaultCell:isDefaultCell];
882                 
883                 
884         
885                 
886                 
887                 // get the cell comment
888                 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
889                 [cellDBScanner scanUpToCharactersFromSet:returnCS intoString:&tmpString];
890                 [afsCellDBElement setCellComment: tmpString];
891
892                 // get all ip
893                 [cellDBScanner scanUpToString:@">" intoString:&tmpString];
894                 // scann all ip in list
895                 [self scanIpForCell:afsCellDBElement allIP:tmpString];  
896                 
897                 // add cell to list
898                 [cellList addObject:afsCellDBElement];
899                 // release the object becasuse NSMutableArray make a retain on object
900                 [afsCellDBElement release];
901                 //repeat
902                 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
903         }
904         }@catch(NSException *e){
905                 @throw e;
906         }@finally{
907                 //if(strData) [strData release];
908         }
909 }
910
911 // -------------------------------------------------------------------------------
912 //  scanIpForCell:
913 // -------------------------------------------------------------------------------
914 -(void) scanIpForCell:(DBCellElement*) cellElement allIP:(NSString*)allIP {
915     if (!allIP) return;
916         NSScanner *ipScann = [NSScanner scannerWithString:allIP];
917         NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
918         NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
919         NSCharacterSet *startCommentCS = [NSCharacterSet characterSetWithCharactersInString:@"#"];
920         NSString *tmpString = nil;
921         while([ipScann isAtEnd] == NO){
922                 CellIp *cellIpDesc = [[CellIp alloc] init];
923                 
924                 //ip string
925                 [ipScann scanUpToCharactersFromSet:spaceCS 
926                                                                 intoString:&tmpString];
927                 
928                 [cellIpDesc setCellIp:tmpString];
929                 //[tmpString release];
930                 
931                 // go to comment
932                 [ipScann scanUpToCharactersFromSet:startCommentCS intoString:nil];
933                 // skip comment symbol
934                 [ipScann scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
935                 // get comment
936                 [ipScann scanUpToCharactersFromSet:returnCS intoString:&tmpString];
937                 [cellIpDesc setCellComment:tmpString];
938                 //[tmpString release];
939                 
940                 [cellElement addIpToCell:cellIpDesc];
941                 // release the object becasuse NSMutableArray make a retain on object
942                 [cellIpDesc release];
943         }
944 }
945
946
947 // -------------------------------------------------------------------------------
948 //  scanIpForCell:
949 // -------------------------------------------------------------------------------
950 -(NSArray*) readTheseCell:(NSString*) configFile {
951         
952         NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
953         NSData *dbCellData = [fileH readDataToEndOfFile];
954         NSString *strData = [[NSString alloc] initWithData:dbCellData
955                                                                                           encoding:NSASCIIStringEncoding];
956         
957         return [strData componentsSeparatedByString:@"\n"];
958 }
959
960 // -------------------------------------------------------------------------------
961 //  -(void) getTokenList
962 // -------------------------------------------------------------------------------
963 -(NSArray*) getTokenList
964 {
965         int line = 0;
966         NSString *tokenLine = nil;
967         //NSString *tmpValue = nil;
968         NSMutableArray *tokenList = [[NSMutableArray alloc] init];
969         NSString *tokensOutput = [TaskUtil executeTaskSearchingPath:@"tokens" args:[NSArray arrayWithObjects:nil]];
970         
971         if (!tokensOutput) return tokenList;
972         // scan the tokens
973         NSScanner *tokenScan = [NSScanner scannerWithString:tokensOutput];
974         NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
975         
976         while([tokenScan isAtEnd] == NO){
977                 line++;
978                 // get the next line
979                 [tokenScan scanUpToCharactersFromSet:returnCS 
980                                                                 intoString:&tokenLine];
981                 
982                 // check for tokens end
983                 if([tokenLine rangeOfString:@"--End of list--"].location != NSNotFound) break;
984                 
985                 
986                 if(line >= 2){
987                         // add enteir row to result
988                         [tokenList addObject:tokenLine];
989                         // create the line scanner for all the row that contains token info
990                 }
991         }
992         //
993         return tokenList;
994 }
995
996 // -------------------------------------------------------------------------------
997 //  +(void) klog:(NSString*)uName uPwd:(NSString*)uPwd
998 // -------------------------------------------------------------------------------
999 -(void) klog:(NSString*)uName uPwd:(NSString*)uPwd cell:(NSString*)theCell
1000 {
1001         if(uName == @"" ||  uPwd == @"") return;
1002         
1003         [TaskUtil executeTaskSearchingPath:@"klog" 
1004                                                                   args:(theCell==nil?[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, nil]:[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, @"-c", theCell, nil])];
1005         
1006         
1007 }
1008
1009
1010 // -------------------------------------------------------------------------------
1011 //  +(void) aklog
1012 // -------------------------------------------------------------------------------
1013 -(void) aklog:(NSString*)theCell noKerberosCall:(BOOL)krb5CallEnable {
1014         KLStatus                kstatus = noErr;
1015         @try {
1016                 // trying to ket kerberos ticket
1017                 if(krb5CallEnable) {
1018                         kstatus = [Krb5Util getNewTicketIfNotPresent];
1019                 } else kstatus = klNoErr;
1020                 
1021                 //ok to launch aklog
1022                 if(kstatus == klNoErr) [TaskUtil executeTaskSearchingPath:@"aklog" 
1023                                                                                                                          args:(theCell==nil?[NSArray arrayWithObjects:nil]:[NSArray arrayWithObjects:@"-c", theCell, nil])];
1024                 
1025         }
1026         @catch (NSException * e) {
1027                 @throw e;
1028         }
1029         @finally {
1030
1031         }
1032                 
1033 }
1034
1035
1036 // -------------------------------------------------------------------------------
1037 //  getTokens:
1038 // -------------------------------------------------------------------------------
1039 - (void) getTokens:(BOOL)klogAklogFlag usr:(NSString*)usr pwd:(NSString*)pwd {
1040         
1041         NSString *celStrName = nil;
1042         NSArray *tmpDefaultArray = [self getDefaultForTokenCellsName];
1043         if(tmpDefaultArray && [tmpDefaultArray count] > 1) {
1044                 //there are other cell to autenticate
1045                 for(int idx=0; idx < [tmpDefaultArray count]; idx++){
1046                         celStrName = [tmpDefaultArray objectAtIndex:idx];
1047                         if(klogAklogFlag)
1048                                 [self klog:usr 
1049                                           uPwd:pwd  
1050                                           cell:celStrName];
1051                         else
1052                                 [self aklog:celStrName noKerberosCall:YES];     
1053                 }
1054                 
1055         } else {
1056                 //there is only default cell to autenticate
1057                 if(klogAklogFlag)
1058                         [self klog:usr 
1059                                   uPwd:pwd 
1060                                   cell:nil];
1061                 else
1062                         [self aklog:nil noKerberosCall:YES];
1063         }
1064         
1065 }
1066
1067 // -------------------------------------------------------------------------------
1068 //  +(void) unlog
1069 // -------------------------------------------------------------------------------
1070 -(void) unlog:(NSString*)cell
1071 {
1072         [TaskUtil executeTaskSearchingPath:@"unlog" 
1073                                                                   args:(cell?[NSArray arrayWithObjects:@"-c",cell,nil]:[NSArray arrayWithObjects:nil])];
1074 }
1075
1076 // -------------------------------------------------------------------------------
1077 //  -(void) shutdown
1078 // -------------------------------------------------------------------------------
1079 -(void) shutdown
1080 {
1081         @try {
1082                 const char *stopArgs[] = {"stop", 0L};
1083                 if([[AuthUtil shared] autorize] == noErr) {
1084                         [[AuthUtil shared] execUnixCommand:AFS_DAEMON_STARTUPSCRIPT
1085                                                                                   args:stopArgs
1086                                                                                 output:nil];
1087                 }
1088
1089         }
1090         @catch (NSException * e) {
1091                 @throw e;
1092         }
1093         @finally {
1094         
1095         }
1096         
1097         
1098 }
1099
1100
1101 // -------------------------------------------------------------------------------
1102 //  -(void) shutdown
1103 // -------------------------------------------------------------------------------
1104 -(void) startup
1105 {
1106         @try {
1107                 const char *startArgs[] = {"start", 0L};
1108                 if([[AuthUtil shared] autorize] == noErr) {
1109                         [[AuthUtil shared] execUnixCommand:AFS_DAEMON_STARTUPSCRIPT
1110                                                                                   args:startArgs
1111                                                                                 output:nil];
1112                 }
1113
1114         }
1115         @catch (NSException * e) {
1116                 @throw e;
1117         }
1118         @finally {
1119
1120         }
1121
1122
1123 }
1124 // -------------------------------------------------------------------------------
1125 //  -(void) saveConfigurationFiles
1126 // -------------------------------------------------------------------------------
1127 -(void) saveConfigurationFiles:(BOOL) makeBackup
1128 {
1129         NSError *err;
1130         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1131         NSMutableString *cellServDBString = [[NSMutableString alloc] initWithCapacity:256];
1132         NSMutableString *theseCellString = [[NSMutableString alloc] initWithCapacity:256];
1133         DBCellElement *cellElement = nil;
1134         
1135         // save the configuration file
1136         @try{
1137                 [self exceptionOnInvalidPath];
1138
1139                 // ThisCell
1140                 [filePath setString: @"/tmp/ThisCell"];
1141                 [cellName writeToFile: [filePath stringByExpandingTildeInPath]
1142                                    atomically:YES 
1143                                          encoding: NSASCIIStringEncoding 
1144                                                 error:nil];
1145                 // CellServDB
1146                 
1147                 for(int idx = 0; idx < [cellList count]; idx++){
1148                         cellElement = [cellList objectAtIndex:idx];
1149                         [cellServDBString appendString:[cellElement description]];
1150                         if([cellElement userDefaultForToken]) {
1151                                 [theseCellString  appendString:[cellElement getCellName]];
1152                                 [theseCellString  appendString:@"\n"];
1153                         }
1154                 }
1155                 
1156                 
1157                 [filePath setString: @"/tmp/CellServDB"];
1158                 [cellServDBString writeToFile: [filePath stringByExpandingTildeInPath]
1159                                                    atomically:YES 
1160                                                          encoding:  NSUTF8StringEncoding 
1161                                                                 error:&err];
1162                 
1163                 [filePath setString: @"/tmp/TheseCells"];
1164                 [theseCellString writeToFile: [filePath stringByExpandingTildeInPath]
1165                                                    atomically:YES 
1166                                                          encoding:  NSUTF8StringEncoding 
1167                                                                 error:&err];
1168                 
1169                 // backup original file
1170                 if([futil startAutorization] != noErr){
1171                         @throw [NSException exceptionWithName:@"saveConfigurationFiles:startAutorization" 
1172                                                                                    reason:kUserNotAuth
1173                                                                                  userInfo:nil];
1174                 }
1175
1176                 if(makeBackup) [self backupConfigurationFiles];
1177
1178                 // install ThisCell
1179                 [filePath setString: installationPath];
1180                 [filePath appendString: @"/etc/ThisCell"];
1181                 [self installConfigurationFile:@"/tmp/ThisCell" 
1182                                                           destPath:filePath];           
1183                 
1184                 // install CellServDB
1185                 [filePath setString: installationPath];
1186                 [filePath appendString: @"/etc/CellServDB"];
1187                 [self installConfigurationFile:@"/tmp/CellServDB" 
1188                                                           destPath:filePath];
1189                 
1190                 // install CellServDB
1191                 [filePath setString: installationPath];
1192                 [filePath appendString: @"/etc/TheseCells"];
1193                 [self installConfigurationFile:@"/tmp/TheseCells" 
1194                                                           destPath:filePath];
1195                 
1196         } @catch (NSException *e) {
1197                 @throw e;
1198         }@finally {
1199                 // dispose all variable used
1200                 if(filePath) [filePath release];
1201                 if(cellServDBString) [cellServDBString release];
1202         }
1203         
1204 }
1205
1206 // -------------------------------------------------------------------------------
1207 //  -(void) saveCacheConfigurationFiles
1208 // -------------------------------------------------------------------------------
1209 -(void) saveCacheConfigurationFiles:(BOOL)makeBackup
1210 {
1211         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];     
1212         // save the configuration file
1213         @try{
1214                 [self exceptionOnInvalidPath];
1215                 
1216                 // cacheinfo file creation
1217                 [self writeCacheInfo:@"/tmp/cacheinfo"];
1218                 
1219                 //afsd.option or afs.conf file creation
1220                 [self writeAfsdOption:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE];
1221                 
1222                 // backup original file
1223                 if([futil startAutorization] != noErr){
1224                         @throw [NSException exceptionWithName:@"AFSPropertyManager:saveCacheConfigurationFiles:startAutorization" 
1225                                                                                    reason:kUserNotAuth
1226                                                                                  userInfo:nil];
1227                 }
1228                 
1229                 if(makeBackup) {
1230                         //cacheinfo
1231                         [self backupFile:@"/etc/cacheinfo"];
1232                         
1233                         //afsd.options
1234                         [self backupFile:useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE]; 
1235                 }
1236                 
1237                 // install cacheinfo
1238                 [filePath setString:installationPath];
1239                 [filePath appendString: @"/etc/cacheinfo"];
1240                 [self installConfigurationFile:@"/tmp/cacheinfo" 
1241                                                           destPath:filePath];           
1242                 
1243                 // install afsd.conf or afs.conf configuration file 
1244                 [filePath setString: installationPath];
1245                 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
1246                 [self installConfigurationFile:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE
1247                                                           destPath:filePath];
1248                 
1249         } @catch (NSException *e) {
1250                 @throw e;
1251         }@finally {
1252                 if(filePath) [filePath release];
1253         }
1254         
1255         
1256 }
1257
1258 // -------------------------------------------------------------------------------
1259 //  -(void) installConfigurationFile
1260 // -------------------------------------------------------------------------------
1261 -(void) installConfigurationFile:(NSString*)srcConfFile 
1262                                                 destPath:(NSString*)destPath
1263 {
1264         // delete the file original file
1265
1266         if([futil autorizedDelete:destPath] != noErr){
1267                 @throw [NSException exceptionWithName:@"installConfigurationFile:autorizedDelete" 
1268                                                                            reason:destPath
1269                                                                          userInfo:nil];
1270         }
1271         
1272         // move the file
1273         if([futil autorizedMoveFile:srcConfFile
1274                                                  toPath:destPath] != noErr) {
1275                 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedMoveFile" 
1276                                                                            reason:srcConfFile
1277                                                                          userInfo:nil];
1278         }
1279         
1280         
1281         if([futil autorizedChown:destPath 
1282                                            owner:@"root"
1283                                            group:@"wheel"]!= noErr) {
1284                 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedChown" 
1285                                                                            reason:destPath
1286                                                                          userInfo:nil];
1287         }
1288 }
1289
1290 // -------------------------------------------------------------------------------
1291 //  -(void) backupConfigurationFiles
1292 // -------------------------------------------------------------------------------
1293 -(void) backupConfigurationFiles
1294 {       
1295
1296         @try{
1297                 if([futil startAutorization] != noErr){
1298                         @throw [NSException exceptionWithName:@"backupConfigurationFiles:startAutorization" 
1299                                                                                    reason:kUserNotAuth
1300                                                                                  userInfo:nil];
1301                 }
1302                 //This cell
1303                 [self backupFile:@"/etc/ThisCell"];
1304         
1305                 //CellServDB
1306                 [self backupFile:@"/etc/CellServDB"];
1307                 
1308                 //TheseCell
1309                 [self backupFile:@"/etc/TheseCells"];
1310                 
1311                 //[futil endAutorization];
1312         } @catch (NSException *e) {
1313                 @throw e;
1314         } @finally {
1315         }
1316 }
1317
1318 // -------------------------------------------------------------------------------
1319 //  -(void) backupFile:(NSString*)localAfsFilePath
1320 // -------------------------------------------------------------------------------
1321 -(void) backupFile:(NSString*)localAfsFilePath
1322 {
1323         NSString *srcString = nil;
1324         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1325         OSStatus err = noErr;
1326         @try{
1327                 [filePath setString: installationPath];
1328                 [filePath appendString: localAfsFilePath];
1329                 
1330                 //Check if the file at path exist
1331                 NSFileManager *fileManager = [NSFileManager defaultManager];
1332                 
1333                 //check if th efile exist
1334                 if(![fileManager fileExistsAtPath:[filePath stringByExpandingTildeInPath]]) return;
1335                 
1336                 // store the source path
1337                 srcString  = [filePath stringByExpandingTildeInPath];
1338                 [filePath appendString: @".afscommander_bk"];
1339                 
1340                 // check for presence of bk file
1341                 if(![[NSFileManager defaultManager] fileExistsAtPath:[filePath stringByExpandingTildeInPath]]){
1342                         // backup the file
1343                         err = [futil autorizedCopy:srcString 
1344                                                   toPath:[filePath stringByExpandingTildeInPath]];
1345                 }
1346         } @catch (NSException *e) {
1347                 @throw e;
1348         } @finally {
1349                 if(filePath) [filePath release];
1350         }
1351 }
1352
1353 // -------------------------------------------------------------------------------
1354 //  checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1355 // -------------------------------------------------------------------------------
1356 -(BOOL) checkAfsStatus
1357 {
1358         BOOL result = NO;
1359         NSString *dfResult = [TaskUtil executeTaskSearchingPath:@"/bin/df" args:[NSArray arrayWithObjects:nil]];
1360         result = (dfResult?([dfResult rangeOfString:@AFS_FS_MOUNT].location != NSNotFound):NO);
1361         return result;  
1362 }
1363
1364 // -------------------------------------------------------------------------------
1365 //  checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1366 // -------------------------------------------------------------------------------
1367 -(BOOL) checkAfsStatusForStartup {
1368         BOOL result = NO;
1369                 //NSString *fsResult = [TaskUtil executeTaskSearchingPath:@"launchctl" args:[NSArray arrayWithObjects: @"list", nil]];
1370                 //result = (fsResult?([fsResult rangeOfString:@AFS_LAUNCHCTL_GREP_STR].location != NSNotFound):NO);
1371         return result;
1372 }
1373
1374 // -------------------------------------------------------------------------------
1375 //  makeChaceParamString
1376 // -------------------------------------------------------------------------------
1377 -(NSString*) makeChaceParamString
1378 {
1379         NSNumber *tmpNum = nil;
1380         NSMutableString *afsdOption = [[NSMutableString alloc] init];
1381         if(!afsdOption) return @"";
1382         //write the data for afsd config file '-afsdb -stat x -dcache x -daemons x -volumes x -dynroot -fakestat-all'
1383         //afsdb
1384         //dynRoot
1385         if([self afsDB]) {
1386                 [afsdOption appendString:@AFSD_OPTION_AFSDB_KEY];[afsdOption appendString:@" "];
1387         }
1388         
1389         //Verbose
1390         if([self verbose]) {
1391                 [afsdOption appendString:@AFSD_OPTION_VERBOSE_KEY];[afsdOption appendString:@" "];
1392         }
1393         
1394         //stat entry
1395         tmpNum = [NSNumber numberWithInt:[self statCacheEntry]];
1396         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_STAT_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1397         
1398         //dcace
1399         tmpNum = [NSNumber numberWithInt:[self dCacheDim]];
1400         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_DCACHE_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1401         
1402         //daemons
1403         tmpNum = [NSNumber numberWithInt:[self daemonNumber]];
1404         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_DAEMONS_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1405         
1406         //volumes
1407         tmpNum = [NSNumber numberWithInt:[self nVolEntry]];
1408         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_VOLUMES_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1409         
1410         //dynRoot
1411         if([self dynRoot]) {
1412                 [afsdOption appendString:@AFSD_OPTION_DYNROOT_KEY];[afsdOption appendString:@" "];
1413         }
1414         
1415         //fakestat-all
1416         [afsdOption appendString:@AFSD_OPTION_FKESTAT_ALL];[afsdOption appendString:@" "];
1417         
1418         return [afsdOption autorelease];
1419 }
1420
1421 @end
1422