Skip to content

Commit

Permalink
fix(updater): Fix fallback download location for roborock
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypfer committed Oct 21, 2021
1 parent d975fd7 commit d515f04
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
11 changes: 3 additions & 8 deletions backend/lib/Tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class Tools {
* Returns the total and free size in bytes
*
* @param {string} path
* @returns {{total: number, free: number} | null}
*/
static GET_DISK_SPACE_INFO(path) {
try {
Expand All @@ -179,21 +180,15 @@ class Tools {
});

if (dfOutput.length !== 1 || dfOutput[0].length !== 6) {
return {
total: 0,
free: 0
};
return null;
}

return {
total: parseInt(dfOutput[0][1], 10) * 1024,
free: parseInt(dfOutput[0][3], 10) * 1024,
};
} catch (e) {
return {
total: 0,
free: 0,
};
return null;
}
}

Expand Down
39 changes: 32 additions & 7 deletions backend/lib/updater/Updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,7 @@ class Updater {
* @return {string|null}
*/
getDownloadPath() {
let downloadLocation = os.tmpdir();
let space;
let downloadLocation;

try {
fs.accessSync(process.argv0, fs.constants.W_OK);
Expand All @@ -345,19 +344,45 @@ class Updater {
return null;
}

space = Tools.GET_DISK_SPACE_INFO(downloadLocation);
const tmpPath = os.tmpdir();
const spaceTmp = Tools.GET_DISK_SPACE_INFO(tmpPath);

if (space.free < Updater.SPACE_REQUIREMENTS) {
downloadLocation = "/dev/shm";
if (spaceTmp === null) {
this.state = new States.ValetudoUpdaterErrorState({
type: States.ValetudoUpdaterErrorState.ERROR_TYPE.NOT_ENOUGH_SPACE,
message: `Unable to determine the free space of ${tmpPath}.`
});

return null;
} else if (spaceTmp.free < Updater.SPACE_REQUIREMENTS) {
const shmPath = "/dev/shm";
const spaceShm = Tools.GET_DISK_SPACE_INFO(shmPath);

if (spaceShm === null) {
this.state = new States.ValetudoUpdaterErrorState({
type: States.ValetudoUpdaterErrorState.ERROR_TYPE.NOT_ENOUGH_SPACE,
message: `Unable to determine the free space of ${shmPath}.`
});

if (space.free < Updater.SPACE_REQUIREMENTS) {
return null;
} else if (spaceShm.free < Updater.SPACE_REQUIREMENTS) {
this.state = new States.ValetudoUpdaterErrorState({
type: States.ValetudoUpdaterErrorState.ERROR_TYPE.NOT_ENOUGH_SPACE,
message: `Updating is impossible because ${downloadLocation} only has ${space.free} bytes of free space. Required: ${Updater.SPACE_REQUIREMENTS} bytes.`
message: `
Updating is impossible because there's no download location with enough free space available.
Required: ${Updater.SPACE_REQUIREMENTS} bytes.
${tmpPath} only has ${spaceTmp.free} bytes of free space.
${shmPath} only has ${spaceShm.free} bytes of free space.
`
});

return null;
} else {
downloadLocation = shmPath;
}
} else {
downloadLocation = tmpPath;
}

try {
Expand Down

0 comments on commit d515f04

Please sign in to comment.