首页 > 代码库 > FATFS 初学之 f_rename

FATFS 初学之 f_rename

 1 /*-----------------------------------------------------------------------*/ 2 /* Rename File/Directory                                                 */ 3 /*-----------------------------------------------------------------------*/ 4  5 FRESULT f_rename ( 6     const TCHAR *path_old,    /* Pointer to the old name */ 7     const TCHAR *path_new    /* Pointer to the new name */ 8 ) 9 {10     FRESULT res;11     DIR djo, djn;12     BYTE buf[21], *dir;13     DWORD dw;14     DEF_NAMEBUF;15 16 17     res = chk_mounted(&path_old, &djo.fs, 1);18     if (res == FR_OK) {19         djn.fs = djo.fs;20         INIT_BUF(djo);21         res = follow_path(&djo, path_old);        /* Check old object */22         if (_FS_RPATH && res == FR_OK && (djo.fn[NS] & NS_DOT))23             res = FR_INVALID_NAME;24 #if _FS_SHARE25         if (res == FR_OK) res = chk_lock(&djo, 2);26 #endif27         if (res == FR_OK) {                        /* Old object is found */28             if (!djo.dir) {                        /* Is root dir? */29                 res = FR_NO_FILE;30             } else {31                 mem_cpy(buf, djo.dir+DIR_Attr, 21);        /* Save the object information except for name */32                 mem_cpy(&djn, &djo, sizeof(DIR));        /* Check new object */33                 res = follow_path(&djn, path_new);34                 if (res == FR_OK) res = FR_EXIST;        /* The new object name is already existing */35                 if (res == FR_NO_FILE) {                 /* Is it a valid path and no name collision? */36 /* Start critical section that any interruption or error can cause cross-link */37                     res = dir_register(&djn);            /* Register the new entry */38                     if (res == FR_OK) {39                         dir = djn.dir;                    /* Copy object information except for name */40                         mem_cpy(dir+13, buf+2, 19);41                         dir[DIR_Attr] = buf[0] | AM_ARC;42                         djo.fs->wflag = 1;43                         if (djo.sclust != djn.sclust && (dir[DIR_Attr] & AM_DIR)) {        /* Update .. entry in the directory if needed */44                             dw = clust2sect(djn.fs, LD_CLUST(dir));45                             if (!dw) {46                                 res = FR_INT_ERR;47                             } else {48                                 res = move_window(djn.fs, dw);49                                 dir = djn.fs->win+SZ_DIR;    /* .. entry */50                                 if (res == FR_OK && dir[1] == .) {51                                     dw = (djn.fs->fs_type == FS_FAT32 && djn.sclust == djn.fs->dirbase) ? 0 : djn.sclust;52                                     ST_CLUST(dir, dw);53                                     djn.fs->wflag = 1;54                                 }55                             }56                         }57                         if (res == FR_OK) {58                             res = dir_remove(&djo);        /* Remove old entry */59                             if (res == FR_OK)60                                 res = sync(djo.fs);61                         }62                     }63 /* End critical section */64                 }65             }66         }67         FREE_BUF();68     }69     LEAVE_FF(djo.fs, res);70 }
View Code

函数功能:重命名一个对象。

描述:

f_rename函数当_FS_READONLY == 0并且_FS_MINIMIZE == 0时可用。
f_rename函数重命名一个对象,并且也可以将对象移动到其他目录。逻辑驱动器号由原名决定,新名不能包含一个逻辑驱动器号。不要重命名打开的对象。

 

例:

1     /* 重命名一个对象 */2     f_rename("oldname.txt", "newname.txt");3  4     /* 重命名并且移动一个对象到另一个目录 */5     f_rename("oldname.txt", "dir1/newname.txt");
View Code