Skip to content

Commit

Permalink
vc: Clean up directories in case MkdirAll fails
Browse files Browse the repository at this point in the history
Fixes kata-containers#2230

Signed-off-by: Ted Yu <yuzhihong@gmail.com>
  • Loading branch information
yutedz committed Nov 21, 2019
1 parent 48c8d66 commit 03478d4
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions virtcontainers/persist/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,22 @@ func (fs *FS) ToDisk(ss persistapi.SandboxState, cs map[string]persistapi.Contai
return err
}

var dirCreationErr error
var createdDirs []string
defer func() {
if dirCreationErr != nil && len(createdDirs) > 0 {
for _, dir := range createdDirs {
os.RemoveAll(dir)
}
}
}()
// persist container configuration data
for cid, cstate := range fs.containerState {
cdir := filepath.Join(sandboxDir, cid)
if err := os.MkdirAll(cdir, dirMode); err != nil {
return err
if dirCreationErr = os.MkdirAll(cdir, dirMode); dirCreationErr != nil {
return dirCreationErr
}
createdDirs = append(createdDirs, cdir)

cfile := filepath.Join(cdir, persistFile)
cf, err := os.OpenFile(cfile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileMode)
Expand Down

0 comments on commit 03478d4

Please sign in to comment.