Example #1
0
func (gw *gameWalker) visit(path string, f os.FileInfo, err error) error {
	if f == nil || f.Name() == ".DS_Store" {
		return nil
	}
	if f.IsDir() {
		return nil
	}

	hh, err := HashesForFile(path)
	if err != nil {
		return err
	}

	romName, err := filepath.Rel(gw.gamepath, path)
	if err != nil {
		return err
	}

	rom := new(types.Rom)
	rom.Name = romName
	rom.Size = f.Size()
	rom.Crc = hh.Crc
	rom.Md5 = hh.Md5
	rom.Sha1 = hh.Sha1

	gw.game.Roms = append(gw.game.Roms, rom)

	return nil
}
Example #2
0
func fixHashes(rom *types.Rom) {
	if rom.Crc != nil {
		strV := string(rom.Crc)
		if strV != "" {
			v, err := hex.DecodeString(string(rom.Crc))
			if err != nil {
				rom.Crc = nil
			}
			rom.Crc = v
		} else {
			rom.Crc = nil
		}
	}
	if rom.Md5 != nil {
		strV := string(rom.Md5)
		if strV != "" {
			v, err := hex.DecodeString(string(rom.Md5))
			if err != nil {
				rom.Md5 = nil
			}
			rom.Md5 = v
		} else {
			rom.Md5 = nil
		}
	}
	if rom.Sha1 != nil {
		strV := string(rom.Sha1)
		if strV != "" {
			v, err := hex.DecodeString(string(rom.Sha1))
			if err != nil {
				rom.Sha1 = nil
			}
			rom.Sha1 = v
		} else {
			rom.Sha1 = nil
		}
	}
}
Example #3
0
func (rs *RombaService) lookup(cmd *commander.Command, args []string) error {
	for _, arg := range args {
		hash, err := hex.DecodeString(arg)
		if err != nil {
			return err
		}

		if len(hash) == sha1.Size {
			dat, err := rs.romDB.GetDat(hash)
			if err != nil {
				return err
			}

			if dat != nil {
				fmt.Fprintf(cmd.Stdout, "dat with sha1 %s = %s\n", arg, types.PrintShortDat(dat))
			}
		}

		r := new(types.Rom)
		switch len(hash) {
		case md5.Size:
			r.Md5 = hash
		case crc32.Size:
			r.Crc = hash
		case sha1.Size:
			r.Sha1 = hash
		default:
			return fmt.Errorf("found unknown hash size: %d", len(hash))
		}

		dats, err := rs.romDB.DatsForRom(r)
		if err != nil {
			return err
		}

		err = rs.romDB.CompleteRom(r)
		if err != nil {
			return err
		}

		if len(dats) > 0 {
			fmt.Fprintf(cmd.Stdout, "rom in %s\n", types.PrintRomInDats(dats))
		}
	}

	return nil
}
Example #4
0
func (rs *RombaService) lookupRom(cmd *commander.Command, r *types.Rom, outpath string) error {
	err := rs.romDB.CompleteRom(r)
	if err != nil {
		return err
	}

	if r.Sha1 != nil {
		sha1Str := hex.EncodeToString(r.Sha1)

		inDepot, hh, rompath, size, err := rs.depot.SHA1InDepot(sha1Str)
		if err != nil {
			return err
		}

		if inDepot {
			fmt.Fprintf(cmd.Stdout, "-----------------\n")
			fmt.Fprintf(cmd.Stdout, "rom file %s in depot\n", rompath)
			fmt.Fprintf(cmd.Stdout, "crc = %s\n", hex.EncodeToString(hh.Crc))
			fmt.Fprintf(cmd.Stdout, "md5 = %s\n", hex.EncodeToString(hh.Md5))
			fmt.Fprintf(cmd.Stdout, "size = %d\n", size)
			r.Crc = hh.Crc
			r.Md5 = hh.Md5

			if outpath != "" {
				worker.Cp(rompath, filepath.Join(outpath, filepath.Base(rompath)))
			}
		}
	}

	dats, err := rs.romDB.DatsForRom(r)
	if err != nil {
		return err
	}

	if len(dats) > 0 {
		fmt.Fprintf(cmd.Stdout, "-----------------\n")
		fmt.Fprintf(cmd.Stdout, "rom found in:\n")
		for _, dat := range dats {
			dn := dat.NarrowToRom(r)
			if dn != nil {
				fmt.Fprintf(cmd.Stdout, "%s\n", types.PrintDat(dn))
			}
		}
	}
	return nil
}
Example #5
0
File: main.go Project: sbinet/romba
func lookupByHash(hash []byte) (bool, error) {
	found := false
	if len(hash) == sha1.Size {
		dat, err := romDB.GetDat(hash)
		if err != nil {
			return false, err
		}

		if dat != nil {
			fmt.Printf("dat = %s\n", types.PrintDat(dat))
			found = true
		}
	}

	r := new(types.Rom)
	switch len(hash) {
	case md5.Size:
		r.Md5 = hash
	case crc32.Size:
		r.Crc = hash
	case sha1.Size:
		r.Sha1 = hash
	default:
		return false, fmt.Errorf("found unknown hash size: %d", len(hash))
	}

	dats, err := romDB.DatsForRom(r)
	if err != nil {
		return false, err
	}

	for _, dat := range dats {
		fmt.Printf("dat = %s\n", types.PrintDat(dat))
	}

	found = found || len(dats) > 0
	return found, nil
}
Example #6
0
func (w *archiveWorker) archive(ro readerOpener, name, path string, size int64, hh *Hashes, md5crcBuffer []byte) (int64, error) {
	r, err := ro()
	if err != nil {
		return 0, err
	}

	br := bufio.NewReader(r)

	err = hh.forReader(br)
	if err != nil {
		r.Close()
		return 0, err
	}
	err = r.Close()
	if err != nil {
		return 0, err
	}

	// if filestat size is different than size read then size read wins
	if size != hh.Size {
		size = hh.Size
	}

	copy(md5crcBuffer[0:md5.Size], hh.Md5)
	copy(md5crcBuffer[md5.Size:md5.Size+crc32.Size], hh.Crc)
	util.Int64ToBytes(size, md5crcBuffer[md5.Size+crc32.Size:])

	rom := new(types.Rom)
	rom.Crc = make([]byte, crc32.Size)
	rom.Md5 = make([]byte, md5.Size)
	rom.Sha1 = make([]byte, sha1.Size)
	copy(rom.Crc, hh.Crc)
	copy(rom.Md5, hh.Md5)
	copy(rom.Sha1, hh.Sha1)
	rom.Name = name
	rom.Size = size
	rom.Path = path

	if !w.pm.noDB {
		if w.pm.onlyneeded {
			dats, err := w.depot.romDB.DatsForRom(rom)
			if err != nil {
				return 0, err
			}

			if len(dats) == 0 {
				return 0, nil
			}
		}

		err = w.depot.romDB.IndexRom(rom)
		if err != nil {
			return 0, err
		}
	}

	sha1Hex := hex.EncodeToString(hh.Sha1)
	exists, _, err := w.depot.RomInDepot(sha1Hex)
	if err != nil {
		return 0, err
	}

	if exists {
		glog.V(4).Infof("%s already in depot, skipping %s/%s", sha1Hex, path, name)
		return 0, nil
	}

	estimatedCompressedSize := size / 5

	root, err := w.depot.reserveRoot(estimatedCompressedSize)
	if err != nil {
		return 0, err
	}

	outpath := pathFromSha1HexEncoding(w.depot.roots[root], sha1Hex, gzipSuffix)

	r, err = ro()
	if err != nil {
		return 0, err
	}
	defer r.Close()

	compressedSize, err := archive(outpath, r, md5crcBuffer)
	if err != nil {
		return 0, err
	}

	w.depot.adjustSize(root, compressedSize-estimatedCompressedSize)
	return compressedSize, nil
}
Example #7
0
func (rs *RombaService) lookup(cmd *commander.Command, args []string) error {
	size := cmd.Flag.Lookup("size").Value.Get().(int64)
	outpath := cmd.Flag.Lookup("out").Value.Get().(string)

	for _, arg := range args {
		fmt.Fprintf(cmd.Stdout, "----------------------------------------\n")
		fmt.Fprintf(cmd.Stdout, "key: %s\n", arg)

		if strings.HasPrefix(arg, "0x") {
			arg = arg[2:]
		}

		hash, err := hex.DecodeString(arg)
		if err != nil {
			return err
		}

		if len(hash) == sha1.Size {
			dat, err := rs.romDB.GetDat(hash)
			if err != nil {
				return err
			}

			if dat != nil {
				fmt.Fprintf(cmd.Stdout, "-----------------\n")
				fmt.Fprintf(cmd.Stdout, "dat with sha1 %s = %s\n", arg, types.PrintShortDat(dat))
			}
		}

		if size != -1 || len(hash) == sha1.Size {
			r := new(types.Rom)
			r.Size = size
			switch len(hash) {
			case md5.Size:
				r.Md5 = hash
			case crc32.Size:
				r.Crc = hash
			case sha1.Size:
				r.Sha1 = hash
			default:
				return fmt.Errorf("found unknown hash size: %d", len(hash))
			}

			err = rs.lookupRom(cmd, r, outpath)
			if err != nil {
				return err
			}

			fmt.Fprintf(cmd.Stdout, "-----------------\n")
			fmt.Fprintf(cmd.Stdout, "DebugGet:\n%s\n", rs.romDB.DebugGet(hash, size))
		} else {
			suffixes, err := rs.romDB.ResolveHash(hash)
			if err != nil {
				return err
			}

			for i := 0; i < len(suffixes); i += sha1.Size + 8 {
				r := new(types.Rom)
				r.Size = util.BytesToInt64(suffixes[i : i+8])
				switch len(hash) {
				case md5.Size:
					r.Md5 = hash
				case crc32.Size:
					r.Crc = hash
				default:
					return fmt.Errorf("found unknown hash size: %d", len(hash))
				}
				r.Sha1 = suffixes[i+8 : i+8+sha1.Size]

				err = rs.lookupRom(cmd, r, outpath)
				if err != nil {
					return err
				}
			}
		}
	}

	return nil
}
Example #8
0
func (w *archiveWorker) archive(ro readerOpener, root int, name, path string, size int64) (int64, error) {
	r, err := ro()
	if err != nil {
		return 0, err
	}

	br := bufio.NewReader(r)

	err = w.hh.forReader(br)
	if err != nil {
		r.Close()
		return 0, err
	}
	err = r.Close()
	if err != nil {
		return 0, err
	}

	copy(w.md5crcBuffer[0:md5.Size], w.hh.Md5)
	copy(w.md5crcBuffer[md5.Size:], w.hh.Crc)

	rom := new(types.Rom)
	rom.Crc = make([]byte, crc32.Size)
	rom.Md5 = make([]byte, md5.Size)
	rom.Sha1 = make([]byte, sha1.Size)
	copy(rom.Crc, w.hh.Crc)
	copy(rom.Md5, w.hh.Md5)
	copy(rom.Sha1, w.hh.Sha1)
	rom.Name = name
	rom.Size = size
	rom.Path = path

	if w.pm.onlyneeded {
		dats, err := w.depot.romDB.DatsForRom(rom)
		if err != nil {
			return 0, err
		}

		needed := false

		for _, dat := range dats {
			if !dat.Artificial {
				needed = true
				break
			}
		}
		if !needed {
			return 0, nil
		}
	}

	err = w.depot.romDB.IndexRom(rom)
	if err != nil {
		return 0, err
	}

	sha1Hex := hex.EncodeToString(w.hh.Sha1)

	outpath := pathFromSha1HexEncoding(w.depot.roots[root], sha1Hex, gzipSuffix)

	exists, err := PathExists(outpath)
	if err != nil {
		return 0, err
	}

	if exists {
		return 0, nil
	}

	r, err = ro()
	if err != nil {
		return 0, err
	}
	defer r.Close()

	compressedSize, err := archive(outpath, r, w.md5crcBuffer)
	if err != nil {
		return 0, err
	}

	w.depot.adjustSize(root, compressedSize)
	return compressedSize, nil
}