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: do not cleanup api and repo.lock files #711

Merged
merged 2 commits into from
Nov 24, 2018
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
16 changes: 11 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,23 @@ function handleError (e) {

async function setupConnection () {
let config = store.get('config')
let updateCfg = false

if (config === null) {
config = {
type: 'go',
path: join(app.getPath('home'), '.ipfs')
}
config = { type: 'go' }
updateCfg = true
}

const ipfsd = await createDaemon(config)

// createDaemon has changed the config object,
// but didn't add the repo variable.
if (updateCfg) {
config.path = ipfsd.repoPath
store.set('config', config)
}

return createDaemon(config)
return ipfsd
}

async function run () {
Expand Down
38 changes: 1 addition & 37 deletions src/utils/daemon.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import IPFSFactory from 'ipfsd-ctl'
import { join } from 'path'
import fs from 'fs-extra'
import logger from './logger'

export default async function createDaemon (opts) {
opts.type = opts.type || 'go'
Expand All @@ -13,24 +10,16 @@ export default async function createDaemon (opts) {
throw new Error(`${opts.type} connection is not supported yet`)
}

const init = !(await fs.pathExists(opts.path)) || fs.readdirSync(opts.path).length === 0

if (!init) {
await cleanLocks(opts.path)
}

const factory = IPFSFactory.create({ type: opts.type })

const ipfsd = await new Promise((resolve, reject) => {
factory.spawn({
init: false,
start: false,
disposable: false,
defaultAddrs: true,
repoPath: opts.path
}, (e, ipfsd) => {
if (e) return reject(e)
if (ipfsd.initialized || !init) {
if (ipfsd.initialized) {
return resolve(ipfsd)
}

Expand Down Expand Up @@ -65,28 +54,3 @@ export default async function createDaemon (opts) {

return ipfsd
}

function cleanLocks (path) {
// This fixes a bug on Windows, where the daemon seems
// not to be exiting correctly, hence the file is not
// removed.
logger.info('Cleaning repo.lock and api files')
const lockPath = join(path, 'repo.lock')
const apiPath = join(path, 'api')

if (fs.existsSync(lockPath)) {
try {
fs.unlinkSync(lockPath)
} catch (_) {
logger.warn('Could not remove repo.lock. Daemon might be running')
}
}

if (fs.existsSync(apiPath)) {
try {
fs.unlinkSync(apiPath)
} catch (_) {
logger.warn('Could not remove api. Daemon might be running')
}
}
}