From 36fcd116dae5d58ed0e94cf2f07f0c5d28050f94 Mon Sep 17 00:00:00 2001 From: Abhishek Pareek Date: Thu, 5 Oct 2023 13:47:30 -0700 Subject: [PATCH] return error message if the mount point is not writable --- cmd/cmd.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 730aeb8..383ccf6 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -38,7 +38,10 @@ import ( var Version = "dev" // O_DIRECT align size. -const alignSize = 4096 +const ( + alignSize = 4096 + tmpFile = "..tmpFile" +) // flags var ( @@ -154,8 +157,13 @@ $ dperf --serial /mnt/drive{1..6} } if !stat.Mode().IsDir() { - return errors.New("path '" + path + "' is not a directory ") + return errors.New("path '" + path + "' is not a directory") + } + + if !isDirWritable(path) { + return errors.New("directory at path '" + path + "' is not writable") } + paths = append(paths, filepath.Clean(arg)) } return perf.RunAndRender(c.Context(), paths...) @@ -208,3 +216,15 @@ func init() { func Execute(ctx context.Context) error { return dperfCmd.ExecuteContext(ctx) } + +// Check if the directory is writable or not +func isDirWritable(dir string) bool { + file, err := os.CreateTemp(dir, tmpFile) + if err != nil { + return false + } + file.Close() + // clean up + os.Remove(file.Name()) + return true +}