@cryptotaxi247 / kubo / commits / 740cf4a3b

cmd/ipfs: check whether repo is accessible before attempting to load plugins

License: MIT Signed-off-by: keks <keks@cryptoscope.co>

keks committed Dec 18, 2017 at 10:23 UTC 740cf4a3b43fcb6615790885cc801fae9f764bba
2 files changed +27 -4
cmd/ipfs/main.go
+26 -3
@@ -182,9 +182,18 @@ func makeExecutor(req *cmds.Request, env interface{}) (cmds.Executor, error) {
182 if client != nil && !req.Command.External {
183 exctr = client.(cmds.Executor)
184 } else {
185 - pluginpath := filepath.Join(env.(*oldcmds.Context).ConfigRoot, "plugins")
186 - if _, err := loader.LoadPlugins(pluginpath); err != nil {
187 - log.Warning("error loading plugins: ", err)
185 + cctx := env.(*oldcmds.Context)
186 + pluginpath := filepath.Join(cctx.ConfigRoot, "plugins")
187 +
188 + // check if repo is accessible before loading plugins
189 + ok, err := checkPermissions(cctx.ConfigRoot)
190 + if err != nil {
191 + return nil, err
192 + }
193 + if ok {
194 + if _, err := loader.LoadPlugins(pluginpath); err != nil {
195 + log.Warning("error loading plugins: ", err)
196 + }
197 }
198
199 exctr = cmds.NewExecutor(req.Root)
@@ -193,6 +202,20 @@ func makeExecutor(req *cmds.Request, env interface{}) (cmds.Executor, error) {
202 return exctr, nil
203 }
204
205 +func checkPermissions(path string) (bool, error) {
206 + _, err := os.Open(path)
207 + if os.IsNotExist(err) {
208 + // repo does not exist yet - don't load plugins, but also don't fail
209 + return false, nil
210 + }
211 + if os.IsPermission(err) {
212 + // repo is not accessible. error out.
213 + return false, fmt.Errorf("error opening repository at %s: permission denied", path)
214 + }
215 +
216 + return true, nil
217 +}
218 +
219 // commandDetails returns a command's details for the command given by |path|
220 // within the |root| command tree.
221 //
test/sharness/t0020-init.sh
+1 -1
@@ -22,7 +22,7 @@ test_expect_success "ipfs init fails" '
22 # Under Windows/Cygwin the error message is different,
23 # so we use the STD_ERR_MSG prereq.
24 if test_have_prereq STD_ERR_MSG; then
25 - init_err_msg="Error: failed to take lock at $IPFS_PATH: permission denied"
25 + init_err_msg="Error: error opening repository at $IPFS_PATH: permission denied"
26 else
27 init_err_msg="Error: mkdir $IPFS_PATH: The system cannot find the path specified."
28 fi