Skip to content

Commit df4b0d7

Browse files
committed
Fix and issue with file handle leak
1 parent 34a4cf2 commit df4b0d7

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

manager/manager.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,22 +146,15 @@ func (m Manager) Create(name string, sizeInBytes int64, sparse bool, fs string,
146146

147147
// create data file
148148
dataFilePath := filepath.Join(m.dataDir, name+"."+fs)
149-
dataFileInfo, err := os.Create(dataFilePath)
150-
if err != nil {
151-
_ = os.Remove(dataFilePath) // attempt to cleanup
152-
153-
return errors.Wrapf(err,
154-
"Error creating volume '%s' - cannot create datafile '%s'",
155-
name, dataFilePath)
156-
}
157149

158150
if sparse {
159-
err = dataFileInfo.Truncate(sizeInBytes)
151+
errBytes, err := exec.Command("truncate", "-s", fmt.Sprint(sizeInBytes), dataFilePath).CombinedOutput()
160152
if err != nil {
153+
errStr := strings.TrimSpace(string(errBytes[:]))
161154
_ = os.Remove(dataFilePath) // attempt to cleanup
162155
return errors.Wrapf(err,
163-
"Error creating volume '%s' - error creating data file",
164-
name, sizeInBytes)
156+
"Error creating volume '%s' - error creating sparse data file: %s",
157+
name, errStr)
165158
}
166159
} else {
167160
// Try using fallocate - super fast if data dir is on ext4 or xfs

0 commit comments

Comments
 (0)