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