From ee7d68c9fe11ab965be124bbfd571280d2eef22b Mon Sep 17 00:00:00 2001 From: John Safranek Date: Tue, 12 May 2026 15:56:06 -0700 Subject: [PATCH] Convert FatFS date/time to Unix timestamp in SFTP - Add TimeTo32_FatFS() to decode FatFS fdate/ftime fields via mktime() - Use converted timestamp for atime/mtime instead of raw fdate --- src/wolfsftp.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/src/wolfsftp.c b/src/wolfsftp.c index 0e62d87cf..094b4f8de 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -4850,6 +4850,54 @@ static int SFTP_GetAttributes_Handle(WOLFSSH* ssh, byte* handle, int handleSz, #elif defined(WOLFSSH_FATFS) +/* + * fdate + * The date when the file was modified or the directory was created. + * bit15:9 + * Year origin from 1980 (0..127) + * bit8:5 + * Month (1..12) + * bit4:0 + * Day (1..31) + * + * ftime + * The time when the file was modified or the directory was created. + * bit15:11 + * Hour (0..23) + * bit10:5 + * Minute (0..59) + * bit4:0 + * Second / 2 (0..29) + * + * mktime() expects month from 0 to 11. FatFS months + * are saved as 1 to 12. Hence 1 is being deducted to + * make it compatible with Unix time stamp. + */ +#define WS_GETDAY(d) ((d) & 0x001f) +#define WS_GETMON(d) ((((d) >> 5) & 0x000f) - 1) +#define WS_GETYEAR(d) (((d) >> 9) & 0x007f) +#define WS_GETHOUR(t) (((t) >> 11) & 0x001f) +#define WS_GETMIN(t) (((t) >> 5 ) & 0x003f) +#define WS_GETSEC(t) (((t) << 1 ) & 0x003f) + +#ifndef NO_WOLFSSH_MKTIME +/* convert nucleus date and time shorts to word32 + * returns results in Unix time stamp */ +static word32 TimeTo32_FatFS(word16 d, word16 t) +{ + struct tm tmp = {0}; + + tmp.tm_mday = WS_GETDAY(d); + tmp.tm_mon = WS_GETMON(d); + tmp.tm_year = WS_GETYEAR(d); + tmp.tm_hour = WS_GETHOUR(t); + tmp.tm_min = WS_GETMIN(t); + tmp.tm_sec = WS_GETSEC(t); + + return mktime(&tmp); +} +#endif /* NO_WOLFSSH_MKTIME */ + /* FatFs has its own structure for file attributes */ static int SFTP_GetAttributes(void* fs, const char* fileName, @@ -4907,10 +4955,13 @@ static int SFTP_GetAttributes(void* fs, const char* fileName, } #ifndef NO_WOLFSSH_MKTIME - /* get file times */ - atr->flags |= WOLFSSH_FILEATRB_TIME; - atr->atime = info.fdate; - atr->mtime = info.fdate; + { + /* get file times */ + word32 daytime = TimeTo32_FatFS(info.fdate, info.ftime); + atr->flags |= WOLFSSH_FILEATRB_TIME; + atr->atime = daytime; + atr->mtime = daytime; + } #endif /* NO_WOLFSSH_MKTIME */ return WS_SUCCESS; }