Skip to content

Commit

Permalink
fixed an issue with ._ files
Browse files Browse the repository at this point in the history
  • Loading branch information
nils-tekampe committed Jun 4, 2015
1 parent 400666a commit f433f7a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
1 change: 1 addition & 0 deletions macbrush/macbrush/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ void mycallback(

bool processFile(NSString* file);
void cleanDirectory(NSString *directory);
bool isFile(NSString *file);

62 changes: 25 additions & 37 deletions macbrush/macbrush/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ bool processFile(NSString* file){

NSString* pattern=@"";

logger(file, true);

///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.
Expand All @@ -259,28 +259,24 @@ bool processFile(NSString* file){

pattern=@"._";

if ([file rangeOfString:pattern].location != NSNotFound) {
if ([file rangeOfString:pattern].location == NSNotFound) {

//let's build the name of the potentially corresponding file
NSString* theFileName = file.lastPathComponent;
NSString *firstThreeChar = [theFileName substringToIndex:2];
NSString *cuttedFileName = [theFileName substringFromIndex:2];
NSString *path = file.stringByDeletingLastPathComponent;
NSString *potentialBaseFile=[NSString stringWithFormat:@"%@/%@", path,cuttedFileName];
NSString *path = file.stringByDeletingLastPathComponent;
NSString *potentialTmpFile=[NSString stringWithFormat:@"%@/%@%@", path,@"._",theFileName];

//check that it really starts with ._
if ([firstThreeChar isEqualToString:pattern])
{

if([[NSFileManager defaultManager] fileExistsAtPath:potentialBaseFile])
if([[NSFileManager defaultManager] fileExistsAtPath:potentialTmpFile])
{

if (isFile(potentialTmpFile)){

logger([NSString stringWithFormat:@"%@%@", @"Found the following ._ file:" , file],true);


if (!simulate){

if ([manager removeItemAtPath:file error:&error])
if ([manager removeItemAtPath:potentialTmpFile error:&error])
{
logger(@"Sucesfully removed file",true);
sumDotUnderscore++;
Expand All @@ -291,11 +287,11 @@ bool processFile(NSString* file){
return false;
}

}

}
}
}

}
}

///Now looking for .APDisk files. They should be removed as long as the user did not choose the option
Expand Down Expand Up @@ -403,33 +399,17 @@ void cleanDirectory(NSString *directory)
sumDSStore=0;
sumVolumeIcon=0;

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


/// for (NSString *file in directoryEnumerator) {
/// NSString *filename;
/// filename=file;
/// filename= [directory stringByAppendingPathComponent:file];
/// processFile(filename);
/// }

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

// NSString *directory = @"/private/tmp";

NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtURL:[NSURL fileURLWithPath:directory isDirectory:YES] includingPropertiesForKeys:nil options:0 errorHandler:^BOOL(NSURL *url, NSError *error) {

if(error)
{
NSLog(@"error at file URL %@ - %@", [url absoluteString], [error localizedDescription]);
}

return NO;
}];

for (NSURL *file in directoryEnumerator) {
processFile(file.path);
for (NSString *file in directoryEnumerator) {
NSString *filename=file;
filename= [directory stringByAppendingPathComponent:file];
processFile(filename);
}




logger([NSString stringWithFormat:@"%@%@", @"Finished cleaning directory :" , directory],false);
logger([NSString stringWithFormat:@"%d%@",sumDotAPDisk, @" .AP_Disk files have been removed"],false);
Expand All @@ -438,6 +418,14 @@ void cleanDirectory(NSString *directory)
logger([NSString stringWithFormat:@"%d%@",sumVolumeIcon, @" .VolumeIcon.icns files have been removed"],false);
}

bool isFile(NSString *file){
BOOL isDir = NO;
if([[NSFileManager defaultManager]fileExistsAtPath:file isDirectory:&isDir] && isDir)
return false;
else
return true;
}




0 comments on commit f433f7a

Please sign in to comment.