Esempio n. 1
0
func main() {
	defer func() {
		if r := recover(); r != nil {
			err := r.(error)

			fmt.Printf("ERROR: %s\n", err.Error())
			os.Exit(1)
		}
	}()

	var catalogFilepath string
	var hashAlgorithm string
	var relPath string
	var showExtended bool

	o := readOptions()

	catalogFilepath = o.CatalogFilepath
	hashAlgorithm = o.HashAlgorithm
	relPath = o.RelPath
	showExtended = o.ShowExtended

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

	pfinternal.ConfigureRootLogger()

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

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

	defer cr.Close()

	rr, err := cr.ResolvePath(&relPath)
	if err != nil {
		panic(err)
	}

	if showExtended == true {
		fmt.Printf("Path name: [%s]\n", rr.RelPath)
		fmt.Printf("Path ID: (%d)\n", rr.PathId)
		fmt.Printf("File name: [%s]\n", rr.Filename)
		fmt.Printf("File ID: [%d]\n", rr.FileId)
		fmt.Printf("Hash: [%s]\n", rr.Hash)
	} else {
		fmt.Println(rr.Hash)
	}
}
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)
}