Skip to content

Commit 5aa829e

Browse files
authored
Merge pull request #59 from munnerz/cleanup-corrupt-dirs
if a volume containing incomplete metadata is found during a ListVolumes call, clean up and continue
2 parents 9811918 + 2e92b6c commit 5aa829e

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

storage/filesystem.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Filesystem struct {
4747
baseDir string
4848

4949
// used by the 'read only' methods
50-
fs fs.FS
50+
fs fs.StatFS
5151

5252
// FixedFSGroup is an optional field which will set the gid ownership of all
5353
// volume's data directories to this value.
@@ -71,7 +71,7 @@ func NewFilesystem(log logr.Logger, baseDir string) (*Filesystem, error) {
7171
baseDir: baseDir,
7272
// Use the rootfs as the DirFS so that paths passed to both read &
7373
// write methods on this struct use a consistent root.
74-
fs: os.DirFS("/"),
74+
fs: os.DirFS("/").(fs.StatFS),
7575
}
7676

7777
notMnt, err := mount.IsNotMountPoint(mount.New(""), f.tempfsPath())
@@ -110,14 +110,19 @@ func (f *Filesystem) ListVolumes() ([]string, error) {
110110

111111
var vols []string
112112
for _, dir := range dirs {
113-
file, err := f.fs.Open(f.metadataPathForVolumeID(dir.Name()))
114-
if err != nil {
113+
_, err := f.fs.Stat(f.metadataPathForVolumeID(dir.Name()))
114+
switch {
115+
case errors.Is(err, fs.ErrNotExist):
116+
f.log.Info("Directory exists but does not contain a metadata file - deleting directory and its contents", "volume_id", dir.Name())
117+
if err := f.RemoveVolume(dir.Name()); err != nil {
118+
return nil, fmt.Errorf("deleting stale volume: %v", err)
119+
}
120+
// continue to skip this loop iteration
121+
continue
122+
case err != nil:
115123
// discovered a volume/directory that does not contain a metadata file
116-
// TODO: log this error to allow startup to continue
117124
return nil, err
118125
}
119-
// immediately close the file as we just need to verify it exists
120-
file.Close()
121126
vols = append(vols, dir.Name())
122127
}
123128

storage/filesystem_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ func TestFilesystem_ListVolumes(t *testing.T) {
118118
}
119119
}
120120

121+
func TestFilesystem_ListVolumes_CleansUpCorruptVolumes(t *testing.T) {
122+
backend := &Filesystem{
123+
fs: fstest.MapFS{
124+
"inmemfs/fake-volume/metadata.json": &fstest.MapFile{Data: []byte{}},
125+
"inmemfs/fake-emptyvolume/nothing": &fstest.MapFile{Data: []byte{}},
126+
},
127+
}
128+
129+
vols, err := backend.ListVolumes()
130+
if err != nil {
131+
t.Errorf("unexpected error: %v", err)
132+
}
133+
if len(vols) != 1 {
134+
t.Errorf("expected 1 volume to be returned but got: %+v", vols)
135+
}
136+
if vols[0] != "fake-volume" {
137+
t.Errorf("expected only entry to be 'fake-volume' but got: %s", vols[0])
138+
}
139+
}
121140
func Test_fsGroupForMetadata(t *testing.T) {
122141
intPtr := func(i int64) *int64 {
123142
return &i

0 commit comments

Comments
 (0)