zoukankan      html  css  js  c++  java
  • 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 }
    View Code

    函数功能:创建一个目录

    描述:

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

    函数功能:移除一个对象

    描述:

    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
    View Code
  • 相关阅读:
    看某视频开始做LINUX笔记的第一天
    shell作业01
    学习shell的第一天
    学python的第三天
    【安全】573- 大前端网络安全精简指南手册
    【JS】572- JS 经典实例收集整理
    【Vuejs】571- Vue 虚拟DOM和Diff算法源码解析
    【JS】570- 揭开 JavaScript 引擎的面纱
    【JS】569- 如何避免这4类 JavaScript 内存泄漏?
    【拓展】如何在Gihub上面精准搜索开源项目?
  • 原文地址:https://www.cnblogs.com/Danhuise/p/3911883.html
Copyright © 2011-2022 走看看