MCPcopy
hub / github.com/dqzboy/Docker-Proxy / getDiskInfo

Function getDiskInfo

hubcmdui/server-utils.js:211–290  ·  view source on GitHub ↗

* 根据平台获取磁盘信息 * @param {string} platform - 操作系统平台 * @returns {Promise } 磁盘信息

(platform)

Source from the content-addressed store, hash-verified

209 * @returns {Promise<object>} 磁盘信息
210 */
211async function getDiskInfo(platform) {
212 if (platform === 'linux' || platform === 'darwin') {
213 try {
214 // Linux/macOS使用df命令
215 const diskCommand = platform === 'linux'
216 ? 'df -h / | tail -1'
217 : 'df -h / | tail -1';
218
219 const diskData = await execCommand(diskCommand);
220 const parts = diskData.trim().split(/\s+/);
221
222 if (parts.length >= 5) {
223 return {
224 filesystem: parts[0],
225 size: parts[1],
226 used: parts[2],
227 available: parts[3],
228 percent: parts[4]
229 };
230 } else {
231 throw new Error('磁盘信息格式不正确');
232 }
233 } catch (error) {
234 throw error;
235 }
236 } else if (platform === 'win32') {
237 // Windows平台
238 try {
239 // 使用wmic获取C盘信息
240 const diskData = await execCommand('wmic logicaldisk where DeviceID="C:" get Size,FreeSpace /value');
241 const lines = diskData.split(/\r\n|\n/);
242 let freeSpace, totalSize;
243
244 lines.forEach(line => {
245 if (line.startsWith('FreeSpace=')) {
246 freeSpace = parseInt(line.split('=')[1]);
247 } else if (line.startsWith('Size=')) {
248 totalSize = parseInt(line.split('=')[1]);
249 }
250 });
251
252 if (freeSpace !== undefined && totalSize !== undefined) {
253 const usedSpace = totalSize - freeSpace;
254 const usedPercent = Math.round((usedSpace / totalSize) * 100);
255
256 return {
257 filesystem: 'C:',
258 size: formatBytes(totalSize),
259 used: formatBytes(usedSpace),
260 available: formatBytes(freeSpace),
261 percent: `${usedPercent}%`
262 };
263 } else {
264 throw new Error('无法解析Windows磁盘信息');
265 }
266 } catch (error) {
267 throw error;
268 }

Callers 1

getSystemInfoFunction · 0.85

Calls 2

execCommandFunction · 0.85
formatBytesFunction · 0.70

Tested by

no test coverage detected