浏览代码

#IQRV #comment [BugFix][Module] 资源实体文件浏览时fileSize存储文件数量

gifur 4 年之前
父节点
当前提交
ba1cc73db6
共有 2 个文件被更改,包括 137 次插入11 次删除
  1. 124 8
      Module/mod_ResourceWatcher/FileSimpleComp.h
  2. 13 3
      Module/mod_ResourceWatcher/ResourceWatcherFSM.cpp

+ 124 - 8
Module/mod_ResourceWatcher/FileSimpleComp.h

@@ -189,9 +189,118 @@ typedef struct FILE_LAYOUT_ENTRY {
 } FILE_LAYOUT_ENTRY, *PFILE_LAYOUT_ENTRY;
 
 //Premise: Should be initialize entry->current first.
-static DWORD GetSubFileInfors(PFILE_LAYOUT_ENTRY Entry, DWORD& dwDirCount, DWORD& dwFileCount, const DWORD dwFilterMask = 0);
+static ULONGLONG GetSubFileInfors(PFILE_LAYOUT_ENTRY Entry, DWORD& dwDirCount, DWORD& dwFileCount, const DWORD dwFilterMask = 0);
+static unsigned long long GetDirectorySize(LPCTSTR dirPath, int nDepth = -1024);
+inline static DWORD GetDirectoryItemCount(LPCTSTR dirPath, int nDepth = -1024);
 static void DisplayFileEntities(const PFILE_LAYOUT_ENTRY Entry);
 
+DWORD GetDirectoryItemCount(LPCTSTR dirPath, int nDepth)
+{
+#if defined(RVC_OS_LINUX)
+    DIR* d;
+    struct dirent* entry;
+    struct stat statbuf;
+	DWORD result = 0;
+
+    if ((d = opendir(dirPath)) == NULL) {
+        Dbg("opendir %s failed: %s", dirPath, strerror(errno));
+        return 0;
+    }
+
+    struct dirent* dp = NULL;
+    while ((dp = readdir(d)) != NULL) {
+        if ((!strcmp(dp->d_name, ".")) || (!strcmp(dp->d_name, ".."))) {
+            continue;
+        }
+
+		result++;
+		continue;
+
+        char tempFilePath[MAX_PATH * 2] = { 0 };
+        strcpy(tempFilePath, dirPath);
+        if (strlen(tempFilePath) + strlen(dp->d_name) + 3 >= MAX_PATH * 2) {
+            Dbg("filePath is too long for current.");
+            continue;
+        }
+        strcat(tempFilePath, "/");
+        strcat(tempFilePath, dp->d_name);
+        struct stat fileStat;
+        if (stat(tempFilePath, &fileStat) < 0) {
+            Dbg("stat dir for %s failed: %d ", tempFilePath, errno);
+            continue;
+        }
+
+        if (S_ISDIR(fileStat.st_mode)) {
+            if (nDepth + 1 >= 0) {
+                Dbg("exceed the deepest dir, do not recursive at all: %s", tempFilePath);
+            } else {
+                result += GetDirectoryItemCount(tempFilePath, nDepth + 1);
+            }
+        }
+    }
+    closedir(d);
+
+    return result;
+
+#else
+    return 0;
+#endif //RVC_OS_LINUX
+}
+
+unsigned long long GetDirectorySize(LPCTSTR dirPath, int nDepth)
+{
+#if defined(RVC_OS_LINUX)
+	DIR * d;
+	struct dirent* entry;
+	struct stat statbuf;
+	unsigned long long result = 0;
+
+	if ((d = opendir(dirPath)) == NULL) {
+		Dbg("opendir %s failed: %s", dirPath, strerror(errno));
+		return 0;
+	}
+
+	lstat(dirPath, &statbuf);
+	result += statbuf.st_size;
+
+    struct dirent* dp = NULL;
+    while ((dp = readdir(d)) != NULL) {
+        if ((!strcmp(dp->d_name, ".")) || (!strcmp(dp->d_name, ".."))) {
+            continue;
+        }
+        char tempFilePath[MAX_PATH * 2] = { 0 };
+        strcpy(tempFilePath, dirPath);
+        if (strlen(tempFilePath) + strlen(dp->d_name) + 3 >= MAX_PATH * 2) {
+            Dbg("filePath is too long for current.");
+            continue;
+        }
+        strcat(tempFilePath, "/");
+        strcat(tempFilePath, dp->d_name);
+		struct stat fileStat;
+        if (stat(tempFilePath, &fileStat) < 0) {
+            Dbg("stat dir for %s failed: %d ", tempFilePath, errno);
+            continue;
+        }
+
+		if (S_ISDIR(fileStat.st_mode)) {
+			if (nDepth + 1 >= 0) {
+				Dbg("exceed the deepest dir, do not recursive at all: %s", tempFilePath);
+			} else {
+				result += GetDirectorySize(tempFilePath, nDepth + 1);
+			}
+		} else {
+			result += fileStat.st_size;
+		}
+    }
+	closedir(d);
+
+	return result;
+
+#else
+	return 0;
+#endif //RVC_OS_LINUX
+}
+
 static UINT GetErrorMessage(CSimpleStringA& retMessage, LPCTSTR lpDefault, DWORD error = GetLastError()) 
 { 
 #if defined(_MSC_VER)
@@ -587,8 +696,9 @@ static BOOL TraceExtend(PFILE_LAYOUT_ENTRY Entry, int UpLevel)
 	}
 	return bRet;
 }
+
 /*返回该目录下的文件数量(文件+文件夹)*/
-DWORD GetSubFileInfors(PFILE_LAYOUT_ENTRY Entry, DWORD& dwDirCount, DWORD& dwFileCount, const DWORD dwFilterMask)
+ULONGLONG GetSubFileInfors(PFILE_LAYOUT_ENTRY Entry, DWORD& dwDirCount, DWORD& dwFileCount, const DWORD dwFilterMask)
 {
 	dwDirCount = dwFileCount = 0;
 	if(Entry == NULL) {
@@ -665,11 +775,13 @@ DWORD GetSubFileInfors(PFILE_LAYOUT_ENTRY Entry, DWORD& dwDirCount, DWORD& dwFil
     CSimpleStringA csPath(pszParentPah);
     struct stat fileStat;
     if (stat(csPath, &fileStat) < 0 || !S_ISDIR(fileStat.st_mode)) {
+		Dbg("stat for %s failed or is not dir", csPath.GetData());
 		delete[] pszParentPah;
         return 0;
     }
     DIR* d = opendir(csPath);
     if (!d) {
+		Dbg("opendir %s failed: %s", (LPCTSTR)csPath, strerror(errno));
 		delete[] pszParentPah;
         return 0;
     }
@@ -679,9 +791,9 @@ DWORD GetSubFileInfors(PFILE_LAYOUT_ENTRY Entry, DWORD& dwDirCount, DWORD& dwFil
         if ((!strcmp(dp->d_name, ".")) || (!strcmp(dp->d_name, ".."))) {
             continue;
         }
-		char tempFilePath[MAX_PATH] = { 0 };
+		char tempFilePath[MAX_PATH*2] = { 0 };
         strcpy(tempFilePath, pszParentPah);
-        if (strlen(tempFilePath) + strlen(dp->d_name) + 3 >= MAX_PATH) {
+        if (strlen(tempFilePath) + strlen(dp->d_name) + 3 >= MAX_PATH * 2) {
             Dbg("filePath is too long for current.");
             continue;
         }
@@ -690,8 +802,12 @@ DWORD GetSubFileInfors(PFILE_LAYOUT_ENTRY Entry, DWORD& dwDirCount, DWORD& dwFil
         stat(tempFilePath, &fileStat);
 		CSimpleFileComponent folder;
 		folder.mAttributes = 0;
-        if (S_ISDIR(fileStat.st_mode))
+		if (S_ISDIR(fileStat.st_mode)) {
 			folder.mAttributes |= FILE_ATTRIBUTE_DIRECTORY;
+			folder.mFileSize = GetDirectoryItemCount(tempFilePath); //GetDirectorySize(tempFilePath);
+		} else {
+			folder.mFileSize = fileStat.st_size;
+		}
         if (folder.mAttributes == 0)
 			folder.mAttributes = FILE_ATTRIBUTE_ARCHIVE;
         if (!(fileStat.st_mode & S_IWUSR))
@@ -699,14 +815,14 @@ DWORD GetSubFileInfors(PFILE_LAYOUT_ENTRY Entry, DWORD& dwDirCount, DWORD& dwFil
 		folder.mftAccess = fileStat.st_atime;
         folder.mftCreate = fileStat.st_ctime;
         folder.mftModified = fileStat.st_mtime;
-		folder.mFileSize = fileStat.st_size;
+		
         const DWORD dwNameLen = strlen(dp->d_name);
         Append(Entry->FileNamesBuffer, (PBYTE)dp->d_name, dwNameLen, folder.mNameOffset);
         folder.mNameLength = dwNameLen;
         Entry->SubFiles.Append(&folder, 0, 1);
 
-        if (folder.mAttributes & FILE_ATTRIBUTE_DIRECTORY) dwDirCount++;
-        else dwFileCount++;
+		if (folder.mAttributes & FILE_ATTRIBUTE_DIRECTORY) { dwDirCount++; }
+		else dwFileCount++;
     }
     closedir(d);
 #endif //_MSC_VER

+ 13 - 3
Module/mod_ResourceWatcher/ResourceWatcherFSM.cpp

@@ -1979,13 +1979,18 @@ BOOL ResourceWatcherFSM::InitialDirectoryEntity(LPCTSTR lpszPath, DWORD& dwFileC
 	m_FileEntry.Current.mftModified = st.st_mtime;
 	m_FileEntry.Current.mAttributes = 0;
 	//GetFileAttributesA
-    if (S_ISDIR(st.st_mode))
-		m_FileEntry.Current.mAttributes |= FILE_ATTRIBUTE_DIRECTORY;
+    if (S_ISDIR(st.st_mode)) {
+        m_FileEntry.Current.mFileSize = 0;
+        m_FileEntry.Current.mAttributes |= FILE_ATTRIBUTE_DIRECTORY;
+    } else {
+        m_FileEntry.Current.mFileSize = st.st_size;
+    }
+		
     if (m_FileEntry.Current.mAttributes == 0)
 		m_FileEntry.Current.mAttributes = FILE_ATTRIBUTE_ARCHIVE;
     if (!(st.st_mode & S_IWUSR))
 		m_FileEntry.Current.mAttributes |= FILE_ATTRIBUTE_READONLY;
-    m_FileEntry.Current.mFileSize = st.st_size;
+    
 #endif //_MSC_VER
 	
 	DWORD dwDir = 0, dwFile = 0;
@@ -1993,6 +1998,11 @@ BOOL ResourceWatcherFSM::InitialDirectoryEntity(LPCTSTR lpszPath, DWORD& dwFileC
 	Dbg("Flag: dwDir:%u, dwFile: %u", dwDir, dwFile);
 	DisplayFileEntities(&m_FileEntry);
 	dwFileCnt = dwDir + dwFile;
+
+#if defined(RVC_OS_LINUX)
+    m_FileEntry.Current.mFileSize = dwFileCnt;
+#endif //RVC_OS_LINUX
+
 	return TRUE;
 }