Skip to content

Instantly share code, notes, and snippets.

@iluvcapra
Created March 3, 2010 03:12
Show Gist options
  • Save iluvcapra/320266 to your computer and use it in GitHub Desktop.
Save iluvcapra/320266 to your computer and use it in GitHub Desktop.
A daemon for excluding Pro Tools fade files from Time Machine backups
/* Copyright (c) 2010 Jamie Hardt, All Rights Reserved. */
/*
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
#import <Foundation/Foundation.h>
#import <CoreServices/CoreServices.h>
#include <signal.h>
#include <sys/stat.h>
#include <libgen.h>
#define VERBOSE( ... ) if (verboseReporting) {NSLog( __VA_ARGS__ );};
#define WARN( str_arg ) NSLog(@"WARN: " str_arg )
#define WARN_V( str_arg, ...) NSLog(@"WARN: " str_arg , __VA_ARGS__ )
#define INFO( str_arg ) NSLog(@"INFO: " str_arg )
#define INFO_V( str_arg, ...) NSLog(@"INFO: " str_arg , __VA_ARGS__ )
@interface TachNoSearchPathsException : NSException
@end
@implementation TachNoSearchPathsException
@end
static BOOL verboseReporting;
static BOOL isQuit;
void TachStreamCallback(ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
char ** eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]) {
int i;
for (i = 0 ; i < numEvents; i++) {
VERBOSE(@"Change event with path: %s", eventPaths[i] );
// skip to the next event unless the basename of this event is "Fade Files"
char *bn = basename(eventPaths[i]);
if (strcmp(bn,"Fade Files")) {
break;
}
VERBOSE(@"Path to exclude: %s",eventPaths[i]);
// break if the path isn't a dir
struct stat pathStat;
if( stat(eventPaths[i], &pathStat) ) {
VERBOSE(@"stat() on path %s failed",eventPaths[i]);
break;
}
if (!S_ISDIR(pathStat.st_mode)) {
VERBOSE(@"Path is not a dir: %s",eventPaths[i]);
break;
}
CFURLRef aRef = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault,
(UInt8 *)eventPaths[i],
strlen(eventPaths[i]),
NO);
if (!CSBackupIsItemExcluded(aRef, NULL)) {
INFO_V(@"Excluding folder at path: %s",eventPaths[i]);
CSBackupSetItemExcluded(aRef, YES, NO);
} else {
VERBOSE(@"Folder already excluded: %s",eventPaths[i]);
}
CFRelease(aRef);
}
}
void runloop_quit(int sig) {
VERBOSE(@"receieved signal %i, terminating...",sig);
isQuit = YES;
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = nil;
FSEventStreamRef eventStream = NULL;
verboseReporting = NO;
static CFStringRef TachAppID;
@try {
TachAppID = CFSTR("com.soundepartment.tachyond");
pool = [[NSAutoreleasePool alloc] init];
isQuit = NO;
signal (SIGINT, runloop_quit);
signal (SIGHUP, runloop_quit);
signal (SIGTERM, runloop_quit);
NSArray *searchPaths = (NSArray *)CFPreferencesCopyAppValue(CFSTR("SearchPaths"),
TachAppID
);
NSNumber *lastTimestamp = (NSNumber *)CFPreferencesCopyAppValue(CFSTR("LastTimestamp"),
TachAppID
);
if (!searchPaths || [searchPaths count] == 0) {
@throw [TachNoSearchPathsException exceptionWithName:@"TachNoSearchPathsException"
reason:@"No search paths were provided for monitoring."
userInfo:nil];
}
if (!lastTimestamp || [lastTimestamp unsignedLongValue] == 0) {
lastTimestamp = [NSNumber numberWithUnsignedLong:kFSEventStreamEventIdSinceNow];
VERBOSE(@"Creating event stream starting with event ID = kFSEventStreamEventIdSinceNow");
} else {
VERBOSE(@"Creating event stream starting with event ID = %ul",lastTimestamp);
}
[searchPaths autorelease];
[lastTimestamp autorelease];
INFO(@"tachyond starting");
INFO_V(@"observing paths: %@",searchPaths);
eventStream = FSEventStreamCreate(kCFAllocatorDefault,
(FSEventStreamCallback)TachStreamCallback,
NULL,
(CFArrayRef)searchPaths,
[lastTimestamp unsignedLongValue],
(CFTimeInterval)1,
kFSEventStreamCreateFlagNone);
FSEventStreamScheduleWithRunLoop(eventStream,
CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode);
VERBOSE(@"Starting event stream...");
FSEventStreamStart(eventStream);
while (!isQuit) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[[NSDate date] dateByAddingTimeInterval:1]];
}
} @catch (TachNoSearchPathsException *exception) {
WARN(@"No search paths were provided");
} @catch (NSException *exception) {
@throw exception;
} @finally {
if (eventStream) {
VERBOSE(@"Flushing events...");
FSEventStreamFlushSync(eventStream);
VERBOSE(@"Stopping FSEvent monitor...");
FSEventStreamStop(eventStream);
VERBOSE(@"Saving ID of last event...");
UInt64 lastID = FSEventStreamGetLatestEventId(eventStream);
CFPreferencesSetAppValue(CFSTR("LastTimestamp"),
(CFPropertyListRef)[NSNumber numberWithUnsignedLong:lastID],
TachAppID);
CFPreferencesAppSynchronize(TachAppID);
VERBOSE(@"Closing FSEvent stream...");
FSEventStreamInvalidate(eventStream);
FSEventStreamRelease(eventStream);
}
[pool drain];
}
INFO(@"exiting...");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment