]>
cloudbase.mooo.com Git - z180-stamp.git/blob - fatfs/documents/res/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.
8 /-----------------------------------------------------------------------/
9 / This function can work with FatFs R0.09 - R0.11a.
10 / It is incompatible with R0.12+. Use f_expand function instead.
11 /----------------------------------------------------------------------*/
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
); /* File is contiguous. Return the start sector */
47 /* File is not contiguous */
49 return 0; /* Exit if in read-only cfg. */
51 if (!(fp
->flag
& FA_WRITE
)) return 0; /* Exit if the file object is for read-only */
53 if (f_truncate(fp
)) return 0; /* Remove the non-contiguous chain */
55 /* Find a free contiguous area */
56 ccl
= cl
= 2; ncl
= 0;
58 if (cl
>= fp
->fs
->n_fatent
) return 0; /* No contiguous area is found. */
59 if (get_fat(fp
->fs
, cl
)) { /* Encounterd a cluster in use */
60 do { /* Skip the block of used clusters */
62 if (cl
>= fp
->fs
->n_fatent
) return 0; /* No contiguous area is found. */
63 } while (get_fat(fp
->fs
, cl
));
69 /* Create a contiguous cluster chain */
70 fp
->fs
->last_clust
= ccl
- 1;
71 if (f_lseek(fp
, len
)) return 0;
73 return clust2sect(fp
->fs
, fp
->sclust
); /* Return file start sector */
87 /* Open or create a file to write */
89 fr
= f_open(&fil
, "fastrec.log", FA_READ
| FA_WRITE
| FA_OPEN_ALWAYS
);
92 /* Check if the file is 256MB in size and occupies a contiguous area.
93 / If not, a contiguous area will be re-allocated to the file. */
94 org
= allocate_contiguous_clusters(&fil
, 0x10000000);
96 printf("Function failed due to any error or insufficient contiguous area.\n");
101 /* Now you can read/write the file without filesystem layer. */
103 dr
= disk_write(fil
.fs
->drv
, Buff
, org
, 1024); /* Write 512KiB from top of the file */