1e6865dcdSopenharmony_ci/*----------------------------------------------------------------------/
2e6865dcdSopenharmony_ci/ Test if the file is contiguous                                        /
3e6865dcdSopenharmony_ci/----------------------------------------------------------------------*/
4e6865dcdSopenharmony_ci
5e6865dcdSopenharmony_ciFRESULT test_contiguous_file (
6e6865dcdSopenharmony_ci    FIL* fp,    /* [IN]  Open file object to be checked */
7e6865dcdSopenharmony_ci    int* cont   /* [OUT] 1:Contiguous, 0:Fragmented or zero-length */
8e6865dcdSopenharmony_ci)
9e6865dcdSopenharmony_ci{
10e6865dcdSopenharmony_ci    DWORD clst, clsz, step;
11e6865dcdSopenharmony_ci    FSIZE_t fsz;
12e6865dcdSopenharmony_ci    FRESULT fr;
13e6865dcdSopenharmony_ci
14e6865dcdSopenharmony_ci
15e6865dcdSopenharmony_ci    *cont = 0;
16e6865dcdSopenharmony_ci    fr = f_rewind(fp);              /* Validates and prepares the file */
17e6865dcdSopenharmony_ci    if (fr != FR_OK) return fr;
18e6865dcdSopenharmony_ci
19e6865dcdSopenharmony_ci#if FF_MAX_SS == FF_MIN_SS
20e6865dcdSopenharmony_ci    clsz = (DWORD)fp->obj.fs->csize * FF_MAX_SS;    /* Cluster size */
21e6865dcdSopenharmony_ci#else
22e6865dcdSopenharmony_ci    clsz = (DWORD)fp->obj.fs->csize * fp->obj.fs->ssize;
23e6865dcdSopenharmony_ci#endif
24e6865dcdSopenharmony_ci    fsz = f_size(fp);
25e6865dcdSopenharmony_ci    if (fsz > 0) {
26e6865dcdSopenharmony_ci        clst = fp->obj.sclust - 1;  /* A cluster leading the first cluster for first test */
27e6865dcdSopenharmony_ci        while (fsz) {
28e6865dcdSopenharmony_ci            step = (fsz >= clsz) ? clsz : (DWORD)fsz;
29e6865dcdSopenharmony_ci            fr = f_lseek(fp, f_tell(fp) + step);    /* Advances file pointer a cluster */
30e6865dcdSopenharmony_ci            if (fr != FR_OK) return fr;
31e6865dcdSopenharmony_ci            if (clst + 1 != fp->clust) break;       /* Is not the cluster next to previous one? */
32e6865dcdSopenharmony_ci            clst = fp->clust; fsz -= step;          /* Get current cluster for next test */
33e6865dcdSopenharmony_ci        }
34e6865dcdSopenharmony_ci        if (fsz == 0) *cont = 1;    /* All done without fail? */
35e6865dcdSopenharmony_ci    }
36e6865dcdSopenharmony_ci
37e6865dcdSopenharmony_ci    return FR_OK;
38e6865dcdSopenharmony_ci}
39