func Dir2Dat(dat *types.Dat, srcpath, outpath string) error { glog.Infof("composing DAT from source %s into output dir %s", srcpath, outpath) fis, err := ioutil.ReadDir(srcpath) if err != nil { return err } for _, fi := range fis { if fi.IsDir() { game, err := populateGame(srcpath, fi) if err != nil { return err } dat.Games = append(dat.Games, game) } } outfilename := filepath.Join(outpath, dat.Name+datSuffix) outf, err := os.Create(outfilename) if err != nil { return err } defer outf.Close() outbuf := bufio.NewWriter(outf) defer outbuf.Flush() return types.ComposeDat(dat, outbuf) }
func Dedup(d *types.Dat, deduper Deduper) (*types.Dat, error) { dc := new(types.Dat) dc.CopyHeader(d) for _, g := range d.Games { gc := new(types.Game) gc.CopyHeader(g) for _, r := range g.Roms { if !r.Valid() { continue } seen, err := deduper.Seen(r) if err != nil { return nil, err } if !seen { gc.Roms = append(gc.Roms, r) err = deduper.Declare(r) if err != nil { return nil, err } } } if len(gc.Roms) > 0 { dc.Games = append(dc.Games, gc) } } if len(dc.Games) > 0 { return dc, nil } return nil, nil }
func (kvb *kvBatch) IndexRom(rom *types.Rom) error { //glog.Infof("indexing rom %s", rom.Name) dats, err := kvb.db.DatsForRom(rom) if err != nil { return err } if len(dats) > 0 { if rom.Crc != nil && rom.Sha1 != nil { //glog.Infof("declaring crc %s -> sha1 %s ampping", hex.EncodeToString(rom.Crc), hex.EncodeToString(rom.Sha1)) err = kvb.crcsha1Batch.Append(rom.Crc, rom.Sha1) if err != nil { return err } kvb.size += int64(sha1.Size) } if rom.Md5 != nil && rom.Sha1 != nil { //glog.Infof("declaring md5 %s -> sha1 %s ampping", hex.EncodeToString(rom.Md5), hex.EncodeToString(rom.Sha1)) err = kvb.md5sha1Batch.Append(rom.Md5, rom.Sha1) if err != nil { return err } kvb.size += int64(sha1.Size) } return nil } if rom.Sha1 == nil { glog.Warningf("indexing rom %s with missing SHA1", rom.Name) } dat := new(types.Dat) dat.Artificial = true dat.Generation = kvb.db.generation dat.Name = fmt.Sprintf("Artificial Dat for %s", rom.Name) dat.Path = rom.Path game := new(types.Game) game.Roms = []*types.Rom{rom} dat.Games = []*types.Game{game} var buf bytes.Buffer gobEncoder := gob.NewEncoder(&buf) err = gobEncoder.Encode(dat) if err != nil { return err } hh := sha1.New() _, err = io.Copy(hh, &buf) if err != nil { return err } return kvb.IndexDat(dat, hh.Sum(nil)) }
func (depot *Depot) BuildDat(dat *types.Dat, outpath string) (bool, error) { datPath := filepath.Join(outpath, dat.Name) err := os.Mkdir(datPath, 0777) if err != nil { return false, err } var fixDat *types.Dat for _, game := range dat.Games { fixGame, err := depot.buildGame(game, datPath) if err != nil { return false, err } if fixGame != nil { if fixDat == nil { fixDat = new(types.Dat) fixDat.Name = dat.Name fixDat.Description = dat.Description fixDat.Path = dat.Path } fixDat.Games = append(fixDat.Games, fixGame) } } if fixDat != nil { fixDatPath := filepath.Join(outpath, fixPrefix+dat.Name+datSuffix) fixFile, err := os.Create(fixDatPath) if err != nil { return false, err } defer fixFile.Close() fixWriter := bufio.NewWriter(fixFile) defer fixWriter.Flush() err = types.ComposeDat(fixDat, fixWriter) if err != nil { return false, err } } return fixDat == nil, nil }