package steam import ( "archive/tar" "io" "os" "path/filepath" "strings" ) // Package writes the package, returning bytes written and an error if any func (g *Game) Package(wr io.WriteCloser) error { acf, err := FindACF(g.LibraryPath, g.Name) if err != nil { return err } twriter := tar.NewWriter(wr) paths := []string{ filepath.Join(g.LibraryPath, "common", g.Name), acf, } for _, pth := range paths { err := filepath.Walk(pth, tarWalkfn(twriter, g.LibraryPath)) if err != nil { return err } } err = twriter.Flush() if err != nil { return err } err = twriter.Close() return wr.Close() } // Extract will read a tarball from the io.Reader and install the game into // the current library path func (l *Library) Extract(r io.Reader) error { treader := tar.NewReader(r) for { hdr, err := treader.Next() if err == io.EOF { // We've reached the end! Whoee break } if err != nil { return err } // Fix windows slashes... fileName := strings.Replace(hdr.Name, "\\", "/", -1) fileName = filepath.Join(l.Folder, fileName) info := hdr.FileInfo() if info.IsDir() { // I don't like hard-coded permissions but it // it helps with overall platform compatibility if err = os.MkdirAll(fileName, 0775); err != nil { return err } continue } if err = os.MkdirAll(filepath.Dir(fileName), 0775); err != nil { return err } // Create a file handle to work with f, err := os.OpenFile(fileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0664) if err != nil { return err } if _, err := io.Copy(f, treader); err != nil { f.Close() return err } f.Close() } return nil } // Delete removes all of the game files and the ACF func (g *Game) Delete() error { acf, err := FindACF(g.LibraryPath, g.Name) if err != nil { return err } if err := os.Remove(acf); err != nil { return err } err = os.RemoveAll(filepath.Join(g.LibraryPath, "common", g.Name)) if err != nil { return err } return nil } // GetSize returns the size of a game in a pretty format func (g Game) GetSize() string { return formatBytes(g.Size) } func (g *Game) setSizeInfo() error { pth := filepath.Join(g.LibraryPath, "common", g.Name) return filepath.Walk(pth, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if info.Mode().IsRegular() { g.Size += info.Size() } return nil }) }