Skip to content

Commit

Permalink
feat: use custom lockfile if we can't create one under /tmp
Browse files Browse the repository at this point in the history
Signed-off-by: Serge Hallyn <serge@hallyn.com>
  • Loading branch information
hallyn committed Dec 19, 2022
1 parent 6fc877d commit 074908b
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions pkg/atomfs/molecule.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package atomfs

import (
"fmt"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -94,8 +95,26 @@ func (m Molecule) overlayArgs(dest string) (string, error) {
// device exists. so try to cooperate via this lock.
var advisoryLockPath = path.Join(os.TempDir(), ".atomfs-lock")

func makeLock(mountpoint string) (*os.File, error) {
lockfile, err := os.Create(advisoryLockPath)
if err == nil {
return lockfile, nil
}
// backup plan: lock the destination as ${path}.atomfs-lock
mountpoint = strings.TrimSuffix(mountpoint, "/")
lockPath := filepath.Join(mountpoint, ".atomfs-lock")
var err2 error
lockfile, err2 = os.Create(lockPath)
if err2 == nil {
return lockfile, nil
}

err = fmt.Errorf("Failed locking %s: %v\nFailed locking %s: %v", advisoryLockPath, err, lockPath, err2)
return lockfile, err
}

func (m Molecule) Mount(dest string) error {
lockfile, err := os.Create(advisoryLockPath)
lockfile, err := makeLock(dest)
if err != nil {
return errors.WithStack(err)
}
Expand Down Expand Up @@ -134,7 +153,7 @@ func Umount(dest string) error {
return errors.Wrapf(err, "couldn't create abs path for %v", dest)
}

lockfile, err := os.Create(advisoryLockPath)
lockfile, err := makeLock(dest)
if err != nil {
return errors.WithStack(err)
}
Expand Down

0 comments on commit 074908b

Please sign in to comment.