From 53425d8fe244bcecb2ae8d72eaf13d30626b79fb Mon Sep 17 00:00:00 2001 From: Franco Montenegro Date: Tue, 1 Mar 2022 15:38:53 -0300 Subject: [PATCH] Add disk space functions for windows and mac. --- electron/index.js | 23 ++++++++++------ ui/util/diskspace.js | 62 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 67 insertions(+), 18 deletions(-) diff --git a/electron/index.js b/electron/index.js index 4a2ee0182..c65cf3425 100644 --- a/electron/index.js +++ b/electron/index.js @@ -17,7 +17,7 @@ import startSandbox from './startSandbox'; import installDevtools from './installDevtools'; import fs from 'fs'; import path from 'path'; -import { diskSpaceLinux } from '../ui/util/diskspace'; +import { diskSpaceLinux, diskSpaceWindows, diskSpaceMac } from '../ui/util/diskspace'; const { download } = require('electron-dl'); const remote = require('@electron/remote/main'); @@ -299,14 +299,21 @@ app.on('before-quit', () => { ipcMain.on('get-disk-space', async (event) => { try { const { data_dir } = await Lbry.settings_get(); - // mac error for this is 'df /Users/username/Library/Application no such..' - // due to not escaping the ' ' in /`Application Support` - if (os.platform() === 'linux') { - const stdout = await diskSpaceLinux(data_dir); - const dfResult = stdout.split('\n')[1].split(/\s+/); - const total_and_available = { total: dfResult[1], free: dfResult[3]}; - rendererWindow.webContents.send('send-disk-space', { diskSpace: total_and_available }); + let diskSpace = undefined; + switch (os.platform()) { + case 'linux': + diskSpace = await diskSpaceLinux(data_dir); + break; + case 'darwin': + diskSpace = await diskSpaceMac(data_dir); + break; + case 'win32': + diskSpace = await diskSpaceWindows(data_dir); + break; + default: + throw new Error('unknown platform'); } + rendererWindow.webContents.send('send-disk-space', { diskSpace }); } catch (e) { rendererWindow.webContents.send('send-disk-space', { error: e.message || e }); console.log('Failed to start LbryFirst', e); diff --git a/ui/util/diskspace.js b/ui/util/diskspace.js index 787f72804..ed9508bea 100644 --- a/ui/util/diskspace.js +++ b/ui/util/diskspace.js @@ -4,20 +4,62 @@ export const diskSpaceLinux = (path) => { return new Promise((resolve, reject) => { exec(`df ${path}`, (error, stdout, stderr) => { if (error) { - console.log(`error: ${error.message}`); - return reject(); + return reject(error); } if (stderr) { - return reject(); + return reject(new Error(stderr)); } - resolve(stdout); + // Sample df command output: + // Filesystem 1K-blocks Used Available Use% Mounted on + // C:\ 185087700 120552556 64535144 66% /mnt/c + const dfResult = stdout.split('\n')[1].split(/\s+/); + resolve({ + total: dfResult[1], + free: dfResult[3], + }); }); }); }; -// to implement -// export diskSpaceWindows = (path) => { -// new Promise((resolve, reject) => { -// -// }); -// } +export const diskSpaceMac = (path) => { + // Escape spaces in path to prevent errors. + // Example: + // "/Users/username/Library/Application Support/LBRY" gets updated to + // "/Users/username/Library/Application\\ Support/LBRY" + const escapedPath = path.replace(/(\s+)/g, '\\$1'); + return diskSpaceLinux(escapedPath); +}; + +export const diskSpaceWindows = (path) => { + return new Promise((resolve, reject) => { + exec(`wmic logicaldisk get size,freespace,caption`, (error, stdout, stderr) => { + if (error) { + return reject(error); + } + if (stderr) { + return reject(new Error(stderr)); + } + + // Drive used in the path (ie, C:, D:, etc.) + const pathDrive = path.split(':')[0] + ':'; + + // Sample outout: + // Caption FreeSpace Size + // C: 66218471424 189529804800 + // D: + // E: 536829952 536854528 + const stdoutLines = stdout.split('\n'); + // Find the drive used in the path. + const driveLine = stdoutLines.find((line) => line.startsWith(pathDrive)); + // Parse the values in each column by filtering out the + // empty spaces. + // eslint-disable-next-line no-unused-vars + const [drive, freeSpace, totalSize] = driveLine.split(' ').filter((x) => x); + + resolve({ + total: totalSize, + free: freeSpace, + }); + }); + }); +};