internal/storage: fsstore ensures directory exists before creating file

Change-Id: I8703be7e8028a7f76bb4a28f097db061a5096940
Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/499917
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jamal Carvalho <jamal@golang.org>
diff --git a/godev/internal/storage/storage.go b/godev/internal/storage/storage.go
index 38b830c..9d3a883 100644
--- a/godev/internal/storage/storage.go
+++ b/godev/internal/storage/storage.go
@@ -10,7 +10,7 @@
 	"context"
 	"io"
 	"os"
-	"path"
+	"path/filepath"
 
 	"cloud.google.com/go/storage"
 )
@@ -75,10 +75,14 @@
 // Writer creates a new file if it does not exist. Any previous file with the same
 // name will be truncated.
 func (s *fsStore) Writer(ctx context.Context, file string) (io.WriteCloser, error) {
-	return os.Create(path.Join(s.dir, file))
+	name := filepath.Join(s.dir, file)
+	if err := os.MkdirAll(filepath.Dir(name), os.ModePerm); err != nil {
+		return nil, err
+	}
+	return os.Create(name)
 }
 
 // Reader opens the named file for reading.
 func (s *fsStore) Reader(ctx context.Context, file string) (io.ReadCloser, error) {
-	return os.Open(path.Join(s.dir, file))
+	return os.Open(filepath.Join(s.dir, file))
 }