PageRenderTime 62ms CodeModel.GetById 26ms app.highlight 33ms RepoModel.GetById 2ms app.codeStats 0ms

/BEE.framework/BEE/BEEFileSystemObject.m

http://eeframework.googlecode.com/
Objective C | 119 lines | 83 code | 25 blank | 11 comment | 7 complexity | 70b27eef96d6c89ea6b134e4ad59bbeb MD5 | raw file
  1// -*- objc -*-
  2
  3#import "BEE/BEE.h"
  4
  5@implementation BEEFileSystemObject
  6
  7- (id)init
  8{
  9  self = [super init];
 10  
 11  isArchived = YES;
 12
 13  return self;
 14}
 15
 16- (void)dealloc
 17{
 18  RELEASE(_path);
 19  [super dealloc];
 20}
 21
 22/* BEE specific */
 23
 24- (NSString *)path
 25{
 26  return _path;
 27}
 28
 29- (BOOL)isArchived
 30{
 31  return isArchived;
 32}
 33
 34- (void)setPath:(NSString *)path
 35{
 36  NSFileManager *fm = [NSFileManager defaultManager];
 37
 38  if(![path isAbsolutePath])
 39    {
 40      path = [[fm currentDirectoryPath] stringByAppendingFormat:@"/%@", [path stringByStandardizingPath]];
 41    }
 42
 43
 44  [self setShortName:[path lastPathComponent]];
 45  ASSIGN(_path, path);
 46}
 47
 48- (void)backup
 49{
 50  NSString *path = [NSString stringWithString:@"/"];
 51  NSString *pathPart;
 52  NSFileManager *fm = [NSFileManager defaultManager];
 53  BOOL isDir = NO;
 54  NSString *backupPath = [[self backupRoot] stringByAppendingString:_path];
 55  //  NSString *incrPath = [[self incrementalRoot] stringByAppendingString:_path];
 56
 57  NSEnumerator *en = [[backupPath pathComponents] objectEnumerator];
 58
 59  while((pathPart = [en nextObject]))
 60    {
 61      if([pathPart isEqualToString:@"/"])
 62	{
 63	  continue;
 64	}
 65      if([pathPart isEqualToString:[backupPath lastPathComponent]])
 66	{
 67	  continue;
 68	}
 69      
 70      path = [path stringByAppendingFormat:@"%@/",pathPart];
 71      if(![fm fileExistsAtPath:path isDirectory:&isDir])
 72	{
 73	  [fm createDirectoryAtPath:path attributes:nil];
 74	}
 75      else if(!isDir)
 76	{
 77	  [NSException raise:NSGenericException 
 78		       format:@"The backup tree has been broken by an existing file suddenly"];
 79	}
 80    }
 81
 82  if(isArchived)
 83    {
 84      [fm copyPath:_path toPath:backupPath handler:self];
 85    }
 86}
 87
 88- (void)setArchived:(BOOL)flag
 89{
 90  isArchived = YES;
 91}
 92
 93@end /* BEEFileSystemObject */
 94
 95@implementation BEEFileSystemObject ( NSFileManagerDelegate )
 96
 97- (void)fileManager:(NSFileManager *)manager willProcessPath:(NSString *)path
 98{
 99  /*
100  if(!([_path isEqualToString:path]))
101    {
102      NSLog(@"my path %@", _path);
103      NSLog(@"%@", path);
104      [NSException raise:NSInvalidArgumentException format:@"That's not my path"];
105    }
106  */
107}
108
109- (BOOL)fileManager:(NSFileManager *)manager 
110        shouldProceedAfterError:(NSDictionary *)errorInfo
111{
112  NSLog(@"Something wrong with paths. Please check:"); 
113  NSLog(@"%@",[self path]);
114  NSLog(@"%@",[[self backupRoot] stringByAppendingString:[self path]]);
115
116  return NO;
117}
118
119@end /* BEEFileSystemObject ( NSFileManagerDelegate ) */