From e1f6bbac9f7c2a1d1ce0cacebb6efcf6714776f5 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Sun, 16 Aug 2015 13:16:02 +0200 Subject: [PATCH] Create uid/gid convertion function. Create separate function to convert user ID / group ID of a user to integer. Windows is a stub, since this doesn't work on Windows (uid/gid is not a number). --- lock.go | 12 ++---------- lock_unix.go | 24 ++++++++++++++++++++++++ lock_windows.go | 10 ++++++++++ snapshot.go | 13 +++---------- 4 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 lock_unix.go create mode 100644 lock_windows.go diff --git a/lock.go b/lock.go index 767f21511..dd24e8b15 100644 --- a/lock.go +++ b/lock.go @@ -5,7 +5,6 @@ import ( "os" "os/signal" "os/user" - "strconv" "sync" "syscall" "time" @@ -117,15 +116,8 @@ func (l *Lock) fillUserInfo() error { } l.Username = usr.Username - // We ignore the error. On Windows Uid is not a number - uid, _ := strconv.ParseInt(usr.Uid, 10, 32) - l.UID = uint32(uid) - - // We ignore the error. On Windows Gid is not a number - gid, _ := strconv.ParseInt(usr.Gid, 10, 32) - l.GID = uint32(gid) - - return nil + l.UID, l.GID, err = uidGidInt(*usr) + return err } // checkForOtherLocks looks for other locks that currently exist in the repository. diff --git a/lock_unix.go b/lock_unix.go new file mode 100644 index 000000000..851071ab2 --- /dev/null +++ b/lock_unix.go @@ -0,0 +1,24 @@ +// +build !windows + +package restic + +import ( + "os/user" + "strconv" +) + +// uidGidInt returns uid, gid of the user as a number. +func uidGidInt(u user.User) (uid, gid uint32, err error) { + var ui, gi int + ui, err = strconv.ParseInt(u.Uid, 10, 32) + if err != nil { + return + } + gi, err = strconv.ParseInt(u.Gid, 10, 32) + if err != nil { + return + } + uid = uint32(ui) + gid = uint32(gi) + return +} diff --git a/lock_windows.go b/lock_windows.go new file mode 100644 index 000000000..2e0c68b48 --- /dev/null +++ b/lock_windows.go @@ -0,0 +1,10 @@ +package restic + +import ( + "os/user" +) + +// uidGidInt always returns 0 on Windows, since uid isn't numbers +func uidGidInt(u user.User) (uid, gid uint32, err error) { + return 0, 0, nil +} diff --git a/snapshot.go b/snapshot.go index 1693d2b75..91c40a62d 100644 --- a/snapshot.go +++ b/snapshot.go @@ -5,7 +5,6 @@ import ( "os" "os/user" "path/filepath" - "strconv" "time" "github.com/restic/restic/backend" @@ -76,15 +75,9 @@ func (sn *Snapshot) fillUserInfo() error { } sn.Username = usr.Username - // We ignore the error. On Windows Uid is not a number - uid, _ := strconv.ParseInt(usr.Uid, 10, 32) - sn.UID = uint32(uid) - - // We ignore the error. On Windows Gid is not a number - gid, _ := strconv.ParseInt(usr.Gid, 10, 32) - sn.GID = uint32(gid) - - return nil + // set userid and groupid + sn.UID, sn.GID, err = uidGidInt(*usr) + return err } // FindSnapshot takes a string and tries to find a snapshot whose ID matches