Esempio n. 1
0
func recordChanges(reportFilename string, reportingChannel <-chan *pfinternal.ChangeEvent, reportingQuit <-chan bool) {
	l := pfinternal.NewLogger("recordChanges")

	var f *os.File
	if reportFilename == "-" {
		f = os.Stderr
	} else {
		f, err := os.Create(reportFilename)
		if err != nil {
			panic(err)
		}

		defer f.Close()
	}

	l.Debug("Reporter running.")

	for {
		select {
		case change := <-reportingChannel:
			changeTypeName := pfinternal.UpdateTypeName(change.ChangeType)
			entityTypeName := pfinternal.EntityTypeName(change.EntityType)

			l.Debug("Catalog change.", "EntityType", entityTypeName, "ChangeType", changeTypeName, "RelPath", change.RelPath)

			var effectiveRelPath string
			if change.EntityType == pfinternal.EntityTypePath && change.RelPath == "" {
				effectiveRelPath = "."
			} else {
				effectiveRelPath = change.RelPath
			}

			f.WriteString(changeTypeName)
			f.WriteString(" ")
			f.WriteString(entityTypeName)
			f.WriteString(" ")
			f.WriteString(effectiveRelPath)
			f.WriteString("\n")

		case <-reportingQuit:
			return
		}
	}
}
Esempio n. 2
0
func main() {
	defer func() {
		if r := recover(); r != nil {
			os.Exit(1)
		}
	}()

	var scanPath string
	var catalogFilepath string
	var hashAlgorithm string
	var allowUpdates bool
	var reportFilename string
	var profileFilename string

	o := readOptions()

	scanPath = o.ScanPath
	catalogFilepath = o.CatalogFilepath
	hashAlgorithm = o.HashAlgorithm
	allowUpdates = o.NoUpdates == false
	reportFilename = o.ReportFilename
	profileFilename = o.ProfileFilename

	var reportingDataChannel chan *pfinternal.ChangeEvent = nil
	var reportingQuitChannel chan bool = nil
	var c *pfinternal.Catalog
	var err error

	if o.ShowDebugLogging == true {
		pfinternal.SetDebugLogging()
	}

	l := pfinternal.NewLogger("pfhash")
	pfinternal.ConfigureRootLogger()

	if profileFilename != "" {
		l.Debug("Profiling enabled.")

		f, err := os.Create(profileFilename)
		if err != nil {
			panic(err)
		}

		//        runtime.SetCPUProfileRate(100)
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if reportFilename != "" {
		reportingDataChannel = make(chan *pfinternal.ChangeEvent, 1000)
		reportingQuitChannel = make(chan bool)

		go recordChanges(reportFilename, reportingDataChannel, reportingQuitChannel)
	}

	p := pfinternal.NewPath(&hashAlgorithm, reportingDataChannel)

	cr, err := pfinternal.NewCatalogResource(&catalogFilepath, &hashAlgorithm)
	if err != nil {
		panic(err)
	}

	err = cr.Open()
	if err != nil {
		panic(err)
	}

	defer cr.Close()

	c, err = pfinternal.NewCatalog(cr, &scanPath, allowUpdates, &hashAlgorithm, reportingDataChannel)
	if err != nil {
		panic(err)
	}

	err = c.Open()
	if err != nil {
		panic(err)
	}

	defer c.Close()

	l.Debug("Generating root hash.")

	relPath := ""
	hash, err := p.GeneratePathHash(&scanPath, &relPath, c)
	if err != nil {
		panic(err)
	}

	c.Cleanup()

	if reportFilename != "" {
		reportingQuitChannel <- true
	}

	fmt.Printf("%s\n", hash)
}