Skip to content

Commit

Permalink
Refactoring and do not change modification time
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuench committed Aug 16, 2020
1 parent 396c608 commit 040352b
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 12 deletions.
13 changes: 13 additions & 0 deletions internal/util/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package util

import "os"

func FileExists(path string) bool {
info, err := os.Stat(path)

if os.IsNotExist(err) {
return false
}

return !info.IsDir()
}
15 changes: 3 additions & 12 deletions internal/watcher/walker.go → internal/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package watcher

import (
"github.com/cmuench/inotify-proxy/internal/profile/validator"
"github.com/cmuench/inotify-proxy/internal/util"
"github.com/gookit/color"
"github.com/karrick/godirwalk"
"os"
Expand Down Expand Up @@ -81,7 +82,7 @@ func isFileChanged(path string) bool {

currentTime := time.Now()

err := os.Chtimes(path, currentModificationTime, currentTime)
err := os.Chtimes(path, currentTime, currentModificationTime)

if err != nil {
panic("Error touching file" + path)
Expand All @@ -98,19 +99,9 @@ func isFileChanged(path string) bool {

func garbageCollection() {
for path, _ := range fileMap {
if !fileExists(path) {
if !util.FileExists(path) {
delete(fileMap, path)
color.Style{color.FgGray}.Printf("Deleted: %s\n", path)
}
}
}

func fileExists(path string) bool {
info, err := os.Stat(path)

if os.IsNotExist(err) {
return false
}

return !info.IsDir()
}
107 changes: 107 additions & 0 deletions internal/watcher/watcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package watcher

import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"syscall"
"testing"
"time"
)

type sysDir struct {
name string
files []string
}

var sysdir = func() *sysDir {
switch runtime.GOOS {
case "darwin":
switch runtime.GOARCH {
case "arm64":
wd, err := syscall.Getwd()
if err != nil {
wd = err.Error()
}
sd := &sysDir{
filepath.Join(wd, "..", ".."),
[]string{
"ResourceRules.plist",
"Info.plist",
},
}
found := true
for _, f := range sd.files {
path := filepath.Join(sd.name, f)
if _, err := os.Stat(path); err != nil {
found = false
break
}
}
if found {
return sd
}
// In a self-hosted iOS build the above files might
// not exist. Look for system files instead below.
}
case "windows":
return &sysDir{
os.Getenv("SystemRoot") + "\\system32\\drivers\\etc",
[]string{
"networks",
"protocol",
"services",
},
}
case "plan9":
return &sysDir{
"/lib/ndb",
[]string{
"common",
"local",
},
}
}
return &sysDir{
"/etc",
[]string{
"passwd",
"group",
"hosts",
},
}
}()

func TestIfFileChangedNotModified(t *testing.T) {
var sfdir = sysdir.name
var sfname = sysdir.files[0]

// First call registers file
assert.False(t, isFileChanged(filepath.Join(sfdir, sfname)))

// Second call checks if file is changed
assert.False(t, isFileChanged(filepath.Join(sfdir, sfname)))
}

func TestIfFileChangedModified(t *testing.T) {
tmpFile, err := ioutil.TempFile(os.TempDir(), "inotify-watch-test-")
if err != nil {
t.Fatal("Cannot create temporary file")
}

// First call registers temp file
assert.False(t, isFileChanged(tmpFile.Name()))

currentTime := time.Now()

// simulate change
chtimesError := os.Chtimes(tmpFile.Name(), currentTime, currentTime)

if chtimesError != nil {
t.Fatal("Cannot chtimes of temporary file")
}

assert.False(t, isFileChanged(tmpFile.Name()))
}

0 comments on commit 040352b

Please sign in to comment.