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