aboutsummaryrefslogtreecommitdiff
path: root/pbuilder-modules
blob: 9b5819d2c600da03600ee0fb7a1969fdc21530ca (plain)
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#! /bin/bash
# common modules for pbuilder.
#   pbuilder -- personal Debian package builder
#   Copyright (C) 2001-2009 Junichi Uekawa
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

. "${BASH_SOURCE%/*}/pbuilder-runhooks"

function showhelp () {
    cat <<EOF
pbuilder - a personal builder
Copyright 2001-2007 Junichi Uekawa
Distributed under GNU Public License version 2 or later

pbuilder [operation] [pbuilder-options]
pdebuild [pdebuild-options] -- [pbuilder-options]

command lines:
pbuilder --create [--basetgz base.tgz-path] [--distribution sid|experimental|...]
  Creates a base.tgz

pbuilder --update [--basetgz base.tgz-path] [--distribution sid|experimental|...]
  Updates a base.tgz

pbuilder --build [--basetgz base.tgz-path] pbuilder_2.2.0-1.dsc
  Builds using the base.tgz. Requires a .dsc filename

pbuilder --clean
  Cleans the temporal build directory.

pbuilder --login
pbuilder --execute -- [command] [command-options]
  Logs in to the build environment and execute command.

pbuilder --dumpconfig
  Dumps configuration information to stdout for debugging.

pbuilder-options:
 --basetgz [base.tgz location]
 --buildplace [location of build]
 --mirror [mirror location]
 --othermirror [other mirror location in apt deb-line format, delimited with | signs]
 --http-proxy [proxy]
 --distribution [distribution(sid|experimental|...)]
 --architecture [architecture]
 --components [components]
 --buildresult [location-to-copy-build-result]
 --aptcache [location of retrieved package files]
 --removepackages [packages-to-remove on pbuilder create]
 --extrapackages [packages-to-add on pbuilder create]
 --configfile [configuration file to load]
 --hookdir [hook directory]
 --debemail [mail address]
 --debbuildopts [dpkg-buildpackage options]
 --logfile [filename to output log]
 --pkgname-logfile
 --aptconfdir [overriding apt config dir]
 --timeout [timeout time]
 --override-config
 --binary-arch
 --preserve-buildplace
 --bindmounts [bind-mount-point]
 --debug
 --twice
 --autocleanaptcache
 --compressprog [program]
 --debootstrapopts [debootstrap options]
 --save-after-login/--save-after-exec
 --debootstrap [debootstrap|cdebootstrap]

pdebuild-specific pbuilder-options:
 --pbuilderroot [command to obtain root privilege for pbuilder]
 --pbuildersatisfydepends [command to satisfy build-dependencies]
 --buildsourceroot [command to obtain root privilege for dpkg-buildpackage]
 --use-pdebuild-internal
 --architecture
 --auto-debsign
 --debsign-k [keyid]
EOF
    exit 1
}

# Log a message
# message is of a format
#  E: error message
#  W: warning message
#  I: informational message
function log() {
    case "$*" in
        "E: "*)
            echo "$*" >&2
            ;;
         "W: "*)
            echo "$*" >&2
            ;;
         "I: "*)
            echo "$*"
            ;;
        *)
            echo "malformed log message: $*"
            exit 1
            ;;
    esac
}

log.e() {
    log "E: $*"
}

log.w() {
    log "W: $*"
}

log.i() {
    log "I: $*"
}

# test whether a directory is empty
# fails if "$*" exists but isn't a directory
# fails and outputs garbage if "$*" doesn't actually exist
is_empty_dir() {
  return "$(find "$*" -maxdepth 0 -type d -empty -printf 0 -o -printf 1)"
}

# sanity checks to ensure mountpoint $1 is truly unmounted in $BUILDPLACE
# (fails relatively often to ensure we don't "rm -rf" a bind mount)
function seems_truly_unmounted() {
    local mountpoint
    mountpoint="$1"
    if ! [ -e "$BUILDPLACE/$mountpoint" ]; then
        log.w "$mountpoint doesn't exist"
        return 1
    fi
    if ! [ -d "$BUILDPLACE/$mountpoint" ]; then
        log.w "$mountpoint isn't a directory"
        return 1
    fi
    if [ -r "$BUILDPLACE/proc/mounts" ] && \
        grep -q "^[^ ]* $mountpoint " "$BUILDPLACE/proc/mounts"; then
        log.w "$mountpoint is mounted according to build place's /proc/mounts"
        return 1
    fi
    if [ -r "/proc/mounts" ] && \
        grep -q "^[^ ]* $BUILDPLACE/$mountpoint " "/proc/mounts"; then
        log.w "$mountpoint is mounted according to system's /proc/mounts"
        return 1
    fi
    if ! is_empty_dir "$BUILDPLACE/$mountpoint"; then
        log.w "$mountpoint not empty"
        return 1
    fi
    return 0
}

function umount_one () {
    DEB_BUILD_ARCH_OS=$(dpkg-architecture -qDEB_BUILD_ARCH_OS)
    if [ "${IGNORE_UMOUNT}" = "yes" ]; then
        # support ignore umount option.
        log.i "ignoring umount of $1 filesystem"
        return
    fi
    log.i "unmounting $1 filesystem"
    local UMOUNT_OUTPUT
    if ! UMOUNT_OUTPUT="$(LC_ALL=C umount "$BUILDPLACE/$1" 2>&1)"; then
        log.w "Could not unmount $1: $UMOUNT_OUTPUT"
        local ignore_umount_error="no"
        case $UMOUNT_OUTPUT in
          "umount: "*": not found"|"umount:"*": not mounted")
            # run an additional set of sanity checks
            if seems_truly_unmounted "$1"; then
                ignore_umount_error="yes"
            else
                log.w "Tried ignoring error in unmount, but sanity check failed: $1 might still be mounted"
            fi
            ;;
          *)
            :
            ;;
        esac
        if [ "$ignore_umount_error" != "yes" ]; then
            log.w "Retrying to unmount $1 in 5s"
            sleep 5s
            while ! umount "$BUILDPLACE/$1"; do
                sleep 5s
                cat <<EOF

  Could not unmount $1, some programs might
  still be using files in /proc (klogd?).
  Please check and kill these processes manually
  so that I can unmount $1.  Last umount error was:
$UMOUNT_OUTPUT

  This error only affects chroots; you may want to use
  user-mode-linux to avoid this message.

EOF
                chroot "$BUILDPLACE" bin/sh
            done
        else
            log.w "Ignored error in unmount"
        fi
    fi
}

function umountproc () {
    # push arguments on a stack to reverse direction.
    local reversed
    DEB_BUILD_ARCH_OS=$(dpkg-architecture -qDEB_BUILD_ARCH_OS)
    reversed=
    for mnt in $BINDMOUNTS; do
        reversed="$mnt $reversed"
    done
    for mnt in $reversed; do
        umount_one "$mnt"
    done
    if [ -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
        umount_one "$(grep -m 1 ^selinuxfs /proc/mounts | cut -d ' ' -f 2)"
    fi
    if [ "$DEB_BUILD_ARCH_OS" = "linux" ] && [ "$USEDEVPTS" = "yes" ]; then
        umount_one "dev/pts"
    fi
    if [ "$USEDEVFS" = "yes" ]; then
        umount_one "dev"
    fi
    if [ "$USERUNSHM" = "yes" ] && [ "$DEB_BUILD_ARCH_OS" != "hurd" ]; then
        umount_one "run/shm"
    fi
    if [ "$USEPROC" = "yes" ]; then
        if [ "$DEB_BUILD_ARCH_OS" = "linux" ] && [ -e "$BUILDPLACE/proc/sys/fs/binfmt_misc/status" ]; then
            umount_one "proc/sys/fs/binfmt_misc"
        fi
        umount_one "proc"
    fi
    if [ "$DEB_BUILD_ARCH_OS" = "kfreebsd" ]; then
        umount_one "sys"
    fi
    if [ "$DEB_BUILD_ARCH_OS" = "hurd" ]; then
        umount_one "servers"
        umount_one "dev"

        # Workaround to remove chroot on Hurd: once /dev firmlink is
        # removed, chroot removal either gets stuck or fails by removing
        # some devices.
        for dev in "$BUILDPLACE"/dev/{netdde,tty*,pty*,fd,vcs}; do
            settrans -fg "$dev"
        done
    fi
}


# Mount /proc /dev/pts /dev and bind-mount points
# Also create a policy-rc.d script if it doesn't already exist.
function mountproc () {
    local -a mounted
    DEB_BUILD_ARCH_OS=$(dpkg-architecture -qDEB_BUILD_ARCH_OS)
    if [ "$USEPROC" = "yes" ]; then
        log.i "mounting /proc filesystem"
        mkdir -p "$BUILDPLACE/proc"
        case "$DEB_BUILD_ARCH_OS" in
            kfreebsd)
                PROCFS="linprocfs"
                ;;
            hurd)
                settrans -fg "$BUILDPLACE/proc"
                PROCFS="firmlink"
                ;;
            *)
                PROCFS="proc"
                ;;
        esac
        mount -t "$PROCFS" /proc "$BUILDPLACE/proc" || [ "$DEB_BUILD_ARCH_OS" = hurd ]
        ln -s ../proc/mounts "$BUILDPLACE/etc/mtab" 2> /dev/null || true
        mounted[${#mounted[@]}]="$BUILDPLACE/proc"
    fi
    if [ "$USEDEVFS" = "yes" ]; then
        log.i "mounting /dev filesystem"
        mkdir -p "$BUILDPLACE/dev" || true
        mount -t devfs /dev "$BUILDPLACE/dev"
        mounted[${#mounted[@]}]="$BUILDPLACE/dev"
    fi
    if [ "$DEB_BUILD_ARCH_OS" = "linux" ] && [ "$USERUNSHM" = "yes" ]; then
        log.i "mounting /run/shm filesystem"
        mkdir -p "$BUILDPLACE/run/shm" || true
        mount -t tmpfs tmpfs "$BUILDPLACE/run/shm"
        mounted[${#mounted[@]}]="$BUILDPLACE/run/shm"
    fi
    if [ "$DEB_BUILD_ARCH_OS" = "linux" ] && [ "$USEDEVPTS" = "yes" ]; then
        log.i "mounting /dev/pts filesystem"
        mkdir -p "$BUILDPLACE/dev/pts" || true
        TTYGRP=5
        TTYMODE=620
        [ -f /etc/default/devpts ] && . /etc/default/devpts
        mount -t devpts none "$BUILDPLACE/dev/pts" -onoexec,nosuid,gid=$TTYGRP,mode=$TTYMODE
        mounted[${#mounted[@]}]="$BUILDPLACE/dev/pts"
    fi
    if [ -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
        log.i "mounting selinux filesystem"
        local SELINUX="$(grep -m 1 ^selinuxfs /proc/mounts | cut -d ' ' -f 2)"
        mkdir -p "$BUILDPLACE/$SELINUX"
        mount --bind "$SELINUX" "$BUILDPLACE/$SELINUX"
        mount -o remount,ro,bind "$BUILDPLACE/$SELINUX"
        mounted[${#mounted[@]}]="$BUILDPLACE/$SELINUX"
    fi
    if [ "$DEB_BUILD_ARCH_OS" = "hurd" ]; then
        # /dev and /servers might have already been mounted at
        # debootstrap chroot creation
        mount -t firmlink /dev "$BUILDPLACE/dev" || true
        mounted[${#mounted[@]}]="$BUILDPLACE/dev"
        mount -t firmlink /servers "$BUILDPLACE/servers" || true
        mounted[${#mounted[@]}]="$BUILDPLACE/servers"
    fi
    MOUNTPARAMS="-obind"
    [ "$DEB_BUILD_ARCH_OS" = "kfreebsd" ] && MOUNTPARAMS="-t nullfs"
    for mnt in $BINDMOUNTS; do
        log.i "Mounting $mnt"
        if mkdir -p "$BUILDPLACE/$mnt" &&
            mount $MOUNTPARAMS "$mnt" "$BUILDPLACE/$mnt"; then
            # successful.
            mounted[${#mounted[@]}]="$mnt"
        else
            # this part of code is the only part which is supposed to fail.
            # When unsuccessful, backtrack / umount and abort.
            if [ -n "${mounted[*]}" ]; then
                log.i "error recovery: umount successfully mounted mount-points: ${mounted[@]}"
                for umnt in "${mounted[@]}"; do
                    log.i "umounting $umnt"
                    umount "$umnt"
                done
            fi
            exit 1
        fi
    done
    if [ -f "$BUILDPLACE/usr/sbin/policy-rc.d" ]; then
        log.i "policy-rc.d already exists"
    else
        log.i "installing dummy policy-rc.d"
        echo "\
#!/bin/sh

while true; do
case \"\$1\" in
  -*) shift ;;
  makedev) exit 0;;
  x11-common) exit 0;;
  *)  exit 101;;
esac
done

" >  "$BUILDPLACE/usr/sbin/policy-rc.d"
        chmod a+x "$BUILDPLACE/usr/sbin/policy-rc.d"
    fi
}

## function to clean subdirs, use instead of rm -r
function clean_subdirectories () {
  if [ -z "$1" ]; then
      log.e "Fatal internal error in clean_subdirectories"
      exit 1;
  fi
  if [ ! -d "$1" ]; then
      log.w "directory $1 does not exist in clean_subdirectories"
      return;
  fi
  log.i "removing directory $1 and its subdirectories"
  find "$1" -xdev \( \! -type d \) -print0 |xargs -0 rm -f
  find "$1" -xdev -depth -type d -print0 | \
      (xargs -0 rmdir || true)
}

function cleanbuildplace () {
    if [ "$?" -ne 0 ]; then
        log.w "Aborting with an error";
    fi
    unloadhooks
    if [ "${INTERNAL_BUILD_UML}" != "yes" ]; then
        if [ -d "$BUILDPLACE" ]; then
            # A directory on the same partition as $BUILDPLACE, bind-mounted
            # into $BUILDPLACE, will be cleaned out by clean_subdirectories
            # (because -xdev doesn't know about bind mounts).  To avoid that
            # potential disaster (and also to avoid ugly error messages from
            # rmdir otherwise), we want to make sure that there is *nothing*
            # mounted under the chroot before we do our bulldozer routine.
            #
            # The readlink -f is a simple way to canonicalize the path for
            # $BUILDPLACE (no dirty double slashes for *us*), so it matches
            # what will be in the output of mount.
            if mount |grep -q -F " $(readlink -f "$BUILDPLACE")/"; then
                log.e "Something is still mounted under ${BUILDPLACE}; unmount and remove ${BUILDPLACE} manually"
            else
                log.i "cleaning the build env "
                clean_subdirectories "$BUILDPLACE"
            fi
        fi;
    fi
}

function umountproc_cleanbuildplace () {
    # rolling back to abort.
    if [ "$?" -ne 0 ]; then
        log.w "Aborting with an error";
    fi
    umountproc
    cleanbuildplace
}

function saveaptcache_umountproc_cleanbuildplace () {
    # save the apt cache, and call umountproc_cleanbuildplace
    save_aptcache
    umountproc_cleanbuildplace
}

function installaptlines (){
    log.i "Installing apt-lines"
    rm -f "$BUILDPLACE"/etc/apt/sources.list
    if [ -z "$DISTRIBUTION" ]; then
        log.e "Distribution not specified, please specify"
        exit 1
    fi
    if [ -n "$OTHERMIRROR" ]; then
        echo "$OTHERMIRROR" | tr "|" "\n" >> "$BUILDPLACE"/etc/apt/sources.list
    fi
    if [ -n "$MIRRORSITE" ] ; then
        cat >> "$BUILDPLACE"/etc/apt/sources.list << EOF
deb $MIRRORSITE $DISTRIBUTION $COMPONENTS
#deb-src $MIRRORSITE $DISTRIBUTION $COMPONENTS
EOF
    fi
    if [ -n "$APTCONFDIR" ]; then
        log.i "Copy " "$APTCONFDIR"/* " to chroot"
        cp -a "$APTCONFDIR/"* "$BUILDPLACE"/etc/apt
    fi

    if [ ! -d "$BUILDPLACE"/etc/apt/apt.conf.d ]; then
        log.i "Create /etc/apt/apt.conf.d/ inside chroot"
        mkdir "$BUILDPLACE"/etc/apt/apt.conf.d
    fi

    # configure /etc/apt.conf.d/15pbuilder
    cat > "$BUILDPLACE"/etc/apt/apt.conf.d/15pbuilder <<EOF
APT::Install-Recommends "false";
Acquire::Languages none;
EOF
    if [ -n "$EXPERIMENTAL" ]; then
        log.i "Installing apt-lines and pinning for experimental"
        if [ -n "$MIRRORSITE" ] ; then
            echo "deb $MIRRORSITE experimental main" >> "$BUILDPLACE"/etc/apt/sources.list
            echo "#deb-src $MIRRORSITE experimental main" >> "$BUILDPLACE"/etc/apt/sources.list
        fi
        cat >> "$BUILDPLACE"/etc/apt/apt.conf.d/15pbuilder <<EOF
APT::Default-Release "experimental";
EOF
    fi
}

function copy_local_configuration () {
    log.i "copying local configuration"
    if [ -n "$CONFDIR" ] && [ -d "$CONFDIR" ]; then
        log.i "copying files from $CONFDIR (if possible) instead of using the system ones"
    fi
    for a in hosts hostname resolv.conf mailname; do
        if [ -n "$CONFDIR" ] && [ -f "$CONFDIR/$a" ]; then
            rm -f "$BUILDPLACE/etc/$a"
            cp "$( readlink -f "$CONFDIR/$a" )" "$BUILDPLACE/etc/$a"
        elif [ -f "/etc/$a" ]; then
            rm -f "$BUILDPLACE/etc/$a"
            cp "$( readlink -f "/etc/$a" )" "$BUILDPLACE/etc/$a"
        else
            log.w "No local /etc/$a to copy, relying on $BUILDPLACE/etc/$a to be correct"
        fi
    done
}

function extractbuildplace () {
    # after calling this function, umountproc, and cleanbuildplace
    # needs to be called. Please trap it after calling this function.

    if [ "${INTERNAL_BUILD_UML}" != "yes" -a ! \( "${PRESERVE_BUILDPLACE}" = "yes" -a -d "$BUILDPLACE" \) ]; then
        cleanbuildplace
        log.i "Building the build Environment"
        if ! mkdir -p "$BUILDPLACE"; then
            log.e "failed to build the directory to chroot"
            exit 1
        fi
        log.i "extracting base tarball [${BASETGZ}]"
        if [ ! -f "$BASETGZ" ]; then
            log.e "failed to find $BASETGZ, have you done <pbuilder create> to create your base tarball yet?"
            exit 1
        fi
        if ! (cd "$BUILDPLACE" && tar -x --use-compress-program "$COMPRESSPROG" -p -f "$BASETGZ"); then
            log.e "failed to extract $BASETGZ to $BUILDPLACE"
            exit 1
        fi
    fi
    copy_local_configuration
    loadhooks

    # installaptlines may fail with exit 1, do it earlier than mountproc.
    if [ "$OVERRIDE_APTLINES" = "yes" ]; then
        installaptlines
    else # Warn if override config is not set
        if [ "$OVERRIDE_APTLINES_WARN" = "yes" ]; then
            log.w "--override-config is not set; not updating apt.conf Read the manpage for details."
        fi
    fi

    mountproc
    # FIXME maybe add more checks here? - actually it's not even really needed,
    # since it's created at chroot creation time too.
    mkdir -p "${BUILDPLACE}${BUILDDIR}"
    # XXX added in 0.216, to be deprecated in the future
    # Add a compatibility symlink from the old BUILDDIR (/tmp/buildd) to the new
    # one, if different.  (Yes, people should just fix their scripts to use
    # BUILDDIR, please file a bug if you need that variable to also be available
    # elsewhere other than hook script; given that, I won't do too fancy checks)
    if [ "$BUILDDIR" != '/tmp/buildd' ]; then
        if [ -h "$BUILDPLACE/tmp/buildd" ] && [ "$(readlink -f "$BUILDPLACE/tmp/buildd")" = "${BUILDPLACE}$BUILDDIR" ]; then
            rm "$BUILDPLACE/tmp/buildd"
        fi
        if [ -d "$BUILDPLACE/tmp/buildd" ] && [ ! -h "$BUILDPLACE/tmp/buildd" ]; then
            if [ ! "$(ls -A "$BUILDPLACE/tmp/buildd" 2>&1)" ]; then
                # empty /tmp/buildd, let's change it to a symlink to BUILDDIR
                rmdir "$BUILDPLACE/tmp/buildd"
                ln -rs "${BUILDPLACE}$BUILDDIR" "$BUILDPLACE/tmp/buildd"
            else
                log.w "Could not create compatibility symlink because /tmp/buildd is not empty"
            fi
        elif [ ! -e "$BUILDPLACE/tmp/buildd" ]; then
            ln -rs "${BUILDPLACE}$BUILDDIR" "$BUILDPLACE/tmp/buildd"
        else
            log.w "Could not create compatibility symlink because /tmp/buildd exists and it is not a directory"
        fi
    fi
    executehooks "H"
}

function echobacktime () {
    log.i "Current time: $(date)"
    log.i "pbuilder-time-stamp: $(date +%s)"
}

function recover_aptcache() {
    local doit
    # recover the aptcache archive
    if [ -n "$APTCACHE" ]; then
        if [ "$APTCACHEHARDLINK" = "yes" ]; then
            doit=ln
        else
            doit=cp
        fi
        log.i "Obtaining the cached apt archive contents"
        find "$APTCACHE" -maxdepth 1 -name \*.deb | \
            while read A ; do
            $doit "$A" "$BUILDPLACE/var/cache/apt/archives/" || true
        done
    fi
}

function save_aptcache() {
    # save the current aptcache archive
    # it is safe to call this function several times.
    local doit
    if [ -n "$APTCACHE" ]; then
        log.i "Copying back the cached apt archive contents"
        mkdir -p "$APTCACHE" ;
        if [ "$APTCACHEHARDLINK" = "yes" ]; then
            doit=ln
        else
            doit=cp
        fi
        find "$BUILDPLACE/var/cache/apt/archives/" -maxdepth 1 -name \*.deb | \
            while read A ;do
            if [ ! -f "$APTCACHE/$(basename "$A")" -a -f "$A" ]; then
                log.i "new cache content '$(basename "$A")' added"
                $doit "$A" "$APTCACHE/" || true
            fi
        done
    fi
}

function create_basetgz() {
    # don't pack the hooks in
    unloadhooks
    # create base.tgz
    (
        if ! cd "$BUILDPLACE"; then
            log.e "unexpected error in chdir to $BUILDPLACE"
            exit 1;
        fi
        while test -f "${BASETGZ}.tmp"; do
            log.i "Someone else has lock over ${BASETGZ}.tmp, waiting"
            sleep 10s
        done
        log.i "creating base tarball [${BASETGZ}]"
    if [ -h "$BUILDPLACE/tmp/buildd" ] && [ "$(readlink -f "$BUILDPLACE/tmp/buildd")" = "${BUILDPLACE}$BUILDDIR" ]; then
        rm "$BUILDPLACE/tmp/buildd"
    fi
        if ! tar -c --use-compress-program "$COMPRESSPROG" -f "${BASETGZ}.tmp" ./* ; then
            log.e "failed building base tarball"
            rm -f "${BASETGZ}.tmp"
            exit 1;
        fi
        mv "${BASETGZ}.tmp" "${BASETGZ}"
    )
}

function copyinputfile() {
    # copy files to inside chroot, copy all files specified by INPUTFILE[] parameter.
    TARGETDIR="$1"
    if [ -z "$TARGETDIR" ]; then
        log.e "Unexpected error in copyinputfile"
        exit 1x
    fi
    if [ -n "$INPUTFILE" ]; then
        log.i "copy ${INPUTFILE[*]} to target directory"
        cp "${INPUTFILE[@]}" "${TARGETDIR}"
    fi
}

# all trap hooks that should lead to 'exit'; and error exit.
function cleanbuildplace_trap () {
    trap "" sigpipe sighup
    set +e
    cleanbuildplace
    trap - exit sighup sigpipe
    exit 1
}
function saveaptcache_umountproc_cleanbuildplace_trap () {
    trap "" sigpipe sighup
    set +e
    saveaptcache_umountproc_cleanbuildplace
    trap - exit sighup sigpipe
    exit 1
}
function umountproc_cleanbuildplace_trap () {
    trap "" sigpipe sighup
    set +e
    umountproc_cleanbuildplace
    trap - exit sighup sigpipe
    exit 1
}
function umountproc_trap () {
    trap "" sigpipe sighup
    set +e
    umountproc
    trap - exit sighup sigpipe
    exit 1
}

# copy to .. if target directory is not ..
function conditional_cp_a() {
    local source_file="$1"
    # NOTE: target_dir must not end with /, which is usually the case
    # with 'readlink -f' result, which BUILDRESULT usually is.
    local target_dir="$2"
    # For testability, make cp overridable.
    local cp="${3:-cp}"

    # $PWD should end with non-'/', so dirname should give us the parent dir.
    local parent_dir=$(dirname "$PWD")

    if [ "${parent_dir}" != "${target_dir}" ]; then
        "$cp" -a "$source_file" "$target_dir"
    else
        log.i "file ${source_file} is already in target, not copying."
    fi
}

function add_additional_aptkeyrings() {
# To support package verification inside the repository we may have to import
# additional keys.
    for KEY in "${APTKEYRINGS[@]}"; do
        log.i "adding apt key file ${KEY}."
        $CHROOTEXEC /usr/bin/apt-key add - < "${KEY}" > /dev/null
    done
}

#Setting environmental variables that are really required:
#required for some packages to install...
export LANG=C
export LC_ALL=C