|
4 | 4 | "context"
|
5 | 5 | "fmt"
|
6 | 6 | "io"
|
| 7 | + "io/fs" |
7 | 8 | "os"
|
8 | 9 |
|
9 | 10 | ot "github.com/opentracing/opentracing-go"
|
@@ -67,17 +68,31 @@ func DoParallelQueries(
|
67 | 68 |
|
68 | 69 | // EnsureDirectory makes sure directory is there, if not creates it if not
|
69 | 70 | func EnsureDirectory(dir string) error {
|
| 71 | + return EnsureDirectoryWithDefaultPermissions(dir, 0o777) |
| 72 | +} |
| 73 | + |
| 74 | +func EnsureDirectoryWithDefaultPermissions(dir string, mode fs.FileMode) error { |
70 | 75 | info, err := os.Stat(dir)
|
71 | 76 | if os.IsNotExist(err) {
|
72 |
| - return os.MkdirAll(dir, 0o777) |
| 77 | + return os.MkdirAll(dir, mode) |
73 | 78 | } else if err == nil && !info.IsDir() {
|
74 | 79 | return fmt.Errorf("not a directory: %s", dir)
|
75 |
| - } else if err == nil && info.Mode()&0700 != 0700 { |
76 |
| - return fmt.Errorf("insufficient permissions: %s %s", dir, info.Mode()) |
77 | 80 | }
|
78 | 81 | return err
|
79 | 82 | }
|
80 | 83 |
|
| 84 | +func RequirePermissions(path string, required fs.FileMode) error { |
| 85 | + info, err := os.Stat(path) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + if mode := info.Mode(); mode&required != required { |
| 91 | + return fmt.Errorf("insufficient permissions for path %s: required %s but found %s", path, required.String(), mode.String()) |
| 92 | + } |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
81 | 96 | // ReadCloserWithContextCancelFunc helps with cancelling the context when closing a ReadCloser.
|
82 | 97 | // NOTE: The consumer of ReadCloserWithContextCancelFunc should always call the Close method when it is done reading which otherwise could cause a resource leak.
|
83 | 98 | type ReadCloserWithContextCancelFunc struct {
|
|
0 commit comments