Remove and create workdir if state dir does not exist
This is the case where the work dir could still exist if a machine
reboots, reseting the state dir.  On container creation, we should just
clear out the work dir.
@@ -58,23 +58,36 @@ func NewBundle(ctx context.Context, root, state, id string, spec []byte) (b *Bun
Path: filepath.Join(state, ns, id),
Namespace: ns,
}
- paths := []string{b.Path, work}
- // create base directories
+ var paths []string
+ defer func() {
+ if err != nil {
for _, d := range paths {
- if err := os.MkdirAll(filepath.Dir(d), 0711); err != nil {
+ os.RemoveAll(d)
+ }
+ }
+ }()
+ // create state directory for the bundle
+ if err := os.MkdirAll(filepath.Dir(b.Path), 0711); err != nil {
return nil, err
}
- if err := os.Mkdir(d, 0711); err != nil {
+ if err := os.Mkdir(b.Path, 0711); err != nil {
return nil, err
}
+ paths = append(paths, b.Path)
+ // create working directory for the bundle
+ if err := os.MkdirAll(filepath.Dir(work), 0711); err != nil {
+ return nil, err
}
- defer func() {
- if err != nil {
- for _, d := range paths {
- os.RemoveAll(d)
+ if err := os.Mkdir(work, 0711); err != nil {
+ if !os.IsExist(err) {
+ return nil, err
}
+ os.RemoveAll(work)
+ if err := os.Mkdir(work, 0711); err != nil {
+ return nil, err
}
- }()
+ }
+ paths = append(paths, work)
// create rootfs dir
if err := os.Mkdir(filepath.Join(b.Path, "rootfs"), 0711); err != nil {
return nil, err
