Beispiel #1
0
func (d *Directory) ensurePresent(result *task.Result) (*task.Result, error) {
	fi, err := os.Lstat(d.Path)
	if err != nil {
		if isNoSuchFileOrDirectory(err) {
			err = os.MkdirAll(d.Path, d.Mode)
			if err == nil {
				result.Changed = true
			}
		}
	} else {
		if fi.IsDir() {
			oldMode := fi.Mode() & os.ModePerm
			if oldMode == d.Mode {
				result.Skipped = true
			} else {
				result.Extra["old_mode"] = fmt.Sprintf("%o", oldMode)
				err = os.Chmod(d.Path, d.Mode)
				if err == nil {
					result.Changed = true
				}
			}
		} else {
			err = errors.New("something not directory exists or permission denied")
		}
	}
	return result, err
}
Beispiel #2
0
func (d *Directory) ensureAbsent(result *task.Result) (*task.Result, error) {
	fi, err := os.Lstat(d.Path)
	if err != nil {
		if isNoSuchFileOrDirectory(err) {
			result.Skipped = true
			err = nil
		}
	} else {
		if fi.IsDir() {
			err = os.RemoveAll(d.Path)
			if err == nil {
				result.Changed = true
			}
		} else {
			err = errors.New("something not directory exists or permission denied")
		}
	}
	return result, err
}