diff options
Diffstat (limited to 'src/logic/slbt_exec_install.c')
-rw-r--r-- | src/logic/slbt_exec_install.c | 231 |
1 files changed, 116 insertions, 115 deletions
diff --git a/src/logic/slbt_exec_install.c b/src/logic/slbt_exec_install.c index 4461d0b..228d2ff 100644 --- a/src/logic/slbt_exec_install.c +++ b/src/logic/slbt_exec_install.c @@ -1,6 +1,6 @@ /*******************************************************************/ -/* slibtool: a skinny libtool implementation, written in C */ -/* Copyright (C) 2016--2021 Z. Gilboa */ +/* slibtool: a strong libtool implementation, written in C */ +/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */ /* Released under the Standard MIT License; see COPYING.SLIBTOOL. */ /*******************************************************************/ @@ -12,8 +12,6 @@ #include <errno.h> #include <sys/stat.h> -#define ARGV_DRIVER - #include <slibtool/slibtool.h> #include "slibtool_driver_impl.h" #include "slibtool_install_impl.h" @@ -22,6 +20,7 @@ #include "slibtool_spawn_impl.h" #include "slibtool_symlink_impl.h" #include "slibtool_errinfo_impl.h" +#include "slibtool_snprintf_impl.h" #include "argv/argv.h" static int slbt_install_usage( @@ -41,26 +40,26 @@ static int slbt_install_usage( switch (noclr) { case 0: - argv_usage(fdout,header,optv,arg); + slbt_argv_usage(fdout,header,optv,arg); break; default: - argv_usage_plain(fdout,header,optv,arg); + slbt_argv_usage_plain(fdout,header,optv,arg); break; } - argv_free(meta); + slbt_argv_free(meta); return SLBT_USAGE; } static int slbt_exec_install_fail( - struct slbt_exec_ctx * actx, + struct slbt_exec_ctx * ectx, struct argv_meta * meta, int ret) { - argv_free(meta); - slbt_free_exec_ctx(actx); + slbt_argv_free(meta); + slbt_ectx_free_exec_ctx(ectx); return ret; } @@ -83,8 +82,8 @@ static int slbt_exec_install_init_dstdir( last = dest; /* dstdir: initial string */ - if ((size_t)snprintf(dstdir,PATH_MAX,"%s", - last->arg) >= PATH_MAX) + if (slbt_snprintf(dstdir,PATH_MAX, + "%s",last->arg) < 0) return SLBT_BUFFER_ERROR(dctx); /* dstdir might end with a slash */ @@ -126,8 +125,8 @@ static int slbt_exec_install_import_libraries( char rev [128]; /* .libs/libfoo.so.x.y.z */ - if ((size_t)snprintf(srcbuf,sizeof(srcbuf),"%s", - srcdso) >= sizeof(srcbuf)) + if (slbt_snprintf(srcbuf,sizeof(srcbuf), + "%s",srcdso) <0) return SLBT_BUFFER_ERROR(dctx); /* (dso is under .libs) */ @@ -135,9 +134,11 @@ static int slbt_exec_install_import_libraries( return SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_INSTALL_FLOW); /* libfoo.so.x.y.z */ - if ((size_t)snprintf(implib,sizeof(implib),"%s", - ++slash) >= sizeof(implib) - - strlen(dctx->cctx->settings.impsuffix)) + const char * impsuffix = dctx->cctx->settings.impsuffix; + + if (slbt_snprintf(implib, + sizeof(implib) - strlen(impsuffix), + "%s",++slash) < 0) return SLBT_BUFFER_ERROR(dctx); /* guard against an infinitely long version */ @@ -190,7 +191,7 @@ static int slbt_exec_install_import_libraries( dctx->cctx->asettings.impsuffix); /* copy: .libs/libfoo.x.y.z.lib.a --> dstdir */ - if (slbt_copy_file(dctx,ectx,srcbuf,dstdir)) + if (slbt_util_copy_file(ectx,srcbuf,dstdir)) return SLBT_NESTED_ERROR(dctx); /* .libs/libfoo.x.lib.a */ @@ -199,15 +200,15 @@ static int slbt_exec_install_import_libraries( dctx->cctx->asettings.impsuffix); /* copy: .libs/libfoo.x.lib.a --> dstdir */ - if (slbt_copy_file(dctx,ectx,srcbuf,dstdir)) + if (slbt_util_copy_file(ectx,srcbuf,dstdir)) return SLBT_NESTED_ERROR(dctx); /* /dstdir/libfoo.lib.a */ strcpy(implib,slash); strcpy(dot,dctx->cctx->asettings.impsuffix); - if ((size_t)snprintf(hostlnk,sizeof(hostlnk),"%s/%s", - dstdir,slash) >= sizeof(hostlnk)) + if (slbt_snprintf(hostlnk,sizeof(hostlnk), + "%s/%s",dstdir,slash) <0) return SLBT_BUFFER_ERROR(dctx); if (slbt_create_symlink( @@ -244,13 +245,14 @@ static int slbt_exec_install_library_wrapper( base = entry->arg; /* /dstdir/libfoo.la */ - if ((size_t)snprintf(instname,sizeof(instname),"%s/%s", - dstdir,base) >= sizeof(instname)) + if (slbt_snprintf(instname,sizeof(instname), + "%s/%s",dstdir,base) < 0) return SLBT_BUFFER_ERROR(dctx); /* libfoo.la.slibtool.install */ - if ((size_t)snprintf(clainame,sizeof(clainame),"%s.slibtool.install", - entry->arg) >= sizeof(clainame)) + if (slbt_snprintf(clainame,sizeof(clainame), + "%s.slibtool.install", + entry->arg) < 0) return SLBT_BUFFER_ERROR(dctx); /* fdcwd */ @@ -307,8 +309,8 @@ static int slbt_exec_install_library_wrapper( close(fddst); slbt_unmap_file(mapinfo); - /* cp libfoo.la.slibtool.instal /dstdir/libfoo.la */ - if (slbt_copy_file(dctx,ectx,clainame,instname)) + /* cp libfoo.la.slibtool.install /dstdir/libfoo.la */ + if (slbt_util_copy_file(ectx,clainame,instname)) return SLBT_NESTED_ERROR(dctx); return 0; @@ -324,7 +326,6 @@ static int slbt_exec_install_entry( char ** src, char ** dst) { - int ret; int fdcwd; const char * base; char * dot; @@ -345,8 +346,6 @@ static int slbt_exec_install_entry( bool fpe; bool frelease; bool fdualver; - bool fstatic; - bool farchive; size_t slen; size_t dlen; struct stat st; @@ -359,9 +358,9 @@ static int slbt_exec_install_entry( mark = &slnkname[base - entry->arg]; slen = sizeof(slnkname) - (mark - slnkname); - if ((size_t)snprintf(mark,slen, + if (slbt_snprintf(mark,slen, ".libs/%s.exe.wrapper", - base) >= slen) + base) < 0) return SLBT_BUFFER_ERROR(dctx); /* fdcwd */ @@ -396,19 +395,27 @@ static int slbt_exec_install_entry( *dst = dest ? 0 : (char *)last->arg; if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT)) - if (slbt_output_install(dctx,ectx)) + if (slbt_output_install(ectx)) return SLBT_NESTED_ERROR(dctx); - return (((ret = slbt_spawn(ectx,true)) < 0) || ectx->exitcode) - ? SLBT_SPAWN_ERROR(dctx) : 0; + if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) { + return SLBT_SPAWN_ERROR(dctx); + + } else if (ectx->exitcode) { + return SLBT_CUSTOM_ERROR( + dctx, + SLBT_ERR_INSTALL_ERROR); + } + + return 0; } /* -shrext, dsosuffix */ strcpy(sobuf,dctx->cctx->settings.dsosuffix); dsosuffix = sobuf; - if ((size_t)snprintf(slnkname,sizeof(slnkname),"%s.shrext", - srcfile) >= sizeof(slnkname)) + if (slbt_snprintf(slnkname,sizeof(slnkname), + "%s.shrext",srcfile) < 0) return SLBT_BUFFER_ERROR(dctx); if (!fstatat(fdcwd,slnkname,&st,0)) { @@ -439,6 +446,14 @@ static int slbt_exec_install_entry( dot = strrchr(srcfile,'.'); strcpy(dot,dctx->cctx->settings.arsuffix); + /* libfoo.a installation */ + if (!slbt_symlink_is_a_placeholder(fdcwd,srcfile)) + if (slbt_util_copy_file( + ectx, + srcfile, + dest ? (char *)dest->arg : *dst)) + return SLBT_NESTED_ERROR(dctx); + /* dot/suffix */ strcpy(slnkname,srcfile); dot = strrchr(slnkname,'.'); @@ -447,33 +462,29 @@ static int slbt_exec_install_entry( slen = sizeof(slnkname); slen -= (dot - slnkname); + /* static library only? */ + sprintf(dot,"%s",dsosuffix); + + if (slbt_symlink_is_a_placeholder(fdcwd,slnkname)) + return 0; + /* detect -release, exclusively or alongside -version-info */ frelease = false; fdualver = false; - fstatic = false; fpe = false; - /* static library only? */ - sprintf(dot,"%s",dsosuffix); - fstatic = slbt_symlink_is_a_placeholder(fdcwd,slnkname); - /* libfoo.a --> libfoo.so.release */ - if (!fstatic) { - sprintf(dot,"%s.release",dsosuffix); - frelease = !fstatat(fdcwd,slnkname,&st,0); - } + sprintf(dot,"%s.release",dsosuffix); + frelease = !fstatat(fdcwd,slnkname,&st,0); /* libfoo.a --> libfoo.so.dualver */ - if (!fstatic && !frelease) { + if (!frelease) { sprintf(dot,"%s.dualver",dsosuffix); fdualver = !fstatat(fdcwd,slnkname,&st,0); } /* libfoo.so.def.{flavor} */ - if (fstatic) { - (void)0; - - } else if (frelease || fdualver) { + if (frelease || fdualver) { strcpy(dlnkname,slnkname); slash = strrchr(dlnkname,'/'); @@ -509,7 +520,7 @@ static int slbt_exec_install_entry( if (slbt_readlinkat(fdcwd,dlnkname,hosttag,sizeof(hosttag))) return SLBT_SYSTEM_ERROR(dctx,slnkname); } else { - if ((size_t)snprintf(dot,slen,"%s.def.host",dsosuffix) >= slen) + if (slbt_snprintf(dot,slen,"%s.def.host",dsosuffix) < 0) return SLBT_BUFFER_ERROR(dctx); if (slbt_readlinkat(fdcwd,frelease ? dlnkname : slnkname,hosttag,sizeof(hosttag))) @@ -517,40 +528,21 @@ static int slbt_exec_install_entry( } /* host/flabor */ - if (fstatic) { - (void)0; - - } else if (!(host = strrchr(hosttag,'.'))) { + if (!(host = strrchr(hosttag,'.'))) { return SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_INSTALL_FLOW); } else { host++; } /* symlink-based alternate host */ - if (!fstatic) { - if (slbt_set_alternate_host(dctx,host,host)) - return SLBT_NESTED_ERROR(dctx); + if (slbt_host_set_althost(dctx,host,host)) + return SLBT_NESTED_ERROR(dctx); - fpe = !strcmp(dctx->cctx->asettings.imagefmt,"pe"); - } + fpe = !strcmp(dctx->cctx->asettings.imagefmt,"pe"); /* libfoo.a --> libfoo.so */ strcpy(dot,dsosuffix); - /* libfoo.a installation */ - if (!(dctx->cctx->drvflags & SLBT_DRIVER_DISABLE_STATIC)) - farchive = true; - else if (fstatic) - farchive = true; - else - farchive = false; - - if (farchive) - if (slbt_copy_file(dctx,ectx, - srcfile, - dest ? (char *)dest->arg : *dst)) - return SLBT_NESTED_ERROR(dctx); - /* basename */ if ((base = strrchr(slnkname,'/'))) base++; @@ -559,17 +551,13 @@ static int slbt_exec_install_entry( /* source (build) symlink target */ if (slbt_readlinkat(fdcwd,slnkname,target,sizeof(target)) < 0) { - /* -all-static? */ - if (fstatic) - return 0; - /* -avoid-version? */ if (fstatat(fdcwd,slnkname,&st,0)) return SLBT_SYSTEM_ERROR(dctx,slnkname); /* dstfile */ - if ((size_t)snprintf(dstfile,sizeof(dstfile),"%s/%s", - dstdir,base) >= sizeof(dstfile)) + if (slbt_snprintf(dstfile,sizeof(dstfile), + "%s/%s",dstdir,base) < 0) return SLBT_BUFFER_ERROR(dctx); /* single spawn, no symlinks */ @@ -577,12 +565,18 @@ static int slbt_exec_install_entry( *dst = dest ? 0 : dstfile; if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT)) - if (slbt_output_install(dctx,ectx)) + if (slbt_output_install(ectx)) return SLBT_NESTED_ERROR(dctx); - if (((ret = slbt_spawn(ectx,true)) < 0) || ectx->exitcode) + if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) { return SLBT_SPAWN_ERROR(dctx); + } else if (ectx->exitcode) { + return SLBT_CUSTOM_ERROR( + dctx, + SLBT_ERR_INSTALL_ERROR); + } + return 0; } @@ -592,8 +586,8 @@ static int slbt_exec_install_entry( /* dstfile */ if (!dest) - if ((size_t)snprintf(dstfile,sizeof(dstfile),"%s/%s", - dstdir,target) >= sizeof(dstfile)) + if (slbt_snprintf(dstfile,sizeof(dstfile), + "%s/%s",dstdir,target) < 0) return SLBT_BUFFER_ERROR(dctx); /* spawn */ @@ -601,15 +595,21 @@ static int slbt_exec_install_entry( *dst = dest ? 0 : dstfile; if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT)) - if (slbt_output_install(dctx,ectx)) + if (slbt_output_install(ectx)) return SLBT_NESTED_ERROR(dctx); - if (((ret = slbt_spawn(ectx,true)) < 0) || ectx->exitcode) + if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) { return SLBT_SPAWN_ERROR(dctx); + } else if (ectx->exitcode) { + return SLBT_CUSTOM_ERROR( + dctx, + SLBT_ERR_INSTALL_ERROR); + } + /* destination symlink: dstdir/libfoo.so */ - if ((size_t)snprintf(dlnkname,sizeof(dlnkname),"%s/%s", - dstdir,base) >= sizeof(dlnkname)) + if (slbt_snprintf(dlnkname,sizeof(dlnkname), + "%s/%s",dstdir,base) < 0) return SLBT_BUFFER_ERROR(dctx); /* create symlink: libfoo.so --> libfoo.so.x.y.z */ @@ -655,14 +655,14 @@ static int slbt_exec_install_entry( } /* destination symlink: dstdir/libfoo.so.x */ - if ((size_t)snprintf(dlnkname,sizeof(dlnkname),"%s/%s", - dstdir,slnkname) >= sizeof(dlnkname)) + if (slbt_snprintf(dlnkname,sizeof(dlnkname), + "%s/%s",dstdir,slnkname) < 0) return SLBT_BUFFER_ERROR(dctx); if (fpe) { /* copy: .libs/libfoo.so.x.y.z --> libfoo.so.x */ - if (slbt_copy_file( - dctx,ectx, + if (slbt_util_copy_file( + ectx, srcfile, dlnkname)) return SLBT_NESTED_ERROR(dctx); @@ -685,11 +685,8 @@ static int slbt_exec_install_entry( return 0; } -int slbt_exec_install( - const struct slbt_driver_ctx * dctx, - struct slbt_exec_ctx * ectx) +int slbt_exec_install(const struct slbt_driver_ctx * dctx) { - int ret; int fdout; char ** argv; char ** iargv; @@ -699,7 +696,7 @@ int slbt_exec_install( char * optsh; char * script; char * shtool; - struct slbt_exec_ctx * actx; + struct slbt_exec_ctx * ectx; struct argv_meta * meta; struct argv_entry * entry; struct argv_entry * copy; @@ -713,15 +710,11 @@ int slbt_exec_install( return 0; /* context */ - if (ectx) - actx = 0; - else if ((ret = slbt_get_exec_ctx(dctx,&ectx))) - return ret; - else - actx = ectx; + if (slbt_ectx_get_exec_ctx(dctx,&ectx) < 0) + return SLBT_NESTED_ERROR(dctx); /* initial state, install mode skin */ - slbt_reset_arguments(ectx); + slbt_ectx_reset_arguments(ectx); slbt_disable_placeholders(ectx); iargv = ectx->cargv; fdout = slbt_driver_fdout(dctx); @@ -751,7 +744,7 @@ int slbt_exec_install( } /* missing arguments? */ - argv_optv_init(slbt_install_options,optv); + slbt_optv_init(slbt_install_options,optv); if (!iargv[1] && (dctx->cctx->drvflags & SLBT_DRIVER_VERBOSITY_USAGE)) return slbt_install_usage( @@ -761,14 +754,14 @@ int slbt_exec_install( dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_NEVER); /* <install> argv meta */ - if (!(meta = argv_get( + if (!(meta = slbt_argv_get( iargv,optv, dctx->cctx->drvflags & SLBT_DRIVER_VERBOSITY_ERRORS ? ARGV_VERBOSITY_ERRORS : ARGV_VERBOSITY_NONE, fdout))) return slbt_exec_install_fail( - actx,meta, + ectx,meta, SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_INSTALL_FAIL)); /* dest, alternate argument vector options */ @@ -859,7 +852,7 @@ int slbt_exec_install( /* dstdir */ if (slbt_exec_install_init_dstdir(dctx,dest,last,dstdir)) return slbt_exec_install_fail( - actx,meta, + ectx,meta, SLBT_NESTED_ERROR(dctx)); /* install entries one at a time */ @@ -871,7 +864,7 @@ int slbt_exec_install( dest,dstdir, src,dst)) return slbt_exec_install_fail( - actx,meta, + ectx,meta, SLBT_NESTED_ERROR(dctx)); } else { /* using original argument vector */ @@ -880,17 +873,25 @@ int slbt_exec_install( /* spawn */ if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT)) - if (slbt_output_install(dctx,ectx)) + if (slbt_output_install(ectx)) return SLBT_NESTED_ERROR(dctx); - if (((ret = slbt_spawn(ectx,true)) < 0) || ectx->exitcode) + if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) { return slbt_exec_install_fail( - actx,meta, + ectx,meta, SLBT_SPAWN_ERROR(dctx)); + + } else if (ectx->exitcode) { + return slbt_exec_install_fail( + ectx,meta, + SLBT_CUSTOM_ERROR( + dctx, + SLBT_ERR_INSTALL_ERROR)); + } } - argv_free(meta); - slbt_free_exec_ctx(actx); + slbt_argv_free(meta); + slbt_ectx_free_exec_ctx(ectx); return 0; } |