Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env node
/*jshint node: true, esversion: 10 */
/**
* Hide container filesystems in ~/.local/share/user-places.xbel
*
* @url https://gist.github.com/duzun/e583e1634ed6f6765dbaba0ac846e5cc
* @author Dumitru Uzun
* @version 0.0.1
*/
const path = require('path');
const fs = require('fs');
const filename = path.join(process.env.HOME, '.local/share/user-places.xbel');
try {
const xml2js = require('xml2js');
let places = fs.readFileSync(filename, 'utf8');
xml2js.parseString(places, (err, places) => {
if (err) console.log(err);
let mountPaths = {};
let mounts = fs.readFileSync('/proc/mounts', 'utf8').trim().split('\n').map((ln) => {
ln = ln.split(' ');
let ret = {
dev: ln[0],
mount: ln[1],
type: ln[2],
options: ln[3],
// 4: ln[4],
// 5: ln[5],
};
// if(mountPaths[ret.mount]) {
// console.log('dup', ret, mountPaths[ret.mount]);
// }
mountPaths[ret.mount] = ret;
return ret;
});
let changed = 0;
const { separator } = places.xbel;
let p = separator.filter((s) => {
let href = s.$ && s.$.href;
if(href) {
href = href.replace(/^file:\/\/\//, '/');
}
else {
href = s?.info[0]?.metadata[0]?.UDI[0];
if(href) {
href = href.replace(/^\/org\/kde\/fstab\/overlay/, '');
href = href.replace(/^\/org\/freedesktop\/UDisks2\/block_devices/, '/dev');
}
}
if(!href) return true;
let isHidden = (s?.info[0]?.metadata[0]?.IsHidden+'') == 'true';
if(!mountPaths[href]) {
if(!isHidden) {
++changed;
return false;
}
console.log('missing', href, s);
}
if(isHidden) return true;
if(~href.indexOf(`/docker/overlay2/`) || ~href.indexOf(`/containerd/`)) {
++changed;
if(!s.info) s.info = [{}];
if(!s.info[0].metadata) s.info[0].metadata = [{}];
s.info[0].metadata[0].IsHidden = ['true'];
// return false;
}
return true;
});
if(changed) {
console.log(`Changed ${changed} devices (${p.length} / ${separator.length})`);
places.xbel.separator = p;
let builder = new xml2js.Builder();
let xml = builder.buildObject(places);
console.log(`Saving to "${filename}" ${xml.length} symbols`);
fs.writeFileSync(filename, xml);
process.exit(changed);
}
else {
console.log('No changes');
}
// console.log(JSON.stringify(p, null, 2));
});
}
catch(err) {
if(err.code != 'MODULE_NOT_FOUND') throw err;
if(!process.env.NODE_PATH && !process.env.PREFIX) {
const { spawn, execSync } = require('child_process');
// Make sure there is xml2js module installed globally
// execSync(`npm ls -g xml2js || npm i -g xml2js`);
let NODE_PATH = execSync(`npm config get prefix`);
NODE_PATH = path.join(NODE_PATH.toString('utf8').trim(), 'lib/node_modules');
spawn(process.argv[0], process.argv.slice(1), { stdio: 'inherit', env: Object.assign({}, process.env, {NODE_PATH}) });
// NODE_PATH=`npm config get prefix`/lib/node_modules process.argv.join(' ')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment