Skip to content

Commit a4601b5

Browse files
committed
fatfs: get correct cluster size for root directory
1 parent 06c8084 commit a4601b5

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

include/filesys/fatfs/fatfs.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,9 @@ int v_stat(const char *name, struct stat *buf)
223223
{
224224
int r;
225225
FILINFO finfo;
226+
VOLINFO vinfo;
226227
unsigned mode;
227-
unsigned clustersize = 4096;
228+
unsigned clustersize = 32768; /* default */
228229
#ifdef _DEBUG_FATFS
229230
__builtin_printf("v_stat(%s)\n", name);
230231
#endif
@@ -235,11 +236,16 @@ int v_stat(const char *name, struct stat *buf)
235236
r = 0;
236237
} else {
237238
r = f_stat(name, &finfo);
238-
clustersize = finfo.fclust;
239239
}
240240
if (r != 0) {
241241
return _set_dos_error(r);
242242
}
243+
if (f_getvolinfo(name, &vinfo) == FR_OK) {
244+
#ifdef _DEBUG_FATFS
245+
__builtin_printf("f_getvolinfo: ssize=%u csize=%u\n", vinfo.ssize, vinfo.csize);
246+
#endif
247+
clustersize = vinfo.csize * vinfo.ssize;
248+
}
243249
mode = S_IRUSR | S_IRGRP | S_IROTH;
244250
if (!(finfo.fattrib & AM_RDO)) {
245251
mode |= S_IWUSR | S_IWGRP | S_IWOTH;

include/filesys/fatfs/ff.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4722,6 +4722,26 @@ FRESULT f_findfirst (
47224722

47234723
#endif /* FF_USE_FIND */
47244724

4725+
#if FF_FS_MINIMIZE == 0
4726+
FRESULT f_getvolinfo (
4727+
const TCHAR* path,
4728+
VOLINFO *info
4729+
)
4730+
{
4731+
FRESULT res;
4732+
DIR dj;
4733+
DEF_NAMBUF
4734+
4735+
/* Get logical drive */
4736+
res = mount_volume(&path, &dj.obj.fs, 0);
4737+
if (res == FR_OK) {
4738+
info->csize = dj.obj.fs->csize;
4739+
info->ssize = SS(dj.obj.fs);
4740+
}
4741+
LEAVE_FF(dj.obj.fs, res);
4742+
4743+
}
4744+
#endif
47254745

47264746

47274747
#if FF_FS_MINIMIZE == 0
@@ -4754,7 +4774,6 @@ FRESULT f_stat (
47544774
FREE_NAMBUF();
47554775
}
47564776

4757-
if (fno) fno->fclust = (DWORD)dj.obj.fs->csize * SS(dj.obj.fs);
47584777
LEAVE_FF(dj.obj.fs, res);
47594778
}
47604779

@@ -5379,8 +5398,6 @@ FRESULT f_getlabel (
53795398
LEAVE_FF(fs, res);
53805399
}
53815400

5382-
5383-
53845401
#if !FF_FS_READONLY
53855402
/*-----------------------------------------------------------------------*/
53865403
/* Set Volume Label */

include/filesys/fatfs/ff.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ typedef struct {
249249
WORD fdate; /* Modified date */
250250
WORD ftime; /* Modified time */
251251
BYTE fattrib; /* File attribute */
252-
DWORD fclust; /* File cluster size in bytes */
253252
#if FF_USE_LFN
254253
TCHAR altname[FF_SFN_BUF + 1];/* Altenative file name */
255254
TCHAR fname[FF_LFN_BUF + 1]; /* Primary file name */
@@ -259,6 +258,12 @@ typedef struct {
259258
} FILINFO;
260259

261260

261+
/* Volume information structure (VOLINFO) */
262+
263+
typedef struct {
264+
DWORD ssize; /* Sector size in bytes */
265+
DWORD csize; /* Cluster size in sectors */
266+
} VOLINFO;
262267

263268
/* Format parameter structure (MKFS_PARM) */
264269

@@ -326,6 +331,7 @@ FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */
326331
FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */
327332
FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */
328333
FRESULT f_setlabel (const TCHAR* label); /* Set volume label */
334+
FRESULT f_getvolinfo (const TCHAR* path, VOLINFO *info); /* Get information about the volume */
329335
FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
330336
FRESULT f_expand (FIL* fp, FSIZE_t fsz, BYTE opt); /* Allocate a contiguous block to the file */
331337
FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */

0 commit comments

Comments
 (0)