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 }
View Code

相关文章: