[OpenWrt-Devel] [PATCH v2] base-files: init/sysfixtime - exclude dnsmasq.time

Justin Vallon justinvallon at gmail.com
Sat Sep 26 01:26:56 EDT 2015


On 9/22/15 1:52 PM, Bastian Bittorf wrote:
> +maxtime() {
> +	local dir file
> +
> +	find /etc -type d | while read dir; do
> +		file="$dir/$( ls -1t "$dir" | head -n1 )"
> +		[ -e "$file" -a "$file" != '/etc/dnsmasq.time' ] && date -r "$file" +%s
> +	done | sort -nr | head -n1
> +}
It appears that if /etc/dnsmasq.time is the newest file in /etc, it will
"shadow" the next-newest file.  "ls -lt /etc | head -n1" will be
/etc/dnsmasq.time, and no other /etc/* files will be checked.  The new
code will also check the mod time of directories and symlinks, unlike
the previous implementation.

I was experimenting a little.  The fastest is probably "ls -t $(find
/etc -type f) | head -1", but that does not behave well if there are too
many files in /etc (too many args).  Using xargs to split the ls
invocations is possible, but then each xargs invocation needs to take
the max separately.  The fourth sample below does this.

"test" has: test $a -ot $b ($a older than $b).  This makes the script
reasonably straightforward (only checks files, ignores arbitrary paths)
and pretty fast in my test:

local file newest
for file in $( find /etc -type f ! -path /etc/dnsmasq.time ) ; do
    [ -z "$newest" -o "$newest" -ot "$file"] && newest=$file
done
echo $newest

# time sh -c 'find /etc -type f -exec date -r {} +%s \; | sort -nr |
head -n1'
1443239314
real    0m 0.77s
user    0m 0.17s
sys     0m 0.58s
# time sh -c 'date -r $(ls -t $( find /etc -type f ) | head -1) +%s'   
# fails if /etc contains too many files
1443239314
real    0m 0.04s
user    0m 0.02s
sys     0m 0.03s
# time sh -c 'local file newest; for file in $(find /etc -type f ! -path
/etc/dnsmasq.time) ; do [ -z "$newest" -o "$newest" -ot "$file" ] &&
newest=$file ; done ; [ "$newest" ] && date -r "$newest" +%s'
1443239314
real    0m 0.06s
user    0m 0.03s
sys     0m 0.03s
# time sh -c 'find /etc -type f ! -path /etc/dnsmasq.time | xargs sh -c
'\''ls -t "$@" | head -1'\'' - | while read p ; do date -r $p +%s ; done
| sort -nr | head -1'
1443239314
real    0m 0.07s
user    0m 0.02s
sys     0m 0.06s

Feel free to use or ignore.

-- 
-Justin
JustinVallon at gmail.com


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4251 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.infradead.org/pipermail/openwrt-devel/attachments/20150926/35470c94/attachment.p7s>
-------------- next part --------------
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list