From 2deae8f3c2f287671e2d291e56d18b9b9bfb7bb0 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 20 Apr 2016 11:44:49 -0700 Subject: [PATCH 1/2] Move user data to ~/.vscode[-variant]/user-data Permanently move the user data to a sub-folder of the config directory for the current variant of code. For example: - ~/.config/Code -> ~/.vscode/user-data - ~/.config/Code\ -\ OSS -> ~/.vscode-oss/user-data - ~/.config/Code-Development -> ~/.vscode-oss-dev/user-data Fixes #3884 --- src/main.js | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/main.js b/src/main.js index 362345c3ff19c..92d5b4f4b5d05 100644 --- a/src/main.js +++ b/src/main.js @@ -9,6 +9,7 @@ global.vscodeStart = Date.now(); var app = require('electron').app; var fs = require('fs'); var path = require('path'); +var product = require('../product.json'); function stripComments(content) { var regexp = /("(?:[^\\\"]*(?:\\.)?)*")|('(?:[^\\\']*(?:\\.)?)*')|(\/\*(?:\r?\n|.)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g; @@ -95,6 +96,26 @@ function getNLSConfiguration() { return { locale: initialLocale, availableLanguages: {} }; } +function migrateUserDataDir(newDir) { + var oldDir = path.join(process.env.HOME, '.config', product.nameShort); + if (!fs.existsSync(oldDir)) { + return; + } + mkdirp(path.dirname(newDir)); + fs.renameSync(oldDir, newDir); +} + +function mkdirp(newDir) { + var stack = []; + while (!fs.existsSync(newDir)) { + stack.push(newDir); + newDir = path.dirname(newDir); + } + while (stack.length > 0) { + fs.mkdirSync(stack.pop()); + } +} + // Update cwd based on environment and platform try { if (process.platform === 'win32') { @@ -107,11 +128,19 @@ try { console.error(err); } -// Set path according to being built or not +// If root user, use a root-owned directory for user data +var configDir = path.join(process.env.HOME, product.dataFolderName); if (process.env.VSCODE_DEV) { - var appData = app.getPath('appData'); - app.setPath('userData', path.join(appData, 'Code-Development')); + configDir += '-dev'; } +var userDataDir = path.join(configDir, 'user-data'); + +// Attempt migrations if userDataDir does not exist +if (!fs.existsSync(userDataDir)) { + migrateUserDataDir(userDataDir); +} + +app.setPath('userData', userDataDir); // Use custom user data dir if specified, required to run as root on Linux var args = process.argv; From 0e4b1bd6e7971c0c71b534dc60f210df22e5580c Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 20 Apr 2016 11:51:50 -0700 Subject: [PATCH 2/2] Set the config dir automatically when running via sudo Fixes #5561 --- resources/linux/bin/code.sh | 15 --------------- src/main.js | 3 +++ 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/resources/linux/bin/code.sh b/resources/linux/bin/code.sh index ce8e57287591a..4df71021fa865 100755 --- a/resources/linux/bin/code.sh +++ b/resources/linux/bin/code.sh @@ -5,21 +5,6 @@ ARGS=$@ -# If root, ensure that --user-data-dir is specified -if [ "$(id -u)" = "0" ]; then - while test $# -gt 0 - do - if [[ $1 == --user-data-dir=* ]]; then - DATA_DIR_SET=1 - fi - shift - done - if [ -z $DATA_DIR_SET ]; then - echo "It is recommended to start vscode as a normal user. To run as root, you must specify an alternate user data directory with the --user-data-dir argument." 1>&2 - exit 1 - fi -fi - if [ ! -L $0 ]; then # if path is not a symlink, find relatively VSCODE_PATH="$(dirname $0)/.." diff --git a/src/main.js b/src/main.js index 92d5b4f4b5d05..50225e69766c7 100644 --- a/src/main.js +++ b/src/main.js @@ -134,6 +134,9 @@ if (process.env.VSCODE_DEV) { configDir += '-dev'; } var userDataDir = path.join(configDir, 'user-data'); +if (process.getuid() === 0) { + userDataDir += '-root'; +} // Attempt migrations if userDataDir does not exist if (!fs.existsSync(userDataDir)) {