コード例 #1
0
ファイル: main.go プロジェクト: uwedeportivo/torrentzip
func main() {
	flag.Usage = usage

	help := flag.Bool("help", false, "show this message")
	version := flag.Bool("version", false, "show version")

	outpath := flag.String("out", "", "zip file")

	flag.Parse()

	if *help {
		flag.Usage()
		os.Exit(0)
	}

	if *version {
		fmt.Fprintf(os.Stdout, "%s version %s, Copyright (c) 2013 Uwe Hoffmann. All rights reserved.\n", os.Args[0], versionStr)
		os.Exit(0)
	}

	if *outpath == "" {
		flag.Usage()
		os.Exit(0)
	}

	file, err := os.Create(*outpath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "creating zip file %s failed: %v\n", *outpath, err)
		os.Exit(1)
	}
	defer file.Close()

	bf := bufio.NewWriter(file)
	defer bf.Flush()

	hh := sha1.New()

	zw, err := torrentzip.NewWriter(io.MultiWriter(bf, hh))
	if err != nil {
		fmt.Fprintf(os.Stderr, "creating zip file writer %s failed: %v\n", *outpath, err)
		os.Exit(1)
	}

	progress := pb.New(len(flag.Args()) + 1)
	progress.RefreshRate = 5 * time.Second
	progress.ShowCounters = false
	progress.Start()

	pwdName, err := os.Getwd()
	if err != nil {
		fmt.Fprintf(os.Stderr, "cannot establish current working directory %v\n", err)
		os.Exit(1)
	}

	av := &adderVisitor{
		zw:      zw,
		pwdName: pwdName,
	}

	for _, name := range flag.Args() {
		if filepath.IsAbs(name) {
			fmt.Fprintf(os.Stderr, "cannot add absolute paths to a zip file:  %s\n", name)
			os.Exit(1)
		}

		av.skipFirstLevel = strings.HasSuffix(name, string(filepath.Separator))

		err = filepath.Walk(name, av.visit)
		if err != nil {
			fmt.Fprintf(os.Stderr, "adding files from %s failed: %v\n", name, err)
			os.Exit(1)
		}

		progress.Increment()
	}

	err = zw.Close()
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to close zip file %s: %v\n", *outpath, err)
		os.Exit(1)
	}

	progress.Increment()

	progress.Finish()

	fmt.Fprintf(os.Stdout, "finished creating zip file: %s\n", *outpath)
	fmt.Fprintf(os.Stdout, "sha1 of created zip file: %s\n", hex.EncodeToString(hh.Sum(nil)))
}
コード例 #2
0
ファイル: main.go プロジェクト: uwedeportivo/torrentzip
func testZip(path string, goldensha1 []byte) error {
	r, err := czip.OpenReader(path)
	if err != nil {
		return err
	}
	defer r.Close()

	w, err := ioutil.TempFile(os.TempDir(), "testtorrentzip")
	if err != nil {
		return err
	}

	hh := sha1.New()

	zw, err := torrentzip.NewWriter(io.MultiWriter(w, hh))
	if err != nil {
		return err
	}

	for _, fh := range r.File {
		cw, err := zw.Create(fh.Name)
		if err != nil {
			return err
		}
		cr, err := fh.Open()
		if err != nil {
			return err
		}

		_, err = io.Copy(cw, cr)
		if err != nil {
			return err
		}

		err = cr.Close()
		if err != nil {
			return err
		}
	}

	err = zw.Close()
	if err != nil {
		return err
	}

	err = w.Close()
	if err != nil {
		return err
	}

	err = os.Remove(w.Name())
	if err != nil {
		return err
	}

	testsha1 := hh.Sum(nil)

	if !bytes.Equal(testsha1, goldensha1) {
		return fmt.Errorf("produced torrentzip for %s differs from golden", path)
	}

	return nil
}
コード例 #3
0
ファイル: depot.go プロジェクト: sbinet/romba
func (depot *Depot) buildGame(game *types.Game, datPath string) (*types.Game, error) {
	gamePath := filepath.Join(datPath, game.Name+zipSuffix)
	gameFile, err := os.Create(gamePath)
	if err != nil {
		return nil, err
	}
	defer gameFile.Close()

	gameTorrent, err := torrentzip.NewWriter(gameFile)
	if err != nil {
		return nil, err
	}
	defer gameTorrent.Close()

	var fixGame *types.Game

	for _, rom := range game.Roms {
		if rom.Sha1 == nil {
			glog.Warningf("game %s has rom with missing SHA1 %s", game.Name, rom.Name)
			if fixGame == nil {
				fixGame = new(types.Game)
				fixGame.Name = game.Name
				fixGame.Description = game.Description
			}

			fixGame.Roms = append(fixGame.Roms, rom)
			continue
		}

		romGZ, err := depot.OpenRomGZ(rom)
		if err != nil {
			return nil, err
		}

		if romGZ == nil {
			glog.Warningf("game %s has missing rom %s (sha1 %s)", game.Name, rom.Name, hex.EncodeToString(rom.Sha1))
			if fixGame == nil {
				fixGame = new(types.Game)
				fixGame.Name = game.Name
				fixGame.Description = game.Description
			}

			fixGame.Roms = append(fixGame.Roms, rom)
			continue
		}

		src, err := cgzip.NewReader(romGZ)
		if err != nil {
			return nil, err
		}

		dst, err := gameTorrent.Create(rom.Name)
		if err != nil {
			return nil, err
		}

		_, err = io.Copy(dst, src)
		if err != nil {
			return nil, err
		}

		src.Close()
		romGZ.Close()
	}
	return fixGame, nil
}