From 7e4e14dde5fbfade46311fbf75386d5371062d7d Mon Sep 17 00:00:00 2001
From: gcontini <1121667+gcontini@users.noreply.github.com>
Date: 周六, 24 10月 2020 19:59:33 +0800
Subject: [PATCH] review of disk strategy linux

---
 src/library/os/windows/os_win.cpp |   61 +++++++++++-------------------
 1 files changed, 23 insertions(+), 38 deletions(-)

diff --git a/src/library/os/windows/os_win.cpp b/src/library/os/windows/os_win.cpp
index b6094da..cfa6b0d 100644
--- a/src/library/os/windows/os_win.cpp
+++ b/src/library/os/windows/os_win.cpp
@@ -21,77 +21,63 @@
 	return result;
 }
 
-//http://www.ok-soft-gmbh.com/ForStackOverflow/EnumMassStorage.c
-//http://stackoverflow.com/questions/3098696/same-code-returns-diffrent-result-on-windows7-32-bit-system
+// http://www.ok-soft-gmbh.com/ForStackOverflow/EnumMassStorage.c
+// http://stackoverflow.com/questions/3098696/same-code-returns-diffrent-result-on-windows7-32-bit-system
 #define MAX_UNITS 30
-//bug check return with diskinfos == null func_ret_ok
-FUNCTION_RETURN getDiskInfos(DiskInfo * diskInfos, size_t * disk_info_size) {
+// bug check return with diskinfos == null func_ret_ok
+FUNCTION_RETURN getDiskInfos(std::vector<DiskInfo>& diskInfos) {
 	DWORD fileMaxLen;
 	size_t ndrives = 0;
 	DWORD fileFlags;
 	char volName[MAX_PATH], fileSysName[MAX_PATH];
 	DWORD volSerial = 0;
 	const DWORD dwSize = MAX_PATH;
-	char szLogicalDrives[MAX_PATH] = { 0 };
-
+	char szLogicalDrives[MAX_PATH] = {0};
 
 	FUNCTION_RETURN return_value = FUNC_RET_NOT_AVAIL;
 	const DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);
 
-	if (dwResult > 0 && dwResult <= MAX_PATH) {
+	if (dwResult > 0) {
 		return_value = FUNC_RET_OK;
 		char* szSingleDrive = szLogicalDrives;
 		while (*szSingleDrive && ndrives < MAX_UNITS) {
-
 			// get the next drive
 			UINT driveType = GetDriveType(szSingleDrive);
 			if (driveType == DRIVE_FIXED) {
-				BOOL success = GetVolumeInformation(szSingleDrive, volName, MAX_PATH,
-				                                    &volSerial, &fileMaxLen, &fileFlags, fileSysName,
-				                                    MAX_PATH);
+				BOOL success = GetVolumeInformation(szSingleDrive, volName, MAX_PATH, &volSerial, &fileMaxLen,
+													&fileFlags, fileSysName, MAX_PATH);
 				if (success) {
 					LOG_INFO("drive         : %s", szSingleDrive);
 					LOG_INFO("Volume Name   : %s", volName);
 					LOG_INFO("Volume Serial : 0x%x", volSerial);
 					LOG_DEBUG("Max file length : %d", fileMaxLen);
 					LOG_DEBUG("Filesystem      : %s", fileSysName);
-					if (diskInfos != NULL) {
-						if (ndrives < (int)*disk_info_size) {
-							diskInfos[ndrives].id = (int)ndrives;
-							strncpy(diskInfos[ndrives].device, volName,
-									min(std::size_t{MAX_PATH}, sizeof(volName)) - 1);
-							strncpy(diskInfos[ndrives].label, fileSysName,
-									min(sizeof(diskInfos[ndrives].label), sizeof(fileSysName)) - 1);
-							memcpy(diskInfos[ndrives].disk_sn, &volSerial, sizeof(DWORD));
-							diskInfos[ndrives].preferred = (szSingleDrive[0] == 'C');
-						} else {
-							return_value = FUNC_RET_BUFFER_TOO_SMALL;
-						}
-					}
+					DiskInfo diskInfo;
+					diskInfo.id = (int)ndrives;
+					strncpy(diskInfo.device, volName, min(std::size_t{MAX_PATH}, sizeof(volName)) - 1);
+					strncpy(diskInfo.label, fileSysName,
+							min(sizeof(diskInfos[ndrives].label), sizeof(fileSysName)) - 1);
+					memcpy(diskInfo.disk_sn, &volSerial, sizeof(DWORD));
+					diskInfo.preferred = (szSingleDrive[0] == 'C');
+					diskInfos.push_back(diskInfo);
 					ndrives++;
 				} else {
-					LOG_WARN("Unable to retrieve information of '%s'", szSingleDrive);
+					LOG_DEBUG("Unable to retrieve information of '%s'", szSingleDrive);
 				}
 			} else {
-				LOG_INFO("This volume is not fixed : %s, type: %d",	szSingleDrive);
+				LOG_DEBUG("This volume is not fixed : %s, type: %d", szSingleDrive);
 			}
-			szSingleDrive += strlen(szSingleDrive) + 1;
 		}
 	}
-	if (diskInfos == NULL || *disk_info_size == 0) {
-		if (ndrives > 0) {
-			return_value = FUNC_RET_OK;
-		} else {
-			return_value = FUNC_RET_NOT_AVAIL;
-			LOG_INFO("No fixed drive was detected");
-		}
-		*disk_info_size = ndrives;
+	if (diskInfos.size() > 0) {
+		return_value = FUNC_RET_OK;
 	} else {
-		*disk_info_size = min(ndrives, *disk_info_size);
+		return_value = FUNC_RET_NOT_AVAIL;
+		LOG_INFO("No fixed drive were detected");
 	}
+
 	return return_value;
 }
-
 
 FUNCTION_RETURN getModuleName(char buffer[MAX_PATH]) {
 	FUNCTION_RETURN result = FUNC_RET_OK;
@@ -101,4 +87,3 @@
 	}
 	return result;
 }
-

--
Gitblit v1.9.1