All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 2/2] fat: Support file modification times
@ 2020-03-07  5:59 David Michael
  2020-03-09 11:33 ` Daniel Kiper
  0 siblings, 1 reply; 3+ messages in thread
From: David Michael @ 2020-03-07  5:59 UTC (permalink / raw)
  To: grub-devel

This allows comparing file ages on EFI system partitions.

Signed-off-by: David Michael <fedora.dm0@gmail.com>
---

Changes since v2:
  * Added comments referencing the specs
  * Set errno when the timestamp is invalid

I set errno rather than print to the console since it looks like most
other file systems don't tend to write to the console.  I also went with
GRUB_ERR_OUT_OF_RANGE since that error only occurs when a time field is
above the valid range, but maybe GRUB_ERR_BAD_FS belongs there.

 grub-core/fs/fat.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/grub-core/fs/fat.c b/grub-core/fs/fat.c
index dc493add2..24a47e2df 100644
--- a/grub-core/fs/fat.c
+++ b/grub-core/fs/fat.c
@@ -26,6 +26,7 @@
 #include <grub/err.h>
 #include <grub/dl.h>
 #include <grub/charset.h>
+#include <grub/datetime.h>
 #ifndef MODE_EXFAT
 #include <grub/fat.h>
 #else
@@ -730,6 +731,31 @@ grub_fat_iterate_dir_next (grub_fshelp_node_t node,
   return grub_errno ? : GRUB_ERR_EOF;
 }
 
+/* Convert a timestamp in exFAT format to seconds since the UNIX epoch
+   according to sections 7.4.8 and 7.4.9 in the exFAT specification.
+   https://docs.microsoft.com/en-us/windows/win32/fileio/exfat-specification */
+static int
+grub_exfat_timestamp (grub_uint32_t field, grub_uint8_t msec, grub_int32_t *nix) {
+  struct grub_datetime datetime = {
+    .year   = (field >> 25) + 1980,
+    .month  = (field & 0x01E00000) >> 21,
+    .day    = (field & 0x001F0000) >> 16,
+    .hour   = (field & 0x0000F800) >> 11,
+    .minute = (field & 0x000007E0) >>  5,
+    .second = (field & 0x0000001F) * 2 + (msec >= 100 ? 1 : 0),
+  };
+
+  /* The conversion below allows seconds=60, so don't trust its validation.  */
+  if ((field & 0x1F) > 29)
+    return 0;
+
+  /* Validate the 10-msec field even though it is rounded down to seconds.  */
+  if (msec > 199)
+    return 0;
+
+  return grub_datetime2unixtime (&datetime, nix);
+}
+
 #else
 
 static grub_err_t
@@ -857,6 +883,27 @@ grub_fat_iterate_dir_next (grub_fshelp_node_t node,
   return grub_errno ? : GRUB_ERR_EOF;
 }
 
+/* Convert a date and time in FAT format to seconds since the UNIX epoch
+   according to sections 11.3.5 and 11.3.6 in ECMA-107.
+   https://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf */
+static int
+grub_fat_timestamp (grub_uint16_t time, grub_uint16_t date, grub_int32_t *nix) {
+  struct grub_datetime datetime = {
+    .year   = (date >> 9) + 1980,
+    .month  = (date & 0x01E0) >> 5,
+    .day    = (date & 0x001F),
+    .hour   = (time >> 11),
+    .minute = (time & 0x07E0) >> 5,
+    .second = (time & 0x001F) * 2,
+  };
+
+  /* The conversion below allows seconds=60, so don't trust its validation.  */
+  if ((time & 0x1F) > 29)
+    return 0;
+
+  return grub_datetime2unixtime (&datetime, nix);
+}
+
 #endif
 
 static grub_err_t lookup_file (grub_fshelp_node_t node,
@@ -966,10 +1013,19 @@ grub_fat_dir (grub_device_t device, const char *path, grub_fs_dir_hook_t hook,
 #ifdef MODE_EXFAT
       if (!ctxt.dir.have_stream)
 	continue;
+      info.mtimeset = grub_exfat_timestamp (grub_le_to_cpu32 (ctxt.entry.type_specific.file.m_time),
+					    ctxt.entry.type_specific.file.m_time_tenth,
+					    &info.mtime);
 #else
       if (ctxt.dir.attr & GRUB_FAT_ATTR_VOLUME_ID)
 	continue;
+      info.mtimeset = grub_fat_timestamp (grub_le_to_cpu16 (ctxt.dir.w_time),
+					  grub_le_to_cpu16 (ctxt.dir.w_date),
+					  &info.mtime);
 #endif
+      if (info.mtimeset == 0)
+	grub_error (GRUB_ERR_OUT_OF_RANGE,
+		    "invalid modification timestamp for %s", path);
 
       if (hook (ctxt.filename, &info, hook_data))
 	break;
-- 
2.21.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v3 2/2] fat: Support file modification times
  2020-03-07  5:59 [PATCH v3 2/2] fat: Support file modification times David Michael
@ 2020-03-09 11:33 ` Daniel Kiper
  2020-03-09 13:47   ` David Michael
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Kiper @ 2020-03-09 11:33 UTC (permalink / raw)
  To: David Michael; +Cc: grub-devel

On Sat, Mar 07, 2020 at 12:59:52AM -0500, David Michael wrote:
> This allows comparing file ages on EFI system partitions.
>
> Signed-off-by: David Michael <fedora.dm0@gmail.com>

Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>

...except...

> ---
>
> Changes since v2:
>   * Added comments referencing the specs
>   * Set errno when the timestamp is invalid
>
> I set errno rather than print to the console since it looks like most
> other file systems don't tend to write to the console.  I also went with
> GRUB_ERR_OUT_OF_RANGE since that error only occurs when a time field is
> above the valid range, but maybe GRUB_ERR_BAD_FS belongs there.
>
>  grub-core/fs/fat.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 56 insertions(+)
>
> diff --git a/grub-core/fs/fat.c b/grub-core/fs/fat.c
> index dc493add2..24a47e2df 100644
> --- a/grub-core/fs/fat.c
> +++ b/grub-core/fs/fat.c
> @@ -26,6 +26,7 @@
>  #include <grub/err.h>
>  #include <grub/dl.h>
>  #include <grub/charset.h>
> +#include <grub/datetime.h>
>  #ifndef MODE_EXFAT
>  #include <grub/fat.h>
>  #else
> @@ -730,6 +731,31 @@ grub_fat_iterate_dir_next (grub_fshelp_node_t node,
>    return grub_errno ? : GRUB_ERR_EOF;
>  }
>
> +/* Convert a timestamp in exFAT format to seconds since the UNIX epoch
> +   according to sections 7.4.8 and 7.4.9 in the exFAT specification.
> +   https://docs.microsoft.com/en-us/windows/win32/fileio/exfat-specification */

The comment is formated incorrectly...

https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Multi_002dLine-Comments

> +static int
> +grub_exfat_timestamp (grub_uint32_t field, grub_uint8_t msec, grub_int32_t *nix) {
> +  struct grub_datetime datetime = {
> +    .year   = (field >> 25) + 1980,
> +    .month  = (field & 0x01E00000) >> 21,
> +    .day    = (field & 0x001F0000) >> 16,
> +    .hour   = (field & 0x0000F800) >> 11,
> +    .minute = (field & 0x000007E0) >>  5,
> +    .second = (field & 0x0000001F) * 2 + (msec >= 100 ? 1 : 0),
> +  };
> +
> +  /* The conversion below allows seconds=60, so don't trust its validation.  */
> +  if ((field & 0x1F) > 29)
> +    return 0;
> +
> +  /* Validate the 10-msec field even though it is rounded down to seconds.  */
> +  if (msec > 199)
> +    return 0;
> +
> +  return grub_datetime2unixtime (&datetime, nix);
> +}
> +
>  #else
>
>  static grub_err_t
> @@ -857,6 +883,27 @@ grub_fat_iterate_dir_next (grub_fshelp_node_t node,
>    return grub_errno ? : GRUB_ERR_EOF;
>  }
>
> +/* Convert a date and time in FAT format to seconds since the UNIX epoch
> +   according to sections 11.3.5 and 11.3.6 in ECMA-107.
> +   https://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf */

Ditto...

This time I can fix them both before commiting if you wish...

Daniel


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3 2/2] fat: Support file modification times
  2020-03-09 11:33 ` Daniel Kiper
@ 2020-03-09 13:47   ` David Michael
  0 siblings, 0 replies; 3+ messages in thread
From: David Michael @ 2020-03-09 13:47 UTC (permalink / raw)
  To: Daniel Kiper; +Cc: The development of GNU GRUB

On Mon, Mar 9, 2020 at 7:33 AM Daniel Kiper <dkiper@net-space.pl> wrote:
> On Sat, Mar 07, 2020 at 12:59:52AM -0500, David Michael wrote:
> > This allows comparing file ages on EFI system partitions.
> >
> > Signed-off-by: David Michael <fedora.dm0@gmail.com>
>
> Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
>
> ...except...
>
> > ---
> >
> > Changes since v2:
> >   * Added comments referencing the specs
> >   * Set errno when the timestamp is invalid
> >
> > I set errno rather than print to the console since it looks like most
> > other file systems don't tend to write to the console.  I also went with
> > GRUB_ERR_OUT_OF_RANGE since that error only occurs when a time field is
> > above the valid range, but maybe GRUB_ERR_BAD_FS belongs there.
> >
> >  grub-core/fs/fat.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 56 insertions(+)
> >
> > diff --git a/grub-core/fs/fat.c b/grub-core/fs/fat.c
> > index dc493add2..24a47e2df 100644
> > --- a/grub-core/fs/fat.c
> > +++ b/grub-core/fs/fat.c
> > @@ -26,6 +26,7 @@
> >  #include <grub/err.h>
> >  #include <grub/dl.h>
> >  #include <grub/charset.h>
> > +#include <grub/datetime.h>
> >  #ifndef MODE_EXFAT
> >  #include <grub/fat.h>
> >  #else
> > @@ -730,6 +731,31 @@ grub_fat_iterate_dir_next (grub_fshelp_node_t node,
> >    return grub_errno ? : GRUB_ERR_EOF;
> >  }
> >
> > +/* Convert a timestamp in exFAT format to seconds since the UNIX epoch
> > +   according to sections 7.4.8 and 7.4.9 in the exFAT specification.
> > +   https://docs.microsoft.com/en-us/windows/win32/fileio/exfat-specification */
>
> The comment is formated incorrectly...
>
> https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Multi_002dLine-Comments
>
> > +static int
> > +grub_exfat_timestamp (grub_uint32_t field, grub_uint8_t msec, grub_int32_t *nix) {
> > +  struct grub_datetime datetime = {
> > +    .year   = (field >> 25) + 1980,
> > +    .month  = (field & 0x01E00000) >> 21,
> > +    .day    = (field & 0x001F0000) >> 16,
> > +    .hour   = (field & 0x0000F800) >> 11,
> > +    .minute = (field & 0x000007E0) >>  5,
> > +    .second = (field & 0x0000001F) * 2 + (msec >= 100 ? 1 : 0),
> > +  };
> > +
> > +  /* The conversion below allows seconds=60, so don't trust its validation.  */
> > +  if ((field & 0x1F) > 29)
> > +    return 0;
> > +
> > +  /* Validate the 10-msec field even though it is rounded down to seconds.  */
> > +  if (msec > 199)
> > +    return 0;
> > +
> > +  return grub_datetime2unixtime (&datetime, nix);
> > +}
> > +
> >  #else
> >
> >  static grub_err_t
> > @@ -857,6 +883,27 @@ grub_fat_iterate_dir_next (grub_fshelp_node_t node,
> >    return grub_errno ? : GRUB_ERR_EOF;
> >  }
> >
> > +/* Convert a date and time in FAT format to seconds since the UNIX epoch
> > +   according to sections 11.3.5 and 11.3.6 in ECMA-107.
> > +   https://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf */
>
> Ditto...
>
> This time I can fix them both before commiting if you wish...

Yes, please.  I just wrote them that way to be consistent with the
other multiline comments in that file.

Thanks.

David


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-03-09 13:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-03-07  5:59 [PATCH v3 2/2] fat: Support file modification times David Michael
2020-03-09 11:33 ` Daniel Kiper
2020-03-09 13:47   ` David Michael

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.