-
Notifications
You must be signed in to change notification settings - Fork 11
/
xattr.c
181 lines (138 loc) · 4.64 KB
/
xattr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "xattr.h"
#include "fingerprint.h"
#ifdef USE_XATTR
#include <sys/types.h>
#include <sys/xattr.h>
#endif
#ifdef USE_EXTATTR
#include <sys/types.h>
#include <sys/extattr.h>
#endif
char **XAttrList=NULL;
void SetupXAttrList(const char *Arg)
{
char *Token=NULL;
const char *ptr;
//count starts at 1 because even if the first string is an empty string, we need to alloc space
//for it.
int count=1;
ptr=strchr(Arg, ',');
while (ptr)
{
count++;
ptr++;
ptr=strchr(ptr, ',');
}
//count+1 to allow room for terminating NULL
XAttrList=calloc(count+1,sizeof(char *));
count=0;
ptr=GetToken(Arg, ",",&Token,0);
while (ptr)
{
XAttrList[count]=CopyStr(XAttrList[count],Token);
ptr=GetToken(ptr, ",",&Token,0);
count++;
}
Destroy(Token);
}
void HashRatSetXAttr(HashratCtx *Ctx, const char *Path, struct stat *Stat, const char *HashType, const char *Hash)
{
char *Tempstr=NULL, *Attr=NULL;
char **ptr;
int result;
Tempstr=FormatStr(Tempstr,"%llu:%llu:%s",(unsigned long long) time(NULL), (unsigned long long) Stat->st_size, Hash);
#ifdef USE_XATTR
if ((Ctx->Flags & CTX_XATTR_ROOT) && (getuid()==0)) Attr=MCopyStr(Attr,"trusted.","hashrat:",HashType,NULL);
else Attr=MCopyStr(Attr,"user.","hashrat:",HashType,NULL);
result=setxattr(Path, Attr, Tempstr, StrLen(Tempstr)+1, 0);
if (result != 0) fprintf(stderr,"ERROR: Failed to store hash in extended attributes for '%s' error was '%s'\n", Path, strerror(errno));
#elif defined USE_EXTATTR
Attr=MCopyStr(Attr,"hashrat:",HashType,NULL);
if ((Ctx->Flags & CTX_XATTR_ROOT) && (getuid()==0)) result=extattr_set_file(Path, EXTATTR_NAMESPACE_SYSTEM, Attr, Tempstr, StrLen(Tempstr)+1);
else result=extattr_set_file(Path, EXTATTR_NAMESPACE_USER, Attr, Tempstr, StrLen(Tempstr)+1);
if (result < 1) fprintf(stderr,"ERROR: Failed to store hash in extended attributes for '%s' error was '%s'\n", Path, strerror(errno));
#else
fprintf(stderr,"XATTR Support not compiled in.\n");
exit(1);
#endif
if (XAttrList)
{
for(ptr=XAttrList; *ptr !=NULL; ptr++)
{
#ifdef USE_XATTR
setxattr(Path, *ptr, Hash, StrLen(Hash), 0);
#elif defined USE_EXTATTR
extattr_set_file(Path, EXTATTR_NAMESPACE_USER, Attr, Tempstr, StrLen(Tempstr)+1);
#endif
}
}
Destroy(Tempstr);
Destroy(Attr);
}
int XAttrGetHash(HashratCtx *Ctx, const char *XattrType, const char *HashType, const char *Path, struct stat *FStat, char **Hash)
{
int result=FALSE;
char *Tempstr=NULL;
const char *ptr;
int len;
*Hash=SetStrLen(*Hash,255);
#ifdef USE_XATTR
Tempstr=MCopyStr(Tempstr,XattrType, ".hashrat:",HashType,NULL);
len=getxattr(Path, Tempstr, *Hash, 255);
#elif defined USE_EXTATTR
Tempstr=MCopyStr(Tempstr,"hashrat:",HashType,NULL);
if ((strcmp(XattrType,"trusted")==0) || (strcmp(XattrType,"system")==0)) len=extattr_get_file(Path, EXTATTR_NAMESPACE_SYSTEM, Tempstr, *Hash, 255);
else len=extattr_get_file(Path, EXTATTR_NAMESPACE_USER, Tempstr, *Hash, 255);
#else
fprintf(stderr,"XATTR Support not compiled in.\n");
#endif
if (len > 0)
{
(*Hash)[len]='\0';
ptr=*Hash;
ptr=GetToken(ptr, ":", &Tempstr, 0);
if (FStat) FStat->st_mtime=(time_t) strtoll(Tempstr,NULL,10);
ptr=GetToken(ptr, ":", &Tempstr, 0);
if (FStat) FStat->st_size=(off_t) strtoll(Tempstr,NULL,10);
len=StrLen(ptr);
memmove(*Hash,ptr,len+1);
result=TRUE;
}
Destroy(Tempstr);
return(result);
}
TFingerprint *XAttrLoadHash(HashratCtx *Ctx, const char *Path)
{
char *XattrTypes[]= {"trusted","user",NULL};
TFingerprint *FP=NULL;
char *Tempstr=NULL;
int i, j, found=FALSE;
FP=(TFingerprint *) calloc(1, sizeof(TFingerprint));
FP->Path=CopyStr(FP->Path, Path);
FP->Hash=SetStrLen(FP->Hash,1024);
for (j=0; XattrTypes[j]; j++)
{
if (StrValid(Ctx->HashType))
{
found=XAttrGetHash(Ctx, XattrTypes[j], Ctx->HashType, Path, &FP->FStat, &FP->Hash);
FP->HashType=CopyStr(FP->HashType,Ctx->HashType);
}
else for (i=0; HashratHashTypes[i] != NULL; i++)
{
found=XAttrGetHash(Ctx, XattrTypes[j], HashratHashTypes[i], Path, &FP->FStat, &FP->Hash);
if (found > 0)
{
FP->HashType=CopyStr(FP->HashType,HashratHashTypes[i]);
break;
}
}
if (found) break;
}
Destroy(Tempstr);
if (! found)
{
TFingerprintDestroy(FP);
return(NULL);
}
return(FP);
}