]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
debugfs: fix string_to_time for MacOS strptime()
authorAndreas Dilger <adilger@dilger.ca>
Tue, 20 May 2014 22:31:10 +0000 (16:31 -0600)
committerTheodore Ts'o <tytso@mit.edu>
Thu, 22 May 2014 21:52:23 +0000 (17:52 -0400)
The strptime() function does not update fields in struct tm that are
not specified in the input format.  The glibc implementation sets the
tm_yday field (%j) when any of the year (%Y), month (%m), or day (%d)
fields are changed, but the MacOS strptime() does not set tm_yday in
this case.  This caused string_to_time() to calculate the wrong Unix
epoch on MacOS. If tm_yday is unset, compute it in string_to_time().

This also fixes test regression failures for FreeBSD.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
debugfs/util.c
tests/f_dup_resize/script
tests/f_uninit_last_uninit/script

index 9ddfe0ba089a989d3c8980e2b938b007a13f641d..20c6c61a3fed228ed1d82aa79b6649f833794359 100644 (file)
@@ -245,6 +245,20 @@ time_t string_to_time(const char *arg)
                ts.tm_mday = 0;
 #endif
        ts.tm_isdst = -1;
+       /* strptime() may only update the specified fields, which does not
+        * necessarily include ts.tm_yday (%j).  Calculate this if unset:
+        *
+        * Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
+        * 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
+        *
+        * Start with 31 days per month.  Even months have only 30 days, but
+        * reverse in August, subtract one day for those months. February has
+        * only 28 days, not 30, subtract two days. Add day of month, minus
+        * one, since day is not finished yet.  Leap years handled afterward. */
+       if (ts.tm_yday == 0)
+               ts.tm_yday = (ts.tm_mon * 31) -
+                       ((ts.tm_mon - (ts.tm_mon > 7)) / 2) -
+                       2 * (ts.tm_mon > 1) + ts.tm_mday - 1;
        ret = ts.tm_sec + ts.tm_min*60 + ts.tm_hour*3600 + ts.tm_yday*86400 +
                (ts.tm_year-70)*31536000 + ((ts.tm_year-69)/4)*86400 -
                ((ts.tm_year-1)/100)*86400 + ((ts.tm_year+299)/400)*86400;
index 659c92ee1d4daf6ef3924df2b85079876bfb5ee1..19fa120fc5805d6addd306763e4039fb37f876da 100644 (file)
@@ -12,7 +12,7 @@ freeb 4 4
 freeb 8195 4
 write $TEST_DATA debugfs
 set_current_time 20050411000000
-set_inode_field debugfs mtime 2005041100000000
+set_inode_field debugfs mtime 20050411000000
 q
 EOF
 
index 2fe4f3a00fb70dc550065d45b5f87041c3399340..376c97823d59d8fa1fe2a825116b781abdbf50be 100644 (file)
@@ -5,7 +5,7 @@ SKIP_GUNZIP="true"
 touch $TMPFILE
 $MKE2FS -N 32 -F -o Linux -O uninit_bg -b 1024 $TMPFILE 10000 > /dev/null 2>&1 
 $DEBUGFS -w $TMPFILE << EOF > /dev/null 2>&1
-set_current_time 200704102100
+set_current_time 20070410210000
 set_super_value lastcheck 0
 set_super_value hash_seed null
 set_super_value mkfs_time 0