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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/*******************************************************************/
/* slibtool: a skinny libtool implementation, written in C */
/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */
/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
/*******************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <slibtool/slibtool.h>
#include "slibtool_driver_impl.h"
#include "slibtool_uninstall_impl.h"
#include "slibtool_readlink_impl.h"
#include "slibtool_snprintf_impl.h"
#include "slibtool_errinfo_impl.h"
#include "argv/argv.h"
static int slbt_uninstall_usage(
int fdout,
const char * program,
const char * arg,
const struct argv_option ** optv,
struct argv_meta * meta,
int noclr)
{
char header[512];
snprintf(header,sizeof(header),
"Usage: %s --mode=uninstall <rm> [options] [DEST]...\n"
"Options:\n",
program);
switch (noclr) {
case 0:
slbt_argv_usage(fdout,header,optv,arg);
break;
default:
slbt_argv_usage_plain(fdout,header,optv,arg);
break;
}
slbt_argv_free(meta);
return SLBT_USAGE;
}
static int slbt_exec_uninstall_fail(
struct slbt_exec_ctx * actx,
struct argv_meta * meta,
int ret)
{
slbt_argv_free(meta);
slbt_free_exec_ctx(actx);
return ret;
}
static int slbt_exec_uninstall_fs_entry(
const struct slbt_driver_ctx * dctx,
struct slbt_exec_ctx * ectx,
char ** parg,
char * path,
uint32_t flags)
{
struct stat st;
int fdcwd;
char * slash;
char dpath[PATH_MAX];
/* fdcwd */
fdcwd = slbt_driver_fdcwd(dctx);
/* needed? */
if (fstatat(fdcwd,path,&st,0) && (errno == ENOENT))
if (fstatat(fdcwd,path,&st,AT_SYMLINK_NOFOLLOW))
if (errno == ENOENT)
return 0;
/* output */
*parg = path;
if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
if (slbt_output_uninstall(dctx,ectx))
return SLBT_NESTED_ERROR(dctx);
/* directory? */
if (S_ISDIR(st.st_mode)) {
if (!unlinkat(fdcwd,path,AT_REMOVEDIR))
return 0;
else if ((errno == EEXIST) || (errno == ENOTEMPTY))
return 0;
else
return SLBT_SYSTEM_ERROR(dctx,path);
}
/* remove file or symlink entry */
if (unlinkat(fdcwd,path,0))
return SLBT_SYSTEM_ERROR(dctx,path);
/* remove empty containing directory? */
if (flags & SLBT_UNINSTALL_RMDIR) {
strcpy(dpath,path);
/* invalid (current) directory? */
if (!(slash = strrchr(dpath,'/')))
return 0;
*slash = 0;
if (unlinkat(fdcwd,dpath,AT_REMOVEDIR))
return SLBT_SYSTEM_ERROR(dctx,dpath);
}
return 0;
}
static int slbt_exec_uninstall_versioned_library(
const struct slbt_driver_ctx * dctx,
struct slbt_exec_ctx * ectx,
char ** parg,
char * rpath,
char * lpath,
const char * suffix,
uint32_t flags)
{
char * slash;
char * dot;
char * path;
char apath[PATH_MAX];
/* normalize library link path */
if (lpath[0] == '/') {
path = lpath;
} else if (!(slash = strrchr(rpath,'/'))) {
path = lpath;
} else {
strcpy(apath,rpath);
strcpy(&apath[slash-rpath+1],lpath);
path = apath;
}
/* delete associated version files */
while ((dot = strrchr(path,'.')) && (strcmp(dot,suffix))) {
if (slbt_exec_uninstall_fs_entry(dctx,ectx,parg,path,flags))
return SLBT_NESTED_ERROR(dctx);
*dot = 0;
}
return 0;
}
static int slbt_exec_uninstall_entry(
const struct slbt_driver_ctx * dctx,
struct slbt_exec_ctx * ectx,
struct argv_entry * entry,
char ** parg,
uint32_t flags)
{
int fdcwd;
const char * dsosuffix;
char * dot;
char path [PATH_MAX];
char lpath[PATH_MAX];
if (slbt_snprintf(path,
PATH_MAX - 8,
"%s",entry->arg) < 0)
return SLBT_BUFFER_ERROR(dctx);
*parg = (char *)entry->arg;
/* fdcwd */
fdcwd = slbt_driver_fdcwd(dctx);
/* remove explicit argument */
if (slbt_exec_uninstall_fs_entry(dctx,ectx,parg,path,flags))
return SLBT_NESTED_ERROR(dctx);
/* non-.la-wrapper argument? */
if (!(dot = strrchr(path,'.')))
return 0;
else if (strcmp(dot,".la"))
return 0;
/* remove .a archive as needed */
strcpy(dot,".a");
if (slbt_exec_uninstall_fs_entry(dctx,ectx,parg,path,flags))
return SLBT_NESTED_ERROR(dctx);
/* dsosuffix */
dsosuffix = dctx->cctx->settings.dsosuffix;
/* .so symlink? */
strcpy(dot,dsosuffix);
if (!(slbt_readlinkat(fdcwd,path,lpath,sizeof(lpath))))
if (slbt_exec_uninstall_versioned_library(
dctx,ectx,parg,
path,lpath,
dsosuffix,flags))
return SLBT_NESTED_ERROR(dctx);
/* .lib.a symlink? */
strcpy(dot,".lib.a");
if (!(slbt_readlinkat(fdcwd,path,lpath,sizeof(lpath))))
if (slbt_exec_uninstall_versioned_library(
dctx,ectx,parg,
path,lpath,
".lib.a",flags))
return SLBT_NESTED_ERROR(dctx);
/* .dll symlink? */
strcpy(dot,".dll");
if (!(slbt_readlinkat(fdcwd,path,lpath,sizeof(lpath))))
if (slbt_exec_uninstall_versioned_library(
dctx,ectx,parg,
path,lpath,
".dll",flags))
return SLBT_NESTED_ERROR(dctx);
/* remove .so library as needed */
strcpy(dot,dsosuffix);
if (slbt_exec_uninstall_fs_entry(dctx,ectx,parg,path,flags))
return SLBT_NESTED_ERROR(dctx);
/* remove .lib.a import library as needed */
strcpy(dot,".lib.a");
if (slbt_exec_uninstall_fs_entry(dctx,ectx,parg,path,flags))
return SLBT_NESTED_ERROR(dctx);
/* remove .dll library as needed */
strcpy(dot,".dll");
if (slbt_exec_uninstall_fs_entry(dctx,ectx,parg,path,flags))
return SLBT_NESTED_ERROR(dctx);
/* remove .exe image as needed */
strcpy(dot,".exe");
if (slbt_exec_uninstall_fs_entry(dctx,ectx,parg,path,flags))
return SLBT_NESTED_ERROR(dctx);
/* remove binary image as needed */
*dot = 0;
if (slbt_exec_uninstall_fs_entry(dctx,ectx,parg,path,flags))
return SLBT_NESTED_ERROR(dctx);
return 0;
}
int slbt_exec_uninstall(
const struct slbt_driver_ctx * dctx,
struct slbt_exec_ctx * ectx)
{
int ret;
int fdout;
char ** argv;
char ** iargv;
uint32_t flags;
struct slbt_exec_ctx * actx;
struct argv_meta * meta;
struct argv_entry * entry;
const struct argv_option * optv[SLBT_OPTV_ELEMENTS];
/* dry run */
if (dctx->cctx->drvflags & SLBT_DRIVER_DRY_RUN)
return 0;
/* context */
if (ectx)
actx = 0;
else if ((ret = slbt_get_exec_ctx(dctx,&ectx)))
return ret;
else
actx = ectx;
/* initial state, uninstall mode skin */
slbt_reset_arguments(ectx);
slbt_disable_placeholders(ectx);
iargv = ectx->cargv;
fdout = slbt_driver_fdout(dctx);
/* missing arguments? */
slbt_optv_init(slbt_uninstall_options,optv);
if (!iargv[1] && (dctx->cctx->drvflags & SLBT_DRIVER_VERBOSITY_USAGE))
return slbt_uninstall_usage(
fdout,
dctx->program,
0,optv,0,
dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_NEVER);
/* <uninstall> argv meta */
if (!(meta = slbt_argv_get(
iargv,optv,
dctx->cctx->drvflags & SLBT_DRIVER_VERBOSITY_ERRORS
? ARGV_VERBOSITY_ERRORS
: ARGV_VERBOSITY_NONE,
fdout)))
return slbt_exec_uninstall_fail(
actx,meta,
SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_UNINSTALL_FAIL));
/* dest, alternate argument vector options */
argv = ectx->altv;
flags = 0;
*argv++ = iargv[0];
for (entry=meta->entries; entry->fopt || entry->arg; entry++) {
if (entry->fopt) {
switch (entry->tag) {
case TAG_UNINSTALL_SYSROOT:
break;
case TAG_UNINSTALL_HELP:
flags |= SLBT_UNINSTALL_HELP;
break;
case TAG_UNINSTALL_VERSION:
flags |= SLBT_UNINSTALL_VERSION;
break;
case TAG_UNINSTALL_FORCE:
*argv++ = "-f";
flags |= SLBT_UNINSTALL_FORCE;
break;
case TAG_UNINSTALL_RMDIR:
*argv++ = "-d";
flags |= SLBT_UNINSTALL_RMDIR;
break;
case TAG_UNINSTALL_VERBOSE:
*argv++ = "-v";
flags |= SLBT_UNINSTALL_VERBOSE;
break;
}
}
}
/* --help */
if (flags & SLBT_UNINSTALL_HELP) {
slbt_uninstall_usage(
fdout,
dctx->program,
0,optv,meta,
dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_NEVER);
return 0;
}
/* uninstall */
ectx->argv = ectx->altv;
ectx->program = ectx->altv[0];
/* uninstall entries one at a time */
for (entry=meta->entries; entry->fopt || entry->arg; entry++)
if (!entry->fopt)
if (slbt_exec_uninstall_entry(dctx,ectx,entry,argv,flags))
return slbt_exec_uninstall_fail(
actx,meta,
SLBT_NESTED_ERROR(dctx));
slbt_argv_free(meta);
slbt_free_exec_ctx(actx);
return 0;
}
|