Example #1
0
// ModTimeAfter checks if the provided file modification time is older than the one passed in as paramter
// It returns error if os.Stat returns error
func ModTimeAfter(f resource.Filer, mtime time.Time) error {
	fi, err := f.Info()
	if err != nil {
		return err
	}
	if fi.ModTime().After(mtime) {
		return nil
	}
	return fmt.Errorf("%s modification time is bigger than %s", f, mtime)
}
Example #2
0
// Size checks if the provided file byte size is the same as the one passed in as paramter
// It returns error if os.Stat returns error
func Size(f resource.Filer, size int64) error {
	fi, err := f.Info()
	if err != nil {
		return err
	}
	if fi.Size() == size {
		return nil
	}
	return fmt.Errorf("%s size is different from %d", f, size)
}
Example #3
0
// LinksTo checks if the provided file is a symlink which links to path
// It returs error if the link can't be read
func LinksTo(f resource.Filer, path string) error {
	dst, err := os.Readlink(path)
	if err != nil {
		return err
	}
	if dst == f.Path() {
		return nil
	}
	return fmt.Errorf("%s does not link to %s", f, path)
}
Example #4
0
// IsGrupedInto checks if the provided file is owned by groupname group
// It returns an error if os.Stat returns error
func IsGrupedInto(f resource.Filer, groupname string) error {
	fi, err := f.Info()
	if err != nil {
		return err
	}
	gid, err := utils.RoleToID("group", groupname)
	if err != nil {
		return err
	}
	return isTrueOrError(fi.Sys().(*syscall.Stat_t).Gid == uint32(gid), fmt.Errorf("%s file is not grouped to %s", f, groupname))
}
Example #5
0
// IsOwnedBy checks if the provided file is owned by username user
// It returns an error if os.Stat returns error
func IsOwnedBy(f resource.Filer, username string) error {
	fi, err := f.Info()
	if err != nil {
		return nil
	}
	uid, err := utils.RoleToID("user", username)
	if err != nil {
		return err
	}
	return isTrueOrError(fi.Sys().(*syscall.Stat_t).Uid == uint32(uid), fmt.Errorf("%s file is not owned by %s", f, username))
}
Example #6
0
// Sha256 checks if the provided file sha256 checksum is the same as the one passed in as paramter
// It returs error if the provided file can't be open
func Sha256(f resource.Filer, sum string) error {
	return withOsFile(f.Path(), func(file *os.File) error {
		hasher := sha256.New()
		if _, err := io.Copy(hasher, file); err != nil {
			return nil
		}
		if sum == hex.EncodeToString(hasher.Sum(nil)) {
			return nil
		}
		return fmt.Errorf("%s sha256 does not equal to %s", f, sum)
	})
}
Example #7
0
// IsMode checks if the provided file has the same mode as the one passed in via paramter
// It returns error if os.Stat returns error
func IsMode(f resource.Filer, mode os.FileMode) error {
	fi, err := f.Info()
	if err != nil {
		return err
	}

	if verboseMode, ok := verboseNames[mode]; ok {
		err = fmt.Errorf("%s file is not a %s", f, verboseMode)
	} else {
		err = fmt.Errorf("%s file is not mode %v", f, mode)
	}
	return isTrueOrError(fi.Mode()&mode != 0, err)
}
Example #8
0
// Contains checks if the provided file content can be matched with any of the regexps
// passed in as paramter. It returs error if the provided file can't be open
func Contains(f resource.Filer, contents ...*regexp.Regexp) error {
	return withOsFile(f.Path(), func(file *os.File) error {
		scanner := bufio.NewScanner(file)
		for scanner.Scan() {
			for _, content := range contents {
				if content.Match(scanner.Bytes()) {
					return nil
				}
			}
		}
		return fmt.Errorf("%s does not match any provided regular expression", f)
	})
}