func doVerify(signaturePath string, dir string, woundsPath string, healPath string) error { if woundsPath == "" { if healPath == "" { comm.Opf("Verifying %s", dir) } else { comm.Opf("Verifying %s, healing as we go", dir) } } else { if healPath == "" { comm.Opf("Verifying %s, writing wounds to %s", dir, woundsPath) } else { comm.Dief("Options --wounds and --heal cannot be used at the same time") } } startTime := time.Now() signatureReader, err := eos.Open(signaturePath) if err != nil { return errors.Wrap(err, 1) } defer signatureReader.Close() signature, err := pwr.ReadSignature(signatureReader) if err != nil { return errors.Wrap(err, 1) } vc := &pwr.ValidatorContext{ Consumer: comm.NewStateConsumer(), WoundsPath: woundsPath, HealPath: healPath, } comm.StartProgressWithTotalBytes(signature.Container.Size) err = vc.Validate(dir, signature) if err != nil { return errors.Wrap(err, 1) } comm.EndProgress() prettySize := humanize.IBytes(uint64(signature.Container.Size)) perSecond := humanize.IBytes(uint64(float64(signature.Container.Size) / time.Since(startTime).Seconds())) comm.Statf("%s (%s) @ %s/s\n", prettySize, signature.Container.Stats(), perSecond) if vc.WoundsConsumer.HasWounds() { if healer, ok := vc.WoundsConsumer.(pwr.Healer); ok { comm.Statf("%s corrupted data found, %s healed", humanize.IBytes(uint64(vc.WoundsConsumer.TotalCorrupted())), humanize.IBytes(uint64(healer.TotalHealed()))) } else { comm.Dief("%s corrupted data found", humanize.IBytes(uint64(vc.WoundsConsumer.TotalCorrupted()))) } } return nil }
func wipe(path string) { // why have retry logic built into wipe? sometimes when uninstalling // games on windows, the os will randomly return I/O errors, retrying // usually helps. attempt := 0 sleepPatterns := []time.Duration{ time.Millisecond * 200, time.Millisecond * 800, time.Millisecond * 1600, time.Millisecond * 3200, } for attempt <= len(sleepPatterns) { err := tryWipe(path) if err == nil { break } if attempt == len(sleepPatterns) { comm.Dief("Could not wipe %s: %s", path, err.Error()) } comm.Logf("While wiping %s: %s", path, err.Error()) comm.Logf("Trying to brute-force permissions, who knows...") err = tryChmod(path) if err != nil { comm.Logf("While bruteforcing: %s", err.Error()) } comm.Logf("Sleeping for a bit before we retry...") sleepDuration := sleepPatterns[attempt] time.Sleep(sleepDuration) attempt++ } }
func doPush(buildPath string, specStr string, userVersion string, fixPerms bool) error { // start walking source container while waiting on auth flow sourceContainerChan := make(chan walkResult) walkErrs := make(chan error) go doWalk(buildPath, sourceContainerChan, walkErrs, fixPerms) spec, err := itchio.ParseSpec(specStr) if err != nil { return errors.Wrap(err, 1) } err = spec.EnsureChannel() if err != nil { return errors.Wrap(err, 1) } client, err := authenticateViaOauth() if err != nil { return errors.Wrap(err, 1) } newBuildRes, err := client.CreateBuild(spec.Target, spec.Channel, userVersion) if err != nil { return errors.Wrap(err, 1) } buildID := newBuildRes.Build.ID parentID := newBuildRes.Build.ParentBuild.ID var targetSignature *pwr.SignatureInfo if parentID == 0 { comm.Opf("For channel `%s`: pushing first build", spec.Channel) targetSignature = &pwr.SignatureInfo{ Container: &tlc.Container{}, Hashes: make([]wsync.BlockHash, 0), } } else { comm.Opf("For channel `%s`: last build is %d, downloading its signature", spec.Channel, parentID) var buildFiles itchio.ListBuildFilesResponse buildFiles, err = client.ListBuildFiles(parentID) if err != nil { return errors.Wrap(err, 1) } signatureFile := itchio.FindBuildFile(itchio.BuildFileType_SIGNATURE, buildFiles.Files) if signatureFile == nil { comm.Dief("Could not find signature for parent build %d, aborting", parentID) } var signatureReader io.Reader signatureReader, err = client.DownloadBuildFile(parentID, signatureFile.ID) if err != nil { return errors.Wrap(err, 1) } targetSignature, err = pwr.ReadSignature(signatureReader) if err != nil { return errors.Wrap(err, 1) } } newPatchRes, newSignatureRes, err := createBothFiles(client, buildID) if err != nil { return errors.Wrap(err, 1) } uploadDone := make(chan bool) uploadErrs := make(chan error) patchWriter, err := uploader.NewResumableUpload(newPatchRes.File.UploadURL, uploadDone, uploadErrs, uploader.ResumableUploadSettings{ Consumer: comm.NewStateConsumer(), }) patchWriter.MaxChunkGroup = *appArgs.maxChunkGroup if err != nil { return errors.Wrap(err, 1) } signatureWriter, err := uploader.NewResumableUpload(newSignatureRes.File.UploadURL, uploadDone, uploadErrs, uploader.ResumableUploadSettings{ Consumer: comm.NewStateConsumer(), }) signatureWriter.MaxChunkGroup = *appArgs.maxChunkGroup if err != nil { return errors.Wrap(err, 1) } comm.Debugf("Launching patch & signature channels") patchCounter := counter.NewWriter(patchWriter) signatureCounter := counter.NewWriter(signatureWriter) // we started walking the source container in the beginning, // we actually need it now. // note that we could actually start diffing before all the file // creation & upload setup is done var sourceContainer *tlc.Container var sourcePool wsync.Pool comm.Debugf("Waiting for source container") select { case walkErr := <-walkErrs: return errors.Wrap(walkErr, 1) case walkies := <-sourceContainerChan: comm.Debugf("Got sourceContainer!") sourceContainer = walkies.container sourcePool = walkies.pool break } comm.Opf("Pushing %s (%s)", humanize.IBytes(uint64(sourceContainer.Size)), sourceContainer.Stats()) comm.Debugf("Building diff context") var readBytes int64 bytesPerSec := float64(0) lastUploadedBytes := int64(0) stopTicking := make(chan struct{}) updateProgress := func() { uploadedBytes := int64(float64(patchWriter.UploadedBytes)) // input bytes that aren't in output, for example: // - bytes that have been compressed away // - bytes that were in old build and were simply reused goneBytes := readBytes - patchWriter.TotalBytes conservativeTotalBytes := sourceContainer.Size - goneBytes leftBytes := conservativeTotalBytes - uploadedBytes if leftBytes > AlmostThereThreshold { netStatus := "- network idle" if bytesPerSec > 1 { netStatus = fmt.Sprintf("@ %s/s", humanize.IBytes(uint64(bytesPerSec))) } comm.ProgressLabel(fmt.Sprintf("%s, %s left", netStatus, humanize.IBytes(uint64(leftBytes)))) } else { comm.ProgressLabel(fmt.Sprintf("- almost there")) } conservativeProgress := float64(uploadedBytes) / float64(conservativeTotalBytes) conservativeProgress = min(1.0, conservativeProgress) comm.Progress(conservativeProgress) comm.ProgressScale(float64(readBytes) / float64(sourceContainer.Size)) } go func() { ticker := time.NewTicker(time.Second * time.Duration(2)) for { select { case <-ticker.C: bytesPerSec = float64(patchWriter.UploadedBytes-lastUploadedBytes) / 2.0 lastUploadedBytes = patchWriter.UploadedBytes updateProgress() case <-stopTicking: break } } }() patchWriter.OnProgress = updateProgress stateConsumer := &state.Consumer{ OnProgress: func(progress float64) { readBytes = int64(float64(sourceContainer.Size) * progress) updateProgress() }, } dctx := &pwr.DiffContext{ Compression: &pwr.CompressionSettings{ Algorithm: pwr.CompressionAlgorithm_BROTLI, Quality: 1, }, SourceContainer: sourceContainer, Pool: sourcePool, TargetContainer: targetSignature.Container, TargetSignature: targetSignature.Hashes, Consumer: stateConsumer, } comm.StartProgress() comm.ProgressScale(0.0) err = dctx.WritePatch(patchCounter, signatureCounter) if err != nil { return errors.Wrap(err, 1) } // close in a goroutine to avoid deadlocking doClose := func(c io.Closer, done chan bool, errs chan error) { closeErr := c.Close() if closeErr != nil { errs <- errors.Wrap(closeErr, 1) return } done <- true } go doClose(patchWriter, uploadDone, uploadErrs) go doClose(signatureWriter, uploadDone, uploadErrs) for c := 0; c < 4; c++ { select { case uploadErr := <-uploadErrs: return errors.Wrap(uploadErr, 1) case <-uploadDone: comm.Debugf("upload done") } } close(stopTicking) comm.ProgressLabel("finalizing build") finalDone := make(chan bool) finalErrs := make(chan error) doFinalize := func(fileID int64, fileSize int64, done chan bool, errs chan error) { _, err = client.FinalizeBuildFile(buildID, fileID, fileSize) if err != nil { errs <- errors.Wrap(err, 1) return } done <- true } go doFinalize(newPatchRes.File.ID, patchCounter.Count(), finalDone, finalErrs) go doFinalize(newSignatureRes.File.ID, signatureCounter.Count(), finalDone, finalErrs) for i := 0; i < 2; i++ { select { case err := <-finalErrs: return errors.Wrap(err, 1) case <-finalDone: } } comm.EndProgress() { prettyPatchSize := humanize.IBytes(uint64(patchCounter.Count())) percReused := 100.0 * float64(dctx.ReusedBytes) / float64(dctx.FreshBytes+dctx.ReusedBytes) relToNew := 100.0 * float64(patchCounter.Count()) / float64(sourceContainer.Size) prettyFreshSize := humanize.IBytes(uint64(dctx.FreshBytes)) savings := 100.0 - relToNew if dctx.ReusedBytes > 0 { comm.Statf("Re-used %.2f%% of old, added %s fresh data", percReused, prettyFreshSize) } else { comm.Statf("Added %s fresh data", prettyFreshSize) } if savings > 0 && !math.IsNaN(savings) { comm.Statf("%s patch (%.2f%% savings)", prettyPatchSize, 100.0-relToNew) } else { comm.Statf("%s patch (no savings)", prettyPatchSize) } } comm.Opf("Build is now processing, should be up in a bit (see `butler status`)") comm.Logf("") return nil }
func doApply(patch string, target string, output string, inplace bool, signaturePath string, woundsPath string) error { if output == "" { output = target } target = path.Clean(target) output = path.Clean(output) if output == target { if !inplace { comm.Dief("Refusing to destructively patch %s without --inplace", output) } } if signaturePath == "" { comm.Opf("Patching %s", output) } else { comm.Opf("Patching %s with validation", output) } startTime := time.Now() patchReader, err := eos.Open(patch) if err != nil { return errors.Wrap(err, 1) } var signature *pwr.SignatureInfo if signaturePath != "" { sigReader, sigErr := eos.Open(signaturePath) if sigErr != nil { return errors.Wrap(sigErr, 1) } defer sigReader.Close() signature, sigErr = pwr.ReadSignature(sigReader) if sigErr != nil { return errors.Wrap(sigErr, 1) } } actx := &pwr.ApplyContext{ TargetPath: target, OutputPath: output, InPlace: inplace, Signature: signature, WoundsPath: woundsPath, Consumer: comm.NewStateConsumer(), } comm.StartProgress() err = actx.ApplyPatch(patchReader) if err != nil { return errors.Wrap(err, 1) } comm.EndProgress() container := actx.SourceContainer prettySize := humanize.IBytes(uint64(container.Size)) perSecond := humanize.IBytes(uint64(float64(container.Size) / time.Since(startTime).Seconds())) if actx.InPlace { statStr := "" if actx.Stats.TouchedFiles > 0 { statStr += fmt.Sprintf("patched %d, ", actx.Stats.TouchedFiles) } if actx.Stats.MovedFiles > 0 { statStr += fmt.Sprintf("renamed %d, ", actx.Stats.MovedFiles) } if actx.Stats.DeletedFiles > 0 { statStr += fmt.Sprintf("deleted %d, ", actx.Stats.DeletedFiles) } comm.Statf("%s (%s stage)", statStr, humanize.IBytes(uint64(actx.Stats.StageSize))) } comm.Statf("%s (%s) @ %s/s\n", prettySize, container.Stats(), perSecond) if actx.WoundsConsumer != nil && actx.WoundsConsumer.HasWounds() { extra := "" if actx.WoundsPath != "" { extra = fmt.Sprintf(" (written to %s)", actx.WoundsPath) } totalCorrupted := actx.WoundsConsumer.TotalCorrupted() comm.Logf("Result has wounds, %s corrupted data%s", humanize.IBytes(uint64(totalCorrupted)), extra) } return nil }
func file(path string) { reader, err := eos.Open(path) must(err) defer reader.Close() stats, err := reader.Stat() if os.IsNotExist(err) { comm.Dief("%s: no such file or directory", path) } must(err) if stats.IsDir() { comm.Logf("%s: directory", path) return } if stats.Size() == 0 { comm.Logf("%s: empty file. peaceful.", path) return } prettySize := humanize.IBytes(uint64(stats.Size())) var magic int32 must(binary.Read(reader, wire.Endianness, &magic)) switch magic { case pwr.PatchMagic: { ph := &pwr.PatchHeader{} rctx := wire.NewReadContext(reader) must(rctx.ReadMessage(ph)) rctx, err = pwr.DecompressWire(rctx, ph.GetCompression()) must(err) container := &tlc.Container{} must(rctx.ReadMessage(container)) // target container container.Reset() must(rctx.ReadMessage(container)) // source container comm.Logf("%s: %s wharf patch file (%s) with %s", path, prettySize, ph.GetCompression().ToString(), container.Stats()) comm.Result(ContainerResult{ Type: "wharf/patch", NumFiles: len(container.Files), NumDirs: len(container.Dirs), NumSymlinks: len(container.Symlinks), UncompressedSize: container.Size, }) } case pwr.SignatureMagic: { sh := &pwr.SignatureHeader{} rctx := wire.NewReadContext(reader) must(rctx.ReadMessage(sh)) rctx, err = pwr.DecompressWire(rctx, sh.GetCompression()) must(err) container := &tlc.Container{} must(rctx.ReadMessage(container)) comm.Logf("%s: %s wharf signature file (%s) with %s", path, prettySize, sh.GetCompression().ToString(), container.Stats()) comm.Result(ContainerResult{ Type: "wharf/signature", NumFiles: len(container.Files), NumDirs: len(container.Dirs), NumSymlinks: len(container.Symlinks), UncompressedSize: container.Size, }) } case pwr.ManifestMagic: { mh := &pwr.ManifestHeader{} rctx := wire.NewReadContext(reader) must(rctx.ReadMessage(mh)) rctx, err = pwr.DecompressWire(rctx, mh.GetCompression()) must(err) container := &tlc.Container{} must(rctx.ReadMessage(container)) comm.Logf("%s: %s wharf manifest file (%s) with %s", path, prettySize, mh.GetCompression().ToString(), container.Stats()) comm.Result(ContainerResult{ Type: "wharf/manifest", NumFiles: len(container.Files), NumDirs: len(container.Dirs), NumSymlinks: len(container.Symlinks), UncompressedSize: container.Size, }) } case pwr.WoundsMagic: { wh := &pwr.WoundsHeader{} rctx := wire.NewReadContext(reader) must(rctx.ReadMessage(wh)) container := &tlc.Container{} must(rctx.ReadMessage(container)) files := make(map[int64]bool) totalWounds := int64(0) for { wound := &pwr.Wound{} err = rctx.ReadMessage(wound) if err != nil { if errors.Is(err, io.EOF) { break } else { must(err) } } if wound.Kind == pwr.WoundKind_FILE { totalWounds += (wound.End - wound.Start) files[wound.Index] = true } } comm.Logf("%s: %s wharf wounds file with %s, %s wounds in %d files", path, prettySize, container.Stats(), humanize.IBytes(uint64(totalWounds)), len(files)) comm.Result(ContainerResult{ Type: "wharf/wounds", }) } default: _, err := reader.Seek(0, os.SEEK_SET) must(err) wasZip := func() bool { zr, err := zip.NewReader(reader, stats.Size()) if err != nil { if err != zip.ErrFormat { must(err) } return false } container, err := tlc.WalkZip(zr, func(fi os.FileInfo) bool { return true }) must(err) comm.Logf("%s: %s zip file with %s", path, prettySize, container.Stats()) comm.Result(ContainerResult{ Type: "zip", NumFiles: len(container.Files), NumDirs: len(container.Dirs), NumSymlinks: len(container.Symlinks), UncompressedSize: container.Size, }) return true }() if !wasZip { comm.Logf("%s: not sure - try the file(1) command if your system has it!", path) } } }
func ls(path string) { reader, err := eos.Open(path) must(err) defer reader.Close() stats, err := reader.Stat() if os.IsNotExist(err) { comm.Dief("%s: no such file or directory", path) } must(err) if stats.IsDir() { comm.Logf("%s: directory", path) return } if stats.Size() == 0 { comm.Logf("%s: empty file. peaceful.", path) return } log := func(line string) { comm.Logf(line) } var magic int32 must(binary.Read(reader, wire.Endianness, &magic)) switch magic { case pwr.PatchMagic: { h := &pwr.PatchHeader{} rctx := wire.NewReadContext(reader) must(rctx.ReadMessage(h)) rctx, err = pwr.DecompressWire(rctx, h.GetCompression()) must(err) container := &tlc.Container{} must(rctx.ReadMessage(container)) log("pre-patch container:") container.Print(log) container.Reset() must(rctx.ReadMessage(container)) log("================================") log("post-patch container:") container.Print(log) } case pwr.SignatureMagic: { h := &pwr.SignatureHeader{} rctx := wire.NewReadContext(reader) must(rctx.ReadMessage(h)) rctx, err = pwr.DecompressWire(rctx, h.GetCompression()) must(err) container := &tlc.Container{} must(rctx.ReadMessage(container)) container.Print(log) } case pwr.ManifestMagic: { h := &pwr.ManifestHeader{} rctx := wire.NewReadContext(reader) must(rctx.ReadMessage(h)) rctx, err = pwr.DecompressWire(rctx, h.GetCompression()) must(err) container := &tlc.Container{} must(rctx.ReadMessage(container)) container.Print(log) } case pwr.WoundsMagic: { wh := &pwr.WoundsHeader{} rctx := wire.NewReadContext(reader) must(rctx.ReadMessage(wh)) container := &tlc.Container{} must(rctx.ReadMessage(container)) container.Print(log) for { wound := &pwr.Wound{} err = rctx.ReadMessage(wound) if err != nil { if errors.Is(err, io.EOF) { break } else { must(err) } } comm.Logf(wound.PrettyString(container)) } } default: _, err := reader.Seek(0, os.SEEK_SET) must(err) wasZip := func() bool { zr, err := zip.NewReader(reader, stats.Size()) if err != nil { if err != zip.ErrFormat { must(err) } return false } container, err := tlc.WalkZip(zr, func(fi os.FileInfo) bool { return true }) must(err) container.Print(log) return true }() if !wasZip { comm.Logf("%s: not sure - try the file(1) command if your system has it!", path) } } }