]>
cloudbase.mooo.com Git - z180-stamp.git/blob - fatfs/doc/img/app3.c
1 /*----------------------------------------------------------------------/
2 / Allocate a contiguous area to the file
3 /-----------------------------------------------------------------------/
4 / This function checks if the file is contiguous with desired size.
5 / If not, a block of contiguous sectors is allocated to the file.
6 / If the file has been opened without FA_WRITE flag, it only checks if
7 / the file is contiguous and returns the resulut. */
9 #if _FATFS != 80367 /* Check if R0.10c */
10 #error This function may not be compatible with this revision of FatFs module.
13 /* Declarations of FatFs internal functions accessible from applications.
14 / This is intended to be used for disk checking/fixing or dirty hacks :-) */
15 DWORD
clust2sect (FATFS
* fs
, DWORD clst
);
16 DWORD
get_fat (FATFS
* fs
, DWORD clst
);
17 FRESULT
put_fat (FATFS
* fs
, DWORD clst
, DWORD val
);
20 DWORD
allocate_contiguous_clusters ( /* Returns the first sector in LBA (0:error or not contiguous) */
21 FIL
* fp
, /* Pointer to the open file object */
22 DWORD len
/* Number of bytes to allocate */
25 DWORD csz
, tcl
, ncl
, ccl
, cl
;
28 if (f_lseek(fp
, 0) || !len
) /* Check if the given parameters are valid */
30 csz
= 512UL * fp
->fs
->csize
; /* Cluster size in unit of byte (assuming 512 bytes/sector) */
31 tcl
= (len
+ csz
- 1) / csz
; /* Total number of clusters required */
32 len
= tcl
* csz
; /* Round-up file size to the cluster boundary */
34 /* Check if the existing cluster chain is contiguous */
35 if (len
== fp
->fsize
) {
36 ncl
= 0; ccl
= fp
->sclust
;
38 cl
= get_fat(fp
->fs
, ccl
); /* Get the cluster status */
39 if (cl
+ 1 < 3) return 0; /* Hard error? */
40 if (cl
!= ccl
+ 1 &&; cl
< fp
->fs
->n_fatent
) break; /* Not contiguous? */
42 } while (++ncl
< tcl
);
43 if (ncl
== tcl
) /* Is the file contiguous? */
44 return clust2sect(fp
->fs
, fp
->sclust
); /* Return file start sector */
49 if (f_truncate(fp
)) return 0; /* Remove the existing chain */
51 /* Find a free contiguous area */
52 ccl
= cl
= 2; ncl
= 0;
54 if (cl
>= fp
->fs
->n_fatent
) return 0; /* No contiguous area is found. */
55 if (get_fat(fp
->fs
, cl
)) { /* Encounterd a cluster in use */
56 do { /* Skip the block of used clusters */
58 if (cl
>= fp
->fs
->n_fatent
) return 0; /* No contiguous area is found. */
59 } while (get_fat(fp
->fs
, cl
));
65 /* Create a contiguous cluster chain */
66 fp
->fs
->last_clust
= ccl
- 1;
67 if (f_lseek(fp
, len
)) return 0;
69 return clust2sect(fp
->fs
, fp
->sclust
); /* Return file start sector */
83 /* Open or create a file */
85 fr
= f_open(&fil
, "swapfile.sys", FA_READ
| FA_WRITE
| FA_OPEN_ALWAYS
);
88 /* Check if the file is 64MB in size and occupies a contiguous area.
89 / If not, a contiguous area will be re-allocated to the file. */
90 org
= allocate_contiguous_clusters(&fil
, 0x4000000);
92 printf("Function failed due to any error or insufficient contiguous area.\n");
97 /* Now you can read/write the file with disk functions bypassing the file system layer. */
99 dr
= disk_write(fil
.fs
->drv
, Buff
, org
, 1024); /* Write 512KiB from top of the file */