2 // AFSPropertyManager.m
5 // Created by Claudio Bisegni on 21/05/07.
6 // Copyright 2007 INFN - National Institute of Nuclear Physics. All rights reserved.
10 #import "AFSPropertyManager.h"
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")
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"
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"
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"
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"
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
53 @implementation AFSPropertyManager
56 // -------------------------------------------------------------------------------
58 // -------------------------------------------------------------------------------
62 cellList = [[NSMutableArray alloc] init];
64 userDefaultCellArray = nil;
65 useAfsdConfVersion = NO;
69 futil = [[FileUtil alloc] init];
73 // -------------------------------------------------------------------------------
75 // -------------------------------------------------------------------------------
76 - (id)initWithAfsPath:(NSString*)path
78 // avvio la creazione standard dell'oggetto
86 // -------------------------------------------------------------------------------
88 // -------------------------------------------------------------------------------
91 if(installationPath){ [installationPath release];}
92 if(cellList) {[cellList removeAllObjects];[cellList release];}
93 if(cellName) {[cellName release];}
95 [futil endAutorization];
102 // -------------------------------------------------------------------------------
104 // -------------------------------------------------------------------------------
105 - (void) setPath:(NSString*)path {
106 if(installationPath)[installationPath release];
109 installationPath = [path retain];
112 // -------------------------------------------------------------------------------
114 // -------------------------------------------------------------------------------
116 return installationPath;
119 // -------------------------------------------------------------------------------
121 // -------------------------------------------------------------------------------
122 -(int) statCacheEntry
124 return statCacheEntry;
127 // -------------------------------------------------------------------------------
128 // setStatCacheEntry:
129 // -------------------------------------------------------------------------------
130 -(void) setStatCacheEntry:(int)statEntry
132 statCacheEntry = statEntry;
136 // -------------------------------------------------------------------------------
138 // -------------------------------------------------------------------------------
144 // -------------------------------------------------------------------------------
146 // -------------------------------------------------------------------------------
147 -(void) setDCacheDim:(int)dcDim
152 // -------------------------------------------------------------------------------
154 // -------------------------------------------------------------------------------
155 -(int) cacheDimension
157 return cacheDimension;
160 // -------------------------------------------------------------------------------
161 // setCacheDimension:
162 // -------------------------------------------------------------------------------
163 -(void) setCacheDimension:(int)cacheDim
165 cacheDimension = cacheDim;
168 // -------------------------------------------------------------------------------
170 // -------------------------------------------------------------------------------
176 // -------------------------------------------------------------------------------
178 // -------------------------------------------------------------------------------
179 -(void) setDaemonNumber:(int)dNumber
181 daemonNumber = dNumber;
184 // -------------------------------------------------------------------------------
185 // afsRootMountPoint:
186 // -------------------------------------------------------------------------------
187 -(NSString*) afsRootMountPoint
189 return afsRootMountPoint;
192 // -------------------------------------------------------------------------------
193 // setAfsRootMountPoint:
194 // -------------------------------------------------------------------------------
195 -(void) setAfsRootMountPoint:(NSString*)mountPoint
197 if(afsRootMountPoint)[afsRootMountPoint release];
200 afsRootMountPoint = [mountPoint retain];
203 // -------------------------------------------------------------------------------
205 // -------------------------------------------------------------------------------
211 // -------------------------------------------------------------------------------
213 // -------------------------------------------------------------------------------
214 -(void) setNVolEntry:(int)entry
219 // -------------------------------------------------------------------------------
221 // -------------------------------------------------------------------------------
227 // -------------------------------------------------------------------------------
229 // -------------------------------------------------------------------------------
230 -(void) setDynRoot:(bool)flag
235 // -------------------------------------------------------------------------------
237 // -------------------------------------------------------------------------------
243 // -------------------------------------------------------------------------------
245 // -------------------------------------------------------------------------------
246 -(void) setAfsDB:(bool)flag
251 // -------------------------------------------------------------------------------
253 // -------------------------------------------------------------------------------
259 // -------------------------------------------------------------------------------
261 // -------------------------------------------------------------------------------
262 -(void) setVerbose:(bool)flag
267 // -------------------------------------------------------------------------------
268 // useAfsdConfVersion:
269 // -------------------------------------------------------------------------------
270 -(BOOL) useAfsdConfConfigFile
272 return useAfsdConfVersion;
275 // -------------------------------------------------------------------------------
276 // exceptionOnInvalidPath:
277 // -------------------------------------------------------------------------------
278 - (void) exceptionOnInvalidPath
280 if(!installationPath || ([installationPath length] == 0)) {
281 @throw [NSException exceptionWithName:@"AFSPropertyManager:exceptionOnInvalidPath"
285 if(![[NSFileManager defaultManager] fileExistsAtPath:installationPath]){
286 @throw [NSException exceptionWithName:@"AFSPropertyManager:exceptionOnInvalidPath"
287 reason:kPathDontContainAfsInstallation
293 // -------------------------------------------------------------------------------
294 // loadConfiguration:
295 // -------------------------------------------------------------------------------
296 - (void) loadConfiguration {
297 int mjVersion = [self getAfsMajorVersionVersion];
298 int miVersion = [self getAfsMinorVersionVersion];
299 int paVersion = [self getAfsPatchVersionVersion];
301 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
304 [self exceptionOnInvalidPath];
305 [self clearConfiguration];
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);
311 // read thiscell config file
312 [filePath setString:installationPath];
313 [filePath appendString: @"/etc/ThisCell"];
315 [self readCellInfo:filePath];
317 @throw [NSException exceptionWithName:@"readCellInfo"
318 reason:kThisCellFOError
321 //read TheseCell file
322 [filePath setString: installationPath];
323 [filePath appendString: @"/etc/TheseCells"];
324 userDefaultCellArray = [self readTheseCell:filePath];
327 [filePath setString: installationPath];
328 [filePath appendString: @"/etc/CellServDB"];
329 [self readCellDB:filePath];
332 [filePath setString: installationPath];
333 [filePath appendString: @"/etc/cacheinfo"];
334 [self readCacheInfo:filePath];
336 //Read config/afsd.options
337 [filePath setString: installationPath];
338 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
339 [self readAfsdOption:filePath];
340 } @catch(NSException * e){
347 // -------------------------------------------------------------------------------
349 // file template "/afs:/var/db/openafs/cache:30000"
350 // -------------------------------------------------------------------------------
351 -(void) readCacheInfo:(NSString*)filePath
354 NSString *tmpString = nil;
355 NSCharacterSet *fullStopCS = [NSCharacterSet characterSetWithCharactersInString:@":"];
356 NSMutableCharacterSet *chunkStartCS = [[NSMutableCharacterSet alloc] init];
357 [chunkStartCS formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
358 [chunkStartCS formUnionWithCharacterSet:[NSMutableCharacterSet characterSetWithCharactersInString:@"/"]];
360 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
361 NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:filePath];
362 NSData *fileHData = [fileH readDataToEndOfFile];
363 NSString *cacheInfoStrData = [[NSString alloc] initWithData:fileHData
364 encoding:NSASCIIStringEncoding];
365 NSScanner *cacheInfoS = [NSScanner scannerWithString:cacheInfoStrData];
373 [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
374 [self setAfsRootMountPoint:tmpString];
375 [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
379 //cache path default '/var/db/openafs/cache'
380 [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
381 [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
386 [cacheInfoS scanUpToCharactersFromSet:returnCS intoString:&tmpString];
387 [self setCacheDimension:[tmpString intValue]];
390 }while(cicle < 3 && ![cacheInfoS isAtEnd]);
391 }@catch(NSException *e){
394 //if(cacheInfoStrData) [cacheInfoStrData release];
395 if(chunkStartCS) [chunkStartCS release];
401 // -------------------------------------------------------------------------------
403 // -------------------------------------------------------------------------------
404 -(int) writeCacheInfo:(NSString*)filePath
406 NSNumber *tmpNum = nil;
407 NSMutableString *cacheInfoContent = [[NSMutableString alloc] init];
408 if(!cacheInfoContent) return -1;
410 //Afs root mount point
411 if([[self afsRootMountPoint] rangeOfString:@"/"].location == NSNotFound || [[self afsRootMountPoint] length] == 0)
412 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"
413 reason:@"Bad afs path"
415 [cacheInfoContent appendString:[self afsRootMountPoint]];
417 //standard cache path
419 [cacheInfoContent appendString:@":"]; [cacheInfoContent appendString:@AFS_CACHE_PATH]; [cacheInfoContent appendString:@":"];
422 tmpNum = [NSNumber numberWithInt:[self cacheDimension]];
423 if([tmpNum intValue] == 0)
424 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"
425 reason:@"Bad cache dimension"
427 [cacheInfoContent appendString:[tmpNum stringValue]];
429 [cacheInfoContent writeToFile: [filePath stringByExpandingTildeInPath]
431 encoding: NSASCIIStringEncoding
434 [cacheInfoContent release];
438 // -------------------------------------------------------------------------------
440 // file template "/afs:/var/db/openafs/cache:30000"
441 // -------------------------------------------------------------------------------
442 -(void) readAfsdOption:(NSString*)filePath
445 if(useAfsdConfVersion) {
446 [self readNewAfsdOption:filePath];
448 [self readOldAfsdOption:filePath];
451 }@catch(NSException *e){
459 // -------------------------------------------------------------------------------
460 // readOldAfsdOption:
461 // -------------------------------------------------------------------------------
462 -(void) readOldAfsdOption:(NSString*)filePath
465 /*NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:filePath];
466 NSData *fileHData = [fileH readDataToEndOfFile];
467 NSString *afsdOptionStrData = [[NSString alloc] initWithData:fileHData
468 encoding:NSASCIIStringEncoding];*/
469 if(!filePath) return;
470 [self readAFSDParamLineContent:[[NSString stringWithContentsOfFile:filePath
471 encoding:NSUTF8StringEncoding
472 error:nil] stringByStandardizingPath]];
475 // -------------------------------------------------------------------------------
476 // readAFSDParamLineContent:
477 // -------------------------------------------------------------------------------
478 -(void) readAFSDParamLineContent:(NSString*) paramLine{
479 NSString *tmpString = nil;
480 NSCharacterSet *space = [NSCharacterSet characterSetWithCharactersInString:@" "];
481 NSScanner *afsdOptionS = [NSScanner scannerWithString:paramLine];
484 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
485 if(!tmpString) continue;
486 //check parameter type
487 if([tmpString isEqualToString:@AFSD_OPTION_DAEMONS_KEY])
489 // get number of daemon
490 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
491 [self setDaemonNumber:[tmpString intValue]];
493 //check parameter type
494 if([tmpString isEqualToString:@AFSD_OPTION_DCACHE_KEY])
496 //get dcache dimension
497 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
498 [self setDCacheDim:[tmpString intValue]];
500 //check parameter type
501 if([tmpString isEqualToString:@AFSD_OPTION_DYNROOT_KEY])
503 [self setDynRoot:true];
505 if([tmpString isEqualToString:@AFSD_OPTION_VERBOSE_KEY])
507 [self setVerbose:true];
508 } else if([tmpString isEqualToString:@AFSD_OPTION_AFSDB_KEY])
510 [self setAfsDB:true];
512 //check parameter type
513 if([tmpString isEqualToString:@AFSD_OPTION_STAT_KEY])
515 // get fstat entry num
516 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
517 [self setStatCacheEntry:[tmpString intValue]];
520 //check parameter type
521 if([tmpString isEqualToString:@AFSD_OPTION_VOLUMES_KEY])
523 // get fstat entry num
524 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
525 [self setNVolEntry:[tmpString intValue]];
530 }while(![afsdOptionS isAtEnd]);
534 // -------------------------------------------------------------------------------
535 // readNewAfsdOption:
536 // -------------------------------------------------------------------------------
537 -(void) readNewAfsdOption:(NSString*)filePath
539 if(!filePath) return;
540 NSString *currentLines = nil;
541 NSString *paramValue = nil;
542 NSScanner *lineScanner = nil;
545 NSString *newAFSDConfContent = [NSString stringWithContentsOfFile:filePath
546 encoding:NSUTF8StringEncoding
550 NSArray *confLines = [newAFSDConfContent componentsSeparatedByString:@"\n"];
552 //Get the lines enumerator
553 NSEnumerator *lineEnumerator = [confLines objectEnumerator];
556 while(currentLines = [lineEnumerator nextObject]) {
557 if([currentLines rangeOfString:@"#"].location != NSNotFound) continue;
559 if([currentLines rangeOfString:AFSD_CONF_OPTION].location != NSNotFound) {
560 lineScanner = [NSScanner scannerWithString:currentLines];
561 if(!lineScanner) continue;
565 [lineScanner scanUpToString:@"\"" intoString:¶mValue];[lineScanner scanUpToString:@"-" intoString:¶mValue];
566 [lineScanner scanUpToString:@"\"" intoString:¶mValue];
568 // read the asfd option param line
569 [self readAFSDParamLineContent:paramValue];
570 } else if([currentLines rangeOfString:AFSD_CONF_SYSNAME].location != NSNotFound) {
572 } else if([currentLines rangeOfString:AFSD_CONF_POST_INI].location != NSNotFound) {
574 } else if([currentLines rangeOfString:AFSD_CONF_PRE_SHUTDOWN].location != NSNotFound) {
581 // -------------------------------------------------------------------------------
583 // -------------------------------------------------------------------------------
584 -(int) writeAfsdOption:(NSString*)filePath
588 if(useAfsdConfVersion) {
589 result = [self writeNewAfsdOption:filePath];
591 result = [self writeOldAfsdOption:filePath];
594 }@catch(NSException *e){
602 // -------------------------------------------------------------------------------
603 // writeOldAfsdOption:
604 // -------------------------------------------------------------------------------
605 -(int) writeOldAfsdOption:(NSString*)filePath;
607 NSMutableString *oldConfFileContent = [[[NSMutableString alloc] init] autorelease];
609 [oldConfFileContent appendString:[self makeChaceParamString]];
611 //add cariage return at end of file
612 [oldConfFileContent appendString:@"\n"];
614 //write content on file
615 [oldConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
617 encoding: NSASCIIStringEncoding
623 // -------------------------------------------------------------------------------
624 // writeNewAfsdOption:
627 // AFS_POST_INIT=afs_server_prefs
629 // -------------------------------------------------------------------------------
630 -(int) writeNewAfsdOption:(NSString*)filePath
632 NSMutableString *newConfFileContent = [[[NSMutableString alloc] init] autorelease];
635 [newConfFileContent appendString:AFSD_CONF_VERBOSE]; [newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
638 [newConfFileContent appendString:AFSD_CONF_OPTION];[newConfFileContent appendString:@"=\""]; [newConfFileContent appendString:[self makeChaceParamString]]; [newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"];
641 [newConfFileContent appendString:AFSD_CONF_SYSNAME];[newConfFileContent appendString:@"=\""];[newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"];
644 [newConfFileContent appendString:AFSD_CONF_POST_INI];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
647 [newConfFileContent appendString:AFSD_CONF_PRE_SHUTDOWN];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
649 //add content on file
650 [newConfFileContent appendString:@"\n"];
653 [newConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
655 encoding: NSASCIIStringEncoding
660 // -------------------------------------------------------------------------------
662 // -------------------------------------------------------------------------------
663 -(NSString*) getAfsVersion
665 NSString *tmpString = nil;
666 NSString *result = [TaskUtil executeTaskSearchingPath:@"fs" args:[NSArray arrayWithObjects:@"-version", nil]];
667 NSCharacterSet *endVersionCS = [NSCharacterSet characterSetWithCharactersInString:@"qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREWQ"];
668 NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" "];
669 NSScanner *versionS = [NSScanner scannerWithString:result];
670 //go to start of version
671 [versionS scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
673 //get the total version string
674 [versionS scanUpToCharactersFromSet:endVersionCS intoString:&tmpString];
679 // -------------------------------------------------------------------------------
680 // getAfsMajorVersionVersion:
681 // -------------------------------------------------------------------------------
682 -(int) getAfsMajorVersionVersion
684 NSString *tmpString = nil;
685 NSString *totalVersion = [self getAfsVersion];
686 NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
687 NSScanner *versionMS = [NSScanner scannerWithString:totalVersion];
688 [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
689 return [tmpString intValue];
692 // -------------------------------------------------------------------------------
693 // getAfsMinorVersionVersion:
694 // -------------------------------------------------------------------------------
695 -(int) getAfsMinorVersionVersion
697 NSString *tmpString = nil;
698 NSString *totalVersion = [self getAfsVersion];
699 NSCharacterSet *numCS = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
700 NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
701 NSScanner *versionMS = [NSScanner scannerWithString:totalVersion];
702 [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];[versionMS scanUpToCharactersFromSet:numCS intoString:&tmpString];[versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
703 return [tmpString intValue];
706 // -------------------------------------------------------------------------------
707 // getAfsPatchVersionVersion:
708 // -------------------------------------------------------------------------------
709 -(int) getAfsPatchVersionVersion
711 NSString *totalVersion = [self getAfsVersion];
712 NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
713 int lastPointIndex = [totalVersion rangeOfCharacterFromSet:pointCS
714 options:NSBackwardsSearch].location;
715 int patchVersion = [[totalVersion substringFromIndex:lastPointIndex+1] intValue];
720 // -------------------------------------------------------------------------------
721 // clearConfiguration:
722 // -------------------------------------------------------------------------------
723 - (void) clearConfiguration{
725 // clear list of cell
726 [cellList removeAllObjects];
728 userDefaultCellArray= nil;
730 // remove the old cell name
737 // -------------------------------------------------------------------------------
739 // -------------------------------------------------------------------------------
740 -(NSMutableArray*) getCellList
747 // -------------------------------------------------------------------------------
749 // -------------------------------------------------------------------------------
750 -(NSArray*) getAllCellsName {
751 NSMutableArray *allCelListName = [[[NSMutableArray alloc] init] autorelease];
752 for(int idx = 0; idx < [cellList count]; idx++){
753 DBCellElement *elemnt = [cellList objectAtIndex:idx];
754 [allCelListName addObject:[elemnt getCellName]];
756 return allCelListName;
759 // -------------------------------------------------------------------------------
761 // -------------------------------------------------------------------------------
762 -(NSArray*) getUserDefaultForTokenCells {
763 return userDefaultCellArray;
766 // -------------------------------------------------------------------------------
767 // getDefaultCellName:
768 // -------------------------------------------------------------------------------
769 -(NSArray*) getDefaultForTokenCellsName {
770 NSMutableArray *allCelListName = [[[NSMutableArray alloc] init] autorelease];
771 for(int idx = 0; idx < [cellList count]; idx++){
772 DBCellElement *elemnt = [cellList objectAtIndex:idx];
773 if([elemnt userDefaultForToken]) [allCelListName addObject:[[elemnt getCellName] retain]];
775 return allCelListName;
778 // -------------------------------------------------------------------------------
780 // -------------------------------------------------------------------------------
781 -(NSString*) getDefaultCellName
786 // -------------------------------------------------------------------------------
787 // setDefaultCellByName:
788 // -------------------------------------------------------------------------------
789 -(void) setDefaultCellByName:(NSString*)name
791 DBCellElement *elementCell = nil;
792 BOOL cellFound = false;
795 for(int idx = 0; idx < [cellList count]; idx++) {
796 // check every cell for delete as old user default cell or selected as neww cell
797 elementCell = [cellList objectAtIndex:idx];
798 cellFound = [name compare:[elementCell getCellName]] == NSOrderedSame;
799 [elementCell setUserDefaultCell:cellFound];
801 [elementCell setUserDefaultForToken:YES];
802 if(cellName)[cellName release];
803 cellName = [name retain];
809 // -------------------------------------------------------------------------------
811 // -------------------------------------------------------------------------------
812 - (void) setCellName:(NSString*)cell {
813 [self setDefaultCellByName:cell];
816 // -------------------------------------------------------------------------------
818 // -------------------------------------------------------------------------------
819 -(void) readCellInfo:(NSString*) configFile {
820 NSError *error = nil;
821 NSString *tmpStr = nil;
822 NSString * result = [NSString stringWithContentsOfFile:configFile
823 encoding:NSASCIIStringEncoding
825 if(!result && error){
827 @throw [NSException exceptionWithName:@"readCellInfo"
828 reason:kConfFileNotExits
831 NSScanner *scanner = [NSScanner scannerWithString:result];
832 [scanner scanUpToString:@"\n"
835 // make a copy of self created string
836 cellName = [tmpStr retain];
839 // -------------------------------------------------------------------------------
841 // -------------------------------------------------------------------------------
842 -(void) readCellDB:(NSString*) configFile {
843 NSString *tmpString = nil;
844 BOOL isInCellDefaultArray = NO; // the cell belong
845 BOOL isDefaultCell = NO;
846 DBCellElement *afsCellDBElement = nil;
847 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
848 NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
850 NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
851 NSData *dbCellData = [fileH readDataToEndOfFile];
852 NSString *strData = [[NSString alloc] initWithData:dbCellData
853 encoding:NSASCIIStringEncoding];
854 NSScanner *cellDBScanner = [NSScanner scannerWithString:strData];
857 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
858 while([cellDBScanner isAtEnd] == NO) {
860 afsCellDBElement = [[DBCellElement alloc] init];
862 // get the name of cell
863 [cellDBScanner scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
864 [afsCellDBElement setCellName: tmpString];
866 //check if this cells is one of user has selected to get token
867 isInCellDefaultArray = [userDefaultCellArray containsObject:tmpString];
868 //check if this cell is also the default cell
869 isDefaultCell = [cellName compare:tmpString]==NSOrderedSame;
871 [afsCellDBElement setUserDefaultForToken:isInCellDefaultArray||isDefaultCell];
872 [afsCellDBElement setUserDefaultCell:isDefaultCell];
878 // get the cell comment
879 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
880 [cellDBScanner scanUpToCharactersFromSet:returnCS intoString:&tmpString];
881 [afsCellDBElement setCellComment: tmpString];
884 [cellDBScanner scanUpToString:@">" intoString:&tmpString];
885 // scann all ip in list
886 [self scanIpForCell:afsCellDBElement allIP:tmpString];
889 [cellList addObject:afsCellDBElement];
890 // release the object becasuse NSMutableArray make a retain on object
891 [afsCellDBElement release];
893 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
895 }@catch(NSException *e){
898 //if(strData) [strData release];
902 // -------------------------------------------------------------------------------
904 // -------------------------------------------------------------------------------
905 -(void) scanIpForCell:(DBCellElement*) cellElement allIP:(NSString*)allIP {
906 NSScanner *ipScann = [NSScanner scannerWithString:allIP];
907 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
908 NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
909 NSCharacterSet *startCommentCS = [NSCharacterSet characterSetWithCharactersInString:@"#"];
910 NSString *tmpString = nil;
911 while([ipScann isAtEnd] == NO){
912 CellIp *cellIpDesc = [[CellIp alloc] init];
915 [ipScann scanUpToCharactersFromSet:spaceCS
916 intoString:&tmpString];
918 [cellIpDesc setCellIp:tmpString];
919 //[tmpString release];
922 [ipScann scanUpToCharactersFromSet:startCommentCS intoString:nil];
923 // skip comment symbol
924 [ipScann scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
926 [ipScann scanUpToCharactersFromSet:returnCS intoString:&tmpString];
927 [cellIpDesc setCellComment:tmpString];
928 //[tmpString release];
930 [cellElement addIpToCell:cellIpDesc];
931 // release the object becasuse NSMutableArray make a retain on object
932 [cellIpDesc release];
937 // -------------------------------------------------------------------------------
939 // -------------------------------------------------------------------------------
940 -(NSArray*) readTheseCell:(NSString*) configFile {
942 NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
943 NSData *dbCellData = [fileH readDataToEndOfFile];
944 NSString *strData = [[NSString alloc] initWithData:dbCellData
945 encoding:NSASCIIStringEncoding];
947 return [strData componentsSeparatedByString:@"\n"];
950 // -------------------------------------------------------------------------------
951 // -(void) getTokenList
952 // -------------------------------------------------------------------------------
953 -(NSArray*) getTokenList
956 NSString *tokenLine = nil;
957 //NSString *tmpValue = nil;
958 NSMutableArray *tokenList = [[NSMutableArray alloc] init];
959 NSString *tokensOutput = [TaskUtil executeTaskSearchingPath:@"tokens" args:[NSArray arrayWithObjects:nil]];
962 NSScanner *tokenScan = [NSScanner scannerWithString:tokensOutput];
963 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
965 while([tokenScan isAtEnd] == NO){
968 [tokenScan scanUpToCharactersFromSet:returnCS
969 intoString:&tokenLine];
971 // check for tokens end
972 if([tokenLine rangeOfString:@"--End of list--"].location != NSNotFound) break;
976 // add enteir row to result
977 [tokenList addObject:tokenLine];
978 // create the line scanner for all the row that contains token info
985 // -------------------------------------------------------------------------------
986 // +(void) klog:(NSString*)uName uPwd:(NSString*)uPwd
987 // -------------------------------------------------------------------------------
988 -(void) klog:(NSString*)uName uPwd:(NSString*)uPwd cell:(NSString*)theCell
990 if(uName == @"" || uPwd == @"") return;
992 [TaskUtil executeTaskSearchingPath:@"klog"
993 args:(theCell==nil?[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, nil]:[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, @"-c", theCell, nil])];
999 // -------------------------------------------------------------------------------
1001 // -------------------------------------------------------------------------------
1002 -(void) aklog:(NSString*)theCell noKerberosCall:(BOOL)krb5CallEnable {
1003 KLStatus kstatus = noErr;
1005 // trying to ket kerberos ticket
1006 if(krb5CallEnable) {
1007 kstatus = [Krb5Util getNewTicketIfNotPresent];
1008 } else kstatus = klNoErr;
1010 //ok to launch aklog
1011 if(kstatus == klNoErr) [TaskUtil executeTaskSearchingPath:@"aklog"
1012 args:(theCell==nil?[NSArray arrayWithObjects:nil]:[NSArray arrayWithObjects:@"-c", theCell, nil])];
1015 @catch (NSException * e) {
1025 // -------------------------------------------------------------------------------
1027 // -------------------------------------------------------------------------------
1028 - (void) getTokens:(BOOL)klogAklogFlag usr:(NSString*)usr pwd:(NSString*)pwd {
1030 NSString *celStrName = nil;
1031 NSArray *tmpDefaultArray = [self getDefaultForTokenCellsName];
1032 if(tmpDefaultArray && [tmpDefaultArray count] > 1) {
1033 //there are other cell to autenticate
1034 for(int idx=0; idx < [tmpDefaultArray count]; idx++){
1035 celStrName = [tmpDefaultArray objectAtIndex:idx];
1041 [self aklog:celStrName noKerberosCall:YES];
1045 //there is only default cell to autenticate
1051 [self aklog:nil noKerberosCall:YES];
1056 // -------------------------------------------------------------------------------
1058 // -------------------------------------------------------------------------------
1059 -(void) unlog:(NSString*)cell
1061 [TaskUtil executeTaskSearchingPath:@"unlog"
1062 args:(cell?[NSArray arrayWithObjects:@"-c",cell,nil]:[NSArray arrayWithObjects:nil])];
1065 // -------------------------------------------------------------------------------
1067 // -------------------------------------------------------------------------------
1071 const char *stopArgs[] = {"stop", 0L};
1072 if([[AuthUtil shared] autorize] == noErr) {
1073 [[AuthUtil shared] execUnixCommand:AFS_DAEMON_STARTUPSCRIPT
1079 @catch (NSException * e) {
1090 // -------------------------------------------------------------------------------
1092 // -------------------------------------------------------------------------------
1096 const char *startArgs[] = {"start", 0L};
1097 if([[AuthUtil shared] autorize] == noErr) {
1098 [[AuthUtil shared] execUnixCommand:AFS_DAEMON_STARTUPSCRIPT
1104 @catch (NSException * e) {
1113 // -------------------------------------------------------------------------------
1114 // -(void) saveConfigurationFiles
1115 // -------------------------------------------------------------------------------
1116 -(void) saveConfigurationFiles:(BOOL) makeBackup
1119 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1120 NSMutableString *cellServDBString = [[NSMutableString alloc] initWithCapacity:256];
1121 NSMutableString *theseCellString = [[NSMutableString alloc] initWithCapacity:256];
1122 DBCellElement *cellElement = nil;
1124 // save the configuration file
1126 [self exceptionOnInvalidPath];
1129 [filePath setString: @"/tmp/ThisCell"];
1130 [cellName writeToFile: [filePath stringByExpandingTildeInPath]
1132 encoding: NSASCIIStringEncoding
1136 for(int idx = 0; idx < [cellList count]; idx++){
1137 cellElement = [cellList objectAtIndex:idx];
1138 [cellServDBString appendString:[cellElement description]];
1139 if([cellElement userDefaultForToken]) {
1140 [theseCellString appendString:[cellElement getCellName]];
1141 [theseCellString appendString:@"\n"];
1146 [filePath setString: @"/tmp/CellServDB"];
1147 [cellServDBString writeToFile: [filePath stringByExpandingTildeInPath]
1149 encoding: NSUTF8StringEncoding
1152 [filePath setString: @"/tmp/TheseCells"];
1153 [theseCellString writeToFile: [filePath stringByExpandingTildeInPath]
1155 encoding: NSUTF8StringEncoding
1158 // backup original file
1159 if([futil startAutorization] != noErr){
1160 @throw [NSException exceptionWithName:@"saveConfigurationFiles:startAutorization"
1165 if(makeBackup) [self backupConfigurationFiles];
1168 [filePath setString: installationPath];
1169 [filePath appendString: @"/etc/ThisCell"];
1170 [self installConfigurationFile:@"/tmp/ThisCell"
1173 // install CellServDB
1174 [filePath setString: installationPath];
1175 [filePath appendString: @"/etc/CellServDB"];
1176 [self installConfigurationFile:@"/tmp/CellServDB"
1179 // install CellServDB
1180 [filePath setString: installationPath];
1181 [filePath appendString: @"/etc/TheseCells"];
1182 [self installConfigurationFile:@"/tmp/TheseCells"
1185 } @catch (NSException *e) {
1188 // dispose all variable used
1189 if(filePath) [filePath release];
1190 if(cellServDBString) [cellServDBString release];
1195 // -------------------------------------------------------------------------------
1196 // -(void) saveCacheConfigurationFiles
1197 // -------------------------------------------------------------------------------
1198 -(void) saveCacheConfigurationFiles:(BOOL)makeBackup
1200 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1201 // save the configuration file
1203 [self exceptionOnInvalidPath];
1205 // cacheinfo file creation
1206 [self writeCacheInfo:@"/tmp/cacheinfo"];
1208 //afsd.option or afs.conf file creation
1209 [self writeAfsdOption:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE];
1211 // backup original file
1212 if([futil startAutorization] != noErr){
1213 @throw [NSException exceptionWithName:@"AFSPropertyManager:saveCacheConfigurationFiles:startAutorization"
1220 [self backupFile:@"/etc/cacheinfo"];
1223 [self backupFile:useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
1226 // install cacheinfo
1227 [filePath setString:installationPath];
1228 [filePath appendString: @"/etc/cacheinfo"];
1229 [self installConfigurationFile:@"/tmp/cacheinfo"
1232 // install afsd.conf or afs.conf configuration file
1233 [filePath setString: installationPath];
1234 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
1235 [self installConfigurationFile:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE
1238 } @catch (NSException *e) {
1241 if(filePath) [filePath release];
1247 // -------------------------------------------------------------------------------
1248 // -(void) installConfigurationFile
1249 // -------------------------------------------------------------------------------
1250 -(void) installConfigurationFile:(NSString*)srcConfFile
1251 destPath:(NSString*)destPath
1253 // delete the file original file
1255 if([futil autorizedDelete:destPath] != noErr){
1256 @throw [NSException exceptionWithName:@"installConfigurationFile:autorizedDelete"
1262 if([futil autorizedMoveFile:srcConfFile
1263 toPath:destPath] != noErr) {
1264 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedMoveFile"
1270 if([futil autorizedChown:destPath
1272 group:@"wheel"]!= noErr) {
1273 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedChown"
1279 // -------------------------------------------------------------------------------
1280 // -(void) backupConfigurationFiles
1281 // -------------------------------------------------------------------------------
1282 -(void) backupConfigurationFiles
1286 if([futil startAutorization] != noErr){
1287 @throw [NSException exceptionWithName:@"backupConfigurationFiles:startAutorization"
1292 [self backupFile:@"/etc/ThisCell"];
1295 [self backupFile:@"/etc/CellServDB"];
1298 [self backupFile:@"/etc/TheseCells"];
1300 //[futil endAutorization];
1301 } @catch (NSException *e) {
1307 // -------------------------------------------------------------------------------
1308 // -(void) backupFile:(NSString*)localAfsFilePath
1309 // -------------------------------------------------------------------------------
1310 -(void) backupFile:(NSString*)localAfsFilePath
1312 NSString *srcString = nil;
1313 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1314 OSStatus err = noErr;
1316 [filePath setString: installationPath];
1317 [filePath appendString: localAfsFilePath];
1319 //Check if the file at path exist
1320 NSFileManager *fileManager = [NSFileManager defaultManager];
1322 //check if th efile exist
1323 if(![fileManager fileExistsAtPath:[filePath stringByExpandingTildeInPath]]) return;
1325 // store the source path
1326 srcString = [filePath stringByExpandingTildeInPath];
1327 [filePath appendString: @".afscommander_bk"];
1329 // check for presence of bk file
1330 if(![[NSFileManager defaultManager] fileExistsAtPath:[filePath stringByExpandingTildeInPath]]){
1332 err = [futil autorizedCopy:srcString
1333 toPath:[filePath stringByExpandingTildeInPath]];
1335 } @catch (NSException *e) {
1338 if(filePath) [filePath release];
1342 // -------------------------------------------------------------------------------
1343 // checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1344 // -------------------------------------------------------------------------------
1345 -(BOOL) checkAfsStatus
1348 NSString *dfResult = [TaskUtil executeTaskSearchingPath:@"/bin/df" args:[NSArray arrayWithObjects:nil]];
1349 result = (dfResult?([dfResult rangeOfString:@AFS_FS_MOUNT].location != NSNotFound):NO);
1353 // -------------------------------------------------------------------------------
1354 // checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1355 // -------------------------------------------------------------------------------
1356 -(BOOL) checkAfsStatusForStartup {
1358 //NSString *fsResult = [TaskUtil executeTaskSearchingPath:@"launchctl" args:[NSArray arrayWithObjects: @"list", nil]];
1359 //result = (fsResult?([fsResult rangeOfString:@AFS_LAUNCHCTL_GREP_STR].location != NSNotFound):NO);
1363 // -------------------------------------------------------------------------------
1364 // makeChaceParamString
1365 // -------------------------------------------------------------------------------
1366 -(NSString*) makeChaceParamString
1368 NSNumber *tmpNum = nil;
1369 NSMutableString *afsdOption = [[NSMutableString alloc] init];
1370 if(!afsdOption) return @"";
1371 //write the data for afsd config file '-afsdb -stat x -dcache x -daemons x -volumes x -dynroot -fakestat-all'
1375 [afsdOption appendString:@AFSD_OPTION_AFSDB_KEY];[afsdOption appendString:@" "];
1379 if([self verbose]) {
1380 [afsdOption appendString:@AFSD_OPTION_VERBOSE_KEY];[afsdOption appendString:@" "];
1384 tmpNum = [NSNumber numberWithInt:[self statCacheEntry]];
1385 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_STAT_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1388 tmpNum = [NSNumber numberWithInt:[self dCacheDim]];
1389 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_DCACHE_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1392 tmpNum = [NSNumber numberWithInt:[self daemonNumber]];
1393 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_DAEMONS_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1396 tmpNum = [NSNumber numberWithInt:[self nVolEntry]];
1397 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_VOLUMES_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1400 if([self dynRoot]) {
1401 [afsdOption appendString:@AFSD_OPTION_DYNROOT_KEY];[afsdOption appendString:@" "];
1405 [afsdOption appendString:@AFSD_OPTION_FKESTAT_ALL];[afsdOption appendString:@" "];
1407 return [afsdOption autorelease];