NBash

Форк
0
/
epm-sh-functions 
1052 строки · 22.7 Кб
1
#!/bin/sh
2
#
3
# Copyright (C) 2012, 2014  Etersoft
4
# Copyright (C) 2012, 2014  Vitaly Lipatov <lav@etersoft.ru>
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Affero General Public License for more details.
15
#
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#
19

20
# copied from /etc/init.d/outformat (ALT Linux)
21

22

23
# FIXME on Android: FIX ME! implement ttyname_r() bionic/libc/bionic/stubs.c:366
24
inputisatty()
25
{
26
    # check stdin
27
    #tty -s 2>/dev/null
28
    test -t 0
29
}
30

31
isatty()
32
{
33
    # check stdout
34
    test -t 1
35
}
36

37
isatty2()
38
{
39
    # check stderr
40
    test -t 2
41
}
42

43
check_tty()
44
{
45
    isatty2 || return
46

47
    # Set a sane TERM required for tput
48
    [ -n "$TERM" ] || TERM=dumb
49
    export TERM
50

51
    check_core_commands
52

53
    # grep -E from busybox may not --color
54
    # grep -E from MacOS print help to stderr
55
    if grep -E --help 2>&1 | grep -q -- "--color" ; then
56
        export EGREPCOLOR="--color"
57
    fi
58

59
    is_command tput || return
60
    # FreeBSD does not support tput -S
61
    echo | a= tput -S >/dev/null 2>/dev/null || return
62
    USETTY="tput -S"
63
}
64

65
: ${BLACK:=0} ${RED:=1} ${GREEN:=2} ${YELLOW:=3} ${BLUE:=4} ${MAGENTA:=5} ${CYAN:=6} ${WHITE:=7}
66

67
set_boldcolor()
68
{
69
    [ -n "$USETTY" ] || return
70
    {
71
        echo bold
72
        echo setaf $1
73
    } | $USETTY
74
}
75

76
set_color()
77
{
78
    [ -n "$USETTY" ] || return
79
    {
80
        echo setaf $1
81
    } | $USETTY
82
}
83

84
restore_color()
85
{
86
    [ -n "$USETTY" ] || return
87
    {
88
        echo op; # set Original color Pair.
89
        echo sgr0; # turn off all special graphics mode (bold in our case).
90
    } | $USETTY
91
}
92

93
echover()
94
{
95
    [ -z "$verbose" ] && return
96
    echo "$*" >&2
97
}
98

99
# echo string without EOL
100
echon()
101
{
102
    # default /bin/sh on MacOS does not recognize -n
103
    echo -n "$*" 2>/dev/null || a= /bin/echo -n "$*"
104
}
105

106

107
# Print command line and run command line
108
showcmd()
109
{
110
    if [ -z "$quiet" ] ; then
111
        set_boldcolor $GREEN
112
        local PROMTSIG="\$"
113
        is_root && PROMTSIG="#"
114
        echo " $PROMTSIG $*"
115
        restore_color
116
    fi >&2
117
}
118

119
# Print command
120
echocmd()
121
{
122
    set_boldcolor $GREEN
123
    local PROMTSIG="\$"
124
    is_root && PROMTSIG="#"
125
    echo -n "$PROMTSIG $*"
126
    restore_color
127
}
128

129
# Print command line and run command line
130
docmd()
131
{
132
    showcmd "$*$EXTRA_SHOWDOCMD"
133
    "$@"
134
}
135

136
# Run every arg with docmd
137
docmd_foreach()
138
{
139
    local cmd pkg
140
    cmd="$1"
141
    #showcmd "$@"
142
    shift
143
    for pkg in "$@" ; do
144
        docmd $cmd $pkg
145
    done
146
}
147

148
# run command line with SUDO
149
sudorun()
150
{
151
    set_sudo
152
    if [ -z "$SUDO" ] ; then
153
        "$@"
154
        return
155
    fi
156
    $SUDO "$@"
157
}
158

159
# Print command line and run command line with SUDO
160
sudocmd()
161
{
162
    set_sudo
163
    [ -n "$SUDO" ] && showcmd "$SUDO $*" || showcmd "$*"
164
    sudorun "$@"
165
}
166

167
# Run every arg with sudocmd
168
# Returns on any error
169
sudocmd_foreach()
170
{
171
    local cmd pkg
172
    cmd="$1"
173
    #showcmd "$@"
174
    shift
175
    for pkg in "$@" ; do
176
        # don't quote $cmd here: it can be a command with an args
177
        sudocmd $cmd $pkg || return
178
    done
179
}
180

181
# print full path to files
182
make_filepath()
183
{
184
    local i
185
    for i in "$@" ; do
186
        [ -f "$i" ] || continue
187
        echo "$i" | grep -q "/" && echo "$i" && continue
188
        echo "./$i"
189
    done
190
}
191

192
get_firstarg()
193
{
194
    echon "$1"
195
}
196

197
get_lastarg()
198
{
199
    local lastarg
200
    eval "lastarg=\${$#}"
201
    echon "$lastarg"
202
}
203

204
# TODO: see etersoft-build-utils/tests/test_isnumber.sh
205
isnumber()
206
{
207
    echo "$*" | filter_strip_spaces | grep -q "^[0-9]\+$"
208
}
209

210
# copied from strings
211
# CHECKME: the same like estrlist has ?
212
# Note: used grep -E! write '[0-9]+(first|two)', not '[0-9]\+...'
213
rhas()
214
{
215
    echo "$1" | grep -E -q -- "$2"
216
}
217

218
# bash specific
219
startwith()
220
{
221
    # rhas "$1" "^$2"
222
    [[ "$1" = ${2}* ]]
223
}
224

225
is_abs_path()
226
{
227
    #echo "$1" | grep -q "^/"
228
    startwith "$1" "/"
229
}
230

231
# copied from strings
232
is_dirpath()
233
{
234
    [ "$1" = "." ] && return $?
235
    # rhas "$1" "/"
236
    startwith "$1" "/"
237
}
238

239

240
filter_strip_spaces()
241
{
242
        # possible use just
243
        #xargs echo
244
        sed -e "s| \+| |g" | \
245
                sed -e "s|^ ||" | sed -e "s| \$||"
246
}
247

248
strip_spaces()
249
{
250
        echo "$*" | filter_strip_spaces
251
}
252

253

254
# https://superuser.com/questions/422459/substitution-in-text-file-without-regular-expressions
255
# http://stackoverflow.com/a/2705678/120999
256
# use for subst complex string with symbols treating as regexp
257
sed_escape()
258
{
259
    echo "$*" | sed -e 's/[]()$*.^|[]/\\&/g'
260
}
261

262

263
# param true false
264
subst_option()
265
{
266
    eval "[ -n \"\$$1\" ]" && echo "$2" || echo "$3"
267
}
268

269
store_output()
270
{
271
    # use make_temp_file from etersoft-build-utils
272
    RC_STDOUT="$(mktemp)" || fatal
273
    remove_on_exit $RC_STDOUT
274
    local CMDSTATUS=$RC_STDOUT.pipestatus
275
    echo 1 >$CMDSTATUS
276
    #RC_STDERR=$(mktemp)
277
    ( LC_ALL=C $@ 2>&1 ; echo $? >$CMDSTATUS ) | tee $RC_STDOUT
278
    return "$(cat $CMDSTATUS)"
279
    # bashism
280
    # http://tldp.org/LDP/abs/html/bashver3.html#PIPEFAILREF
281
    #return $PIPESTATUS
282
}
283

284
showcmd_store_output()
285
{
286
    showcmd "$@"
287
    store_output "$@"
288
}
289

290
clean_store_output()
291
{
292
    rm -f $RC_STDOUT $RC_STDOUT.pipestatus
293
}
294

295
# run epm, possible from side repo
296
epm()
297
{
298
    if [ "$EPMMODE" = "pipe" ] ; then
299
        epm_main --inscript "$@"
300
        return
301
    fi
302

303
    # run epm again to full initialization
304
    local bashopt=''
305
    [ -n "$debug" ] && bashopt='-x'
306

307
    $CMDSHELL $bashopt $PROGDIR/$PROGNAME --inscript "$@"
308
}
309

310
# run $SUDO epm, possible from side repo
311
sudoepm()
312
{
313
    [ "$EPMMODE" = "pipe" ] && fatal "Can't use sudo epm call from the piped script"
314

315
    local bashopt=''
316
    [ -n "$debug" ] && bashopt='-x'
317

318
    sudorun $CMDSHELL $bashopt $PROGDIR/$PROGNAME --inscript "$@"
319
}
320

321
# Print error message and stop the program
322
fatal()
323
{
324
    local PROMOMESSAGE="$EPMPROMOMESSAGE"
325
    [ -n "$PROMOMESSAGE" ] || PROMOMESSAGE=" (you can discuss the epm $EPMVERSION problem in Telegram: https://t.me/useepm)"
326
    if [ -z "$TEXTDOMAIN" ] ; then
327
        set_color $RED >&2
328
        echo -n "ERROR: " >&2
329
        restore_color >&2
330
        echo "$* $PROMOMESSAGE" >&2
331
#    else
332
#        echog "Error in $0: $@" >&2
333
    fi
334
#    [ "$TERM" = "screen" ] && echo "(screen detected: waiting ten seconds to exit ...)" >&2 && sleep 10
335
    exit 1
336
}
337

338
# Print debug message
339
debug()
340
{
341
    [ -n "$debug" ] || return
342
    if [ -z "$TEXTDOMAIN" ] ; then
343
        set_color $YELLOW >&2
344
        echo -n "WARNING: " >&2
345
        restore_color >&2
346
        echo "$*" >&2
347
#    else
348
#        echog "Error in $0: $@" >&2
349
    fi
350
}
351

352

353
# Print warning message
354
warning()
355
{
356
    if [ -z "$TEXTDOMAIN" ] ; then
357
        set_color $YELLOW >&2
358
        echo -n "WARNING: " >&2
359
        restore_color >&2
360
        echo "$*" >&2
361
#    else
362
#        echog "Error in $0: $@" >&2
363
    fi
364
}
365

366
info()
367
{
368
    [ -n "$quiet" ] && return
369

370
    # print message to stderr if stderr forwarded to (a file)
371
    if isatty2 ; then
372
        isatty || return 0
373
        echo "$*"
374
    else
375
        echo "$*" >&2
376
    fi
377
}
378

379

380
check_su_root()
381
{
382
    [ "$BASEDISTRNAME" = "alt" ] || return 0
383

384
    is_root || return 0
385

386
    echo "$PATH" | grep -q "/usr/sbin" && return 0
387

388
    fatal "There is missed /usr/sbin path in PATH. Probably you have used 'su' without '-' to get root access. Use 'esu' or 'su -' command to get root permissions."
389
}
390

391

392
# if we have not sudo, returns 1 and set SUDO variable to fatal
393
SUDO_TESTED=''
394
SUDO_CMD='sudo'
395
set_sudo()
396
{
397
    local nofail="$1"
398

399
    # cache the result
400
    [ -n "$SUDO_TESTED" ] && return "$SUDO_TESTED"
401
    SUDO_TESTED="0"
402

403
    SUDO=""
404
    # skip SUDO if disabled
405
    [ -n "$EPMNOSUDO" ] && return
406
    if [ "$DISTRNAME" = "Cygwin" ] || [ "$DISTRNAME" = "Windows" ] ; then
407
        # skip sudo using on Windows
408
        return
409
    fi
410

411
    check_su_root
412

413
    # if we are root, do not need sudo
414
    is_root && return
415

416
    # start error section
417
    SUDO_TESTED="1"
418

419
    if ! is_command $SUDO_CMD ; then
420
        [ "$nofail" = "nofail" ] || SUDO="fatal 'For this operation run epm under root, or install and tune sudo (http://altlinux.org/sudo)'"
421
        SUDO_TESTED="2"
422
        return "$SUDO_TESTED"
423
    fi
424

425
    # if input is a console
426
    if inputisatty && isatty && isatty2 ; then
427
        if ! $SUDO_CMD -n true ; then
428
            info "Please enter sudo user password to use sudo in the current session."
429
            if ! $SUDO_CMD -l >/dev/null ; then
430
                [ "$nofail" = "nofail" ] || SUDO="fatal 'For this operation run epm under root, or install and tune sudo (http://altlinux.org/sudo)'"
431
                SUDO_TESTED="3"
432
                return "$SUDO_TESTED"
433
            fi
434
        fi
435
    else
436
        # use sudo if one is tuned and tuned without password
437
        if ! $SUDO_CMD -l -n >/dev/null 2>/dev/null ; then
438
            [ "$nofail" = "nofail" ] || SUDO="fatal 'Can't use sudo (only passwordless sudo is supported here). Please run epm under root or check http://altlinux.org/sudo '"
439
            SUDO_TESTED="4"
440
            return "$SUDO_TESTED"
441
        fi
442
    fi
443

444
    SUDO_TESTED="0"
445
    # FIXME: does not work: sudo -- VARIABLE=some command
446
    SUDO="$SUDO_CMD"
447
    #SUDO="$SUDO_CMD --"
448
    # check for < 1.7 version which do not support -- (and --help possible too)
449
    #$SUDO_CMD -h 2>/dev/null | grep -q "  --" || SUDO="$SUDO_CMD"
450

451
}
452

453
# return TRUE if we can run privileged command
454
sudo_allowed()
455
{
456
    set_sudo nofail
457
}
458

459
# wait for n seconds (if possible) during executing command
460
# args: seconds command
461
withtimeout()
462
{
463
    local TO=$(print_command_path timeout || print_command_path gtimeout)
464
    if [ -x "$TO" ] ; then
465
        $TO "$@"
466
        return
467
    fi
468
    fatal "Possible indefinite wait due timeout command is missed"
469
    # fallback: drop time arg and run without timeout
470
    #shift
471
    #"$@"
472
}
473

474
set_eatmydata()
475
{
476
    # don't use eatmydata (useless)
477
    return 0
478
    # skip if disabled
479
    [ -n "$EPMNOEATMYDATA" ] && return
480
    # use if possible
481
    is_command eatmydata || return
482
    set_sudo
483
    # FIXME: check if SUDO already has eatmydata
484
    [ -n "$SUDO" ] && SUDO="$SUDO eatmydata" || SUDO="eatmydata"
485
    [ -n "$verbose" ] && info "Uwaga! eatmydata is installed, we will use it for disable all sync operations."
486
    return 0
487
}
488

489
# 
490
__get_package_for_command()
491
{
492
    case "$1" in
493
        equery|revdep-rebuild)
494
            echo 'gentoolkit'
495
            ;;
496
        update-kernel|remove-old-kernels)
497
            echo 'update-kernel'
498
            ;;
499
    esac
500
}
501

502
# TODO:
503
confirm() {
504
    local response
505
    # call with a prompt string or use a default
506
    read -r -p "${1:-Are you sure? [y/N]} " response
507
    case $response in
508
        [yY][eE][sS]|[yY])
509
            true
510
            ;;
511
        *)
512
            false
513
            ;;
514
    esac
515
}
516

517

518
confirm_info()
519
{
520
    info "$*"
521
    if [ -z "$non_interactive" ] ; then
522
        confirm "Are you sure? [y/N]" || fatal "Exiting"
523
    fi
524

525
}
526

527

528
is_root()
529
{
530
    local EFFUID="$(id -u)"
531
    [ "$EFFUID" = "0" ]
532
}
533

534
assure_root()
535
{
536
    is_root || fatal "run me only under root"
537
}
538

539
check_su_access()
540
{
541
    is_command su && return
542
    [ ! -f /bin/su ] && warning "/bin/su is missed. Try install su package (http://altlinux.org/su)." && return 1
543
    local group="$(stat -c '%G' /bin/su)" || fatal
544
    warning "Check if you are in $group group to have access to su command."
545
    return 1
546
}
547

548
check_sudo_access()
549
{
550
    is_command sudo && return
551
    local cmd=''
552
    local i
553
    for i in /bin/sudo /usr/bin/sudo ; do
554
        [ -f $i ] && cmd="$i"
555
    done
556
    [ ! -f "$cmd" ] && warning "sudo command is missed. Try install sudo package (http://altlinux.org/sudo)." && return 1
557
    local group="$(stat -c '%G' "$cmd")" || fatal
558
    warning "Check if you are in $group group to have access to sudo command."
559
    return 1
560
}
561

562
check_sudo_access_only()
563
{
564
    is_command sudo && return
565
    local cmd=''
566
    local i
567
    for i in /bin/sudo /usr/bin/sudo ; do
568
        [ -f $i ] && cmd="$i"
569
    done
570
    [ ! -f "$cmd" ] && return 1
571
    local group="$(stat -c '%G' "$cmd")" || fatal
572
    warning "sudo command is presence, but is not accessible for you. Check if you are in $group group to have access to sudo command."
573
    return 1
574
}
575

576
esu()
577
{
578
    if is_root ; then
579
        if [ -n "$*" ] ; then
580
            [ -n "$quiet" ] || showcmd "$*"
581
            exec "$@"
582
        else
583
            # just shell
584
            showcmd "su -"
585
            a= exec su -
586
        fi
587
    fi
588

589
    set_pm_type
590

591

592
# TODO:
593
#quote() {
594
#    for arg in "$@"; do
595
#        printf '%s\n' "$arg" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"
596
#    done
597
#}
598

599
    escape_args()
600
    {
601
        local output=''
602
        while [ -n "$1" ] ; do
603
            if has_space "$1" ; then
604
                [ -n "$output" ] && output="$output '$1'" || output="'$1'"
605
            else
606
                [ -n "$output" ] && output="$output $1" || output="$1"
607
            fi
608
            shift
609
        done
610
        echo "$output"
611
    }
612

613
    escaped="$(escape_args "$@")"
614

615
    check_sudo_access_only
616
    # sudo is not accessible, will ask root password
617
    if ! set_sudo ; then
618
        check_su_access
619
        #info "Enter root password:"
620
        if [ -n "$*" ] ; then
621
            [ -n "$quiet" ] || showcmd "su - -c $escaped"
622
            a= exec su - -c "$escaped"
623
        else
624
            # just shell
625
            showcmd "su -"
626
            a= exec su -
627
        fi
628
    fi
629

630
    check_sudo_access
631

632
    #info "You can be asked about your password:"
633
    if [ -n "$*" ] ; then
634
        [ -n "$quiet" ] || showcmd "$SUDO su - -c $escaped"
635
        $SUDO su - -c "$escaped"
636
    else
637
        showcmd "$SUDO su -"
638
        $SUDO su -
639
    fi
640
}
641

642
regexp_subst()
643
{
644
    local expression="$1"
645
    shift
646
    sed -i -r -e "$expression" "$@"
647
}
648

649
# TODO: we we can't use epm directly?
650
assure_exists()
651
{
652
    load_helper epm-assure
653
    local package="$2"
654
    [ -n "$package" ] || package="$(__get_package_for_command "$1")"
655

656
    # ask for install: https://bugzilla.altlinux.org/42240
657
    local ask=''
658
    [ -n "$non_interactive" ] || ask=1
659

660
    ( verbose='' direct='' interactive=$ask epm_assure "$1" $package $3 ) || fatal
661
}
662

663
assure_exists_erc()
664
{
665
    load_helper epm-assure
666
    local package="erc"
667
    ( direct='' epm_assure "$package" ) || epm ei erc || fatal "erc is not available to install."
668
}
669

670
# will replaced within disabled_eget in packaged version
671
eget()
672
{
673
    # use internal eget only if exists
674
    if [ -s $SHAREDIR/tools_eget ] ; then
675
        ( EGET_BACKEND=$eget_backend $CMDSHELL $SHAREDIR/tools_eget "$@" )
676
        return
677
    fi
678
    fatal "Internal error: missed tools_eget"
679

680
    local EGET
681
    # FIXME: we need disable output here, eget can be used for get output
682
    assure_exists eget eget 3.3 >/dev/null
683
    # run external command, not the function
684
    EGET=$(print_command_path eget) || fatal "Missed command eget from installed package eget"
685
    $EGET "$@"
686
}
687

688

689
__epm_assure_7zip()
690
{
691
    # install 7zip in any case (can be used)
692
    if is_command 7z || is_command 7za || is_command 7zr || is_command 7zz ; then
693
        :
694
    else
695
        epm install 7-zip || epm install p7zip
696
    fi
697
}
698

699
# will replaced within disabled_erc in packaged version
700
erc()
701
{
702

703
    __epm_assure_7zip
704

705
    # use internal eget only if exists
706
    if [ -s $SHAREDIR/tools_erc ] ; then
707
        $CMDSHELL $SHAREDIR/tools_erc "$@"
708
        return
709
    fi
710
    fatal "Internal error: missed tools_erc"
711

712
    # FIXME: we need disable output here, ercat can be used for get output
713
    assure_exists_erc >/dev/null
714
    # run external command, not the function
715
    local ERC
716
    ERC=$(print_command_path erc) || fatal "Missed command erc from installed package erc"
717
    $ERC "$@"
718
}
719

720
# will replaced within disabled_ercat in packaged version
721
ercat()
722
{
723
    local ERCAT
724
    # use internal eget only if exists
725
    if [ -s $SHAREDIR/tools_ercat ] ; then
726
        $CMDSHELL $SHAREDIR/tools_ercat "$@"
727
        return
728
    fi
729
    fatal "Internal error: missed tools_ercat"
730

731
    # FIXME: we need disable output here, ercat can be used for get output
732
    assure_exists_erc >/dev/null
733
    # run external command, not the function
734
    ERCAT=$(print_command_path ercat) || fatal "Missed command ercat from installed package erc"
735
    $ERCAT "$@"
736
}
737

738
estrlist()
739
{
740
    if [ -s $SHAREDIR/tools_estrlist ] ; then
741
        $CMDSHELL $SHAREDIR/tools_estrlist "$@"
742
        return
743
    fi
744
    fatal "missed tools_estrlist"
745
}
746

747
onefile_estrlist()
748
{
749
    internal_tools_estrlist "$@"
750
}
751

752
# will replaced within eget() in packed version
753
onefile_eget()
754
{
755
    # check for both
756
    # we really need that cross here,
757
    is_command curl || assure_exists wget
758
    is_command wget || assure_exists curl
759
    internal_tools_eget "$@"
760
}
761

762
# TODO: improve and drop!
763
get_package_type()
764
{
765
    local i
766
    case $1 in
767
        *.deb)
768
            echo "deb"
769
            return
770
            ;;
771
        *.rpm)
772
            echo "rpm"
773
            return
774
            ;;
775
        *.txz)
776
            echo "txz"
777
            return
778
            ;;
779
        *.tbz)
780
            echo "tbz"
781
            return
782
            ;;
783
        *.exe)
784
            echo "exe"
785
            return
786
            ;;
787
        *.msi)
788
            echo "msi"
789
            return
790
            ;;
791
        *.AppImage)
792
            echo "AppImage"
793
            return
794
            ;;
795
        *)
796
            if [ -r "$1" ] && file "$1" | grep -q " ELF " ; then
797
                echo "ELF"
798
                return
799
            fi
800
            # print extension by default
801
            echo "$1" | sed -e 's|.*\.||'
802
            return 1
803
            ;;
804
    esac
805
}
806

807

808
# print options description from HELPCMD/HELPOPT lines in the code
809
# args: section_name, [file with code]
810
get_help()
811
{
812
    if [ "$0" = "/dev/stdin" ] || [ "$0" = "sh" ] ; then
813
        return
814
    fi
815
    local F="$0"
816
    if [ -n "$2" ] ; then
817
        is_dirpath "$2" && F="$2" || F="$(dirname $0)/$2"
818
    fi
819

820
    cat "$F" | grep -- "# $1" | while read -r n ; do
821
        if echo "$n" | grep -q "# $1: PART: " ; then
822
            echo
823
            echo "$n" | sed -e "s|# $1: PART: ||"
824
            continue
825
        fi
826
        echo "$n" | grep -q "^ *#" && continue
827
        opt=`echo $n | sed -e "s|) # $1:.*||g" -e 's|"||g' -e 's@^|@@'`
828
        desc=`echo $n | sed -e "s|.*) # $1:||g"`
829
        printf "    %-20s %s\n" "$opt" "$desc"
830
    done
831
}
832

833
set_bigtmpdir()
834
{
835
    # TODO: improve BIGTMPDIR conception
836
    # https://bugzilla.mozilla.org/show_bug.cgi?id=69938
837
    # https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch05s15.html
838
    # https://geekpeach.net/ru/%D0%BA%D0%B0%D0%BA-systemd-tmpfiles-%D0%BE%D1%87%D0%B8%D1%89%D0%B0%D0%B5%D1%82-tmp-%D0%B8%D0%BB%D0%B8-var-tmp-%D0%B7%D0%B0%D0%BC%D0%B5%D0%BD%D0%B0-tmpwatch-%D0%B2-centos-rhel-7
839
    if [ -z "$BIGTMPDIR" ] ; then
840
        BIGTMPDIR="/var/tmp"
841
        [ -d "$BIGTMPDIR" ] || BIGTMPDIR="$TMPDIR"
842
    fi
843
    export BIGTMPDIR
844
}
845

846
assure_tmpdir()
847
{
848
    if [ -z "$TMPDIR" ] ; then
849
        export TMPDIR="/tmp"
850
        debug "Your have no TMPDIR defined. Using $TMPDIR as fallback."
851
    fi
852

853
    if [ ! -d "$TMPDIR" ] ; then
854
        fatal "TMPDIR $TMPDIR does not exist."
855
    fi
856

857
    if [ ! -w "$TMPDIR" ] ; then
858
        fatal "TMPDIR $TMPDIR is not writable."
859
    fi
860
}
861

862
test_shell()
863
{
864
    local R
865
    R="$($CMDSHELL /dev/null 2>&1)"
866
    [ -n "$R" ] && fatal "$CMDSHELL is broken (bash wrongly printing out '$R'). Check ~/.bashrc and /etc/bashrc, run $CMDSHELL manually for test."
867
}
868

869

870
set_distro_info()
871
{
872

873
    test_shell
874

875
    assure_tmpdir
876

877
    set_bigtmpdir
878

879
    # don't run again in subprocesses
880
    [ -n "$DISTRVENDOR" ] && return 0
881

882
    DISTRVENDOR=$PROGDIR/distr_info
883

884
    # export pack of variables, see epm print info --print-eepm-env
885
    [ -n "$verbose" ] && $DISTRVENDOR --print-eepm-env
886
    eval $($DISTRVENDOR --print-eepm-env | grep -v '^ *#')
887
}
888

889
# FIXME: detect if not recognized
890
set_pm_type()
891
{
892
    local CMD
893
    set_distro_info
894

895
# override package manager detection result
896
if [ -n "$EPM_BACKEND" ] ; then
897
    PMTYPE=$EPM_BACKEND
898
    return
899
fi
900
# obsoleted
901
if [ -n "$FORCEPM" ] ; then
902
    PMTYPE=$FORCEPM
903
    return
904
fi
905

906
}
907

908
is_active_systemd()
909
{
910
    [ "$DISTRCONTROL" = "systemd" ]
911
}
912

913
assure_distr()
914
{
915
    local TEXT="this option"
916
    [ -n "$2" ] && TEXT="$2"
917
    [ "$DISTRNAME" = "$1" ] || fatal "$TEXT supported only for $1 distro"
918
}
919

920
# return delimiter sign in depend of package type
921
get_pkg_name_delimiter()
922
{
923
   local pkgtype="$1"
924
   [ -n "$pkgtype" ] || pkgtype="$PKGFORMAT"
925

926
   [ "$pkgtype" = "deb" ] && echo "_" && return
927
   echo "-"
928
}
929

930
# used via remove_on_exit
931
__epm_remove_tmp_files()
932
{
933
    trap "-" EXIT
934
    [ -n "$DEBUG" ] && return 0
935

936
    [ -n "$verbose" ] && info "Removing tmp files on exit ..."
937

938
    if [ -n "$to_clean_tmp_dirs" ] ; then
939
        echo "$to_clean_tmp_dirs" | while read p ; do
940
            [ -n "$verbose" ] && echo "rm -rf '$p'"
941
            rm -rf "$p" 2>/dev/null
942
        done
943
    fi
944

945
    if [ -n "$to_clean_tmp_files" ] ; then
946
        echo "$to_clean_tmp_files" | while read p ; do
947
            rm $verbose -f "$p" 2>/dev/null
948
        done
949
    fi
950

951
    return 0
952
}
953

954

955
remove_on_exit()
956
{
957
    if [ -z "$set_remove_on_exit" ] ; then
958
        trap "__epm_remove_tmp_files" EXIT
959
        set_remove_on_exit=1
960
    fi
961
    while [ -n "$1" ] ; do
962
        if [ -d "$1" ] ; then
963
            to_clean_tmp_dirs="$to_clean_tmp_dirs
964
$1"
965
        elif [ -f "$1" ] ; then
966
            to_clean_tmp_files="$to_clean_tmp_files
967
$1"
968
        fi
969
        shift
970
    done
971
}
972

973
#has_space()
974
#{
975
#    estrlist -- has_space "$@"
976
#}
977
# use internal implementation for speed
978
has_space()
979
{
980
        # not for dash:
981
        [ "$1" != "${1/ //}" ]
982
        # [ "$(echo "$*" | sed -e "s| ||")" != "$*" ]
983
}
984

985

986
is_url()
987
{
988
    echo "$1" | grep -q "^[filehtps]*:/"
989
}
990

991
# print a path to the command if exists in $PATH
992
if a= type -a type 2>/dev/null >/dev/null ; then
993
print_command_path()
994
{
995
    a= type -fpP -- "$1" 2>/dev/null
996
}
997
elif a= which which 2>/dev/null >/dev/null ; then
998
    # the best case if we have which command (other ways needs checking)
999
    # TODO: don't use which at all, it is a binary, not builtin shell command
1000
print_command_path()
1001
{
1002
    a= which -- "$1" 2>/dev/null
1003
}
1004
else
1005
print_command_path()
1006
{
1007
    a= type "$1" 2>/dev/null | sed -e 's|.* /|/|'
1008
}
1009
fi
1010

1011
# check if <arg> is a real command
1012
is_command()
1013
{
1014
    print_command_path "$1" >/dev/null
1015
}
1016

1017
# compatibility layer
1018

1019
# add realpath if missed (with -s support)
1020
if ! is_command realpath ; then
1021
realpath()
1022
{
1023
    [ -n "$*" ] || return
1024
    if [ "$1" = "-s" ] ; then
1025
        shift
1026
        echo "$(cd "$(dirname "$1")" && pwd -P)/$(basename "$1")" #"
1027
        return
1028
    fi
1029
    readlink -f "$@"
1030
}
1031
fi
1032

1033

1034
# TODO: use perl if sed -i is not accessible
1035
# sed -i is only supported in GNU sed.
1036
#  sed -i "s/$find/$replace/g" "$@"
1037
#  perl -p -i -e "s/$find/$replace/g" "$@"
1038

1039
# add subst if missed
1040
if ! is_command subst ; then
1041
subst()
1042
{
1043
    sed -i -e "$@"
1044
}
1045
fi
1046

1047
check_core_commands()
1048
{
1049
    #which which >/dev/null || fatal "Can't find which command (which or debianutils package is missed?)"
1050
    is_command grep || fatal "Can't find grep command (coreutils package is missed?)"
1051
    is_command sed || fatal "Can't find sed command (sed package is missed?)"
1052
}
1053

1054

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.