Tuesday 1 April 2014

how to check file is updated or not without using modified time

how to check file is updated or not without using modified time  in cocoa for mac-osx objective-c

How to get checkSum of a file........




#import "NSData+MD5.h"

@interface NSData(MD5)
 - (NSString *)MD5;


@end



#import <CommonCrypto/CommonDigest.h>
 @implementation NSData(MD5)

- (NSString*)MD5
{
  // Create byte array of unsigned chars
  unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];

// Create 16 byte MD5 hash value, store in buffer
  CC_MD5(self.bytes, self.length, md5Buffer);
    
// Convert unsigned char buffer to NSString of hex values
  NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
  for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) 
[output appendFormat:@"%02x",md5Buffer[i]];

  return output;
}


@end




Call from Own class: 

+(NSString *)getCheckSum:(NSString *)filePath{
     NSData *nsData = [NSData dataWithContentsOfFile:filePath];
     if (nsData){
         return [nsData MD5];
     }
    else{
         return nil;
     }
}

How to check file is open or close objective-c in MAC os-x



How to check file is open or close in cocoa for mac osx (objective -c)

                 NSTask *task = [[NSTask alloc] init];
                    [task setLaunchPath:@"/bin/bash"];
                    [task setArguments:@[ @"-c",  [NSString stringWithFormat:@"lsof %@",FILE_PATH]];
                    [task launch];
                    [task waitUntilExit];
                    int status = [task terminationStatus];
                    if(status==0 ){
                      //file is open
                    }
                    if(status==1){
                       //file is closed  
                        }
                    }

                    task=nil;

Call method after every 1 sec. in Background thread




First time call through:

//call from viewdidload
dispatch_queue_t q_background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
    dispatch_async(q_background, ^{
        [self checkFileOpenOrNot:nil];
   });




-(void) checkFileOpenOrNot:(id)sender{

//do your code 



dispatch_queue_t q_background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
            double delayInSeconds = 1.0;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
            dispatch_after(popTime, q_background, ^(void){
                [self checkFileOpenOrNot:nil];

            });


}