restic: extract UidGidInt into separate files

Extract UidGidInt to simplify moving the locking code to the repository.
This commit is contained in:
Michael Eischer
2026-06-07 13:06:04 +02:00
parent 3ee465d363
commit 7015da44ad
4 changed files with 35 additions and 24 deletions
-18
View File
@@ -4,29 +4,11 @@ package restic
import (
"os"
"os/user"
"strconv"
"syscall"
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors"
)
// UidGidInt returns uid, gid of the user as a number.
//
//nolint:revive // capitalization is correct as is
func UidGidInt(u *user.User) (uid, gid uint32, err error) {
ui, err := strconv.ParseUint(u.Uid, 10, 32)
if err != nil {
return 0, 0, errors.Errorf("invalid UID %q", u.Uid)
}
gi, err := strconv.ParseUint(u.Gid, 10, 32)
if err != nil {
return 0, 0, errors.Errorf("invalid GID %q", u.Gid)
}
return uint32(ui), uint32(gi), nil
}
// checkProcess will check if the process retaining the lock
// exists and responds to SIGHUP signal.
// Returns true if the process exists and responds.
-6
View File
@@ -2,16 +2,10 @@ package restic
import (
"os"
"os/user"
"github.com/restic/restic/internal/debug"
)
// UidGidInt always returns 0 on Windows, since uid isn't numbers
func UidGidInt(_ *user.User) (uid, gid uint32, err error) {
return 0, 0, nil
}
// checkProcess will check if the process retaining the lock exists.
// Returns true if the process exists.
func (l *Lock) processExists() bool {
+25
View File
@@ -0,0 +1,25 @@
//go:build !windows
package restic
import (
"os/user"
"strconv"
"github.com/restic/restic/internal/errors"
)
// UidGidInt returns uid, gid of the user as a number.
//
//nolint:revive // capitalization is correct as is
func UidGidInt(u *user.User) (uid, gid uint32, err error) {
ui, err := strconv.ParseUint(u.Uid, 10, 32)
if err != nil {
return 0, 0, errors.Errorf("invalid UID %q", u.Uid)
}
gi, err := strconv.ParseUint(u.Gid, 10, 32)
if err != nil {
return 0, 0, errors.Errorf("invalid GID %q", u.Gid)
}
return uint32(ui), uint32(gi), nil
}
+10
View File
@@ -0,0 +1,10 @@
package restic
import (
"os/user"
)
// UidGidInt always returns 0 on Windows, since uid isn't numbers
func UidGidInt(_ *user.User) (uid, gid uint32, err error) {
return 0, 0, nil
}