From ac025874c9aaf09213365b8f3b36c3d511dc9052 Mon Sep 17 00:00:00 2001 From: Sergiu Costea Date: Fri, 26 Apr 2019 11:24:42 +0200 Subject: [PATCH] Dispatcher creates parent directory of UNIX socket files --- go/godispatcher/BUILD.bazel | 1 + go/godispatcher/main.go | 6 ++++++ go/lib/util/BUILD.bazel | 1 + go/lib/util/fs.go | 31 +++++++++++++++++++++++++++++ go/sciond/internal/config/config.go | 17 ++++------------ 5 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 go/lib/util/fs.go diff --git a/go/godispatcher/BUILD.bazel b/go/godispatcher/BUILD.bazel index 08e313610e..de719c0521 100644 --- a/go/godispatcher/BUILD.bazel +++ b/go/godispatcher/BUILD.bazel @@ -13,6 +13,7 @@ go_library( "//go/lib/env:go_default_library", "//go/lib/fatal:go_default_library", "//go/lib/log:go_default_library", + "//go/lib/util:go_default_library", "@com_github_burntsushi_toml//:go_default_library", ], ) diff --git a/go/godispatcher/main.go b/go/godispatcher/main.go index 2efbe9c854..4a6efe973f 100644 --- a/go/godispatcher/main.go +++ b/go/godispatcher/main.go @@ -30,6 +30,7 @@ import ( "github.com/scionproto/scion/go/lib/env" "github.com/scionproto/scion/go/lib/fatal" "github.com/scionproto/scion/go/lib/log" + "github.com/scionproto/scion/go/lib/util" ) var ( @@ -60,6 +61,11 @@ func realMain() int { return 1 } + if err := util.CreateParentDirs(cfg.Dispatcher.ApplicationSocket); err != nil { + log.Crit("Unable to create directory tree for socket", "err", err) + return 1 + } + go func() { defer log.LogPanicAndExit() err := RunDispatcher( diff --git a/go/lib/util/BUILD.bazel b/go/lib/util/BUILD.bazel index 88764f1188..640a13bee5 100644 --- a/go/lib/util/BUILD.bazel +++ b/go/lib/util/BUILD.bazel @@ -10,6 +10,7 @@ go_library( "duration.go", "duration_wrap.go", "file.go", + "fs.go", "map.go", "padding.go", "raw.go", diff --git a/go/lib/util/fs.go b/go/lib/util/fs.go new file mode 100644 index 0000000000..0264ac5579 --- /dev/null +++ b/go/lib/util/fs.go @@ -0,0 +1,31 @@ +// Copyright 2019 ETH Zurich +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package util + +import ( + "os" + "path/filepath" +) + +// CreateParentDir creates the necessary parent directories for file. +func CreateParentDirs(file string) error { + dir := filepath.Dir(file) + if _, err := os.Stat(dir); os.IsNotExist(err) { + if err = os.MkdirAll(dir, 0755); err != nil { + return err + } + } + return nil +} diff --git a/go/sciond/internal/config/config.go b/go/sciond/internal/config/config.go index 5a50c7f8eb..8835ec0f24 100644 --- a/go/sciond/internal/config/config.go +++ b/go/sciond/internal/config/config.go @@ -17,8 +17,6 @@ package config import ( "io" - "os" - "path/filepath" "time" "github.com/scionproto/scion/go/lib/common" @@ -147,18 +145,11 @@ func (cfg *SDConfig) ConfigName() string { } func (cfg *SDConfig) CreateSocketDirs() error { - reliableDir := filepath.Dir(cfg.Reliable) - if _, err := os.Stat(reliableDir); os.IsNotExist(err) { - if err = os.MkdirAll(reliableDir, 0755); err != nil { - return common.NewBasicError("Cannot create reliable socket dir", err, "dir", - reliableDir) - } + if err := util.CreateParentDirs(cfg.Reliable); err != nil { + return common.NewBasicError("Cannot create reliable socket dir", err) } - unixDir := filepath.Dir(cfg.Unix) - if _, err := os.Stat(unixDir); os.IsNotExist(err) { - if err = os.MkdirAll(unixDir, 0755); err != nil { - return common.NewBasicError("Cannot create unix socket dir", err, "dir", unixDir) - } + if err := util.CreateParentDirs(cfg.Unix); err != nil { + return common.NewBasicError("Cannot create unix socket dir", err) } return nil }