Example #1
0
func cpFile(src, dst string) error {
	s, err := os.Open(src)
	if err != nil {
		log.Warnln("sensor: monitor - cp - error opening source file =>", src)
		return err
	}
	defer s.Close()

	dstDir := utils.FileDir(dst)
	err = os.MkdirAll(dstDir, 0777)
	if err != nil {
		log.Warnln("sensor: monitor - dir error =>", err)
	}

	d, err := os.Create(dst)
	if err != nil {
		log.Warnln("sensor: monitor - cp - error opening dst file =>", dst)
		return err
	}

	srcFileInfo, err := s.Stat()
	if err == nil {
		d.Chmod(srcFileInfo.Mode())
	}

	if _, err := io.Copy(d, s); err != nil {
		d.Close()
		return err
	}
	return d.Close()
}
Example #2
0
func (p *artifactStore) saveArtifacts() {
	var excludePaths map[string]bool
	var includePaths map[string]bool

	preparePaths := func(pathList []string) map[string]bool {
		if len(pathList) < 1 {
			return nil
		}

		paths := map[string]bool{}
		for _, pathValue := range pathList {
			pathInfo, err := os.Stat(pathValue)
			if err != nil {
				log.Debug("saveArtifacts.preparePaths(): skipping path = ", pathValue)
				continue
			}

			if pathInfo.IsDir() {
				paths[pathValue] = true
			} else {
				paths[pathValue] = false
			}
		}

		return paths
	}

	includePaths = preparePaths(p.cmd.Includes)
	excludePaths = preparePaths(p.cmd.Excludes)
	log.Debugf("includePaths: %+v\n", includePaths)
	log.Debugf("excludePaths: %+v\n", excludePaths)

	//TODO: use exludePaths to filter discovered files
	for fileName := range p.fileMap {
		filePath := fmt.Sprintf("%s/files%s", p.storeLocation, fileName)
		log.Debug("saveArtifacts - saving file data => ", filePath)
		err := cpFile(fileName, filePath)
		if err != nil {
			log.Warn("saveArtifacts - error saving file => ", err)
		}
	}

	//TODO: use exludePaths to filter discovered links
	for linkName, linkProps := range p.linkMap {
		linkPath := fmt.Sprintf("%s/files%s", p.storeLocation, linkName)
		linkDir := utils.FileDir(linkPath)
		err := os.MkdirAll(linkDir, 0777)
		if err != nil {
			log.Warn("saveArtifacts - dir error => ", err)
			continue
		}
		err = os.Symlink(linkProps.LinkRef, linkPath)
		if err != nil {
			log.Warn("saveArtifacts - symlink create error ==> ", err)
		}
	}

	for fileName := range p.fileMap {
		filePath := fmt.Sprintf("%s/files%s", p.storeLocation, fileName)

		err := fixPy3CacheFile(fileName, filePath)
		if err != nil {
			log.Warn("saveArtifacts - error fixing py3 cache file => ", err)
		}
	}

	//TODO: use exludePaths to filter included paths
	for inPath, isDir := range includePaths {
		dstPath := fmt.Sprintf("%s/files%s", p.storeLocation, inPath)
		if isDir {
			err, errs := utils.CopyDir(inPath, dstPath, true, true, nil, nil, nil)
			if err != nil {
				log.Warnf("CopyDir(%v,%v) error: %v\n", inPath, dstPath, err)
			}

			if len(errs) > 0 {
				log.Warnf("CopyDir(%v,%v) copy errors: %+v\n", inPath, dstPath, errs)
			}
		} else {
			if err := utils.CopyFile(inPath, dstPath, true); err != nil {
				log.Warnf("CopyFile(%v,%v) error: %v\n", inPath, dstPath, err)
			}
		}
	}
}