Skip to content

Commit

Permalink
feat: clean directories provided in overrides before extracting
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanLatimer committed Mar 15, 2021
1 parent a6a307b commit e4aa185
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 35 deletions.
1 change: 0 additions & 1 deletion .bra.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ ignore = [".git", "node_modules"] # Directories to exclude from watching
ignore_files = [] # Regexps for ignoring specific notifies
init_cmds = [
# Commands run in start
["env", "CGO_ENABLED=0"],
["go", "install", "-race"],
]
interrupt_timout = 15 # Time to wait until force kill
Expand Down
47 changes: 27 additions & 20 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"

"github.com/magiconair/properties"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
)

Expand Down Expand Up @@ -40,31 +41,31 @@ func installForgeServer(forgeVersion string, path string) error {
if fileExists(installerJarPath) {
err := os.Remove(installerJarPath)
if err != nil {
return err
return errors.Wrap(err, "Failed removing existing Forge installer")
}
}

forgeFile, err := os.Create(installerJarPath)
if err != nil {
return err
return errors.Wrap(err, "Failed creating Forge installer")
}
defer os.Remove(forgeFile.Name())
defer forgeFile.Close()

forgeResp, err := http.Get(FORGE_DL_URL + forgeVersion + "/" + installerJar)
if err != nil {
return err
return errors.Wrap(err, "Failed downloading Forge")
}
defer forgeResp.Body.Close()

_, err = io.Copy(forgeFile, forgeResp.Body)
if err != nil {
return err
return errors.Wrap(err, "Failed writing Forge to disk")
}

javaPath, err := exec.LookPath("java")
if err != nil {
return err
return errors.Wrap(err, "Failed finding Java")
}

cmdInstall := &exec.Cmd{
Expand All @@ -77,41 +78,41 @@ func installForgeServer(forgeVersion string, path string) error {
jww.INFO.Println("Installing Forge Server")
err = cmdInstall.Run()
if err != nil {
return err
return errors.Wrap(err, "Failed installing Forge")
}

src := filepath.Join(path, "forge-"+forgeVersion+".jar")
dest := filepath.Join(path, "server.jar")

return os.Rename(src, dest)
return errors.Wrap(os.Rename(src, dest), "Failed renaming server jar")
}

func precreateUserDir(path string) error {
err := os.MkdirAll(filepath.Join(path, "mods"), 0700)
if err != nil {
return err
return errors.Wrap(err, "Failed creating mods directory")
}
err = os.MkdirAll(filepath.Join(path, "config"), 0700)
if err != nil {
return err
return errors.Wrap(err, "Failed creating config directory")
}
file, err := os.Create(filepath.Join(path, "README.txt"))
if err != nil {
return err
return errors.Wrap(err, "Failed creating README")
}
defer file.Close()

_, err = file.WriteString("The contents of this folder should mirror the structure of the pack\n")
if err != nil {
return err
return errors.Wrap(err, "Failed writting to README")
}

_, err = file.WriteString("Any user provided files will be copied from here into the pack\n")
if err != nil {
return err
return errors.Wrap(err, "Failed writting to README")
}

return file.Sync()
return errors.Wrap(file.Sync(), "Failed flushing README to disk")
}

func writeVersionFile(version string, path string) error {
Expand All @@ -120,10 +121,10 @@ func writeVersionFile(version string, path string) error {
}
bytes, err := ver.Marshal()
if err != nil {
return err
return errors.Wrap(err, "Failed to marshal version file")
}

return ioutil.WriteFile(path, bytes, 0700)
return errors.Wrap(ioutil.WriteFile(path, bytes, 0700), "Failed to write version file")
}

// compareVersion will return true if versions match, false if otherwise
Expand All @@ -134,11 +135,11 @@ func compareVersion(version string, path string) (bool, error) {

bytes, err := ioutil.ReadFile(path)
if err != nil {
return false, err
return false, errors.Wrap(err, "Failed to open version file")
}
ver, err := UnmarshalCPVersion(bytes)
if err != nil {
return false, err
return false, errors.Wrap(err, "Failed to parse version file")
}
return ver.Version == version, nil
}
Expand All @@ -147,21 +148,27 @@ func updateServerPropsVersion(version string, path string) error {
if fileExists(path) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return err
return errors.Wrap(err, "Failed to read server properties")
}

props, err := properties.Load(bytes, properties.UTF8)
if err != nil {
return errors.Wrap(err, "Failed to parse server properties")
}
props.SetValue("motd", fmt.Sprintf("Version %s", version))
if err != nil {
return errors.Wrap(err, "Failed to set MOTD")
}

file, err := os.Create(path)
if err != nil {
return err
return errors.Wrap(err, "Failed to open server properties")
}
defer file.Close()

_, err = props.Write(file, properties.UTF8)
if err != nil {
return err
return errors.Wrap(err, "Failed to write server properties")
}
}
return nil
Expand Down
44 changes: 30 additions & 14 deletions zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ func handleZipPack(opts PackInstallOptions) error {

bytes, err := extractZipManifest(zipPath)
if err != nil {
return err
return errors.Wrap(err, "Failed extracting manifest")
}
manifest, err := UnmarshalZipManifest(bytes)
if err != nil {
return err
return errors.Wrap(err, "Failed marshalling manifest")
}

versionFilePath := filepath.Join(packPath, "cpversion.json")
versionMatch, err := compareVersion(manifest.Version, versionFilePath)
if err != nil {
return err
return errors.Wrap(err, "Failed comparing versions")
}
if versionMatch {
jww.INFO.Println("Manifest version matches the currently installed pack version, skipping install.")
Expand All @@ -55,17 +55,17 @@ func handleZipPack(opts PackInstallOptions) error {
modsDest := filepath.Join(packPath, "mods")
err = os.RemoveAll(modsDest)
if err != nil {
return err
return errors.Wrap(err, "Failed removing old mods directory")
}

err = downloadPackMods(manifest, modsDest)
if err != nil {
return err
return errors.Wrap(err, "Failed downloading mods")
}

err = extractZipOverrides(zipPath, manifest.Overrides, packPath)
err = extractZipOverrides(zipPath, filepath.Clean(manifest.Overrides), packPath)
if err != nil {
return err
return errors.Wrap(err, "Failed extracting overrides")
}
err := writeVersionFile(manifest.Version, versionFilePath)
if err != nil {
Expand Down Expand Up @@ -93,6 +93,7 @@ func handleZipPack(opts PackInstallOptions) error {
}
}

jww.DEBUG.Println(opts.Server && !versionMatch)
if opts.Server && !versionMatch {
mcVersion := manifest.Minecraft.Version
modLoader := manifest.Minecraft.ModLoaders[0].ID
Expand All @@ -103,6 +104,7 @@ func handleZipPack(opts PackInstallOptions) error {
}

if opts.ServerMotd {
jww.INFO.Println("Updating MOTD...")
err = updateServerPropsVersion(manifest.Version, filepath.Join(packPath, "server.properties"))
if err != nil {
return err
Expand Down Expand Up @@ -208,7 +210,20 @@ func extractZipOverrides(zipPath string, overrides string, dest string) error {
return err
}
defer zReader.Close()
// clean up directories shipped in overrides
for _, src := range zReader.File {
if filepath.HasPrefix(src.Name, overrides) && strings.HasSuffix(src.Name, "/") && filepath.Clean(src.Name) != overrides {
if !strings.HasSuffix(src.Name, "mods/") {
path := filepath.Join(dest, filepath.Clean(src.Name[len(overrides):]))
err := os.RemoveAll(path)
if err != nil {
return err
}
}
}
}

// extract overrides
var g errgroup.Group
sem := make(chan struct{}, 5)
for _, src := range zReader.File {
Expand All @@ -219,14 +234,15 @@ func extractZipOverrides(zipPath string, overrides string, dest string) error {
defer func() {
<-sem
}()
path := filepath.Join(dest, filepath.Clean(src.Name[len(overrides):]))
dir := filepath.Dir(path)
err := os.MkdirAll(dir, 0700)
if err != nil {
return err
}

if !strings.HasSuffix(src.Name, "/") {
path := filepath.Join(dest, filepath.Clean(src.Name[len(overrides):]))
dir := filepath.Dir(path)
err := os.MkdirAll(dir, 0700)
if err != nil {
return err
}

jww.DEBUG.Printf("%s -> %s", src.Name, path)
file, err := os.Create(path)
if err != nil {
Expand All @@ -244,8 +260,8 @@ func extractZipOverrides(zipPath string, overrides string, dest string) error {
if err != nil {
return err
}

}

return nil
})
}
Expand Down

0 comments on commit e4aa185

Please sign in to comment.