Skip to content

Commit

Permalink
hookscripts can now be used with 'find duplicates' mode. In that mode…
Browse files Browse the repository at this point in the history
… they receive two arguments, the path of the current file, and the path of the duplicate it matches
  • Loading branch information
ColumPaget committed Oct 9, 2017
1 parent 69b9462 commit 7749e91
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
9 changes: 7 additions & 2 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ Options:
-dups Search for duplicate files.
-memcached <server> Specify memcached server. (Overrides reading list from stdin if used with -m, -c or -cf).
-mcd <server> Specify memcached server. (Overrides reading list from stdin if used with -m, -c or -cf).
-h <script> Script to run when a file fails CHECK mode, or is found in MATCH mode.
-hook <script> Script to run when a file fails CHECK mode, or is found in FIND mode
-h <script> Script to run when a file fails CHECK mode, or is found in MATCH mode. (see 'Hookscripts' below)
-hook <script> Script to run when a file fails CHECK mode, or is found in MATCH mode. (see 'Hookscripts' below)
-color Use ANSI color codes on output when checking hashes.
-strict Strict mode: when checking, check file mtime, owner, group, and inode as well as it's hash
-S Strict mode: when checking, check file mtime, owner, group, and inode as well as it's hash
Expand All @@ -112,6 +112,11 @@ Options:
-star-input When reading data from stdin in linemode replace characters with stars.


Hookscripts

hookscripts are passed the path of the appropriate file as an argument. In 'find duplicates' mode a second argument is passed, which is the duplicate file.


Hashrat can also detect if it's being run under any of the following names (e.g., via symlinks)

md5sum run with '-trad -md5'
Expand Down
2 changes: 1 addition & 1 deletion check-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ char *Tempstr=NULL;
if (Flags & FLAG_COLOR) printf("%s%s: FAILED. '%s'.%s\n",ANSICode(ANSI_RED, 0, 0),Path, ErrorMessage, ANSI_NORM);
else printf("%s: FAILED. %s.\n",Path,ErrorMessage);

RunHookScript(DiffHook, Path);
RunHookScript(DiffHook, Path, "");
DestroyString(Tempstr);
}

Expand Down
12 changes: 7 additions & 5 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,20 @@ char *Tempstr=NULL;
}


void RunHookScript(const char *Hook, const char *Path)
void RunHookScript(const char *Hook, const char *Path, const char *Other)
{
char *Tempstr=NULL, *Quoted=NULL;
char *Tempstr=NULL, *QuotedPath=NULL, *QuotedOther=NULL;
STREAM *S;

if (StrValid(Hook))
{
//must quote twice to get through system comamnd
Quoted=QuoteCharsInStr(Quoted, Path,"\"'`!|;<> ");
QuotedPath=QuoteCharsInStr(QuotedPath, Path,"\"'`!|;<> ");
QuotedOther=QuoteCharsInStr(QuotedOther, Other,"\"'`!|;<> ");
S=STREAMSpawnCommand("/bin/sh","","",0);
if (S)
{
Tempstr=MCopyStr(Tempstr, DiffHook," ",Quoted,";exit\n",NULL);
Tempstr=MCopyStr(Tempstr, DiffHook," ",QuotedPath, " ", QuotedOther, ";exit\n",NULL);
STREAMWriteLine(Tempstr,S);
STREAMFlush(S);

Expand All @@ -153,6 +154,7 @@ STREAM *S;
}

DestroyString(Tempstr);
DestroyString(Quoted);
DestroyString(QuotedPath);
DestroyString(QuotedOther);
}

4 changes: 2 additions & 2 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

#define IGNORE -1

#define VERSION "1.8.10"
#define VERSION "1.8.11"


typedef struct
Expand Down Expand Up @@ -115,6 +115,6 @@ TFingerprint *TFingerprintCreate(const char *Hash, const char *HashType, const c
void HashratCtxDestroy(void *p_Ctx);
void HashratStoreHash(HashratCtx *Ctx, char *Path, struct stat *Stat, char *Hash);
int HashratOutputInfo(HashratCtx *Ctx, STREAM *S, char *Path, struct stat *Stat, char *Hash);
void RunHookScript(const char *Hook, const char *Path);
void RunHookScript(const char *Hook, const char *Path, const char *Other);

#endif
16 changes: 10 additions & 6 deletions files.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ int HashratAction(HashratCtx *Ctx, char *Path, struct stat *Stat)
{
char *HashStr=NULL;
int Type, result=FALSE;
TFingerprint *FP;
TFingerprint *FP=NULL;

switch (Ctx->Action)
{
Expand Down Expand Up @@ -464,7 +464,6 @@ case ACT_CHECK:
//we return TRUE on FAILURE, as we are signaling a significant event
result=TRUE;
}
TFingerprintDestroy(FP);
}
else if (Flags & FLAG_VERBOSE) fprintf(stderr,"ZERO LENGTH FILE: %s\n",Path);
}
Expand Down Expand Up @@ -504,7 +503,6 @@ case ACT_CHECK_MEMCACHED:

if (FP && HashratCheckFile(Ctx, Path, NULL, HashStr, FP)) result=FALSE;
else fprintf(stderr,"ERROR: No stored hash for '%s'\n",Path);
TFingerprintDestroy(FP);
}
else if (Flags & FLAG_VERBOSE) fprintf(stderr,"ZERO LENGTH FILE: %s\n",Path);
}
Expand All @@ -526,10 +524,8 @@ case ACT_FINDMATCHES_MEMCACHED:
MatchCount++;
//here we return true if a match found
result=TRUE;
RunHookScript(DiffHook, Path);
}
else DiffCount++;
TFingerprintDestroy(FP);
}
else if (Flags & FLAG_VERBOSE) fprintf(stderr,"ZERO LENGTH FILE: %s\n",Path);
}
Expand All @@ -549,13 +545,14 @@ case ACT_FINDDUPLICATES:
MatchCount++;
//here we return true if a match found
result=TRUE;
TFingerprintDestroy(FP);
}
else
{
FP=TFingerprintCreate(HashStr, Ctx->HashType, "", Path);
DiffCount++;
MatchAdd(FP, Path, 0);
//as we've added FP to an internal list we don't want it destroyed
FP=NULL;
}
}
}
Expand All @@ -564,6 +561,13 @@ case ACT_FINDDUPLICATES:
break;
}

if (result==TRUE)
{
if (FP) RunHookScript(DiffHook, Path, FP->Path);
else RunHookScript(DiffHook, Path, "");
}

if (FP) TFingerprintDestroy(FP);
DestroyString(HashStr);

return(result);
Expand Down
5 changes: 1 addition & 4 deletions hashrat.1
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,8 @@ Specify memcached server. This option overrides reading list from stdin if used
.TP
.B
\fB-h\fP <script>
Script \fIto\fP run when a \fIfile\fP fails CHECK mode, or is found in MATCH mode.
.TP
.B
\fB-hook\fP <script>
Script \fIto\fP run when a \fIfile\fP fails CHECK mode, or is found in FIND mode
Script \fIto\fP run when a \fIfile\fP fails CHECK mode, or is found in MATCH mode. Script is passed the filename as an argument. In 'find duplicates' mode a second file name (the duplicate) will be passed as the second argument.
.TP
.B
\fB-color\fP
Expand Down

0 comments on commit 7749e91

Please sign in to comment.