// 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) }
// 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) }) }
// 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) }) }