Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nils-tekampe committed Jun 3, 2015
1 parent bb5da8b commit cecc9a9
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 44 deletions.
7 changes: 3 additions & 4 deletions macbrush/macbrush.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Macbrush < Formula
desc "Keeping OSX directories clear of temporary files"
homepage "http://nils-tekampe.github.io/MacBrush"
homepage "https://nils-tekampe.github.io/macbrush"
url "https://github.com/nils-tekampe/MacBrush/archive/0.12.tar.gz"
sha256 "ed08874d9c2059a81356419d29d9ff603b4810baea7742a2132dbdfaee064414"

Expand All @@ -12,9 +12,8 @@ def install
xcodebuild
bin.install "./build/Release"
end

test do
mkdir "test"
touch "./test/.DS_Store"
macbrush "--verbose --skip-observation ./test"
macbrush "--verbose --skip-observation " testpath
end
end
6 changes: 4 additions & 2 deletions macbrush/macbrush/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#endif

#define USAGE @"usage: macbrush [-d] [-a] [-o] [-i] [-s] [-v] [-c] [-o] [-h] targetDirectory"

void logger(NSString *message, bool verbose_only);
void mycallback(
ConstFSEventStreamRef streamRef,
Expand All @@ -21,6 +23,6 @@ void mycallback(
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]);

int processFile(NSString* file);
bool processFile(NSString* file);
void cleanDirectory(NSString *directory);
void resetCounter();

131 changes: 93 additions & 38 deletions macbrush/macbrush/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#import "GBCli.h"
#include "main.h"

// I know that global variables are not the best style but for some purposes they are just the easiest way :-)

//variables as representatives for command line options
bool simulate;
bool ignoreDotUnderscore;
bool ignoreDotAPDisk;
Expand All @@ -21,25 +23,19 @@
bool skipClean;
bool skipObservation;

//variables for some statistics
int sumDotUnderscore=0;
int sumDotAPDisk=0;
int sumDSStore=0;
int sumVolumeIcon=0;

int DotUnderScoreX=0;
int DotUnderScoreY=0;
int DotAPDiskX=0;
int DotAPDiskY=0;
int DotDSStoreX=0;
int DotDSStoreY=0;
int DotVolumeIconX=0;
int DotVolumeIconY=0;




int main(int argc, const char * argv[]) {

//****************************************
//Take care of options and arguments
//****************************************

// Create settings stack.
GBSettings *factoryDefaults = [GBSettings settingsWithName:@"Factory" parent:nil];
[factoryDefaults setBool:NO forKey:@"ignore-dot-underscore"];
Expand All @@ -50,7 +46,6 @@ int main(int argc, const char * argv[]) {
[factoryDefaults setBool:NO forKey:@"verbose"];
[factoryDefaults setBool:NO forKey:@"skip-clean"];
[factoryDefaults setBool:NO forKey:@"skip-observation"];
[factoryDefaults setBool:NO forKey:@"help"];

[factoryDefaults setInteger:12 forKey:@"optionb"];
GBSettings *settings = [GBSettings settingsWithName:@"CmdLine" parent:factoryDefaults];
Expand Down Expand Up @@ -82,12 +77,22 @@ int main(int argc, const char * argv[]) {
skipClean=[settings boolForKey:@"skip-clean"];
skipObservation=[settings boolForKey:@"skip-observation"];

//***********************************************
//Do some basic checks with arguments and options
//***********************************************

if ([settings boolForKey:@"help"]){

logger(USAGE, false);
return 0;
}

if ([settings boolForKey:@"skip-observation"]){
if ([settings boolForKey:@"version"]){

logger(@"usage: macbrush [-f] [-v] targetDirectory", false);
logger(@"macbrush, Version 0.6", false);
return 0;
}



NSArray *arguments = parser.arguments;
Expand All @@ -96,7 +101,7 @@ int main(int argc, const char * argv[]) {

if (arguments.count==0)
{
logger(@"Please provide at least one directory as an argument",false);
logger(USAGE,false);
return 1;

}
Expand All @@ -120,6 +125,10 @@ int main(int argc, const char * argv[]) {

}

//********************************************************
//Starting main functionality. 1st step: Clean directories
//********************************************************

if (!skipClean){
for (NSString *entry in arguments) {
cleanDirectory(entry);
Expand All @@ -130,6 +139,9 @@ int main(int argc, const char * argv[]) {
logger(@"Skipping to clean directories. Will continue with observation mode",false);
}

//**********************************************************
//Starting main functionality. 2nd step: observe directories
//**********************************************************
if (!skipObservation){

@try{
Expand All @@ -149,18 +161,16 @@ int main(int argc, const char * argv[]) {

/* Create the stream before calling this. */
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(),kCFRunLoopDefaultMode);

FSEventStreamStart(stream);

//printStatus(arguments);
logger(@"Starting observation mode for the following directories:",false);

for (NSString *entry in arguments) {
logger(entry,false);

}


//When the loop runs, the program can only be exited via Ctrl+C
logger(@"Please press Ctrl+C to end program",false);
CFRunLoopRun();
}
@catch(NSException *e){
Expand All @@ -176,24 +186,38 @@ int main(int argc, const char * argv[]) {

}

/**
Function for logging/user interaction via commandline
Will output text to stdout
@param *message The message to print
@param verbose_only If set to true, the message will only be printed if --verbose==true
*/
void logger(NSString *message, bool verbose_only){


if (!verbose_only){
//These messages will have to be printed in any way. However, the way of printing depend on
//--verbose

if (verbose)
NSLog(@"%@", message);
else
printf("%s\n", [message UTF8String]);
// printf("%s", @"\n");

}


else if (verbose_only){
//These messages will only be printed of --verbose=true
if(verbose){
NSLog(@"%@", message);
}

}

}

/**
Callback function that is called if a change to a files in one of the observed folders has been detected
*/
void mycallback(
ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
Expand All @@ -213,15 +237,21 @@ void mycallback(

}


int processFile(NSString* file){
/**
This function processes a file/folder and determines whether it should be removed. If the file has been identified for removal, the function also tries to remove the file.
@param file The file to be processed
@returns true if the file has been removed sucesfully Returns false if the file has not been identified to be removed or could not be removed (e.g. due to a lack of permissions.
*/
bool processFile(NSString* file){

NSFileManager *manager = [NSFileManager defaultManager];
NSError *error = nil;

NSString* pattern=@"";

//First we look for ´._ files
///First we look for ´._ files
///._ files will only be removed if a corresponding base file is existing
//Example: _.test.txt will be removed if a file name test.txt is existing in the same folder.

if (!ignoreDotAPDisk){

Expand Down Expand Up @@ -252,18 +282,23 @@ int processFile(NSString* file){
{
logger(@"Sucesfully removed file",true);
sumDotUnderscore++;
return true;
}
else {
logger(@"Error removing file",true);
return false;
}
return 1;

}
}
}
}

}

///Now looking for .APDisk files. They should be removed as long as the user did not choose the option
///to ignore these files

if (!ignoreDotAPDisk){

pattern=@".apdisk";
Expand All @@ -278,15 +313,20 @@ int processFile(NSString* file){
{
logger(@"Sucesfully removed file",true);
sumDotAPDisk++;
return true;
}
else {
logger(@"Error removing file",true);
return false;
}
return 2;

}
}
}

///Now looking for .DS_Store files. They should be removed as long as the user did not choose the option
///to ignore these files

if (!ignoreDSStore){

pattern=@".DS_Store";
Expand All @@ -301,15 +341,21 @@ int processFile(NSString* file){
{
logger(@"Sucesfully removed file",true);
sumDSStore++;
return true;
return true;
}
else {
logger(@"Error removing file",true);
return false;
}
return 3;

}
}
}

///Now looking for .VolumeIcon.icns files. They should be removed as long as the user did not choose the
///otpion to ignore these files

if (!ignoreDSStore){

pattern=@".VolumeIcon.icns";
Expand All @@ -324,22 +370,36 @@ int processFile(NSString* file){
{
logger(@"Sucesfully removed file",true);
sumVolumeIcon++;
return true;
}
else {
logger(@"Error removing file",true);
return false;
}
return 4;

}
}
}

return 0;
return false;
}


/**
This function processes a complete directory. It lists all the files and subfolder of the dir and
calls processFile(..) for each of the files/folders in the directory.
@param *directory NSString pointing to the directory to be processesd.
*/
//Todo: function could use some error handling
void cleanDirectory(NSString *directory)
{
logger([NSString stringWithFormat:@"%@%@", @"Starting to clean directory :" , directory],false);
resetCounter();

//reset statistics
sumDotAPDisk=0;
sumDotUnderscore=0;
sumDSStore=0;
sumVolumeIcon=0;

NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:directory];

Expand All @@ -357,11 +417,6 @@ void cleanDirectory(NSString *directory)
logger([NSString stringWithFormat:@"%d%@",sumVolumeIcon, @" .VolumeIcon.icns files have been removed"],false);
}

void resetCounter(){
sumDotAPDisk=0;
sumDotUnderscore=0;
sumDSStore=0;
sumVolumeIcon=0;

}



0 comments on commit cecc9a9

Please sign in to comment.