Skip to content

Commit

Permalink
windows: Show more helpful error for used by another process error
Browse files Browse the repository at this point in the history
  • Loading branch information
Vendicated committed Feb 17, 2023
1 parent abd816c commit 1e45fca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 5 additions & 2 deletions patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/ProtonMail/go-appdir"
"os"
"os/exec"
path "path/filepath"
"strings"

"github.com/ProtonMail/go-appdir"
)

var BaseDir string
Expand Down Expand Up @@ -130,6 +129,8 @@ func patchRenames(dir string, isSystemElectron bool) (err error) {

fmt.Println("Renaming", appAsar, "to", _appAsar)
if err := os.Rename(appAsar, _appAsar); err != nil {
err = CheckIfErrIsCauseItsBusyRn(err)
fmt.Println(err)
return err
}
renamesDone = append(renamesDone, []string{appAsar, _appAsar})
Expand Down Expand Up @@ -253,6 +254,7 @@ func unpatchRenames(dir string, isSystemElectron bool) (errOut error) {

fmt.Println("Deleting", appAsar)
if err := os.Rename(appAsar, appAsarTmp); err != nil {
err = CheckIfErrIsCauseItsBusyRn(err)
fmt.Println(err)
errOut = err
} else {
Expand All @@ -261,6 +263,7 @@ func unpatchRenames(dir string, isSystemElectron bool) (errOut error) {

fmt.Println("Renaming", _appAsar, "to", appAsar)
if err := os.Rename(_appAsar, appAsar); err != nil {
err = CheckIfErrIsCauseItsBusyRn(err)
fmt.Println(err)
errOut = err
} else {
Expand Down
21 changes: 21 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
package main

import (
"errors"
"fmt"
"os"
"runtime"
"strings"
"syscall"
)

func ArrayIncludes[T comparable](arr []T, v T) bool {
Expand Down Expand Up @@ -71,3 +74,21 @@ func GetBranch(name string) string {
func Ptr[T any](v T) *T {
return &v
}

func CheckIfErrIsCauseItsBusyRn(err error) error {
if runtime.GOOS != "windows" {
return err
}

// bruhhhh
if linkError, ok := err.(*os.LinkError); ok {
if errno, ok := linkError.Err.(syscall.Errno); ok && errno == 32 /* ERROR_SHARING_VIOLATION */ {
return errors.New(
"Cannot patch because Discord's files are used by a different process." +
"\nMake sure you close Discord before trying to patch!",
)
}
}

return err
}

0 comments on commit 1e45fca

Please sign in to comment.