FATFS 初学之 f_mkdir/ unlink

f_mkdir:

 1 /*-----------------------------------------------------------------------*/
 2 /* Create a Directory                                                    */
 3 /*-----------------------------------------------------------------------*/
 4
 5 FRESULT f_mkdir (
 6     const TCHAR *path        /* Pointer to the directory path */
 7 )
 8 {
 9     FRESULT res;
10     DIR dj;
11     BYTE *dir, n;
12     DWORD dsc, dcl, pcl, tim = get_fattime();
13     DEF_NAMEBUF;
14
15
16     res = chk_mounted(&path, &dj.fs, 1);
17     if (res == FR_OK) {
18         INIT_BUF(dj);
19         res = follow_path(&dj, path);            /* Follow the file path */
20         if (res == FR_OK) res = FR_EXIST;        /* Any object with same name is already existing */
21         if (_FS_RPATH && res == FR_NO_FILE && (dj.fn[NS] & NS_DOT))
22             res = FR_INVALID_NAME;
23         if (res == FR_NO_FILE) {                /* Can create a new directory */
24             dcl = create_chain(dj.fs, 0);        /* Allocate a cluster for the new directory table */
25             res = FR_OK;
26             if (dcl == 0) res = FR_DENIED;        /* No space to allocate a new cluster */
27             if (dcl == 1) res = FR_INT_ERR;
28             if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;
29             if (res == FR_OK)                    /* Flush FAT */
30                 res = move_window(dj.fs, 0);
31             if (res == FR_OK) {                    /* Initialize the new directory table */
32                 dsc = clust2sect(dj.fs, dcl);
33                 dir = dj.fs->win;
34                 mem_set(dir, 0, SS(dj.fs));
35                 mem_set(dir+DIR_Name, ‘ ‘, 8+3);    /* Create "." entry */
36                 dir[DIR_Name] = ‘.‘;
37                 dir[DIR_Attr] = AM_DIR;
38                 ST_DWORD(dir+DIR_WrtTime, tim);
39                 ST_CLUST(dir, dcl);
40                 mem_cpy(dir+SZ_DIR, dir, SZ_DIR);     /* Create ".." entry */
41                 dir[33] = ‘.‘; pcl = dj.sclust;
42                 if (dj.fs->fs_type == FS_FAT32 && pcl == dj.fs->dirbase)
43                     pcl = 0;
44                 ST_CLUST(dir+SZ_DIR, pcl);
45                 for (n = dj.fs->csize; n; n--) {    /* Write dot entries and clear following sectors */
46                     dj.fs->winsect = dsc++;
47                     dj.fs->wflag = 1;
48                     res = move_window(dj.fs, 0);
49                     if (res != FR_OK) break;
50                     mem_set(dir, 0, SS(dj.fs));
51                 }
52             }
53             if (res == FR_OK) res = dir_register(&dj);    /* Register the object to the directoy */
54             if (res != FR_OK) {
55                 remove_chain(dj.fs, dcl);            /* Could not register, remove cluster chain */
56             } else {
57                 dir = dj.dir;
58                 dir[DIR_Attr] = AM_DIR;                /* Attribute */
59                 ST_DWORD(dir+DIR_WrtTime, tim);        /* Created time */
60                 ST_CLUST(dir, dcl);                    /* Table start cluster */
61                 dj.fs->wflag = 1;
62                 res = sync(dj.fs);
63             }
64         }
65         FREE_BUF();
66     }
67
68     LEAVE_FF(dj.fs, res);
69 }

函数功能:创建一个目录

描述:

f_mkdir函数当_FS_READONLY == 0并且_FS_MINIMIZE == 0时可用。
f_mkdir函数创建一个新目录。

f_unlink:

 1 /*-----------------------------------------------------------------------*/
 2 /* Delete a File or Directory                                            */
 3 /*-----------------------------------------------------------------------*/
 4
 5 FRESULT f_unlink (
 6     const TCHAR *path        /* Pointer to the file or directory path */
 7 )
 8 {
 9     FRESULT res;
10     DIR dj, sdj;
11     BYTE *dir;
12     DWORD dclst;
13     DEF_NAMEBUF;
14
15
16     res = chk_mounted(&path, &dj.fs, 1);
17     if (res == FR_OK) {
18         INIT_BUF(dj);
19         res = follow_path(&dj, path);        /* Follow the file path */
20         if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
21             res = FR_INVALID_NAME;            /* Cannot remove dot entry */
22 #if _FS_SHARE
23         if (res == FR_OK) res = chk_lock(&dj, 2);    /* Cannot remove open file */
24 #endif
25         if (res == FR_OK) {                    /* The object is accessible */
26             dir = dj.dir;
27             if (!dir) {
28                 res = FR_INVALID_NAME;        /* Cannot remove the start directory */
29             } else {
30                 if (dir[DIR_Attr] & AM_RDO)
31                     res = FR_DENIED;        /* Cannot remove R/O object */
32             }
33             dclst = LD_CLUST(dir);
34             if (res == FR_OK && (dir[DIR_Attr] & AM_DIR)) {    /* Is it a sub-dir? */
35                 if (dclst < 2) {
36                     res = FR_INT_ERR;
37                 } else {
38                     mem_cpy(&sdj, &dj, sizeof(DIR));    /* Check if the sub-dir is empty or not */
39                     sdj.sclust = dclst;
40                     res = dir_sdi(&sdj, 2);        /* Exclude dot entries */
41                     if (res == FR_OK) {
42                         res = dir_read(&sdj);
43                         if (res == FR_OK            /* Not empty dir */
44 #if _FS_RPATH
45                         || dclst == sdj.fs->cdir    /* Current dir */
46 #endif
47                         ) res = FR_DENIED;
48                         if (res == FR_NO_FILE) res = FR_OK;    /* Empty */
49                     }
50                 }
51             }
52             if (res == FR_OK) {
53                 res = dir_remove(&dj);        /* Remove the directory entry */
54                 if (res == FR_OK) {
55                     if (dclst)                /* Remove the cluster chain if exist */
56                         res = remove_chain(dj.fs, dclst);
57                     if (res == FR_OK) res = sync(dj.fs);
58                 }
59             }
60         }
61         FREE_BUF();
62     }
63     LEAVE_FF(dj.fs, res);
64 }

函数功能:移除一个对象

描述:

f_unlink函数当_FS_READONLY == 0并且_FS_MINIMIZE == 0时可用。
f_unlink函数移除一个对象。不要移除打开的对象或当前目录。

例:

1 res = f_mkdir("sub1");                // 在根目录下创建 sub1目录
2 if (res) die(res);
3 res = f_mkdir("sub1/sub2");            // 在 sub1目录下创建 sub2目录
4 if (res) die(res);
5 res = f_mkdir("sub1/sub2/sub3");
6 if (res) die(res);
7 // ...
8 f_unlink("SD.txt");   //删除跟目录下文件 SD.txt

FATFS 初学之 f_mkdir/ unlink

时间: 2024-10-09 16:03:51

FATFS 初学之 f_mkdir/ unlink的相关文章

FATFS 初学之 f_forward

1 /*-----------------------------------------------------------------------*/ 2 /* Forward data to the stream directly (available on only tiny cfg) */ 3 /*-----------------------------------------------------------------------*/ 4 5 FRESULT f_forward

FATFS 初学之 f_rename

1 /*-----------------------------------------------------------------------*/ 2 /* Rename File/Directory */ 3 /*-----------------------------------------------------------------------*/ 4 5 FRESULT f_rename ( 6 const TCHAR *path_old, /* Pointer to th

FATFS 初学之 f_mkfs

1 /*-----------------------------------------------------------------------*/ 2 /* Create File System on the Drive */ 3 /*-----------------------------------------------------------------------*/ 4 5 FRESULT f_mkfs ( 6 BYTE drv, /* Logical drive numb

FATFS 初学之 f_getfree

1 /*-----------------------------------------------------------------------*/ 2 /* Get Number of Free Clusters */ 3 /*-----------------------------------------------------------------------*/ 4 5 FRESULT f_getfree ( 6 const TCHAR *path, /* Pointer to

FATFS 初学之 f_chmod/ f_utime

f_chmod: 1 /*-----------------------------------------------------------------------*/ 2 /* Change Attribute */ 3 /*-----------------------------------------------------------------------*/ 4 5 FRESULT f_chmod ( 6 const TCHAR *path, /* Pointer to the

FATFS 初学之 f_open/ f_close

f_open: 1 /*-----------------------------------------------------------------------*/ 2 /* Open or Create a File */ 3 /*-----------------------------------------------------------------------*/ 4 5 FRESULT f_open ( 6 FIL *fp, /* Pointer to the blank

FATFS 初学之 f_chdir/ f_chdrive

1 FRESULT f_chdir ( 2 const TCHAR *path /* Pointer to the directory path */ 3 ) 4 { 5 FRESULT res; 6 DIR dj; 7 DEF_NAMEBUF; 8 9 10 res = chk_mounted(&path, &dj.fs, 0); 11 if (res == FR_OK) { 12 INIT_BUF(dj); 13 res = follow_path(&dj, path); /*

FATFS 初学之 f_opendir/ f_readdir

f_opendir: 1 /*-----------------------------------------------------------------------*/ 2 /* Create a Directroy Object */ 3 /*-----------------------------------------------------------------------*/ 4 5 FRESULT f_opendir ( 6 DIR *dj, /* Pointer to

FATFS 初学之 f_gets/ f_putc/ f_puts/ f_printf

详见:嵌入式大讲堂 f_gets: 1 /*-----------------------------------------------------------------------*/ 2 /* Get a string from the file */ 3 /*-----------------------------------------------------------------------*/ 4 TCHAR* f_gets ( 5 TCHAR* buff, /* Point