Ejemplo n.º 1
0
func (p *DmSrcProvider) getNextMsg() ([][]byte, error) {
	var files [][]byte
	var err error
	var lastFile string
	var fileList []string
	var newFileList []string

	for path, lastRecordFile := range p.lastFileMap {
		lastFile, err = common.ReadFile(lastRecordFile)
		if err != nil {
			return files, err
		}
		lastFile = strings.TrimSpace(lastFile)

		fileList, err = p.getFileList(path)
		if err != nil {
			return files, err
		}

		if newFileList, err = p.getNewFile(lastFile, fileList); err != nil {
			return files, err
		}

		if len(newFileList) <= 0 {
			continue
		}

		// write last file
		for _, newFile := range newFileList {
			err = common.WriteFile(lastRecordFile, newFile)
			if err != nil {
				return files, err
			}
			files = append(files, []byte(path+"/"+newFile))
		}
	}
	return files, nil
}
Ejemplo n.º 2
0
func (p *DmSrcProvider) initMonitorPaths(paths []string) error {

	curDir, err := os.Getwd()
	if err != nil {
		return err
	}
	inodeMap := make(map[uint64]int)
	for _, path := range paths {
		path = strings.TrimRight(path, "/")
		if !strings.HasPrefix(path, "/") { // in case it is a relative dir
			path = curDir + "/" + path
		}

		// remove the duplicated dirs according to inode
		var stat syscall.Stat_t
		if err = syscall.Stat(path, &stat); err != nil {
			return err
		}
		if _, ok := inodeMap[stat.Ino]; ok {
			continue
		}

		fi, err := os.Stat(path)
		if (err != nil && !os.IsExist(err)) || !fi.IsDir() {
			return errors.New(path + " data dir not valid")
		}
		p.lastFileMap[path] = p.getLastRecordFileName(path)
		_, err = os.Stat(p.lastFileMap[path])
		if err != nil && !os.IsExist(err) {
			if err := common.WriteFile(p.lastFileMap[path], "start"); err != nil {
				return err
			}
		}
	}
	return nil
}