aboutsummaryrefslogtreecommitdiff
path: root/steam/package.go
blob: bd5bfb53cf96b01a84b36a2259b0eac6d6c8584e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
	})
}