@@ -24,6 +24,7 @@ import (
24
24
25
25
"github.com/containerd/containerd/errdefs"
26
26
"github.com/containerd/containerd/identifiers"
27
+ "github.com/containerd/log"
27
28
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
28
29
"github.com/containerd/nerdctl/v2/pkg/lockutil"
29
30
"github.com/containerd/nerdctl/v2/pkg/strutil"
@@ -83,13 +84,24 @@ func (vs *volumeStore) Create(name string, labels []string) (*native.Volume, err
83
84
}
84
85
volPath := filepath .Join (vs .dir , name )
85
86
volDataPath := filepath .Join (volPath , DataDirName )
86
- fn := func () error {
87
+ volFilePath := filepath .Join (volPath , volumeJSONFileName )
88
+ fn := func () (err error ) {
87
89
if err := os .Mkdir (volPath , 0700 ); err != nil {
88
90
return err
89
91
}
92
+ defer func () {
93
+ if err != nil {
94
+ os .Remove (volPath )
95
+ }
96
+ }()
90
97
if err := os .Mkdir (volDataPath , 0755 ); err != nil {
91
98
return err
92
99
}
100
+ defer func () {
101
+ if err != nil {
102
+ os .Remove (volDataPath )
103
+ }
104
+ }()
93
105
94
106
type volumeOpts struct {
95
107
Labels map [string ]string `json:"labels"`
@@ -106,14 +118,25 @@ func (vs *volumeStore) Create(name string, labels []string) (*native.Volume, err
106
118
return err
107
119
}
108
120
109
- volFilePath := filepath .Join (volPath , volumeJSONFileName )
121
+ defer func () {
122
+ if err != nil {
123
+ if _ , statErr := os .Stat (volFilePath ); statErr != nil && ! os .IsNotExist (statErr ) {
124
+ log .L .Warnf ("failed to stat volume file: %v" , statErr )
125
+ return
126
+ } else if statErr == nil {
127
+ os .Remove (volFilePath )
128
+ }
129
+ }
130
+ }()
110
131
return os .WriteFile (volFilePath , labelsJSON , 0644 )
111
132
}
112
133
113
134
if err := lockutil .WithDirLock (vs .dir , fn ); err != nil {
114
135
return nil , err
115
136
}
116
137
138
+ // If other new actions that might fail are added below, we should move the cleanup function out of fn.
139
+
117
140
vol := & native.Volume {
118
141
Name : name ,
119
142
Mountpoint : volDataPath ,
0 commit comments