Skip to contentSkip to conversation

fakedate: simplify logic

PATCH New Peter Korsgaard 3 participants 4 messages Feb 7, 2017 – Jul 3, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fakedate: simplify logic

We can use IFS=':' to get the shell to split on colons instead of running tr, and using -ef to check for the same file is nicer than relying on magic symlink-to-fakedate.

Notice that -ef isn't stricly posix (but supported by bash/dash/zsh), so I've changed the shebang to /bin/bash.

While we are at it, restructure the logic to do a single exec at the end instead of handling the epoch/!epoch cases differently for simplicity.

With that out of the way we can directly install it as $HOST/usr/bin/date instead of the fakedate / date symlink.

---
 package/fakedate/fakedate    | 16 +++++-----------
 package/fakedate/fakedate.mk |  3 +--
 2 files changed, 6 insertions(+), 13 deletions(-)
package/fakedate/fakedate +5 -11
index 4a9b9b5e6..abe6b38f9 100755
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 # vim: set sw=4 expandtab:
 #
 # This program is free software; you can redistribute it and/or modify
@@ -18,18 +18,12 @@
 # Copyright (C) 2016 J?r?me Pouiller <[email protected]>
 #
 
-# Sanity check
-if ! readlink -f "$0" | grep -q fakedate; then
-    echo "fakedate: Please name this script \`fakedate'"
-    exit 1
-fi
-
 DATE_BIN=false
 # Do not call `date' directly since it will produce an infinite recursion.
 # Instead, find path of true `date' binary.
-for P in `echo $PATH | tr ':' ' '`; do
+IFS=':'; for P in $PATH; do
     if [ -x "$P/date" ]; then
-        if readlink -f "$P/date" | grep -qv fakedate; then
+        if ! [ "$P/date" -ef "$0" ]; then
             DATE_BIN="$P/date"
             break;
         fi
@@ -50,8 +44,8 @@ if [ -n "$SOURCE_DATE_EPOCH" ]; then
     done
     if [ $FORCE_EPOCH -eq 1 ]; then
         echo "date: Warning: using \$SOURCE_DATE_EPOCH instead of true time" >&2
-        exec $DATE_BIN -d "@$SOURCE_DATE_EPOCH" "$@"
+	ARGS="-d @$SOURCE_DATE_EPOCH"
     fi
 fi
 
-exec $DATE_BIN "$@"
+exec $DATE_BIN $ARGS "$@"
package/fakedate/fakedate.mk +1 -2
index 61d4bd702..e81ce5dac 100644
@@ -8,8 +8,7 @@
 HOST_FAKEDATE_LICENSE = GPLv2+
 
 define HOST_FAKEDATE_INSTALL_CMDS
-	$(INSTALL) -D -m 755 package/fakedate/fakedate $(HOST_DIR)/usr/bin/fakedate
-	ln -sfn fakedate $(HOST_DIR)/usr/bin/date
+	$(INSTALL) -D -m 755 package/fakedate/fakedate $(HOST_DIR)/usr/bin/date
 endef
 
 $(eval $(host-generic-package))
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/1 fakedate: simplify logic

Hello Peter,

On Tuesday 07 February 2017 23:08:17 Peter Korsgaard wrote:
[...]

package/fakedate/fakedate line 4

Quoted 10 lines
package/fakedate/fakedate +1 -1
index 4a9b9b5e6..abe6b38f9 100755
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 # vim: set sw=4 expandtab:
 #
 # This program is free software; you can redistribute it and/or 

modify

package/fakedate/fakedate line 22

Quoted 12 lines
package/fakedate/fakedate +0 -6
@@ -18,18 +18,12 @@
 # Copyright (C) 2016 J?r?me Pouiller <[email protected]>
 #
 
-# Sanity check
-if ! readlink -f "$0" | grep -q fakedate; then
-    echo "fakedate: Please name this script \`fakedate'"
-    exit 1
-fi
-
 DATE_BIN=false
 # Do not call `date' directly since it will produce an infinite 

recursion.

package/fakedate/fakedate line 24

Quoted 3 lines
package/fakedate/fakedate +1 -1
 # Instead, find path of true `date' binary.
-for P in `echo $PATH | tr ':' ' '`; do
+IFS=':'; for P in $PATH; do

I think you have to reset $IFS to its previous value after used it.

Globally, I dislike use of $IFS. I think it may produce unattended side effects.

package/fakedate/fakedate line 26

Quoted 3 lines
package/fakedate/fakedate +1 -1
     if [ -x "$P/date" ]; then
-        if readlink -f "$P/date" | grep -qv fakedate; then
+        if ! [ "$P/date" -ef "$0" ]; then

Nice! I didn't know '-ef'.

package/fakedate/fakedate if [ -n "$SOURCE_DATE_EPOCH" ]; then · line 46

Quoted 7 lines
package/fakedate/fakedate +0 -0
             DATE_BIN="$P/date"
             break;
         fi
@@ -50,8 +44,8 @@ if [ -n "$SOURCE_DATE_EPOCH" ]; then
     done
     if [ $FORCE_EPOCH -eq 1 ]; then
         echo "date: Warning: using \$SOURCE_DATE_EPOCH instead of 

true time" >&2

package/fakedate/fakedate if [ -n "$SOURCE_DATE_EPOCH" ]; then · line 47

Quoted 2 lines
package/fakedate/fakedate +1 -1
-        exec $DATE_BIN -d "@$SOURCE_DATE_EPOCH" "$@"
+	ARGS="-d @$SOURCE_DATE_EPOCH"

Indentation seems incorrect: s/\t/ /

package/fakedate/fakedate if [ -n "$SOURCE_DATE_EPOCH" ]; then · line 51

Quoted 5 lines
package/fakedate/fakedate +1 -1
     fi
 fi
 
-exec $DATE_BIN "$@"
+exec $DATE_BIN $ARGS "$@"

It's matter of taste.

Review

Quoted 1 line
(quoted diff) +0 -0

b/package/fakedate/fakedate.mk

package/fakedate/fakedate.mk line 11

Quoted 8 lines

index 61d4bd702..e81ce5dac 100644

package/fakedate/fakedate.mk +0 -1
@@ -8,8 +8,7 @@
 HOST_FAKEDATE_LICENSE = GPLv2+
 
 define HOST_FAKEDATE_INSTALL_CMDS
-	$(INSTALL) -D -m 755 package/fakedate/fakedate 

$(HOST_DIR)/usr/bin/fakedate

package/fakedate/fakedate.mk line 11

Quoted 2 lines
package/fakedate/fakedate.mk +1 -1
-	ln -sfn fakedate $(HOST_DIR)/usr/bin/date
+	$(INSTALL) -D -m 755 package/fakedate/fakedate 

$(HOST_DIR)/usr/bin/date

Hmm... In order to not confuse user, I prefer to keep fakedate as original name and adding a symlink beside.

Quoted 4 lines
package/fakedate/fakedate.mk +0 -0
 endef
 
 $(eval $(host-generic-package))
Signature · 3 lines
J?r?me Pouiller, Sysmic
Embedded Linux specialist
http://www.sysmic.fr
#2 lore raw

PATCH 1/1 fakedate: simplify logic

Quoted 28 lines · J?r?me Pouiller

Hello Peter,

On Tuesday 07 February 2017 23:08:17 Peter Korsgaard wrote:
[...]

package/fakedate/fakedate line 4

Quoted 10 lines
package/fakedate/fakedate +1 -1
index 4a9b9b5e6..abe6b38f9 100755
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 # vim: set sw=4 expandtab:
 #
 # This program is free software; you can redistribute it and/or 

modify

package/fakedate/fakedate line 22

Quoted 12 lines
package/fakedate/fakedate +0 -6
@@ -18,18 +18,12 @@
 # Copyright (C) 2016 J?r?me Pouiller <[email protected]>
 #
 
-# Sanity check
-if ! readlink -f "$0" | grep -q fakedate; then
-    echo "fakedate: Please name this script \`fakedate'"
-    exit 1
-fi
-
 DATE_BIN=false
 # Do not call `date' directly since it will produce an infinite 

recursion.

Quoted 6 lines

package/fakedate/fakedate line 24

Quoted 3 lines
package/fakedate/fakedate +1 -1
 # Instead, find path of true `date' binary.
-for P in `echo $PATH | tr ':' ' '`; do
+IFS=':'; for P in $PATH; do

I think you have to reset $IFS to its previous value after used it.

That's on principle, though in this script there is no more word splitting
after the loop.

Quoted 3 lines

Globally, I dislike use of $IFS. I think it may produce unattended side effects.

Yeah, I find it particularly confusing when you need a different word splitting
inside the loop...

So indeed, I'd drop the IFS part from this patch. I don't think it makes things
simpler.

Quoted 7 lines

package/fakedate/fakedate line 26

Quoted 5 lines
package/fakedate/fakedate +1 -1
     if [ -x "$P/date" ]; then
-        if readlink -f "$P/date" | grep -qv fakedate; then
+        if ! [ "$P/date" -ef "$0" ]; then

Nice! I didn't know '-ef'.

If you want to avoid the dependency on bash, you can also use

if [ "$(readlink -f "$P/date")" != "$(readlink -f "$0")" ]; then

but it's not exactly more readable :-)

Quoted 9 lines

package/fakedate/fakedate if [ -n "$SOURCE_DATE_EPOCH" ]; then · line 46

Quoted 8 lines
package/fakedate/fakedate +0 -0
             DATE_BIN="$P/date"
             break;
         fi
@@ -50,8 +44,8 @@ if [ -n "$SOURCE_DATE_EPOCH" ]; then
     done
     if [ $FORCE_EPOCH -eq 1 ]; then
         echo "date: Warning: using \$SOURCE_DATE_EPOCH instead of 

true time" >&2

Quoted 4 lines

package/fakedate/fakedate if [ -n "$SOURCE_DATE_EPOCH" ]; then · line 47

Quoted 2 lines
package/fakedate/fakedate +1 -1
-        exec $DATE_BIN -d "@$SOURCE_DATE_EPOCH" "$@"
+	ARGS="-d @$SOURCE_DATE_EPOCH"

Indentation seems incorrect: s/\t/ /

Very true.

Quoted 8 lines

package/fakedate/fakedate if [ -n "$SOURCE_DATE_EPOCH" ]; then · line 51

Quoted 6 lines
package/fakedate/fakedate +1 -1
     fi
 fi
 
-exec $DATE_BIN "$@"
+exec $DATE_BIN $ARGS "$@"

It's matter of taste.

I'm fine either way.

Quoted 13 lines

Review

Quoted 3 lines
(quoted diff) +0 -0

b/package/fakedate/fakedate.mk

package/fakedate/fakedate.mk line 11

Quoted 8 lines

index 61d4bd702..e81ce5dac 100644

package/fakedate/fakedate.mk +0 -1
@@ -8,8 +8,7 @@
 HOST_FAKEDATE_LICENSE = GPLv2+
 
 define HOST_FAKEDATE_INSTALL_CMDS
-	$(INSTALL) -D -m 755 package/fakedate/fakedate 

$(HOST_DIR)/usr/bin/fakedate

Quoted 6 lines

package/fakedate/fakedate.mk line 11

Quoted 2 lines
package/fakedate/fakedate.mk +1 -1
-	ln -sfn fakedate $(HOST_DIR)/usr/bin/date
+	$(INSTALL) -D -m 755 package/fakedate/fakedate 

$(HOST_DIR)/usr/bin/date

Hmm... In order to not confuse user, I prefer to keep fakedate as original name and adding a symlink beside.

I don't, I think it's clearer as a script than as a symlink.

I don't mind much about the IFS part (it really should have been a separate
patch, though), and the tab-space mixup can be fixed up while committing, so

Regards,
Arnout

Quoted 6 lines
Quoted 5 lines
package/fakedate/fakedate.mk +0 -0
 endef
 
 $(eval $(host-generic-package))
Signature · 6 lines
Arnout Vandecappelle                          arnout at mind be
Senior Embedded Software Architect            +32-16-286500
Essensium/Mind                                http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium           BE 872 984 063 RPR Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint:  7493 020B C7E3 8618 8DEC 222C 82EB F404 F9AC 0DDF
#3 lore raw

PATCH 1/1 fakedate: simplify logic

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Arnout" == Arnout Vandecappelle <[email protected]> writes:

Hi,

Quoted 9 lines
Quoted 2 lines

Hmm... In order to not confuse user, I prefer to keep fakedate as original name and adding a symlink beside.

I don't, I think it's clearer as a script than as a symlink.

I don't mind much about the IFS part (it really should have been a separate
patch, though), and the tab-space mixup can be fixed up while committing, so

Thanks, committed after dropping the IFS part.

Signature · 1 line
Bye, Peter Korsgaard