Merge lp:~vorlon/linaro-image-tools/hwpack-always-from-PATH into lp:linaro-image-tools/11.11

Proposed by Steve Langasek
Status: Merged
Merged at revision: 140
Proposed branch: lp:~vorlon/linaro-image-tools/hwpack-always-from-PATH
Merge into: lp:linaro-image-tools/11.11
Diff against target: 23 lines (+3/-3)
1 file modified
linaro-media-create (+3/-3)
To merge this branch: bzr merge lp:~vorlon/linaro-image-tools/hwpack-always-from-PATH
Reviewer Review Type Date Requested Status
Linaro Maintainers Pending
Review via email: mp+38056@code.launchpad.net

Description of the change

Handling of the hwpack stage is wrong when the tools are on the path instead of being invoked as /path/to/linaro-media-create, because dirname returns '.' in this case. This patch fixes things up so that we can trust that the linaro-hwpack-install on the path is always the correct one.

To post a comment you must log in.
Revision history for this message
Peter Maydell (pmaydell) wrote :

It seems a bit ugly to put the directory on the path just so we can say 'which linaro-hwpack-install' later. Wouldn't it be better to say at the start of the script
SCRIPTPATH="$(dirname "$(readlink -f "$0")")"

and then
LINARO_HWPACK_INSTALL="$SCRIPTPATH/linaro-hwpack-install"

?

(This should work whether linaro-media-create is on the PATH or not.)

PS: I think that "when the tools are on the path [...] because dirname returns '.' in this case" is only true when "." is on your PATH and that is where the tools were found. The use of readlink is what handles this case in my proposal. (This avoids creating an implicit requirement not to change current working directory between starting the script and setting LINARO_HWPACK_INSTALL, incidentally.)

If you do prefer your approach then this line needs quotes:
LINARO_HWPACK_INSTALL=$(which linaro-hwpack-install)

Revision history for this message
Steve Langasek (vorlon) wrote :

Thanks for the feedback. Using $PATH for this is partly motivated by the rsync-root-fs patch, which also involves calling an external script that should be installed in the same directory as linaro-media-create; the simplest way to address both of these cases cleanly is by using $PATH, IMHO. This just isn't obvious because the rsync-root-fs patch has now been backed out pending revision.

I've just spotted a buglet in this patch, though; if linaro-media-create is installed on the $PATH, but the user is directly calling the bzr version instead, this code will look for linaro-hwpack-install in the system path instead of in the bzr directory. I'm pushing a correction for this now.

In light of the other future uses of $PATH, do you think this is reasonable, or do you think we should still use a separate $SCRIPTPATH variable?

140. By Steve Langasek

fix a bug in how we look up linaro-hwpack-install when l-m-c is both on the
path and in a bzr checkout

Revision history for this message
Peter Maydell (pmaydell) wrote :

Right, I guess that putting it on the PATH makes sense. In that case why not just say

PATH="$(dirname "$(readlink -f "$0")"):$PATH"

?

The code you have at the moment will be very fragile if the script is invoked via a relative path.

8 +if [ "$(dirname "$0")" != . ] || [ "$0" = "./$(basename "$0")" ]; then

What is the case where this if clause is not true? I can't find one...

Revision history for this message
Steve Langasek (vorlon) wrote :

On Mon, Oct 11, 2010 at 07:15:54AM -0000, Peter Maydell wrote:
> Right, I guess that putting it on the PATH makes sense. In that case why not just say

> PATH="$(dirname "$(readlink -f "$0")"):$PATH"

Because if $0 is invoked as 'linaro-media-create' because it's already on
the path, we don't want to put '.' at the front of the path; that's
insecure.

> The code you have at the moment will be very fragile if the script is
> invoked via a relative path.

Howso? I don't think any of these scripts ever call 'cd', so a relative
path is no worse than an absolute one for our purposes; the only benefit to
your formulation is that if someone has symlinked ~/bin/linaro-media-create
to their bzr checkout, this will look in the bzr working tree instead of
~/bin for the helper scripts. That's probably a worthwhile feature, but
the trivial implementation doesn't work correctly if linaro-media-create is
already on the path, so I would rather defer that for a separate fix instead
of blocking this fix for a proper implementation of that feature.

> 8 +if [ "$(dirname "$0")" != . ] || [ "$0" = "./$(basename "$0")" ]; then

> What is the case where this if clause is not true? I can't find one...

See above :)

--
Steve Langasek Give me a lever long enough and a Free OS
Debian Developer to set it on, and I can move the world.
Ubuntu Developer http://www.debian.org/
<email address hidden> <email address hidden>

141. By Steve Langasek

drop the convoluted dirname handling, sh always gives us the full path in $0
so dirname "$0" is always what we want on the path - thanks to Peter Maydell
for noticing!

Revision history for this message
Peter Maydell (pmaydell) wrote :

You still need the quotes setting LINARO_HWPACK_INSTALL:
LINARO_HWPACK_INSTALL="$(which linaro-hwpack-install)"

(For posterity: the case where [ "$(dirname "$0")" != . ] || [ "$0" = "./$(basename "$0")" ] is true is when $0 is a bareword. However the only way to get that that I'm aware of is if you type "sh scriptname".)

Revision history for this message
Steve Langasek (vorlon) wrote :

On Tue, Oct 12, 2010 at 03:17:39PM -0000, Peter Maydell wrote:
> You still need the quotes setting LINARO_HWPACK_INSTALL:
> LINARO_HWPACK_INSTALL="$(which linaro-hwpack-install)"

Nope:

$ LINARO_HWPACK_INSTALL=$(echo foo bar)
$ echo $LINARO_HWPACK_INSTALL
foo bar
$

:-)

--
Steve Langasek Give me a lever long enough and a Free OS
Debian Developer to set it on, and I can move the world.
Ubuntu Developer http://www.debian.org/
<email address hidden> <email address hidden>

Revision history for this message
Peter Maydell (pmaydell) wrote :

OK, that's one-all I think :-) I'm happy with this patch.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'linaro-media-create'
2--- linaro-media-create 2010-10-11 15:09:55 +0000
3+++ linaro-media-create 2010-10-12 15:02:43 +0000
4@@ -37,6 +37,8 @@
5 BOOT_DISK="${DIR}/boot-disc"
6 ROOT_DISK="${DIR}/root-disc"
7
8+PATH="$(dirname "$(readlink -f "$0")"):$PATH"
9+
10 ensure_command() {
11 # ensure_command foo foo-package
12 which "$1" 2>/dev/null 1>/dev/null || ( echo "Installing required command $1 from package $2" && sudo apt-get install "$2" )
13@@ -287,9 +289,7 @@
14 # rootfs.
15 trap "sudo umount ${chroot}/proc || true" EXIT
16
17- # XXX: Assume linaro-hwpack-install lives on the same directory as this
18- # script. This is far from optimal but should do for now.
19- LINARO_HWPACK_INSTALL=$(dirname $0)/linaro-hwpack-install
20+ LINARO_HWPACK_INSTALL=$(which linaro-hwpack-install)
21
22 sudo mv -f ${chroot}/etc/resolv.conf ${TMP_DIR}/resolv.conf.orig
23 sudo cp /etc/resolv.conf ${chroot}/etc/resolv.conf

Subscribers

People subscribed via source and target branches