Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Watch
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Container Registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Libraries
Go
Utilities
Watch
Commits
ddb13564
Verified
Commit
ddb13564
authored
1 year ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
fix: Same path added with and without slashes are handled differently
#5
parent
6f641cd2
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
devenv.lock
+6
-6
6 additions, 6 deletions
devenv.lock
error.go
+8
-0
8 additions, 0 deletions
error.go
lighthouse.go
+47
-2
47 additions, 2 deletions
lighthouse.go
lighthouse_test.go
+1
-1
1 addition, 1 deletion
lighthouse_test.go
with
62 additions
and
9 deletions
devenv.lock
+
6
−
6
View file @
ddb13564
...
...
@@ -74,11 +74,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1698
288402
,
"narHash": "sha256-
jIIjApPdm+4yt8PglX8pUOexAdEiAax/DXW3S/Mb21E
=",
"lastModified": 1698
434055
,
"narHash": "sha256-
Phxi5mUKSoL7A0IYUiYtkI9e8NcGaaV5PJEaJApU1Ko
=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "
60b9db998f71ea49e1a9c41824d09aa274be1344
",
"rev": "
1a3c95e3b23b3cdb26750621c08cc2f1560cb883
",
"type": "github"
},
"original": {
...
...
@@ -106,11 +106,11 @@
},
"nixpkgs_2": {
"locked": {
"lastModified": 1698
288402
,
"narHash": "sha256-
jIIjApPdm+4yt8PglX8pUOexAdEiAax/DXW3S/Mb21E
=",
"lastModified": 1698
434055
,
"narHash": "sha256-
Phxi5mUKSoL7A0IYUiYtkI9e8NcGaaV5PJEaJApU1Ko
=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "
60b9db998f71ea49e1a9c41824d09aa274be1344
",
"rev": "
1a3c95e3b23b3cdb26750621c08cc2f1560cb883
",
"type": "github"
},
"original": {
...
...
This diff is collapsed.
Click to expand it.
error.go
+
8
−
0
View file @
ddb13564
...
...
@@ -56,3 +56,11 @@ type WatcherAlreadyActiveError struct{}
func
(
e
WatcherAlreadyActiveError
)
Error
()
string
{
return
"Watcher is already active"
}
type
PathNotExistingError
struct
{
Path
string
}
func
(
e
PathNotExistingError
)
Error
()
string
{
return
fmt
.
Sprintf
(
"Path %s does not exist"
,
e
.
Path
)
}
This diff is collapsed.
Click to expand it.
lighthouse.go
+
47
−
2
View file @
ddb13564
...
...
@@ -3,6 +3,7 @@ package watch
import
(
"fmt"
"github.com/fsnotify/fsnotify"
"os"
"path/filepath"
"sync"
"time"
...
...
@@ -183,12 +184,38 @@ func (l *lighthouse) IsActive() bool {
return
l
.
active
}
// normalizePath returns the normalized path. If the path is a directory, a
// trailing slash is added.
// If the path does not exist, an error is returned.
// If the path is a directory, the second return value is true.
func
normalizePath
(
path
string
)
(
string
,
bool
,
error
)
{
isDir
:=
false
fi
,
err
:=
os
.
Stat
(
path
)
if
err
!=
nil
{
return
""
,
isDir
,
err
}
// normalize is path is directory
if
fi
.
IsDir
()
{
path
=
filepath
.
Clean
(
path
)
isDir
=
true
}
return
path
,
isDir
,
nil
}
// Add adds a path to the watcher. If the path is already being watched or the
// watcher is not active, an error is returned.
func
(
l
*
lighthouse
)
Add
(
watch
*
Watch
)
error
{
l
.
mutex
.
Lock
()
defer
l
.
mutex
.
Unlock
()
var
err
error
if
!
l
.
active
{
return
LighthouseNotActiveError
{}
}
...
...
@@ -197,8 +224,13 @@ func (l *lighthouse) Add(watch *Watch) error {
return
AlreadyWatchedPathError
{
AlreadyWatched
:
watch
.
Path
}
}
watch
.
Path
,
_
,
err
=
normalizePath
(
watch
.
Path
)
if
err
!=
nil
{
return
err
}
l
.
watchers
[
watch
.
Path
]
=
watch
err
:
=
l
.
fsnotify
.
Add
(
watch
.
Path
)
err
=
l
.
fsnotify
.
Add
(
watch
.
Path
)
if
err
!=
nil
{
return
err
}
...
...
@@ -216,6 +248,8 @@ func (l *lighthouse) Remove(path string) error {
return
LighthouseNotActiveError
{}
}
path
,
_
,
_
=
normalizePath
(
path
)
if
_
,
ok
:=
l
.
watchers
[
path
];
!
ok
{
return
UnwatchedPathError
{
UnwatchedPath
:
path
}
}
...
...
@@ -249,6 +283,8 @@ func (l *lighthouse) IsWatched(path string) bool {
return
false
}
path
,
_
,
_
=
normalizePath
(
path
)
if
_
,
ok
:=
l
.
watchers
[
path
];
!
ok
{
return
false
}
...
...
@@ -268,10 +304,17 @@ func (l *lighthouse) Get(path string) (*Watch, error) {
var
watch
*
Watch
var
ok
bool
var
isDir
bool
path
,
isDir
,
_
=
normalizePath
(
path
)
if
watch
,
ok
=
l
.
watchers
[
path
];
!
ok
{
if
isDir
{
return
nil
,
UnwatchedPathError
{
UnwatchedPath
:
path
}
}
if
watch
,
ok
=
l
.
watchers
[
filepath
.
Dir
(
path
)];
!
ok
{
dir
:=
filepath
.
Dir
(
path
)
if
watch
,
ok
=
l
.
watchers
[
dir
];
!
ok
{
return
nil
,
UnwatchedPathError
{
UnwatchedPath
:
path
}
}
...
...
@@ -292,6 +335,8 @@ func (l *lighthouse) IsActiveWatched(path string) bool {
l
.
mutex
.
Lock
()
defer
l
.
mutex
.
Unlock
()
path
,
_
,
_
=
normalizePath
(
path
)
// check fsnotify
for
_
,
fsPath
:=
range
l
.
fsnotify
.
WatchList
()
{
if
fsPath
==
path
{
...
...
This diff is collapsed.
Click to expand it.
lighthouse_test.go
+
1
−
1
View file @
ddb13564
...
...
@@ -102,7 +102,7 @@ func TestWatchList(t *testing.T) {
assert
.
Equal
(
t
,
tempDir
,
l
.
WatchList
()[
0
])
}
//
t
est
isinsync
//
T
est
IsInSync with a file that is not watched by fsnotify
func
TestIsInSync
(
t
*
testing
.
T
)
{
tempDir
,
err
:=
os
.
MkdirTemp
(
""
,
"watchtest"
)
assert
.
Nil
(
t
,
err
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment