[C] 纯文本查看 复制代码
static UINT FileXDeleteDirectory(FX_MEDIA *media_ptr, CHAR *dir)
{
UINT status = fx_directory_delete(media_ptr, dir);
if (FX_NOT_FOUND == status || FX_SUCCESS == status)
{
return FX_SUCCESS; // 不存在也可以认为删除成功
}
FS_DBG_INFO("DEL DIR");
if (FX_DIR_NOT_EMPTY != status)
{
return status;
}
// 到这里说明文件夹存在并且非空
CHAR *defaultPath = (CHAR *)FS_MALLOC(FX_MAXIMUM_PATH);
CHAR *entry_name = (CHAR *)FS_MALLOC(FX_MAX_LONG_NAME_LEN);
if (defaultPath == NULL || entry_name == NULL)
{
return FX_NO_MORE_SPACE;
}
memset(defaultPath, 0x00, FX_MAXIMUM_PATH);
status = fx_directory_default_get_copy(media_ptr, defaultPath, FX_MAXIMUM_PATH);
if (status != FX_SUCCESS)
{
goto ERREXIT;
}
if (0 == strlen(defaultPath))
{
defaultPath[0] = '/';
defaultPath[1] = '\0';
}
status = fx_directory_default_set(media_ptr, dir);
if (status != FX_SUCCESS)
{
goto ERREXIT;
}
UINT attributes = 0;
ULONG size = 0;
UINT year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
bool firstFlag = true;
UINT(*findEntry)
(FX_MEDIA *, CHAR *, UINT *, ULONG *, UINT *, UINT *, UINT *, UINT *, UINT *, UINT *) = fx_directory_first_full_entry_find;
for (;;)
{
if (!firstFlag)
{
findEntry = fx_directory_next_full_entry_find;
}
firstFlag = false;
status = findEntry(media_ptr, entry_name, &attributes, &size,
&year, &month, &day, &hour, &minute, &second);
if (FX_SUCCESS != status || !entry_name[0])
{
break;
}
if ('.' == entry_name[0])
{
continue;
}
if (attributes & FX_DIRECTORY)
{
findEntry = fx_directory_first_full_entry_find;
status = FileXDeleteDirectory(media_ptr, entry_name);
fx_directory_default_set(media_ptr, defaultPath); // 还原要删除的路径为默认路径 不能用相对路径还原
if (FX_SUCCESS != status)
{
goto ERREXIT;
}
status = fx_directory_default_set(media_ptr, dir); // 还原要删除的路径为默认路径
if (status != FX_SUCCESS)
{
FS_DBG_ERR("def path:%s/%s err:%d", defaultPath, dir, status);
}
firstFlag = true; // 如果删除完了文件夹,需要重新从头扫描。。。
}
else
{
status = fx_file_delete(media_ptr, entry_name);
if (FX_SUCCESS != status)
{
FS_DBG_ERR("%s del err", entry_name);
fx_directory_default_set(media_ptr, defaultPath);
goto ERREXIT;
}
FS_DBG_INFO("file %s delete success", entry_name);
}
}
status = fx_directory_default_set(media_ptr, defaultPath); // 恢复默认设置,为了删除该文件夹
if (status != FX_SUCCESS)
{
FS_DBG_ERR("org def dir %s err:%d", defaultPath, status);
}
status = fx_directory_delete(media_ptr, dir);
if (FX_SUCCESS != status)
{
FS_DBG_ERR("%s del err:%d", dir, status);
}
else
{
FS_DBG_INFO("dir %s delete success", dir);
}
ERREXIT:
FS_FREE(defaultPath);
FS_FREE(entry_name);
return status;
}