Skip to content

Commit 6c379c6

Browse files
author
Colum Paget
committed
don't update xattr of unchanged files if -cache used
1 parent d598962 commit 6c379c6

11 files changed

+34
-19
lines changed

CHANGELOG

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v1.22 (2024-07-18)
2+
* if '-cache' option is used with '-xattr', then do not update files where xattr hash matches the file (feature request from https://github.com/eatnumber1).
3+
14
v1.21 (2024-06-20)
25
* fix -hmac option that's been broken by changes in libUseful
36
* add '-list-hashes' option to list available has types

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
CC = gcc
22
CFLAGS = -g -O2
3-
LIBS = libUseful-5/libUseful.a -lssl -lcrypto -lz
3+
LIBS = libUseful-5/libUseful.a -lssl -lcrypto -lz -lc
44
INSTALL=/usr/bin/install -c
55
prefix=/usr/local
66
bindir=$(prefix)${exec_prefix}/bin
7-
FLAGS=$(LDFLAGS) $(CPPFLAGS) $(CFLAGS) -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DHAVE_STDIO_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_STRINGS_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_UNISTD_H=1 -DSTDC_HEADERS=1 -DHAVE_LIBZ=1 -DHAVE_LIBCRYPTO=1 -DHAVE_LIBSSL=1
7+
FLAGS=$(LDFLAGS) $(CPPFLAGS) $(CFLAGS) -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DHAVE_STDIO_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_STRINGS_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_UNISTD_H=1 -DSTDC_HEADERS=1 -DHAVE_LIBC=1 -DUSE_XATTR=1 -DHAVE_LIBZ=1 -DHAVE_LIBCRYPTO=1 -DHAVE_LIBSSL=1
88
OBJ=common.o encodings.o command-line-args.o ssh.o http.o fingerprint.o include-exclude.o files.o filesigning.o xattr.o check-hash.o find.o otp.o memcached.o frontend.o cgi.o xdialog.o output.o
99
EXE=hashrat
1010

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ OPTIONS
141141
-xattr Use eXtended file ATTRibutes. In hash mode, store hashes in the file attributes, in check mode compare against hashes stored in file attributes.
142142
-txattr Use TRUSTED eXtended file ATTRibutes. In hash mode, store hashes in 'trusted' file attributes. 'trusted' attributes can only be read and written by root. Under freebsd this menas SYSTEM attributes.
143143
-attrs comma-separated list of filesystem attribute names to be set to the value of the hash.
144-
-cache Use hashes stored in 'user' xattr if they're younger than the mtime of the file. This speeds up outputting hashes.
144+
-cache Use hashes stored in 'user' xattr if they're younger than the mtime of the file. This speeds up outputting hashes. When storing hashes, do not update files that already have the correct hash.
145145
-u <types> Update. In checking mode, update hashes for the files as you go. <types> is a comma-separated list of things to update, which can be 'xattr' 'memcached' or a file name. This will update these targets with the hash that was found at the time of checking.
146146
-hide-input When reading data from stdin in linemode, set the terminal to not echo characters, thus hiding typed input.
147147
-star-input When reading data from stdin in linemode replace characters with stars.

check.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ TestHash z85 "ZEROMQ85 encoding" "=SI2F3[n}kp9Zn?Ra>yK"
207207
Title "Testing Misc. Features"
208208

209209
HR_OUT=`./hashrat -version`
210-
if [ "$HR_OUT" = "version: 1.21" ]
210+
if [ "$HR_OUT" = "version: 1.22" ]
211211
then
212212
OkayMessage "Version (-version) works"
213213
else

command-line-args.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ void CommandLinePrintUsage()
538538
printf(" %-15s %s\n","-xattr", "Use eXtended file ATTRibutes. In hash mode, store hashes in the file attributes, in check mode compare against hashes stored in file attributes.");
539539
printf(" %-15s %s\n","-txattr", "Use TRUSTED eXtended file ATTRibutes. In hash mode, store hashes in 'trusted' file attributes. 'trusted' attributes can only be read and written by root. Under freebsd this menas SYSTEM attributes.");
540540
printf(" %-15s %s\n","-attrs", "comma-separated list of filesystem attribute names to be set to the value of the hash.");
541-
printf(" %-15s %s\n","-cache", "Use hashes stored in 'user' xattr if they're younger than the mtime of the file. This speeds up outputting hashes.");
541+
printf(" %-15s %s\n","-cache", "Use hashes stored in 'user' xattr if they're younger than the mtime of the file. This speeds up outputting hashes. Also do not update xattr of a file if it already matches.");
542542
printf(" %-15s %s\n","-u <types>", "Update. In checking mode, update hashes for the files as you go. <types> is a comma-separated list of things to update, which can be 'xattr' 'memcached' or a file name. This will update these targets with the hash that was found at the time of checking.");
543543
printf(" %-15s %s\n","-hide-input", "When reading data from stdin in linemode, set the terminal to not echo characters, thus hiding typed input.");
544544
printf(" %-15s %s\n","-star-input", "When reading data from stdin in linemode replace characters with stars.");

common.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,16 @@ void HashratStoreHash(HashratCtx *Ctx, const char *Path, struct stat *Stat, cons
6969
// else
7070

7171

72-
if (Ctx->Flags & CTX_STORE_XATTR) HashRatSetXAttr(Ctx, Path, Stat, Ctx->HashType, Hash);
72+
if (Ctx->Flags & CTX_STORE_XATTR)
73+
{
74+
if (Ctx->Flags & CTX_XATTR_CACHE)
75+
{
76+
if ((Ctx->Flags & CTX_XATTR_ROOT) && (getuid()==0)) XAttrGetHash(Ctx, "trusted", Ctx->HashType, Path, NULL, &Tempstr);
77+
else XAttrGetHash(Ctx, "user", Ctx->HashType, Path, NULL, &Tempstr);
78+
if (strcmp(Tempstr, Hash) !=0) HashRatSetXAttr(Ctx, Path, Stat, Ctx->HashType, Hash);
79+
}
80+
else HashRatSetXAttr(Ctx, Path, Stat, Ctx->HashType, Hash);
81+
}
7382

7483
if (Ctx->Flags & CTX_STORE_MEMCACHED)
7584
{

common.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "libUseful-5/libUseful.h"
66
#include "glob.h"
77

8-
#define VERSION "1.21"
8+
#define VERSION "1.22"
99

1010
#define ACT_NONE 0
1111
#define ACT_HASH 1

files.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ int HashratHashSingleFile(HashratCtx *Ctx, const char *HashType, int FileType, c
232232
HASH *Hash;
233233
struct stat XattrStat;
234234
const char *ptr;
235+
typedef long long unsigned int llui;
235236

236237
*RetStr=CopyStr(*RetStr,"");
237238

@@ -240,7 +241,7 @@ int HashratHashSingleFile(HashratCtx *Ctx, const char *HashType, int FileType, c
240241
{
241242
XAttrGetHash(Ctx, "user", Ctx->HashType, Path, &XattrStat, RetStr);
242243
//only use the hash cached in the xattr address if it's younger than the mtime
243-
printf("cache %llu %llu %llu\n",XattrStat.st_mtime, FStat->st_mtime, XattrStat.st_mtime - FStat->st_mtime);
244+
printf("cache %llu %llu %llu\n",(llui) XattrStat.st_mtime, (llui) FStat->st_mtime, (llui) (XattrStat.st_mtime - FStat->st_mtime));
244245
if ( ((XattrStat.st_mtime - FStat->st_mtime) < 10) ) *RetStr=CopyStr(*RetStr,"");
245246
}
246247

hashrat.1

+1-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ Use TRUSTED eXtended \fIfile\fP ATTRibutes. In \fIhash\fP mode, store \fIhashes\
321321
.TP
322322
.B
323323
\fB-cache\fP
324-
Use \fIhashes\fP stored in user xattr if they're younger than the mtime of the \fIfile\fP. This speeds up outputting \fIhashes\fP.
324+
Use \fIhashes\fP stored in user xattr if they're younger than the mtime of the \fIfile\fP. This speeds up outputting \fIhashes\fP. When storing xattr hashes, do not update files that already have the correct hash.
325325
.TP
326326
.B
327327
\fB-u\fP <types>

main.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ int main(int argc, char *argv[])
160160

161161
switch (Ctx->Action)
162162
{
163-
case ACT_LIST_TYPES:
164-
Tempstr=HashAvailableTypes(Tempstr);
165-
strrep(Tempstr, ',', '\n');
166-
printf("%s\n", Tempstr);
167-
break;
163+
case ACT_LIST_TYPES:
164+
Tempstr=HashAvailableTypes(Tempstr);
165+
strrep(Tempstr, ',', '\n');
166+
printf("%s\n", Tempstr);
167+
break;
168168

169169
case ACT_HASH:
170170
result=ProcessTargetItems(Ctx);

xattr.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ int XAttrGetHash(HashratCtx *Ctx, const char *XattrType, const char *HashType, c
100100
{
101101
int result=FALSE;
102102
char *Tempstr=NULL;
103-
char *ptr;
103+
const char *ptr;
104104
int len;
105105

106106
*Hash=SetStrLen(*Hash,255);
@@ -115,14 +115,16 @@ int XAttrGetHash(HashratCtx *Ctx, const char *XattrType, const char *HashType, c
115115
fprintf(stderr,"XATTR Support not compiled in.\n");
116116
#endif
117117

118+
118119
if (len > 0)
119120
{
120121
(*Hash)[len]='\0';
122+
121123
ptr=*Hash;
122-
FStat->st_mtime=(time_t) strtoll(ptr,&ptr,10);
123-
if (*ptr==':') ptr++;
124-
FStat->st_size=(off_t) strtoll(ptr,&ptr,10);
125-
if (*ptr==':') ptr++;
124+
ptr=GetToken(ptr, ":", &Tempstr, 0);
125+
if (FStat) FStat->st_mtime=(time_t) strtoll(Tempstr,NULL,10);
126+
ptr=GetToken(ptr, ":", &Tempstr, 0);
127+
if (FStat) FStat->st_size=(off_t) strtoll(Tempstr,NULL,10);
126128
len=StrLen(ptr);
127129
memmove(*Hash,ptr,len+1);
128130
result=TRUE;

0 commit comments

Comments
 (0)