Skip to content

Commit

Permalink
Desktop: Fixes #9779: Linux: Fix bundled Simple Backup can't create 7…
Browse files Browse the repository at this point in the history
…zip archives (#9780)
  • Loading branch information
personalizedrefrigerator authored Jan 26, 2024
1 parent 7b06090 commit da4d57c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ packages/app-desktop/services/spellChecker/SpellCheckerServiceDriverNative.js
packages/app-desktop/tools/copy7Zip.js
packages/app-desktop/tools/notarizeMacApp.js
packages/app-desktop/tools/renameReleaseAssets.js
packages/app-desktop/utils/7zip/getPathToExecutable7Zip.js
packages/app-desktop/utils/7zip/pathToBundled7Zip.js
packages/app-desktop/utils/checkForUpdatesUtils.test.js
packages/app-desktop/utils/checkForUpdatesUtils.js
packages/app-desktop/utils/checkForUpdatesUtilsTestData.js
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ packages/app-desktop/services/spellChecker/SpellCheckerServiceDriverNative.js
packages/app-desktop/tools/copy7Zip.js
packages/app-desktop/tools/notarizeMacApp.js
packages/app-desktop/tools/renameReleaseAssets.js
packages/app-desktop/utils/7zip/getPathToExecutable7Zip.js
packages/app-desktop/utils/7zip/pathToBundled7Zip.js
packages/app-desktop/utils/checkForUpdatesUtils.test.js
packages/app-desktop/utils/checkForUpdatesUtils.js
packages/app-desktop/utils/checkForUpdatesUtilsTestData.js
Expand Down
7 changes: 6 additions & 1 deletion packages/app-desktop/services/plugins/PluginRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Setting from '@joplin/lib/models/Setting';
import { EventHandlers } from '@joplin/lib/services/plugins/utils/mapEventHandlersToIds';
import shim from '@joplin/lib/shim';
import Logger from '@joplin/utils/Logger';
import getPathToExecutable7Zip from '../../utils/7zip/getPathToExecutable7Zip';
// import BackOffHandler from './BackOffHandler';
const ipcRenderer = require('electron').ipcRenderer;

Expand Down Expand Up @@ -120,11 +121,15 @@ export default class PluginRunner extends BasePluginRunner {

bridge().electronApp().registerPluginWindow(plugin.id, pluginWindow);

const libraryData = {
pathTo7za: await getPathToExecutable7Zip(),
};

void pluginWindow.loadURL(`${require('url').format({
pathname: require('path').join(__dirname, 'plugin_index.html'),
protocol: 'file:',
slashes: true,
})}?pluginId=${encodeURIComponent(plugin.id)}&pluginScript=${encodeURIComponent(`file://${scriptPath}`)}`);
})}?pluginId=${encodeURIComponent(plugin.id)}&pluginScript=${encodeURIComponent(`file://${scriptPath}`)}&libraryData=${encodeURIComponent(JSON.stringify(libraryData))}`);

if (plugin.devMode) {
pluginWindow.webContents.once('dom-ready', () => {
Expand Down
17 changes: 2 additions & 15 deletions packages/app-desktop/services/plugins/plugin_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// TODO: Not sure if that will work once packaged in Electron
const sandboxProxy = require('../../vendor/lib/@joplin/lib/services/plugins/sandboxProxy.js');
const ipcRenderer = require('electron').ipcRenderer;
const nodePath = require('path');

const ipcRendererSend = (message, args) => {
try {
Expand All @@ -15,6 +14,7 @@

const urlParams = new URLSearchParams(window.location.search);
const pluginId = urlParams.get('pluginId');
const libraryData = JSON.parse(decodeURIComponent(urlParams.get('libraryData')));

let eventId_ = 1;
const eventHandlers_ = {};
Expand Down Expand Up @@ -63,20 +63,7 @@

// 7zip-bin is required by one of the default plugins (simple-backup)
if (modulePath === '7zip-bin') {
// 7zip-bin is very large -- return the path to a version of 7zip
// copied from 7zip-bin.
const executableName = process.platform === 'win32' ? '7za.exe' : '7za';

let rootDir = nodePath.dirname(nodePath.dirname(__dirname));

// When bundled, __dirname points to a file within app.asar. The build/ directory
// is outside of app.asar, and thus, we need an extra dirname(...).
if (nodePath.basename(rootDir).startsWith('app.asar')) {
rootDir = nodePath.dirname(rootDir);
}

const pathTo7za = nodePath.join(rootDir, 'build', '7zip', executableName);
return { path7za: nodePath.resolve(pathTo7za) };
return { path7za: libraryData.pathTo7za };
}

throw new Error(`Module not found: ${modulePath}`);
Expand Down
58 changes: 58 additions & 0 deletions packages/app-desktop/utils/7zip/getPathToExecutable7Zip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Setting from '@joplin/lib/models/Setting';
import pathToBundled7Zip from './pathToBundled7Zip';
import { join } from 'path';
import shim from '@joplin/lib/shim';

const pathTo7Za_: undefined|string = undefined;

const getPathToExecutable7Zip = async () => {
if (pathTo7Za_) {
return pathTo7Za_;
}

const { baseDir: bundled7ZipDir, executableName, fullPath: bundled7ZipExecutablePath } = pathToBundled7Zip();
let pathTo7Za = bundled7ZipExecutablePath;

// On Linux (and perhaps Free/OpenBSD?), the bundled 7zip binary can't be executed
// in its default location and must be moved.
if (!shim.isMac() && !shim.isWindows()) {
const targetDir = join(Setting.value('cacheDir'), '7zip');

const fsDriver = shim.fsDriver();
const executablePath = join(targetDir, executableName);

let needsUpdate;

// The 7Zip binary may already be copied, in which case, it may not need to be updated.
if (await shim.fsDriver().exists(targetDir)) {
const currentChecksum = await fsDriver.md5File(executablePath);
const bundledChecksum = await fsDriver.md5File(bundled7ZipExecutablePath);

if (currentChecksum !== bundledChecksum) {
needsUpdate = true;
await shim.fsDriver().remove(targetDir);
} else {
needsUpdate = false;
}
} else {
needsUpdate = true;
}

if (needsUpdate) {
await shim.fsDriver().mkdir(targetDir);
await shim.fsDriver().copy(bundled7ZipDir, targetDir);

// Make executable.
// Self: read+write+execute
// Group: read+execute
// Other: none
await shim.fsDriver().chmod(executablePath, 0o750);
}

pathTo7Za = executablePath;
}

return pathTo7Za;
};

export default getPathToExecutable7Zip;
21 changes: 21 additions & 0 deletions packages/app-desktop/utils/7zip/pathToBundled7Zip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { join, resolve, basename, dirname } from 'path';

const pathToBundled7Zip = () => {
// 7zip-bin is very large -- return the path to a version of 7zip
// copied from 7zip-bin.
const executableName = process.platform === 'win32' ? '7za.exe' : '7za';

let rootDir = dirname(dirname(__dirname));

// When bundled, __dirname points to a file within app.asar. The build/ directory
// is outside of app.asar, and thus, we need an extra dirname(...).
if (basename(rootDir).startsWith('app.asar')) {
rootDir = dirname(rootDir);
}

const baseDir = join(rootDir, 'build', '7zip');

return { baseDir, executableName, fullPath: resolve(join(baseDir, executableName)) };
};

export default pathToBundled7Zip;

0 comments on commit da4d57c

Please sign in to comment.