Skip to content

Commit 040352b

Browse files
committed
Refactoring and do not change modification time
1 parent 396c608 commit 040352b

File tree

3 files changed

+123
-12
lines changed

3 files changed

+123
-12
lines changed

internal/util/file.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package util
2+
3+
import "os"
4+
5+
func FileExists(path string) bool {
6+
info, err := os.Stat(path)
7+
8+
if os.IsNotExist(err) {
9+
return false
10+
}
11+
12+
return !info.IsDir()
13+
}

internal/watcher/walker.go renamed to internal/watcher/watcher.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package watcher
22

33
import (
44
"github.com/cmuench/inotify-proxy/internal/profile/validator"
5+
"github.com/cmuench/inotify-proxy/internal/util"
56
"github.com/gookit/color"
67
"github.com/karrick/godirwalk"
78
"os"
@@ -81,7 +82,7 @@ func isFileChanged(path string) bool {
8182

8283
currentTime := time.Now()
8384

84-
err := os.Chtimes(path, currentModificationTime, currentTime)
85+
err := os.Chtimes(path, currentTime, currentModificationTime)
8586

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

99100
func garbageCollection() {
100101
for path, _ := range fileMap {
101-
if !fileExists(path) {
102+
if !util.FileExists(path) {
102103
delete(fileMap, path)
103104
color.Style{color.FgGray}.Printf("Deleted: %s\n", path)
104105
}
105106
}
106107
}
107-
108-
func fileExists(path string) bool {
109-
info, err := os.Stat(path)
110-
111-
if os.IsNotExist(err) {
112-
return false
113-
}
114-
115-
return !info.IsDir()
116-
}

internal/watcher/watcher_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package watcher
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
"runtime"
9+
"syscall"
10+
"testing"
11+
"time"
12+
)
13+
14+
type sysDir struct {
15+
name string
16+
files []string
17+
}
18+
19+
var sysdir = func() *sysDir {
20+
switch runtime.GOOS {
21+
case "darwin":
22+
switch runtime.GOARCH {
23+
case "arm64":
24+
wd, err := syscall.Getwd()
25+
if err != nil {
26+
wd = err.Error()
27+
}
28+
sd := &sysDir{
29+
filepath.Join(wd, "..", ".."),
30+
[]string{
31+
"ResourceRules.plist",
32+
"Info.plist",
33+
},
34+
}
35+
found := true
36+
for _, f := range sd.files {
37+
path := filepath.Join(sd.name, f)
38+
if _, err := os.Stat(path); err != nil {
39+
found = false
40+
break
41+
}
42+
}
43+
if found {
44+
return sd
45+
}
46+
// In a self-hosted iOS build the above files might
47+
// not exist. Look for system files instead below.
48+
}
49+
case "windows":
50+
return &sysDir{
51+
os.Getenv("SystemRoot") + "\\system32\\drivers\\etc",
52+
[]string{
53+
"networks",
54+
"protocol",
55+
"services",
56+
},
57+
}
58+
case "plan9":
59+
return &sysDir{
60+
"/lib/ndb",
61+
[]string{
62+
"common",
63+
"local",
64+
},
65+
}
66+
}
67+
return &sysDir{
68+
"/etc",
69+
[]string{
70+
"passwd",
71+
"group",
72+
"hosts",
73+
},
74+
}
75+
}()
76+
77+
func TestIfFileChangedNotModified(t *testing.T) {
78+
var sfdir = sysdir.name
79+
var sfname = sysdir.files[0]
80+
81+
// First call registers file
82+
assert.False(t, isFileChanged(filepath.Join(sfdir, sfname)))
83+
84+
// Second call checks if file is changed
85+
assert.False(t, isFileChanged(filepath.Join(sfdir, sfname)))
86+
}
87+
88+
func TestIfFileChangedModified(t *testing.T) {
89+
tmpFile, err := ioutil.TempFile(os.TempDir(), "inotify-watch-test-")
90+
if err != nil {
91+
t.Fatal("Cannot create temporary file")
92+
}
93+
94+
// First call registers temp file
95+
assert.False(t, isFileChanged(tmpFile.Name()))
96+
97+
currentTime := time.Now()
98+
99+
// simulate change
100+
chtimesError := os.Chtimes(tmpFile.Name(), currentTime, currentTime)
101+
102+
if chtimesError != nil {
103+
t.Fatal("Cannot chtimes of temporary file")
104+
}
105+
106+
assert.False(t, isFileChanged(tmpFile.Name()))
107+
}

0 commit comments

Comments
 (0)