Skip to content

Commit

Permalink
Update xtractr lib and remove UPX on Windows (#115)
Browse files Browse the repository at this point in the history
* Update xtractr lib and remove UPX on Windows
* Wait for extracts to finish on stop. expose poll interval
* fix filemode and dirmode display
* only print filemode msg if not windows
  • Loading branch information
davidnewhall authored Mar 25, 2021
1 parent 3b5964a commit b92ddfd
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 25 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ windows: $(BINARY).amd64.exe
$(BINARY).amd64.exe: rsrc.syso main.go
# Building windows 64-bit x86 binary.
GOOS=windows GOARCH=amd64 go build -o $@ -ldflags "-w -s $(VERSION_LDFLAGS) $(EXTRA_LDFLAGS) $(WINDOWS_LDFLAGS)"
[ -z "$(UPXPATH)" ] || $(UPXPATH) -q9 $@
# UPX causes Virus warnings on Windows.
#[ -z "$(UPXPATH)" ] || $(UPXPATH) -q9 $@

####################
##### Packages #####
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ require (
golift.io/rotatorr v0.0.0-20210307012029-65b11a8ea8f9
golift.io/starr v0.9.9
golift.io/version v0.0.2
golift.io/xtractr v0.0.6
golift.io/xtractr v0.0.8-0.20210325050400-2931a0c4c2d3
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ golift.io/starr v0.9.9 h1:5TIjrtwISD04NdtAbd0tIlDtoeVSI6tuO4wHW43ie5E=
golift.io/starr v0.9.9/go.mod h1:EE8B7OlqZlE/EGmBP1bLsK1OHEgWwbNpyjDXX0B2f0Y=
golift.io/version v0.0.2 h1:i0gXRuSDHKs4O0sVDUg4+vNIuOxYoXhaxspftu2FRTE=
golift.io/version v0.0.2/go.mod h1:76aHNz8/Pm7CbuxIsDi97jABL5Zui3f2uZxDm4vB6hU=
golift.io/xtractr v0.0.6 h1:fdaj8hIdSN/zPCes+XrOFNEDaOzhEwf6nrfAp53KhSs=
golift.io/xtractr v0.0.6/go.mod h1:kpUkpBNj/Ir3oz3LxRyy/gOYphe8cJMjpobAaXzy9tw=
golift.io/xtractr v0.0.8-0.20210325033854-6acae81c990d h1:xQqLduFCzdB26ZGDAlJ1WjV4ijz/6LqobmQkK12d39A=
golift.io/xtractr v0.0.8-0.20210325033854-6acae81c990d/go.mod h1:kpUkpBNj/Ir3oz3LxRyy/gOYphe8cJMjpobAaXzy9tw=
golift.io/xtractr v0.0.8-0.20210325050400-2931a0c4c2d3 h1:SoPsYzCog7u3Qz1feKfewWi8ECtHbndXa3tzDKAQpQE=
golift.io/xtractr v0.0.8-0.20210325050400-2931a0c4c2d3/go.mod h1:kpUkpBNj/Ir3oz3LxRyy/gOYphe8cJMjpobAaXzy9tw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
3 changes: 3 additions & 0 deletions pkg/unpackerr/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type Config struct {
Readarr []*ReadarrConfig `json:"readarr,omitempty" toml:"readarr" xml:"readarr" yaml:"readarr,omitempty"`
Folders []*FolderConfig `json:"folder,omitempty" toml:"folder" xml:"folder" yaml:"folder,omitempty"`
Webhook []*WebhookConfig `json:"webhook,omitempty" toml:"webhook" xml:"webhook" yaml:"webhook,omitempty"`
Folder struct {
Interval cnfg.Duration `json:"interval" toml:"interval" xml:"interval" yaml:"interval"` // undocumented.
} `json:"folders,omitempty" toml:"folders" xml:"folders" yaml:"folders,omitempty"` // undocumented.
}

// retrieveAppQueues polls Sonarr, Lidarr and Radarr. At the same time.
Expand Down
4 changes: 2 additions & 2 deletions pkg/unpackerr/cnfgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ func (u *Unpackerr) validateConfig() (uint64, uint64) { //nolint:cyclop
fm, err := strconv.ParseUint(u.FileMode, 8, 32)
if err != nil || u.FileMode == "" {
fm = defaultFileMode
u.FileMode = strconv.FormatUint(fm, 32)
u.FileMode = strconv.FormatUint(fm, 8)
}

dm, err := strconv.ParseUint(u.DirMode, 8, 32)
if err != nil || u.DirMode == "" {
dm = defaultDirMode
u.DirMode = strconv.FormatUint(dm, 32)
u.DirMode = strconv.FormatUint(dm, 8)
}

if u.Parallel == 0 {
Expand Down
35 changes: 17 additions & 18 deletions pkg/unpackerr/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
"golift.io/xtractr"
)

const pollInterval = time.Second
// defaultPollInterval is used if Docker is detected.
const defaultPollInterval = time.Second

// FolderConfig defines the input data for a watched folder.
type FolderConfig struct {
Expand All @@ -30,6 +31,7 @@ type FolderConfig struct {

// Folders holds all known (created) folders in all watch paths.
type Folders struct {
Interval time.Duration
Config []*FolderConfig
Folders map[string]*Folder
Events chan *eventData
Expand Down Expand Up @@ -86,6 +88,10 @@ func (u *Unpackerr) PollFolders() {
err error
)

if isRunningInDocker() && u.Folder.Interval.Duration == 0 {
u.Folder.Interval.Duration = defaultPollInterval
}

u.Folders, flist = u.checkFolders()

if u.folders, err = u.newFolderWatcher(); err != nil {
Expand All @@ -99,18 +105,18 @@ func (u *Unpackerr) PollFolders() {
}

go u.folders.watchFSNotify()
u.Print("[Folder] Watching:", strings.Join(flist, ", "))
u.Print("[Folder] Watching (fsnotify):", strings.Join(flist, ", "))

if !isRunningInDocker() {
if u.Folder.Interval.Duration == 0 {
return
}

go func() {
if err := u.folders.Watcher.Start(pollInterval); err != nil {
if err := u.folders.Watcher.Start(u.Folder.Interval.Duration); err != nil {
u.Print("[ERROR] Folder poller stopped:", err)
}
}()
u.Print("[Folder] Polling:", strings.Join(flist, ", "))
u.Printf("[Folder] Polling @ %v: %s", u.Folder.Interval, strings.Join(flist, ", "))
}

// newFolderWatcher returns a new folder watcher.
Expand Down Expand Up @@ -138,6 +144,7 @@ func (u *Unpackerr) newFolderWatcher() (*Folders, error) {
}

return &Folders{
Interval: u.Folder.Interval.Duration,
Config: u.Folders,
Folders: make(map[string]*Folder),
Events: make(chan *eventData, u.Config.Buffer),
Expand All @@ -151,7 +158,7 @@ func (u *Unpackerr) newFolderWatcher() (*Folders, error) {

// Add uses either fsnotify or watcher.
func (f *Folders) Add(folder string) error {
if isRunningInDocker() {
if f.Interval != 0 {
if err := f.Watcher.Add(folder); err != nil {
return fmt.Errorf("watcher: %w", err)
}
Expand Down Expand Up @@ -299,7 +306,7 @@ func (f *Folders) handleFileEvent(name string) {
// cnfg.Path: "/Users/Documents/watched_folder"
// event.Name: "/Users/Documents/watched_folder/new_folder/file.rar"
// eventData.name: "new_folder"
if !strings.HasPrefix(name, cnfg.Path) {
if !strings.HasPrefix(name, cnfg.Path) || name == cnfg.Path {
continue // Not the configured folder for the event we just got.
} else if p := filepath.Dir(name); p == cnfg.Path {
f.Events <- &eventData{name: filepath.Base(name), cnfg: cnfg, file: name}
Expand Down Expand Up @@ -396,7 +403,7 @@ func (u *Unpackerr) deleteAfterReached(name string, folder *Folder) {
go u.DeleteFiles(name)
webhook = true
} else if folder.cnfg.DeleteOrig && len(folder.rars) > 0 {
go u.DeleteFiles(folder.rars...) // probably does not delete all the files.
go u.DeleteFiles(folder.rars...)
webhook = true
}

Expand Down Expand Up @@ -445,17 +452,9 @@ func (u *Unpackerr) updateQueueStatus(data *newStatus, sendHook bool) {
}
}

var isDocker *bool // nolint:gochecknoglobals

// only run this once.
func isRunningInDocker() bool {
if isDocker != nil {
return *isDocker
}

// docker creates a .dockerenv file at the root of the container.
_, err := os.Stat("/.dockerenv")
w := err == nil
isDocker = &w

return w
return err == nil
}
6 changes: 5 additions & 1 deletion pkg/unpackerr/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"os"
"runtime"

homedir "github.com/mitchellh/go-homedir"
"golift.io/rotatorr"
Expand Down Expand Up @@ -216,7 +217,10 @@ func (u *Unpackerr) logStartupInfo(msg string) {
u.Printf(" => Start Delay: %v", u.Config.StartDelay)
u.Printf(" => Retry Delay: %v, max: %d", u.Config.RetryDelay, u.Config.MaxRetries)
u.Printf(" => Debug / Quiet: %v / %v", u.Config.Debug, u.Config.Quiet)
u.Printf(" => Directory & File Modes: %s & %s", u.Config.DirMode, u.Config.FileMode)

if runtime.GOOS != "windows" {
u.Printf(" => Directory & File Modes: %s & %s", u.Config.DirMode, u.Config.FileMode)
}

if u.Config.LogFile != "" {
msg := "no rotation"
Expand Down
1 change: 1 addition & 0 deletions pkg/unpackerr/tray.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (u *Unpackerr) startTray() {
}

func (u *Unpackerr) exitTray() {
u.Xtractr.Stop() // stop and wait for extractions.
// because systray wants to control the exit code? no..
os.Exit(0)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/unpackerr/tray_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func (u *Unpackerr) startTray() {
defer u.Xtractr.Stop() // stop and wait for extractions to finish.
signal.Notify(u.sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
u.Printf("[unpackerr] Need help? %s\n=====> Exiting! Caught Signal: %v", helpLink, <-u.sigChan)
}
Expand Down

0 comments on commit b92ddfd

Please sign in to comment.