|
| 1 | +package driver |
| 2 | + |
| 3 | +import "os" |
| 4 | + |
| 5 | +type OsClient interface { |
| 6 | + MkDirAllWithPerms(path string, perms os.FileMode, uid, gid int) error |
| 7 | + MkDirAllWithPermsNoOwnership(path string, perms os.FileMode) error |
| 8 | + GetPerms(path string) (os.FileMode, error) |
| 9 | + Remove(path string) error |
| 10 | + RemoveAll(path string) error |
| 11 | +} |
| 12 | + |
| 13 | +// Real OsClient |
| 14 | + |
| 15 | +type RealOsClient struct{} |
| 16 | + |
| 17 | +func NewOsClient() *RealOsClient { |
| 18 | + return &RealOsClient{} |
| 19 | +} |
| 20 | + |
| 21 | +func (o *RealOsClient) MkDirAllWithPerms(path string, perms os.FileMode, uid, gid int) error { |
| 22 | + err := os.MkdirAll(path, perms) |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + // Extra CHMOD guarantees we get the permissions we desire, inspite of the umask |
| 27 | + err = os.Chmod(path, perms) |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + err = os.Chown(path, uid, gid) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + return nil |
| 36 | +} |
| 37 | + |
| 38 | +func (o *RealOsClient) MkDirAllWithPermsNoOwnership(path string, perms os.FileMode) error { |
| 39 | + err := os.MkdirAll(path, perms) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + // Extra CHMOD guarantees we get the permissions we desire, inspite of the umask |
| 44 | + err = os.Chmod(path, perms) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +func (o *RealOsClient) Remove(path string) error { |
| 52 | + return os.Remove(path) |
| 53 | +} |
| 54 | + |
| 55 | +func (o *RealOsClient) RemoveAll(path string) error { |
| 56 | + return os.RemoveAll(path) |
| 57 | +} |
| 58 | + |
| 59 | +func (o *RealOsClient) GetPerms(path string) (os.FileMode, error) { |
| 60 | + fInfo, err := os.Stat(path) |
| 61 | + if err != nil { |
| 62 | + return 0, err |
| 63 | + } |
| 64 | + return fInfo.Mode(), nil |
| 65 | +} |
| 66 | + |
| 67 | +// Fake OsClient |
| 68 | + |
| 69 | +type FakeOsClient struct{} |
| 70 | + |
| 71 | +func (o *FakeOsClient) MkDirAllWithPerms(_ string, _ os.FileMode, _, _ int) error { |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func (o *FakeOsClient) MkDirAllWithPermsNoOwnership(_ string, _ os.FileMode) error { |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func (o *FakeOsClient) Remove(_ string) error { |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func (o *FakeOsClient) RemoveAll(_ string) error { |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +func (o *FakeOsClient) GetPerms(_ string) (os.FileMode, error) { |
| 88 | + return 0, nil |
| 89 | +} |
0 commit comments