Skip to content

Commit c2f40e0

Browse files
ejohnstownpadelsbach
authored andcommitted
Convert FatFS date/time to Unix timestamp in SFTP
- Add SetAttrTime() helper to decode fdate/ftime into a Unix timestamp via mktime() and assigns it to atr->atime/mtime. - Use it from both SFTP_GetAttributes() (was using raw fdate) and SFTP_GetAttributes_Handle() (was using raw ftime, losing the date).
1 parent 7d4fa32 commit c2f40e0

1 file changed

Lines changed: 58 additions & 12 deletions

File tree

src/wolfsftp.c

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4850,6 +4850,61 @@ static int SFTP_GetAttributes_Handle(WOLFSSH* ssh, byte* handle, int handleSz,
48504850

48514851
#elif defined(WOLFSSH_FATFS)
48524852

4853+
/*
4854+
* fdate
4855+
* The date when the file was modified or the directory was created.
4856+
* bit15:9
4857+
* Year origin from 1980 (0..127)
4858+
* bit8:5
4859+
* Month (1..12)
4860+
* bit4:0
4861+
* Day (1..31)
4862+
*
4863+
* ftime
4864+
* The time when the file was modified or the directory was created.
4865+
* bit15:11
4866+
* Hour (0..23)
4867+
* bit10:5
4868+
* Minute (0..59)
4869+
* bit4:0
4870+
* Second / 2 (0..29)
4871+
*
4872+
* mktime() expects month from 0 to 11 and years starting at
4873+
* 1900. FatFS months are saved as 1 to 12 and the years
4874+
* start counting at 1980.
4875+
*/
4876+
#define FATFS_GETDAY(d) ((d) & 0x001f)
4877+
#define FATFS_GETMON(d) ((((d) >> 5) & 0x000f) - 1)
4878+
#define FATFS_GETYEAR(d) ((((d) >> 9) & 0x007f) + 80)
4879+
#define FATFS_GETHOUR(t) (((t) >> 11) & 0x001f)
4880+
#define FATFS_GETMIN(t) (((t) >> 5 ) & 0x003f)
4881+
#define FATFS_GETSEC(t) (((t) << 1 ) & 0x003f)
4882+
4883+
static void SetAttrTime(const FILINFO* info, WS_SFTP_FILEATRB* atr)
4884+
{
4885+
#ifndef NO_WOLFSSH_MKTIME
4886+
struct tm tmp = { 0 };
4887+
word32 daytime;
4888+
4889+
/* convert fatfs date and time shorts to word32
4890+
* returns results in Unix time stamp */
4891+
tmp.tm_mday = FATFS_GETDAY(info->fdate);
4892+
tmp.tm_mon = FATFS_GETMON(info->fdate);
4893+
tmp.tm_year = FATFS_GETYEAR(info->fdate);
4894+
tmp.tm_hour = FATFS_GETHOUR(info->ftime);
4895+
tmp.tm_min = FATFS_GETMIN(info->ftime);
4896+
tmp.tm_sec = FATFS_GETSEC(info->ftime);
4897+
daytime = mktime(&tmp);
4898+
4899+
atr->flags |= WOLFSSH_FILEATRB_TIME;
4900+
atr->atime = daytime;
4901+
atr->mtime = daytime;
4902+
#else
4903+
WOLFSSH_UNUSED(info);
4904+
WOLFSSH_UNUSED(atr);
4905+
#endif /* NO_WOLFSSH_MKTIME */
4906+
}
4907+
48534908
/* FatFs has its own structure for file attributes */
48544909

48554910
static int SFTP_GetAttributes(void* fs, const char* fileName,
@@ -4906,12 +4961,8 @@ static int SFTP_GetAttributes(void* fs, const char* fileName,
49064961
}
49074962
}
49084963

4909-
#ifndef NO_WOLFSSH_MKTIME
4910-
/* get file times */
4911-
atr->flags |= WOLFSSH_FILEATRB_TIME;
4912-
atr->atime = info.fdate;
4913-
atr->mtime = info.fdate;
4914-
#endif /* NO_WOLFSSH_MKTIME */
4964+
SetAttrTime(&info, atr);
4965+
49154966
return WS_SUCCESS;
49164967
}
49174968

@@ -4952,12 +5003,7 @@ static int SFTP_GetAttributes_Handle(WOLFSSH* ssh, byte* handle, int handleSz,
49525003
}
49535004
}
49545005

4955-
#ifndef NO_WOLFSSH_MKTIME
4956-
/* get file times */
4957-
atr->flags |= WOLFSSH_FILEATRB_TIME;
4958-
atr->atime = info.ftime;
4959-
atr->mtime = info.ftime;
4960-
#endif /* NO_WOLFSSH_MKTIME */
5006+
SetAttrTime(&info, atr);
49615007

49625008
WOLFSSH_UNUSED(ssh);
49635009
WOLFSSH_UNUSED(handleSz);

0 commit comments

Comments
 (0)