-
-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Ref/Fix] Improving checkDiskSpace (#3440)
* Move the "Path" schema into its own file This is going to be used outside Legendary in the next commit * Remove the check-disk-space package Doing this within Heroic allows us to use already-established patterns (dynamic import, helper functions like `genericSpawnWrapper`, validation using Zod) * Remove getFirstExistingParentPath This was a little overcomplicated, and lead to issues on Windows (where you don't necessarily have an existing root folder) * Fixup `isWritable` - As Flavio mentioned, `access` doesn't seem to work on Windows, so I replaced that with some PowerShell commands that check the same thing - We have to call isWritable with the full path, as you can of course modify permissions on any folder, not just on a root directory/mount point Since this info is now always accurate, we might want to make the respective warning on the Frontend an error instead
- Loading branch information
Showing
20 changed files
with
220 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { z } from 'zod' | ||
import path from 'path' | ||
|
||
const Path = z | ||
.string() | ||
.refine((val) => path.parse(val).root, 'Path is not valid') | ||
.brand('Path') | ||
type Path = z.infer<typeof Path> | ||
|
||
export { Path } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { Path } from 'backend/schemas' | ||
import { isFlatpak } from 'backend/constants' | ||
|
||
interface DiskInfo { | ||
freeSpace: number | ||
totalSpace: number | ||
} | ||
|
||
async function getDiskInfo(path: Path): Promise<DiskInfo> { | ||
switch (process.platform) { | ||
case 'linux': | ||
case 'darwin': { | ||
const { getDiskInfo_unix } = await import('./unix') | ||
return getDiskInfo_unix(path) | ||
} | ||
case 'win32': { | ||
const { getDiskInfo_windows } = await import('./windows') | ||
return getDiskInfo_windows(path) | ||
} | ||
default: | ||
return { freeSpace: 0, totalSpace: 0 } | ||
} | ||
} | ||
|
||
async function isWritable(path: Path): Promise<boolean> { | ||
switch (process.platform) { | ||
case 'linux': | ||
case 'darwin': { | ||
const { isWritable_unix } = await import('./unix') | ||
return isWritable_unix(path) | ||
} | ||
case 'win32': { | ||
const { isWritable_windows } = await import('./windows') | ||
return isWritable_windows(path) | ||
} | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
const isAccessibleWithinFlatpakSandbox = (path: Path): boolean => | ||
!isFlatpak || !path.startsWith(process.env.XDG_RUNTIME_DIR || '/run/user/') | ||
|
||
export { getDiskInfo, isWritable, isAccessibleWithinFlatpakSandbox } | ||
export type { DiskInfo } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { genericSpawnWrapper } from '../os/processes' | ||
import { access } from 'fs/promises' | ||
|
||
import type { Path } from 'backend/schemas' | ||
import type { DiskInfo } from './index' | ||
|
||
async function getDiskInfo_unix(path: Path): Promise<DiskInfo> { | ||
const { stdout } = await genericSpawnWrapper('df', ['-P', '-k', path]) | ||
const lineSplit = stdout.split('\n')[1].split(/\s+/) | ||
const [, totalSpaceKiBStr, , freeSpaceKiBStr] = lineSplit | ||
return { | ||
totalSpace: Number(totalSpaceKiBStr ?? 0) * 1024, | ||
freeSpace: Number(freeSpaceKiBStr ?? 0) * 1024 | ||
} | ||
} | ||
|
||
async function isWritable_unix(path: Path): Promise<boolean> { | ||
return access(path).then( | ||
() => true, | ||
() => false | ||
) | ||
} | ||
|
||
export { getDiskInfo_unix, isWritable_unix } |
Oops, something went wrong.