@@ -216,23 +216,22 @@ function copy() {
216216 # copies one file (or folder) to the same destination in the root filesystem
217217 # - it does not overwrite contents
218218 local SRC=" $1 "
219- local FILE=" $2 "
219+
220+ if [ " $SRC " == " ." -o " $SRC " == " .." ]; then
221+ p_warning " cowardly refusing to copy folder $SRC "
222+ return
223+ fi
224+
220225 local DST
221226 if [ " $VERBOSE " == " true" ]; then
222227 p_info " processing $SRC ... "
223228 fi
224229 if [ ! -e " $SRC " ]; then
225230 p_error 1 " failed to read $SRC "
226231 fi
227- if [ " $FILE " != " " ]; then
228- mkdir -p " $( dirname " $ROOTFS /$FILE " ) "
229- fi
230232 local SRCDIR=" $( dirname " $SRC " ) "
231233 DST=" $ROOTFS /$SRCDIR "
232234 mkdir -p " $DST "
233- if [ " $FILE " != " " ]; then
234- echo " $SRCDIR " >> " $ROOTFS /$FILE "
235- fi
236235 p_debug " copying $SRC to $DST "
237236 if [ -d " $SRC " ]; then
238237 cp -n -r " $SRC " " $DST "
@@ -241,6 +240,19 @@ function copy() {
241240 fi
242241}
243242
243+ # Credits fot this function go to https://stackoverflow.com/a/18898782
244+ # Return relative path from canonical absolute dir path $1 to canonical
245+ # absolute dir path $2 ($1 and/or $2 may end with one or no "/").
246+ # Does only need POSIX shell builtins (no external command)
247+ function relPath () {
248+ local common path up
249+ common=${1%/ } path=${2%/ } /
250+ while test " ${path# " $common " / } " = " $path " ; do
251+ common=${common%/* } up=../$up
252+ done
253+ path=$up ${path# " $common " / } ; path=${path%/ } ; printf %s " ${path:- .} "
254+ }
255+
244256function is_plugin_active() {
245257 # Checks whether a plugin is activated or not
246258 if [[ " $PLUGINS_ACTIVATED " =~ (^| ,)$1 (| :[^,]+)(,| $) ]]; then
@@ -271,7 +283,41 @@ function plugin_parameter() {
271283 return 1
272284}
273285
274- function PLUGIN_00_folder() {
286+ function PLUGIN_00_link() {
287+ # If the path is a link to other path, we will create the link and analyze the real path
288+ local L_PATH=" $1 "
289+
290+ if [ -h " $L_PATH " ]; then
291+ local L_DST=" $ROOTFS /$( dirname " $L_PATH " ) "
292+ local R_PATH=" $( readlink -f " $L_PATH " ) "
293+ local R_DST=" $ROOTFS /$( dirname " $R_PATH " ) "
294+ mkdir -p " $L_DST "
295+
296+ if [ ! -e " $L_DST /$( basename $L_PATH ) " ]; then
297+ local REL_PATH=" $( relPath " $L_DST " " $R_DST " ) "
298+ p_debug " $L_PATH is a link to $REL_PATH /$( basename $R_PATH ) "
299+ ln -s $REL_PATH /$( basename $R_PATH ) $L_DST /$( basename $L_PATH )
300+ fi
301+
302+ add_command " $R_PATH "
303+ return 1
304+ fi
305+ }
306+
307+ function PLUGIN_01_which() {
308+ # This plugin tries to guess whether the command to analize is in the path or not.
309+ # If the command can be obtained calling which, we'll analyze the actual command and not the short name.
310+ local S_PATH=" $1 "
311+ local W_PATH=" $( which $S_PATH ) "
312+
313+ if [ " $W_PATH " != " " -a " $W_PATH " != " $S_PATH " ]; then
314+ p_debug " $1 is $W_PATH "
315+ add_command " $W_PATH "
316+ return 1
317+ fi
318+ }
319+
320+ function PLUGIN_02_folder() {
275321 # If it is a folder, just copy it to its location in the new FS
276322 local S_PATH=" $1 "
277323
@@ -289,6 +335,7 @@ function PLUGIN_09_ldd() {
289335 local S_PATH=" $1 "
290336 local LIBS= LIB=
291337 local COMMAND=" $( which -- $S_PATH ) "
338+ local LIB_DIR=
292339 if [ " $COMMAND " == " " ]; then
293340 COMMAND=" $S_PATH "
294341 fi
@@ -304,7 +351,12 @@ function PLUGIN_09_ldd() {
304351 if [ $? -eq 0 ]; then
305352 LIBS=" $( ldd " $COMMAND " | grep -v ' linux-vdso' | grep -v ' statically' | sed ' s/^[ \t]*//g' | sed ' s/^.* => //g' | sed ' s/(.*)//' | sed ' /^[ ]*$/d' ) "
306353 for LIB in $LIBS ; do
307- copy " $LIB " " $LDCONFIGGILE "
354+ # Here we build the ld config file to add the new paths where the libraries are located
355+ if [ " $LDCONFIGFILE " != " " ]; then
356+ LIB_DIR=" $( dirname " $LIB " ) "
357+ mkdir -p " $ROOTFS /$( dirname $LDCONFIGFILE ) "
358+ echo " $LIB_DIR " >> " $ROOTFS /$LDCONFIGFILE "
359+ fi
308360 add_command " $LIB "
309361 done
310362 fi
@@ -465,7 +517,8 @@ function PLUGIN_11_scripts() {
465517 # If it is, adds the interpreter to the list of commands to add to the container
466518 p_debug " trying to guess if $1 is a interpreted script"
467519
468- local S_PATH=" $( which -- $1 ) "
520+ local S_PATH=" $( which $1 ) "
521+ local ADD_PATHS=
469522
470523 if [ " $S_PATH " == " " ]; then
471524 p_warning " $1 cannot be executed (if it should, please check the path)"
@@ -477,16 +530,26 @@ function PLUGIN_11_scripts() {
477530 FILE_RES=" ${FILE_RES,,} "
478531 local SHELL_EXEC=
479532 case " $FILE_RES " in
480- python) SHELL_EXEC=$( which python) ;;
533+ python) SHELL_EXEC=$( which python)
534+ ADD_PATHS=" $( python -c ' import sys;print "\n".join(sys.path)' | grep -v ' /home' ) " ;;
481535 " bourne-again shell" ) SHELL_EXEC=$( which bash) ;;
482- " a /usr/bin/perl" ) SHELL_EXEC=$( which perl) ;;
536+ " a /usr/bin/perl" ) SHELL_EXEC=$( which perl)
537+ ADD_PATHS=" $( perl -e " print qq(@INC)" | tr ' ' ' \n' | grep -v ' /home' ) " ;;
483538 esac
484539
485540 # If we found a known interpreter, we'll add to the list of dependencies
486541 if [ " $SHELL_EXEC " != " " ]; then
487542 p_debug " found that $S_PATH needs $SHELL_EXEC "
488543 add_command " $SHELL_EXEC "
489544 fi
545+ # The include folders
546+ if [ " $ADD_PATHS " != " " ]; then
547+ p_debug " found that $S_PATH needs $ADD_PATHS "
548+ local P
549+ while read P; do
550+ add_command " $P "
551+ done <<< " $ADD_PATHS"
552+ fi
490553 return 0
491554}
492555
@@ -504,7 +567,7 @@ function plugin_list() {
504567}
505568
506569# Now we are activating the basic plugins
507- PLUGINS_ACTIVATED=folder,ldd
570+ PLUGINS_ACTIVATED=link,which, folder,ldd
508571COMMANDS_TO_ADD=()
509572FORCEFOLDER=false
510573ROOTFS=
@@ -533,7 +596,7 @@ while [ $n -lt ${#ARR[@]} ]; do
533596 --quiet|-q) QUIET=true;;
534597 --tarfile|-t) n=$(( $n + 1 ))
535598 TARFILE=" ${ARR[$n]} " ;;
536- --ldconfig|-l) LDCONFIGGILE =/etc/ld.so.conf;;
599+ --ldconfig|-l) LDCONFIGFILE =/etc/ld.so.conf;;
537600 --verbose|-v) VERBOSE=true;;
538601 --debug) DEBUG=true;;
539602 --force|-f) FORCEFOLDER=true;;
@@ -606,8 +669,11 @@ while [ $i_current -lt ${#COMMANDS_TO_ADD[@]} ]; do
606669 i_current=$(( $i_current + 1 ))
607670done
608671
609- if [ " $LDCONFIGGILE " != " " -a -e " $ROOTFS /$LDCONFIGGILE " ]; then
672+ if [ " $LDCONFIGFILE " != " " -a -e " $ROOTFS /$LDCONFIGFILE " ]; then
610673 p_debug " creating ldconfig"
674+ TMPFILE=$( tempfile)
675+ awk ' !a[$0]++' " $ROOTFS /$LDCONFIGFILE " > " $TMPFILE "
676+ mv " $TMPFILE " " $ROOTFS /$LDCONFIGFILE "
611677 ldconfig -r " $ROOTFS "
612678fi
613679
0 commit comments