user/user.go
func (u *User) Add() (uid int, err error) {
	loadConfig()

	user, err := LookupUser(u.Name)
	if err != nil {
		if _, ok := err.(NoFoundError); !ok {
			return
		}
	}
	if user != nil {
		return 0, ErrUserExist
	}

	if u.Name == "" {
		return 0, RequiredError("Name")
	}
	if u.Dir == "" {
		return 0, RequiredError("Dir")
	}
	if u.Dir == config.useradd.HOME {
		return 0, HomeError(config.useradd.HOME)
	}
	if u.Shell == "" {
		return 0, RequiredError("Shell")
	}

	var db *dbfile
	if u.UID < 0 {
		db, uid, err = nextUID(u.addSystemUser)
		if err != nil {
			db.close()
			return 0, err
		}
		u.UID = uid
	} else {
		db, err = openDBFile(_USER_FILE, os.O_WRONLY|os.O_APPEND)
		if err != nil {
			return 0, err
		}

		// Check if Id is unique.
		_, err = LookupUID(u.UID)
		if err == nil {
			return 0, IdUsedError(u.UID)
		} else if _, ok := err.(NoFoundError); !ok {
			return 0, err
		}
	}

	u.password = "x"

	_, err = db.file.WriteString(u.String())
	err2 := db.close()
	if err2 != nil && err == nil {
		err = err2
	}
	return
}
func (u *User) Add() (uid int, err error) {
	loadConfig()

	user, err := LookupUser(u.Name)
	if err != nil {
		if _, ok := err.(NoFoundError); !ok {
			return
		}
	}
	if user != nil {
		return 0, ErrUserExist
	}

	if u.Name == "" {
		return 0, RequiredError("Name")
	}
	if u.Dir == "" {
		return 0, RequiredError("Dir")
	}
	if u.Dir == config.useradd.HOME {
		return 0, HomeError(config.useradd.HOME)
	}
	if u.Shell == "" {
		return 0, RequiredError("Shell")
	}

	var db *dbfile
	if u.UID < 0 {
		db, uid, err = nextUID(u.addSystemUser)
		if err != nil {
			db.close()
			return 0, err
		}
		u.UID = uid
	} else {
		db, err = openDBFile(_USER_FILE, os.O_WRONLY|os.O_APPEND)
		if err != nil {
			return 0, err
		}

		// Check if Id is unique.
		_, err = LookupUID(u.UID)
		if err == nil {
			return 0, IdUsedError(u.UID)
		} else if _, ok := err.(NoFoundError); !ok {
			return 0, err
		}
	}

	u.password = "x"

	_, err = db.file.WriteString(u.String())
	err2 := db.close()
	if err2 != nil && err == nil {
		err = err2
	}
	return
}
// Add adds a new user.
// Whether UID is < 0, it will choose the first id available in the range set
// in the system configuration.