From 13c5bfcc6ec165fe0cf988a3e92bbd8d8619411b Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Sat, 17 Jun 2017 12:11:24 +0200 Subject: pkg/osutil: add IsExist helper function --- pkg/osutil/osutil.go | 6 ++++++ pkg/osutil/osutil_test.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 pkg/osutil/osutil_test.go (limited to 'pkg/osutil') diff --git a/pkg/osutil/osutil.go b/pkg/osutil/osutil.go index 3f42dbc12..a9c64ad1c 100644 --- a/pkg/osutil/osutil.go +++ b/pkg/osutil/osutil.go @@ -69,3 +69,9 @@ func Abs(path string) string { } return filepath.Join(wd, path) } + +// IsExist returns true if the file name exists. +func IsExist(name string) bool { + _, err := os.Stat(name) + return err == nil || !os.IsNotExist(err) +} diff --git a/pkg/osutil/osutil_test.go b/pkg/osutil/osutil_test.go new file mode 100644 index 000000000..ee4cb6705 --- /dev/null +++ b/pkg/osutil/osutil_test.go @@ -0,0 +1,18 @@ +// Copyright 2017 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package osutil + +import ( + "os" + "testing" +) + +func TestIsExist(t *testing.T) { + if f := os.Args[0]; !IsExist(f) { + t.Fatalf("executable %v does not exist", f) + } + if f := os.Args[0] + "-foo-bar-buz"; IsExist(f) { + t.Fatalf("file %v exists", f) + } +} -- cgit mrf-deployment