-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFileProperties.c
312 lines (233 loc) · 7.2 KB
/
FileProperties.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "FileProperties.h"
#include "MimeType.h"
#include "ID3.h"
#ifdef USE_XATTR
#include <sys/types.h>
#include <sys/xattr.h>
#endif
const char *PhysicalProperties[]= {"creationdate","CTime-secs", "getlastmodified","MTime-secs","getcontentlength","FileSize","getcontenttype","ContentType","executable","IsExecutable", NULL};
int SaveFileXAttr(const char * Path, ListNode *PropList)
{
ListNode *Curr;
char *Attr=NULL;
int result=FALSE;
#ifdef USE_XATTR
Curr=ListGetNext(PropList);
while (Curr)
{
if (MatchTokenFromList(Curr->Tag,PhysicalProperties,0)==-1)
{
Attr=MCopyStr(Attr,"user.",Curr->Tag,NULL);
if (setxattr(Path, Attr, (char *) Curr->Item, StrLen(Curr->Item), 0)==0) result=TRUE;
}
Curr=ListGetNext(Curr);
}
#endif
Destroy(Attr);
return(result);
}
int LoadFileXAttr(const char *Path, ListNode *PropList)
{
int result=FALSE, len;
char *AttrNamesList=NULL, *Attr=NULL, *Value=NULL;
char *ptr, *end;
#ifdef USE_XATTR
AttrNamesList=SetStrLen(AttrNamesList,BUFSIZ);
len=listxattr(Path, AttrNamesList, BUFSIZ);
if (len ==-1) return(FALSE);
end=AttrNamesList+len;
/* Loop through all EA names, displaying name + value */
for (ptr=AttrNamesList; ptr < end; ptr += strlen(ptr) + 1)
{
//paranoid check in case something goes wrong in our for loop
if (ptr > end) break;
Value=SetStrLen(Value,4096);
len=getxattr(Path, ptr, Value, 4096);
if (strncmp(ptr,"user.",5)==0) ptr+=5;
SetTypedVar(PropList,ptr,Value,FILE_USER_VALUE);
}
#endif
Destroy(Attr);
Destroy(Value);
Destroy(AttrNamesList);
return(result);
}
void LoadDirPropsFile(const char *Dir, const char *RequestedFile, ListNode *Props)
{
char *Tempstr=NULL, *Token=NULL, *FName=NULL;
const char *ptr;
STREAM *S;
Tempstr=MCopyStr(Tempstr,Dir,"/.props",NULL);
S=STREAMFileOpen(Tempstr,SF_CREAT | SF_RDWR);
if (S)
{
Tempstr=STREAMReadLine(Tempstr,S);
while (Tempstr)
{
StripTrailingWhitespace(Tempstr);
if (StrLen(Tempstr))
{
ptr=GetToken(Tempstr,"=",&Token,GETTOKEN_QUOTES);
GetToken(Token,":",&FName,GETTOKEN_QUOTES);
if ( (! StrLen(RequestedFile)) || (strcmp(RequestedFile,FName)==0)) SetTypedVar(Props,Token,ptr,FILE_USER_VALUE);
}
Tempstr=STREAMReadLine(Tempstr,S);
}
STREAMClose(S);
}
Destroy(Tempstr);
Destroy(Token);
Destroy(FName);
}
void SaveDirPropsFile(const char *Dir, const char *RequestedFile, ListNode *Props)
{
char *Tempstr=NULL, *Token=NULL;
ListNode *Curr;
STREAM *S;
Tempstr=MCopyStr(Tempstr,Dir,"/.props",NULL);
S=STREAMFileOpen(Tempstr,SF_CREAT | SF_WRONLY | SF_TRUNC);
if (S)
{
Curr=ListGetNext(Props);
while (Curr)
{
if (MatchTokenFromList(Curr->Tag,PhysicalProperties,0)==-1)
{
Tempstr=MCopyStr(Tempstr,RequestedFile,":",Curr->Tag,"=",Curr->Item,"\n",NULL);
STREAMWriteLine(Tempstr,S);
}
Curr=ListGetNext(Curr);
}
STREAMClose(S);
}
Destroy(Tempstr);
Destroy(Token);
}
//Real Properties are things like file size, mtime, etc, that are not strings defined
//by the user
int PropertiesLoadFromStream(const char *FName, STREAM *S, ListNode *Vars)
{
char *Buffer=NULL;
TFileMagic *FM=NULL;
struct stat FileStat;
int Flags=FILE_EXISTS;
if (stat(FName, &FileStat)==-1) return(FILE_NOSUCH);
Buffer=FormatStr(Buffer, "%llu", (unsigned long long) FileStat.st_size);
SetVar(Vars, "FileSize", Buffer);
Buffer=FormatStr(Buffer, "%d", FileStat.st_ctime);
SetVar(Vars, "CTime-Secs", Buffer);
Buffer=FormatStr(Buffer, "%d", FileStat.st_mtime);
SetVar(Vars, "MTime-Secs", Buffer);
//if it's a directory, don't both doing any more examining
if (S_ISDIR(FileStat.st_mode))
{
SetVar(Vars,"IsCollection","1");
SetVar(Vars,"ContentType","Directory");
Destroy(Buffer);
return(FILE_DIR);
}
if (! (FileStat.st_mode & S_IWUSR))
{
SetVar(Vars,"IsReadOnly","1");
Flags |= FILE_READONLY;
}
if ((FileStat.st_mode & S_IXUSR))
{
SetVar(Vars,"IsExecutable","T");
Flags |= FILE_EXEC;
}
else SetVar(Vars,"IsExecutable","F");
//This function can handle S being NULL
FM=GetFileMagicForFile(FName, S);
if (FM)
{
SetVar(Vars,"ContentType",FM->ContentType);
// if 'S' open then ExamineContents WAS set
if (S)
{
if (FM->Flags & (FM_MEDIA_TAG | FM_IMAGE_TAG)) MediaReadDetails(S, Vars);
}
}
Destroy(Buffer);
return(Flags);
}
int LoadFileRealProperties(const char *FName, int ExamineContents, ListNode *Vars)
{
STREAM *S=NULL;
int result;
if (ExamineContents) S=STREAMOpen(FName, "r");
result=PropertiesLoadFromStream(FName, S, Vars);
STREAMClose(S);
return(result);
}
int LoadFileProperties(const char *Path, ListNode *PropList)
{
char *Dir=NULL;
const char *ptr;
int FType;
ptr=GetToken(Path,"/",&Dir,0);
if (! LoadFileXAttr(Path,PropList))
{
LoadDirPropsFile(Dir, ptr, PropList);
}
FType=LoadFileRealProperties(Path, TRUE, PropList);
//Translate Some Props to DAV names
SetVar(PropList,"creationdate",GetVar(PropList,"CTime-secs"));
SetVar(PropList,"getlastmodified",GetVar(PropList,"MTime-secs"));
SetVar(PropList,"getcontentlength",GetVar(PropList,"FileSize"));
SetVar(PropList,"getcontenttype",GetVar(PropList,"ContentType"));
SetVar(PropList,"executable",GetVar(PropList,"IsExecutable"));
return(FType);
}
int SaveFileProperties(const char *Path, ListNode *PropList)
{
char *Dir=NULL;
const char *ptr;
ptr=GetToken(Path,"/",&Dir,0);
if (! SaveFileXAttr(Path,PropList))
{
SaveDirPropsFile(Dir, ptr, PropList);
}
//Translate Some Props to DAV names
/*
SetVar(PropList,"creationdate",GetVar(PropList,"CTime-secs"));
SetVar(PropList,"getlastmodified",GetVar(PropList,"MTime-secs"));
SetVar(PropList,"getcontentlength",GetVar(PropList,"FileSize"));
SetVar(PropList,"getcontenttype",GetVar(PropList,"ContentType"));
SetVar(PropList,"executable",GetVar(PropList,"IsExecutable"));
*/
return(TRUE);
}
void SetProperties(const char *File, ListNode *Props)
{
char *Token=NULL, *Dir=NULL, *FName=NULL;
ListNode *Curr, *FProps;
const char *ptr;
ptr=strrchr(File,'/');
if (ptr)
{
Dir=CopyStrLen(Dir,File,ptr-File);
ptr++;
}
else
{
Dir=CopyStr(Dir,"/");
ptr=File;
}
FName=CopyStr(FName,ptr);
FProps=ListCreate();
LoadFileProperties(File, FProps);
Curr=ListGetNext(Props);
while (Curr)
{
SetVar(FProps,Curr->Tag,(char *) Curr->Item);
LogToFile(Settings.LogPath,"SP: [%s] [%s]\n",Curr->Tag,Curr->Item);
Curr->Item=CopyStr(Curr->Item,"HTTP/1.1 200 OK");
Curr=ListGetNext(Curr);
}
SaveFileProperties(File,FProps);
ListDestroy(FProps,Destroy);
Destroy(Token);
Destroy(Dir);
Destroy(FName);
}