-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.c
239 lines (186 loc) · 5.78 KB
/
common.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
#include "common.h"
#include "config.h"
#include <fnmatch.h>
#define DEFAULT_WINEPREFIX "$(sommelier_root)$(name)-$(platform)/"
const char *ResolveVar(ListNode *Vars, const char *VarName)
{
char *Tempstr=NULL;
const char *ptr;
ptr=GetVar(Vars, VarName);
Tempstr=SubstituteVarsInString(Tempstr, ptr, Vars, 0);
SetVar(Vars, VarName, Tempstr);
Destroy(Tempstr);
return(GetVar(Vars, VarName));
}
char *FormatPath(char *RetStr, const char *Fmt)
{
ListNode *Vars;
Vars=ListCreate();
SetVar(Vars, "homedir", GetCurrUserHomeDir());
SetVar(Vars, "install_prefix", INSTALL_PREFIX);
RetStr=SubstituteVarsInString(RetStr, Fmt, Vars, 0);
ListDestroy(Vars, Destroy);
return(RetStr);
}
char *URLBasename(char *RetStr, const char *URL)
{
char *Tempstr=NULL;
char *ptr;
Tempstr=CopyStr(Tempstr, URL);
StrTruncChar(Tempstr, '?');
StripDirectorySlash(Tempstr);
RetStr=CopyStr(RetStr, GetBasename(Tempstr));
Destroy(Tempstr);
return(RetStr);
}
int InList(const char *Item, const char *List)
{
char *Match=NULL, *ItemLwr=NULL;
const char *ptr;
if (! StrValid(Item)) return(FALSE);
//strlwr everything to handle case
ItemLwr=CopyStr(ItemLwr, Item);
strlwr(ItemLwr);
ptr=GetToken(List, ",|;", &Match, GETTOKEN_QUOTES|GETTOKEN_MULTI_SEP);
while (ptr)
{
strlwr(Match);
//if match starts with ! this inverts the sense of the match, so anything
//that *doesn't match* matches, and we have an exclusion
if (StrValid(Match))
{
if (*Match=='!')
{
if (fnmatch(Match+1, ItemLwr, 0) != 0)
{
Destroy(ItemLwr);
Destroy(Match);
return(TRUE);
}
}
else if (fnmatch(Match, ItemLwr, 0) == 0)
{
Destroy(ItemLwr);
Destroy(Match);
return(TRUE);
}
}
ptr=GetToken(ptr, ",|;", &Match, GETTOKEN_QUOTES|GETTOKEN_MULTI_SEP);
}
Destroy(ItemLwr);
Destroy(Match);
return(FALSE);
}
TAction *ActionCreate(int Type, const char *Name)
{
TAction *Act;
Act=(TAction *) calloc(1, sizeof(TAction));
Act->Type=Type;
Act->Vars=ListCreate();
Act->Name=CopyStr(Act->Name, Name);
if (StrValid(Name)) SetVar(Act->Vars, "name", Name);
SetVar(Act->Vars, "prefix_template", DEFAULT_WINEPREFIX);
SetVar(Act->Vars, "user", LookupUserName(getuid()));
SetVar(Act->Vars, "homedir", GetCurrUserHomeDir());
return(Act);
}
void ActionDestroy(TAction *Act)
{
Destroy(Act->Name);
Destroy(Act->URL);
Destroy(Act->DownName);
Destroy(Act->InstallPath);
Destroy(Act->Exec);
ListDestroy(Act->Vars, Destroy);
free(Act);
}
int CompareSha256(TAction *Act)
{
char *Hash=NULL, *Tempstr=NULL;
const char *p_ExpectedHash;
int result=FALSE;
p_ExpectedHash=GetVar(Act->Vars, "sha256");
if (StrValid(p_ExpectedHash))
{
HashFile(&Hash, "sha256", Act->SrcPath, ENCODE_HEX);
if (strcmp(Hash, p_ExpectedHash)==0) result=TRUE;
Tempstr=CopyStr(Tempstr, "");
printf(" expected sha256: [%s]\n",p_ExpectedHash);
printf(" actual sha256: [%s]\n",Hash);
Tempstr=CopyStr(Tempstr, "");
if (result) TerminalPutStr("~g~eOKAY:~w Hashes match~0\n",NULL);
else TerminalPutStr("~rERROR: Downloaded file does not match expected hash~0\n",NULL);
}
else
{
TerminalPutStr("~mNo expected hash value is configured for this download~0. This probably means that the source link is updated when a new version is released, but this means sommelier cannot confirm your download's integrity or security.\n", NULL);
if (Act->Flags & FLAG_HASH_DOWNLOAD)
{
HashFile(&Hash, "sha256", Act->SrcPath, ENCODE_HEX);
printf(" actual sha256: [%s]\n",Hash);
}
result=TRUE;
}
Destroy(Tempstr);
Destroy(Hash);
return(result);
}
//Some installers fork into background, perhaps calling 'setsid', which means we
//will no longer consider them child processes and will no longer wait for them.
//Holding open a pipe for their output seems to overcome this, and also allows us
//to suppress a lot of crap that they might print out.
void RunProgramAndConsumeOutput(const char *Cmd, const char *SpawnConfig)
{
STREAM *S;
char *Tempstr=NULL;
S=STREAMSpawnCommand(Cmd, SpawnConfig);
if (S)
{
Tempstr=STREAMReadLine(Tempstr, S);
while (Tempstr)
{
if (Config->Flags & FLAG_DEBUG) printf("DBG: %s", Tempstr);
Tempstr=STREAMReadLine(Tempstr, S);
}
STREAMClose(S);
}
Destroy(Tempstr);
while (wait(0) > 1);
}
int ParseBool(const char *Value)
{
if (! StrValid(Value)) return(FALSE);
if (atoi(Value) > 0) return(TRUE);
if (*Value=='y') return(TRUE);
if (*Value=='Y') return(TRUE);
if (strcasecmp(Value, "true")==0) return(TRUE);
return(FALSE);
}
int GetBoolVar(ListNode *Vars, const char *Name)
{
return(ParseBool(GetVar(Vars, Name)));
}
char *GlobNoCase(char *RetStr, const char *Dir, const char *Name)
{
char *Tempstr=NULL;
const char *ptr;
glob_t Glob;
int i;
RetStr=CopyStr(RetStr, "");
//we go through all the files, and find any that match the request
//but we do a case insensitive match
Tempstr=MCopyStr(Tempstr, Dir, "/*", NULL);
glob(Tempstr, 0, 0, &Glob);
for (i=0; i < Glob.gl_pathc; i++)
{
ptr=GetBasename(Glob.gl_pathv[i]);
if (pmatch_one(Name, ptr, StrLen(ptr), NULL, NULL, PMATCH_NOCASE))
{
RetStr=CopyStr(RetStr, Glob.gl_pathv[i]);
break;
}
}
globfree(&Glob);
Destroy(Tempstr);
return(RetStr);
}