spiegel_podman/libpod/volume_internal.go
Jiwoo Ahn 63ab237222 libpod(refactor): optimize directory empty check
No need to read the entire directory for empty check. Extract redundant check into helper

Signed-off-by: Jiwoo Ahn <ikwydls1314@gmail.com>
2026-08-26 16:40:12 +09:00

133 lines
3.1 KiB
Go

//go:build !remote && (linux || freebsd)
package libpod
import (
"errors"
"fmt"
"io"
"os"
"go.podman.io/podman/v6/libpod/define"
)
// Creates a new volume
func newVolume(runtime *Runtime) *Volume {
volume := new(Volume)
volume.config = new(VolumeConfig)
volume.state = new(VolumeState)
volume.runtime = runtime
volume.config.Labels = make(map[string]string)
volume.config.Options = make(map[string]string)
volume.state.NeedsCopyUp = true
volume.state.NeedsChown = true
return volume
}
// teardownStorage deletes the volume from volumePath
func (v *Volume) teardownStorage() error {
if v.UsesVolumeDriver() {
return nil
}
// Remove the whole volume directory rather than v.config.MountPoint,
// which only points at the _data subdirectory, so no state is left behind.
return os.RemoveAll(v.runtime.volumePath(v.Name()))
}
// Volumes with options set, or a filesystem type, or a device to mount need to
// be mounted and unmounted.
func (v *Volume) needsMount() bool {
// Non-local driver always needs mount
if v.UsesVolumeDriver() {
return true
}
// Image driver always needs mount
if v.config.Driver == define.VolumeDriverImage {
return true
}
// Commit 28138dafcc added the UID and GID options to this map
// However we should only mount when options other than uid and gid are set.
// see https://github.com/containers/podman/issues/10620
index := 0
if _, ok := v.config.Options["UID"]; ok {
index++
}
if _, ok := v.config.Options["GID"]; ok {
index++
}
if _, ok := v.config.Options["SIZE"]; ok {
index++
}
if _, ok := v.config.Options["NOQUOTA"]; ok {
index++
}
if _, ok := v.config.Options["nocopy"]; ok {
index++
}
if _, ok := v.config.Options["copy"]; ok {
index++
}
// when uid or gid is set there is also the "o" option
// set so we have to ignore this one as well
if index > 0 {
index++
}
// Local driver with options other than uid,gid needs mount
return len(v.config.Options) > index
}
func isDirEmpty(path string) (bool, error) {
dir, err := os.Open(path)
if err != nil {
return false, err
}
defer dir.Close()
_, err = dir.Readdirnames(1)
if err == nil {
return false, nil
}
if errors.Is(err, io.EOF) {
return true, nil
}
return false, err
}
// update() updates the volume state from the DB.
func (v *Volume) update() error {
if err := v.runtime.state.UpdateVolume(v); err != nil {
return err
}
if !v.valid {
return define.ErrVolumeRemoved
}
return nil
}
// save() saves the volume state to the DB
func (v *Volume) save() error {
return v.runtime.state.SaveVolume(v)
}
// Refresh volume state after a restart.
func (v *Volume) refresh() error {
lock, err := v.runtime.lockManager.AllocateAndRetrieveLock(v.config.LockID)
if err != nil {
return fmt.Errorf("acquiring lock %d for volume %s: %w", v.config.LockID, v.Name(), err)
}
v.lock = lock
return nil
}
// resetVolumeState resets state fields to default values.
// It is performed before a refresh and clears the state after a reboot.
// It does not save the results - assumes the database will do that for us.
func resetVolumeState(state *VolumeState) {
state.MountCount = 0
state.MountPoint = ""
state.CopiedUp = false
}