示例#1
0
func TestEnsureDirectoryExists(t *testing.T) {
	Convey("Given a unique dir name, a user name and file permissions", t, func() {
		currentUser, err := user.Current()
		So(err, ShouldBeNil)

		dirname, err := file.UniqueName("/tmp", "filetests")
		So(err, ShouldBeNil)

		perms := os.FileMode(0600)

		userID, _ := strconv.Atoi(currentUser.Uid)
		groupID, _ := strconv.Atoi(currentUser.Gid)

		Convey("It should create a new directory with the given permissions if it doesn't exist", func() {
			err := file.EnsureDirectoryExists(dirname, perms, userID, groupID)
			So(err, ShouldBeNil)
			defer os.Remove(dirname)

			fi, err := os.Lstat(dirname)
			So(err, ShouldBeNil)

			So(fi.IsDir(), ShouldBeTrue)

			uid := strconv.Itoa(int(fi.Sys().(*syscall.Stat_t).Uid))
			So(currentUser.Uid, ShouldEqual, uid)

			gid := strconv.Itoa(int(fi.Sys().(*syscall.Stat_t).Gid))
			So(currentUser.Gid, ShouldEqual, gid)
		})

		Convey("It should change the ownership and the file permissions if it exists", func() {
			t.SkipNow()
			err := file.EnsureDirectoryExists(dirname, perms, userID, groupID)
			So(err, ShouldBeNil)
			defer os.Remove(dirname)

			fi, err := os.Lstat(dirname)
			So(err, ShouldBeNil)

			So(fi.IsDir(), ShouldBeTrue)

			uid := strconv.Itoa(int(fi.Sys().(*syscall.Stat_t).Uid))
			So(currentUser.Uid, ShouldEqual, uid)

			gid := strconv.Itoa(int(fi.Sys().(*syscall.Stat_t).Gid))
			So(currentUser.Gid, ShouldEqual, gid)
		})
	})
}
示例#2
0
// InitializeFor checks whether .ssh directory exists in the given user's home
// directory and creates the directory if it doesn't exist.
func InitializeFor(usr *user.User) error {
	userSSHDirPath := filepath.Join(usr.HomeDir, DirPath)
	userAuthorizedKeysPath := filepath.Join(usr.HomeDir, AuthorizedKeysPath)

	userID, err := strconv.Atoi(usr.Uid)
	if err != nil {
		return err
	}

	groupID, err := strconv.Atoi(usr.Gid)
	if err != nil {
		return err
	}

	err = file.EnsureDirectoryExists(userSSHDirPath, 0700, userID, groupID)
	if err != nil {
		return err
	}

	return file.EnsureExists(userAuthorizedKeysPath, 0600, userID, groupID)
}
示例#3
0
文件: ssh.go 项目: rtnpro/flamingo
func InitializeFor(owner *user.User) error {
	userSSHDirPath := owner.HomeDir + "/" + SSHDirPath
	userAuthorizedKeysPath := owner.HomeDir + "/" + AuthorizedKeysPath

	userID, err := strconv.Atoi(owner.Uid)
	if err != nil {
		return err
	}

	groupID, err := strconv.Atoi(owner.Gid)
	if err != nil {
		return err
	}

	err = file.EnsureDirectoryExists(userSSHDirPath, 0700, userID, groupID)
	if err != nil {
		return err
	}

	return file.EnsureExists(userAuthorizedKeysPath, 0600, userID, groupID)
}