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
|
#include "macros.h"
#include "l2su.h"
#include <curl/easy.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
/* handcoded string functions
*
* NOTE: I am aware that this is inefficient but since these are used only a handful of times
* during initialization, I don't think performance is really a huge deal here.
*
* remember that this is a Minecraft launcher, so the PC has to meet the minimum specs of that,
* which are well above the minimum specs of this launcher */
char *l2_launcher_strapp(char *buf, const char *src)
{
size_t buflen = strlen(buf);
size_t srclen = strlen(src);
char *ret = realloc(buf, buflen + srclen + 1);
if (!ret) return NULL;
memcpy(ret + buflen, src, srclen);
ret[buflen + srclen] = '\0'; /* realloc does not initialize like calloc does */
return ret;
}
char *l2_launcher_find_config_path(void)
{
/* check for $L2SU_CONFIG */
char *config = getenv(PROJECT_NAME_UPPER "_CONFIG");
if (config) {
return strdup(config);
}
/* check for $XDG_CONFIG_HOME */
config = getenv("XDG_CONFIG_HOME");
if (config) {
/* want to append '"/" PROJECT_NAME' to our string */
char *ret = strdup(config);
if (!ret) return NULL;
return l2_launcher_strapp(ret, "/" PROJECT_NAME);
}
/* check for $HOME/.config */
config = getenv("HOME");
if (config) {
char *ret = strdup(config);
if (!ret) return NULL;
return l2_launcher_strapp(ret, "/.config/" PROJECT_NAME);
}
/* fail (do NOT attempt to find home directory from passwd */
return NULL;
}
char *l2_launcher_find_data_path(void)
{
/* check for $L2SU_DATA */
char *config = getenv(PROJECT_NAME_UPPER "_DATA");
if (config) {
return strdup(config);
}
/* check for $XDG_DATA_HOME */
config = getenv("XDG_DATA_HOME");
if (config) {
char *ret = strdup(config);
if (!ret) return NULL;
return l2_launcher_strapp(ret, "/" PROJECT_NAME);
}
/* check for $HOME/.local/share */
config = getenv("HOME");
if (config) {
char *ret = strdup(config);
if (!ret) return NULL;
return l2_launcher_strapp(ret, "/.local/share/" PROJECT_NAME);
}
return NULL;
}
int l2_launcher_open_config(const char *path, int flags, mode_t mode)
{
int conffd = open(l2_state.paths.config, O_RDONLY | O_DIRECTORY);
if (conffd < 0) return INSTANCE_ERRNO;
int instfd = openat(conffd, path, flags, mode);
int en = errno; /* back up errno because close can fail */
close(conffd);
errno = en;
return instfd;
}
/* NOTE: There's no portable (or otherwise - see open(2) BUGS) way to do this without race conditions. */
int l2_launcher_mkdir_parents(const char *path)
{
if (*path != '/') return -1;
int reserrno = 0;
char *pathbuf = strdup(path);
char *pcurelem = pathbuf;
if (!pathbuf) return -1;
struct stat stbuf = { 0 };
do { /* strtok is off-limits because it smells bad */
*pcurelem = '/';
pcurelem = strchr(pcurelem + 1, '/');
if (pcurelem) {
*pcurelem = '\0';
}
/* now pathbuf contains our truncated path name which may or may not exist */
if (mkdir(pathbuf, 0755) < 0) {
if (errno == EEXIST) {
/* racy: stat the file and continue if it is a directory */
if (stat(pathbuf, &stbuf) < 0) {
reserrno = errno;
goto mdcleanup;
}
if (!S_ISDIR(stbuf.st_mode)) {
reserrno = ENOTDIR;
goto mdcleanup;
}
} else {
reserrno = errno;
goto mdcleanup;
}
}
} while (pcurelem);
mdcleanup:
free(pathbuf);
if (reserrno != 0) {
errno = reserrno;
return -1;
}
return 0;
}
char *l2_launcher_parse_iso_time(const char *str, struct tm *ts)
{
return strptime(str, "%FT%T%z", ts); /* TODO: replace with something portable */
}
void l2_launcher_download_init(struct l2_dlbuf *buf)
{
buf->data = NULL;
buf->size = 0;
buf->capacity = 0;
}
size_t l2_launcher__download_callback(char *data, size_t size, size_t nmemb, void *user)
{
struct l2_dlbuf *buf = user;
size_t realsz = size * nmemb;
if (buf->size + realsz > buf->capacity) {
size_t newcap = 16;
while (newcap && buf->size + realsz < newcap)
newcap <<= 1;
if (!newcap) return CURLE_WRITE_ERROR;
void *newbuf = realloc(buf->data, newcap);
if (!newbuf) return CURLE_WRITE_ERROR;
buf->data = newbuf;
buf->capacity = newcap;
}
memcpy((char *)buf->data + buf->size, data, realsz);
buf->size += realsz;
return realsz;
}
void *l2_launcher_download_finalize(struct l2_dlbuf *buf, size_t *psz)
{
void *smaller = realloc(buf->data, buf->size);
if (smaller) buf->data = smaller;
void *data = buf->data;
buf->data = NULL;
*psz = buf->size;
return data;
}
void l2_launcher_download_cleanup(struct l2_dlbuf *buf)
{
free(buf->data);
}
const curl_write_callback l2_dlcb = &l2_launcher__download_callback;
int l2_launcher_download(CURL *cd, const char *url, void **odata, size_t *osize)
{
struct l2_dlbuf db;
l2_launcher_download_init(&db);
curl_easy_setopt(cd, CURLOPT_URL, url);
curl_easy_setopt(cd, CURLOPT_WRITEDATA, &db);
curl_easy_setopt(cd, CURLOPT_WRITEFUNCTION, l2_dlcb);
if (curl_easy_perform(cd) != CURLE_OK) {
l2_launcher_download_cleanup(&db);
return -1;
}
*odata = l2_launcher_download_finalize(&db, osize);
l2_launcher_download_cleanup(&db); /* not strictly necessary but ok */
return 0;
}
|