[PATCH] touch: add config option to set microsecond timestamps

Xabier Oneca -- xOneca xoneca at gmail.com
Thu Apr 8 10:27:17 UTC 2021


Hi Peter,

So quick for the patch! Nice! :)

> This patch adds a config option which makes touch set timestamps to the
> current microsecond, instead of the current second.
>
> Signed-off-by: Peter D <urmum69 at snopyta.org>
> ---
> This patch should be applied on top of Xabier Oneca's patches to add the
> -a and -m options to busybox
>
>  coreutils/touch.c | 22 ++++++++++++++++++----
>  1 file changed, 18 insertions(+), 4 deletions(-)
>
> diff --git a/coreutils/touch.c b/coreutils/touch.c
> index e80720982..ac9e233ad 100644
> --- a/coreutils/touch.c
> +++ b/coreutils/touch.c
> @@ -33,6 +33,13 @@
>  //config:      depends on TOUCH
>  //config:      help
>  //config:      Enable touch to use a reference file or a given date/time argument.
> +//config:
> +//config:config FEATURE_TOUCH_MICROSECONDS
> +//config:      bool "Add support for setting microsecond timestamps"
> +//config:      default y

I would make it default 'y' to preserve filesystem timestamps when
using -a/-m, but maybe it is not so important the sub-second precision
in touch..?

> +//config:      depends on TOUCH

Depend on FEATURE_TOUCH_SUSV3 instead.

> +//config:      help
> +//config:      Enable touch to set timestamps to the current microsecond
>
>  //applet:IF_TOUCH(APPLET_NOFORK(touch, touch, BB_DIR_BIN, BB_SUID_DROP, touch))
>
> @@ -142,10 +149,11 @@ int touch_main(int argc UNUSED_PARAM, char **argv)
>                 xstat(reference_file, &stbuf);
>                 timebuf[0].tv_sec = stbuf.st_atime;
>                 timebuf[1].tv_sec = stbuf.st_mtime;
> -               /* Can use .st_mtim.tv_nsec
> -                * (or is it .st_mtimensec?? see date.c)
> -                * to set microseconds too.
> -                */
> +#if ENABLE_FEATURE_TOUCH_MICROSECONDS
> +               /* divide nanoseconds by 1000 to get microseconds */
> +               timebuf[0].tv_usec = stbuf.st_atim.tv_nsec / 1000;
> +               timebuf[1].tv_usec = stbuf.st_mtim.tv_nsec / 1000;
> +#endif
>                 newtime = timebuf;
>         }
>
> @@ -190,8 +198,14 @@ int touch_main(int argc UNUSED_PARAM, char **argv)
>                                  */
>                                 if (!(opts & OPT_a))
>                                         timebuf[0].tv_sec = stbuf.st_atime;
> +#if ENABLE_FEATURE_TOUCH_MICROSECONDS
> +                                       timebuf[0].tv_usec = stbuf.st_atim.tv_nsec / 1000;
> +#endif

Hey! Not so fast! You forgot the curly brackets! ;)

>                                 if (!(opts & OPT_m))
>                                         timebuf[1].tv_sec = stbuf.st_mtime;
> +#if ENABLE_FEATURE_TOUCH_MICROSECONDS
> +                                       timebuf[1].tv_usec = stbuf.st_mtim.tv_nsec / 1000;
> +#endif

Here too.

>                         }
>                 }
>
> --
> 2.31.1

Cheers,

Xabier Oneca_,,_


More information about the busybox mailing list