VOS_UINT32 diag_FsGetDirInfo(VOS_CHAR *pDirName, VOS_UINT32 *pulTotalNum) { DIAG_DIRENT_STRU *pstTmpDirent = NULL; VOS_UINT32 ulTotalNum = 0; VOS_INT32 dir_handle =0; /*打开目录*/ /*lint -save -e628 -e64*/ dir_handle = (VOS_INT32)DIAG_FS_OPENDIR((VOS_CHAR*)pDirName,DIAG_FS_RDONLY|DIAG_FS_DIRECTORY,0); /* [false alarm]:屏蔽Fortify */ if(dir_handle < 0) { return VOS_ERR; } do { /*lint -save -e740*/ pstTmpDirent = (DIAG_DIRENT_STRU *)DIAG_FS_READDIR((VOS_VOID*)dir_handle); /*lint -restore*/ /*readdir的返回值为空,表明目录浏览完毕*/ if (VOS_NULL_PTR != pstTmpDirent) { ulTotalNum++; } }while (VOS_NULL_PTR != pstTmpDirent); *pulTotalNum = ulTotalNum;/* [false alarm]: 屏蔽Fortify 错误 */ /*关闭目录*/ if (DIAG_FS_ERROR == DIAG_FS_CLOSEDIR((VOS_VOID*)dir_handle)) { return VOS_ERR; } /*lint -restore*/ return VOS_OK; }
VOS_UINT32 diag_FsGetItemInfo(VOS_CHAR *pDirectory,DIAG_DIR_FILE_INFO_STRU *pstDirFileInfo) { /*lint -save -e958*/ DIAG_DIRENT_STRU *pstTmpDirent; DIAG_STAT_STRU stStat; VOS_UINT8 *pDirName; VOS_UINT16 usDirLen; VOS_UINT32 ultemp_len = 0; VOS_INT32 dir_handle =0; /*lint -restore*/ /*再次打开目录*/ dir_handle = (VOS_INT32)DIAG_FS_OPENDIR(pDirectory,DIAG_FS_RDONLY|DIAG_FS_DIRECTORY,0); /* [false alarm]:屏蔽Fortify */ if(dir_handle < 0) { return VOS_ERR; } /*分配空间用来存放文件或文件夹的路径,加1是因为后面需要添加斜杠*/ pDirName = (VOS_UINT8*)VOS_MemAlloc(diag_GetAgentPid(), DYNAMIC_MEM_PT,MSP_DF_DIR_MAX_LEN); if (VOS_NULL_PTR == pDirName) { DIAG_FS_CLOSEDIR(dir_handle); return VOS_ERR; } /*将目录路径拷贝进文件路径中*/ usDirLen = (VOS_UINT16)VOS_StrLen((VOS_CHAR*)pDirectory); VOS_MemCpy(pDirName, pDirectory, usDirLen); /*由于目录路径是不以斜杠结束,在与文件名结合时,需要添加斜杠*/ pDirName[usDirLen] = '/'; usDirLen++; do { /*遍历整个pstDir指向路径中的所有文件和文件夹*/ pstTmpDirent = (DIAG_DIRENT_STRU *)DIAG_FS_READDIR(dir_handle); /*readdir的返回值为空,表明目录浏览完毕*/ /*lint -save -e830*/ if (VOS_NULL_PTR != pstTmpDirent) /*lint -restore*/ { /*得到文件或文件夹名的长度*/ ultemp_len = (VOS_UINT16)VOS_StrLen(pstTmpDirent->d_name); /* [false alarm]:屏蔽Fortify */ /*由于文件或文件夹路径需要以'\0'作为结束,所以在Copy时,长度加1*/ VOS_MemCpy(pDirName + usDirLen,pstTmpDirent->d_name, ultemp_len + 1); /*通过stat获取文件或文件夹的信息*/ /*lint -save -e628*/ if (DIAG_FS_ERROR == DIAG_FS_STAT((VOS_CHAR*)pDirName, &stStat))/*lint !e628*/ { /*lint -restore*/ pstDirFileInfo->ulItemType = DIAG_FS_ITEM_FILE; pstDirFileInfo->st_size = 0; pstDirFileInfo->st_mode = 0; pstDirFileInfo->st_atime = 0; pstDirFileInfo->st_mtime = 0; pstDirFileInfo->st_ctime = 0; } else { /*目录*/ if (0 != (DIAG_IF_DIR&stStat.st_mode)) { pstDirFileInfo->ulItemType = DIAG_FS_ITEM_FOLDER; } /*文件*/ else { pstDirFileInfo->ulItemType = DIAG_FS_ITEM_FILE; } pstDirFileInfo->st_size = (stStat.st_size & 0xFFFFFFFF);/* 目前文件大小不会超过32位大小 */ pstDirFileInfo->st_atime = (VOS_INT32)(stStat.st_atime); pstDirFileInfo->st_mtime = (VOS_INT32)(stStat.st_mtime); pstDirFileInfo->st_ctime = (VOS_INT32)(stStat.st_ctime); pstDirFileInfo->st_mode = stStat.st_mode; } /*文件或文件夹名*/ VOS_MemCpy(pstDirFileInfo->aucDirName, pstTmpDirent->d_name, ultemp_len +1); /* coverity[suspicious_pointer_arithmetic] */ pstDirFileInfo = pstDirFileInfo + sizeof(DIAG_DIR_FILE_INFO_STRU ); } }while(VOS_NULL_PTR != pstTmpDirent); VOS_MemFree(diag_GetAgentPid(), pDirName); DIAG_FS_CLOSEDIR(dir_handle); return VOS_OK; }