Skip to content

Commit

Permalink
Set udev rules using echo, fixes #1516
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoTheThird committed Dec 13, 2020
1 parent 542bbbd commit ae098c7
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 89 deletions.
81 changes: 0 additions & 81 deletions build/10-ubports.rules

This file was deleted.

104 changes: 97 additions & 7 deletions src/lib/udev.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,101 @@ const path = require("path");
const log = require("./log.js");
const { ipcMain } = require("electron");

const vendorIds = [
"03f0",
"03fc",
"0408",
"0409",
"0414",
"0451",
"0471",
"0482",
"0489",
"04b7",
"04c5",
"04da",
"04dd",
"04e8",
"0502",
"0531",
"054c",
"05c6",
"067e",
"091e",
"0930",
"0955",
"0b05",
"0bb4",
"0c2e",
"0db0",
"0e79",
"0e8d",
"0f1c",
"0fce",
"1004",
"109b",
"10a9",
"1219",
"12d1",
"1662",
"16d5",
"17ef",
"18d1",
"1949",
"19a5",
"19d2",
"1b8e",
"1bbb",
"1d09",
"1d45",
"1d4d",
"1d91",
"1e85",
"1ebf",
"1f3a",
"1f53",
"2006",
"201e",
"2080",
"2116",
"2207",
"2237",
"2257",
"22b8",
"22d9",
"2314",
"2340",
"2420",
"24e3",
"25e3",
"2717",
"271d",
"2836",
"2916",
"297f",
"29a9",
"29e4",
"2a45",
"2a47",
"2a49",
"2a70",
"2ae5",
"413c",
"8087",
"e040"
];

const rules = vendorIds
.map(id => `SUBSYSTEM=="usb", ATTR{idVendor}=="${id}", MODE="0666"`)
.join("\n");

const udevCommand = [
`echo '${rules}' > /etc/udev/rules.d/10-ubports.rules`,
"(udevadm control --reload-rules || true)",
"(udevadm trigger || true)",
"(service udev restart || true)"
].join(" && ");

/**
* manage udev rules
*/
Expand All @@ -31,15 +126,10 @@ class Udev {
*/
set() {
sudo.exec(
"cp " +
path.join(__dirname, "../build/10-ubports.rules") +
" /etc/udev/rules.d/ && " +
'(udevadm control --reload-rules || echo "") && ' +
'(udevadm trigger || echo "") && ' +
'(service udev restart || echo "")',
udevCommand,
{
name: "UBports Installer",
icns: path.join(__dirname, "../build/icons/icon.icns")
icns: path.join(__dirname, "../../build/icons/icon.icns")
},
error => {
if (error) log.warn(`setting udev rules failed: ${error}`);
Expand Down
25 changes: 24 additions & 1 deletion src/lib/udev.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@ process.argv = [null, null, "-vv"];
const { ipcMain } = require("electron");
jest.mock("electron");

const sudo = require("sudo-prompt");
const udev = require("./udev.js");

it("should be a singleton", () => {
expect(require("./udev.js")).toBe(require("./udev.js"));
expect(udev).toBe(require("./udev.js"));
});

it("should set rules", () => {
sudo.exec.mockImplementation((command, options, cb) => cb());
expect(udev.set()).toBe(undefined);
expect(sudo.exec).toHaveBeenCalledWith(
expect.any(String),
expect.any(Object),
expect.any(Function)
);
});

it("should fail silently", () => {
sudo.exec.mockImplementation((command, options, cb) => cb("problem"));
expect(udev.set()).toBe(undefined);
expect(sudo.exec).toHaveBeenCalledWith(
expect.any(String),
expect.any(Object),
expect.any(Function)
);
});

0 comments on commit ae098c7

Please sign in to comment.