~ubuntu-branches/ubuntu/trusty/juju-core/trusty-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/utils/sudo.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-29 11:40:20 UTC
  • mfrom: (23.1.1 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20140129114020-ejieitm8smtt5vln
Tags: 1.17.1-0ubuntu2
d/tests/local-provider: Don't fail tests if ~/.juju is present as its
created by the juju version command. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
import (
7
7
        "fmt"
8
8
        "os"
 
9
        "path/filepath"
9
10
        "strconv"
10
11
)
11
12
 
 
13
// CheckIfRoot is a simple function that we can use to determine if
 
14
// the ownership of files and directories we create.
 
15
var CheckIfRoot = func() bool {
 
16
        return os.Getuid() == 0
 
17
}
 
18
 
12
19
// SudoCallerIds returns the user id and group id of the SUDO caller.
13
20
// If either is unset, it returns zero for both values.
14
21
// An error is returned if the relevant environment variables
30
37
        }
31
38
        return
32
39
}
 
40
 
 
41
// MkdirForUser will call down to os.Mkdir and if the user is running as root,
 
42
// the ownership will be changed to the sudo user.
 
43
func MkdirForUser(dir string, perm os.FileMode) error {
 
44
        if err := os.Mkdir(dir, perm); err != nil {
 
45
                return err
 
46
        }
 
47
        return ChownToUser(dir)
 
48
}
 
49
 
 
50
// MkdirAllForUser will call down to os.MkdirAll and if the user is running as
 
51
// root, the ownership will be changed to the sudo user for each directory
 
52
// that was created.
 
53
func MkdirAllForUser(dir string, perm os.FileMode) error {
 
54
        toCreate := []string{}
 
55
        path := dir
 
56
        for {
 
57
                _, err := os.Lstat(path)
 
58
                if os.IsNotExist(err) {
 
59
                        toCreate = append(toCreate, path)
 
60
                } else {
 
61
                        break
 
62
                }
 
63
                path = filepath.Dir(path)
 
64
        }
 
65
 
 
66
        if err := os.MkdirAll(dir, perm); err != nil {
 
67
                return err
 
68
        }
 
69
        return ChownToUser(toCreate...)
 
70
}
 
71
 
 
72
// ChownToUser will attempt to change the ownership of all the paths
 
73
// to the user returned by the SudoCallerIds method.  Ownership change
 
74
// will only be attempted if we are running as root.
 
75
func ChownToUser(paths ...string) error {
 
76
        if !CheckIfRoot() {
 
77
                return nil
 
78
        }
 
79
        uid, gid, err := SudoCallerIds()
 
80
        if err != nil {
 
81
                return err
 
82
        }
 
83
        for _, path := range paths {
 
84
                if err := os.Chown(path, uid, gid); err != nil {
 
85
                        return err
 
86
                }
 
87
        }
 
88
        return nil
 
89
}