This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathnvidia-apply-extra.c
311 lines (248 loc) · 6.29 KB
/
nvidia-apply-extra.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
309
310
311
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <archive.h>
#include <archive_entry.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int nvidia_major_version;
void
die_with_error (const char *format, ...)
{
va_list args;
int errsv;
errsv = errno;
va_start (args, format);
vfprintf (stderr, format, args);
va_end (args);
fprintf (stderr, ": %s\n", strerror (errsv));
exit (1);
}
void
die_with_libarchive (struct archive *ar, const char *format)
{
fprintf (stderr, format, archive_error_string (ar));
exit (1);
}
void
die (const char *format, ...)
{
va_list args;
va_start (args, format);
vfprintf (stderr, format, args);
va_end (args);
fprintf (stderr, "\n");
exit (1);
}
static int
has_prefix (const char *str,
const char *prefix)
{
return strncmp (str, prefix, strlen (prefix)) == 0;
}
static int
has_suffix (const char *str, const char *suffix)
{
int str_len;
int suffix_len;
str_len = strlen (str);
suffix_len = strlen (suffix);
if (str_len < suffix_len)
return 0;
return strcmp (str + str_len - suffix_len, suffix) == 0;
}
static void
copy_archive (struct archive *ar,
struct archive *aw)
{
int r;
const void *buff;
size_t size;
int64_t offset;
while (1)
{
r = archive_read_data_block (ar, &buff, &size, &offset);
if (r == ARCHIVE_EOF)
break;
if (r != ARCHIVE_OK)
die_with_libarchive (ar, "archive_write_data_block: %s");
r = archive_write_data_block (aw, buff, size, offset);
if (r != ARCHIVE_OK)
die_with_libarchive (aw, "archive_write_data_block: %s");
}
}
static int
should_extract (struct archive_entry *entry)
{
const char *path = archive_entry_pathname (entry);
if (has_prefix (path, "./"))
path += 2;
/* Skip these as we're using GLVND on majod > 367*/
if (nvidia_major_version > 367 &&
(has_prefix (path, "libGL.so") ||
has_prefix (path, "libEGL.so")))
return 0;
if ((has_prefix (path, "lib") ||
has_prefix (path, "tls/lib"))&&
has_suffix (path, ".so." NVIDIA_VERSION))
return 1;
if (strcmp (path, "nvidia_icd.json") == 0)
{
archive_entry_set_pathname (entry, "./vulkan/icd.d/nvidia_icd.json");
return 1;
}
if (strcmp (path, "10_nvidia.json") == 0)
{
archive_entry_set_pathname (entry, "./glvnd/egl_vendor.d/10_nvidia.json");
return 1;
}
return 0;
}
static void
extract (int fd)
{
struct archive *a;
struct archive *ext;
struct archive_entry *entry;
int r;
a = archive_read_new ();
ext = archive_write_disk_new ();
archive_read_support_format_tar (a);
archive_read_support_filter_xz (a);
archive_read_support_filter_gzip (a);
if ((r = archive_read_open_fd (a, fd, 16*1024)))
die_with_libarchive (a, "archive_read_open_fd: %s");
while (1)
{
r = archive_read_next_header (a, &entry);
if (r == ARCHIVE_EOF)
break;
if (r != ARCHIVE_OK)
die_with_libarchive (a, "archive_read_next_header: %s");
if (!should_extract (entry))
continue;
r = archive_write_header (ext, entry);
if (r != ARCHIVE_OK)
die_with_libarchive (ext, "archive_write_header: %s");
else
{
copy_archive (a, ext);
r = archive_write_finish_entry (ext);
if (r != ARCHIVE_OK)
die_with_libarchive (ext, "archive_write_finish_entry: %s");
}
}
archive_read_close (a);
archive_read_free (a);
archive_write_close (ext);
archive_write_free (ext);
}
static int
find_skip_lines (int fd)
{
char buffer[1024];
ssize_t size;
char *line_start, *line_end, *buffer_end;
char *skip_str = NULL;
int skip_lines;
size = pread (fd, buffer, sizeof buffer - 1, 0);
if (size == -1)
die_with_error ("read extra data");
buffer[size] = 0; /* Ensure zero termination */
buffer_end = buffer + size;
line_start = buffer;
while (line_start < buffer_end)
{
line_end = strchr (line_start, '\n');
if (line_end != NULL)
{
*line_end = 0;
line_end += 1;
}
else
line_end = buffer_end;
if (has_prefix (line_start, "skip="))
{
skip_str = line_start + 5;
break;
}
line_start = line_end;
}
if (skip_str == NULL)
die ("Can't find skip size");
skip_lines = atoi (skip_str);
if (skip_lines == 0)
die ("Can't parse skip=%s", skip_str);
return skip_lines;
}
static off_t
find_line_offset (int fd, int skip_lines)
{
off_t offset, buf_offset;
int n_lines;
ssize_t size;
char buffer[16*1024];
offset = 0;
buf_offset = 0;
n_lines = 1;
while (1)
{
size = pread (fd, buffer, sizeof buffer, offset);
if (size == -1)
die_with_error ("read data");
buf_offset = 0;
while (buf_offset < size)
{
if (buffer[buf_offset] == '\n')
{
n_lines ++;
if (n_lines == skip_lines)
return offset + buf_offset + 1;
}
buf_offset++;
}
offset += size;
}
/* Should not happen */
return 0;
}
int
main (int argc, char *argv[])
{
int fd;
int skip_lines;
off_t tar_start;
nvidia_major_version = atoi (NVIDIA_VERSION);
fd = open (NVIDIA_BASENAME, O_RDONLY);
if (fd == -1)
die_with_error ("open extra data");
skip_lines = find_skip_lines (fd);
tar_start = find_line_offset (fd, skip_lines);
if (lseek (fd, tar_start, SEEK_SET)!= tar_start)
die ("Can't seek to tar");
extract (fd);
close (fd);
unlink (NVIDIA_BASENAME);
if (nvidia_major_version > 367)
{
/* GLVND */
/* Default to nvidia */
symlink ("libGLX_nvidia.so." NVIDIA_VERSION, "libGLX_indirect.so.0");
/* unversioned */
symlink ("libEGL_nvidia.so." NVIDIA_VERSION, "libEGL_nvidia.so.0");
symlink ("libGLESv2_nvidia.so." NVIDIA_VERSION, "libGLESv2_nvidia.so.2");
symlink ("libGLX_nvidia.so." NVIDIA_VERSION, "libGLX_nvidia.so.0");
}
else
{
/* Non GLVND */
symlink ("libEGL.so." NVIDIA_VERSION, "libEGL.so.1");
symlink ("libGL.so." NVIDIA_VERSION, "libGL.so.1");
symlink ("libglx.so." NVIDIA_VERSION, "libglx.so.1");
symlink ("libGLESv2.so." NVIDIA_VERSION, "libGLESv2.so.1");
}
return 0;
}