Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Windows users would hit EPERM error on importing configurations … #384

Merged
merged 3 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env electron

const path = require('path')
const fs = require('fs-extra')
const minimist = require('minimist')
const electron = require('electron')
const isDev = require('electron-is-dev')
Expand All @@ -15,6 +14,7 @@ const rabbit = require('electron-rabbit')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

const userConfig = require('./src/main/user-config')
const Worker = require('./src/worker')
const logger = require('./src/logger')
const electronIpc = require('./src/main/ipc')
Expand Down Expand Up @@ -187,19 +187,12 @@ function initDirectories (done) {
mkdirp.sync(path.join(userDataPath, 'styles'))
mkdirp.sync(path.join(userDataPath, 'presets'))
mkdirp.sync(argv.datadir)
styles.unpackIfNew(userDataPath, function (err) {

styles.unpackIfNew(userDataPath, function (err, newSettings) {
if (err) logger.error('[ERROR] while unpacking styles:', err)
var customLocation = path.join(userDataPath, 'presets', 'default')
fs.stat(path.join(customLocation, 'metadata.json'), function (err) {
if (err) {
if (err.code === 'ENOENT') {
var fallbackPresets = path.join(userDataPath, 'presets', styles.FALLBACK_DIR_NAME)
mkdirp.sync(fallbackPresets)
return fs.copy(fallbackPresets, customLocation, done)
} else logger.error(err)
}
done()
})
var fallbackSettingsLocation = path.join(userDataPath, 'presets', styles.FALLBACK_DIR_NAME)
if (newSettings) userConfig.copyFallbackSettings(fallbackSettingsLocation, done)
else done()
})
}

Expand Down
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@fortawesome/fontawesome-svg-core": "^1.2.24",
"@fortawesome/free-solid-svg-icons": "^5.11.1",
"@fortawesome/react-fontawesome": "^0.1.4",
"@mapeo/settings": "^2.1.0",
"@mapeo/settings": "^2.1.1",
"@material-ui/core": "^4.11.0",
"@material-ui/icons": "^4.5.1",
"@material-ui/lab": "^4.0.0-alpha.53",
Expand Down
3 changes: 2 additions & 1 deletion src/background/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module.exports = function (osm, media, { ipcSend, staticRoot }) {
var mapeoRouter = createMapeoRouter(osm, media, {
staticRoot: staticRoot,
writeFormat: 'osm-p2p-syncfile',
deviceType: 'desktop'
deviceType: 'desktop',
fallbackPresetsDir: path.join(staticRoot, 'DEFAULT_SETTINGS', 'presets')
})

var staticHandler = ecstatic({
Expand Down
2 changes: 1 addition & 1 deletion src/main/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function (win) {

ipc.on('get-user-data', function (event, type) {
var data = userConfig.getSettings(type)
if (!data) logger.debug('unhandled event', type)
if (!data) logger.debug('Could not get data for', type)
event.returnValue = data
})

Expand Down
43 changes: 41 additions & 2 deletions src/main/user-config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const app = require('electron').app
const fs = require('fs-extra')
const mkdirp = require('mkdirp')
const Settings = require('@mapeo/settings')
const path = require('path')

const logger = require('../logger')

const DEFAULT_SETTINGS = 'DEFAULT_SETTINGS'

function importSettings (settingsFile, cb) {
var userDataPath = app.getPath('userData')
var settings = new Settings(userDataPath)
Expand All @@ -24,11 +29,45 @@ function getEncryptionKey () {

function getSettings (type) {
var userDataPath = app.getPath('userData')
return new Settings(userDataPath).getSettings(type)
var data = new Settings(userDataPath).getSettings(type)
if (!data) return getFallbackSettings(type)
else return data
}

function getFallbackSettings (type) {
var userDataPath = app.getPath('userData')
return new Settings(path.join(userDataPath, DEFAULT_SETTINGS)).getSettings(type)
}

function copyFallbackSettings (fallbackSettings, done) {
// location: the full pathname to the fallback default settings directory
// which is unzipped and includes presets.json, icons, etc
// e.g., /path/to/Users/miranda/Mapeo/presets/my-default-fallback-settings/

// XXX: mapeo-server's static router expects
// - userDataPath/presets/default ~or~
// - fallbackPresetsDir/presets/default
// And we can't copy these styles directly into
// ... /userDataPath/presets/default, because Windows will throw
// an EPERM error if you try to copy over them when importing configurations

// This can be cleaned up once imported configurations exist in their own
// directories, and presets don't get overridden or copied over each other.
const userDataPath = app.getPath('userData')
const defaultSettings = path.join(userDataPath, DEFAULT_SETTINGS, 'presets')
mkdirp.sync(defaultSettings)
fs.copy(fallbackSettings, path.join(defaultSettings, 'default'),
(err) => {
if (err) logger.error('[ERROR] while unpacking default presets', err)
else logger.info('Unpacked new default styles and presets')
done()
}
)
}

module.exports = {
importSettings,
getSettings,
getEncryptionKey
getEncryptionKey,
copyFallbackSettings
}