This repository has been archived by the owner on Dec 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
improve-hookfs #12
Merged
Merged
improve-hookfs #12
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
improve-hookfs
- Loading branch information
commit 4b014a7a7a506cdfe2ce43ae1573733247efbc79
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/qiffang/hookfs | ||
|
||
go 1.12 | ||
|
||
require ( | ||
github.com/hanwen/go-fuse v1.0.0 | ||
github.com/sirupsen/logrus v1.4.1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/hanwen/go-fuse v1.0.0 h1:GxS9Zrn6c35/BnfiVsZVWmsG803xwE7eVRDvcf/BEVc= | ||
github.com/hanwen/go-fuse v1.0.0/go.mod h1:unqXarDXqzAk0rt98O2tVndEPIpUgLD9+rwFisZH3Ok= | ||
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= | ||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/qiffang/hookfs v0.0.0-20180612002201-9afd0d5baf9f h1:b9X36mKCEznoe9t16NhQv3Y8Man/2t0dExJfl7nx8eE= | ||
github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k= | ||
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= | ||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= | ||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= | ||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8= | ||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package hookfs | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hanwen/go-fuse/fuse" | ||
"github.com/qiffang/hookfs/pkg/utils" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"syscall" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestRenameHook_RenameHookRenameTest(t *testing.T) { | ||
//init action | ||
original := filepath.Join(string(filepath.Separator), "tmp", fmt.Sprintf("dev-%d", time.Now().Unix())) | ||
mountpoint := filepath.Join(string(filepath.Separator), "tmp", fmt.Sprintf("mountpoint-%d", time.Now().Unix())) | ||
newFuseServer(t, original, mountpoint) | ||
//remember to call unmount after you do not use it | ||
defer cleanUp(mountpoint, original) | ||
|
||
//normal logic | ||
log.Print(filepath.Join(mountpoint, "tsdb.txt")) | ||
_, err := os.Create(filepath.Join(mountpoint, "tsdb.txt")) | ||
|
||
if err != nil { | ||
log.Printf("%v",err) | ||
} | ||
|
||
//rename should be failed | ||
err = os.Rename(filepath.Join(mountpoint, "tsdb.txt"), filepath.Join(mountpoint, "tsdbNew.txt")) | ||
utils.NotOk(t, err) | ||
fmt.Println(err) | ||
} | ||
|
||
func newFuseServer(t *testing.T, original,mountpoint string)(*fuse.Server){ | ||
createDirIfAbsent(original) | ||
createDirIfAbsent(mountpoint) | ||
fs, err := NewHookFs(original, mountpoint, &TestRenameHook{}) | ||
utils.Ok(t, err) | ||
server, err := fs.ServeAsync() | ||
if err != nil { | ||
log.Fatalf("start server failed, %v", err) | ||
} | ||
utils.Ok(t, err) | ||
|
||
return server | ||
} | ||
|
||
func cleanUp(mountpoint string, original string) { | ||
syscall.Unmount(mountpoint, -1) | ||
|
||
os.RemoveAll(mountpoint) | ||
os.RemoveAll(original) | ||
fmt.Println("Done") | ||
} | ||
|
||
func createDirIfAbsent(name string) { | ||
_, err := os.Stat(name) | ||
if err != nil { | ||
os.Mkdir(name, os.ModePerm) | ||
} | ||
} | ||
|
||
type TestRenameHook struct{} | ||
|
||
func (h *TestRenameHook) PreRename(oldPatgh string, newPath string) (hooked bool, ctx HookContext, err error) { | ||
fmt.Printf("Pre renamed file from %s to %s \n", oldPatgh, newPath) | ||
return true, nil, syscall.EIO | ||
} | ||
|
||
func (h *TestRenameHook) PostRename(oldPatgh string, newPath string) (hooked bool, ctx HookContext, err error) { | ||
fmt.Printf("Post renamed file from %s to %s \n", oldPatgh, newPath) | ||
return false, nil, nil | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
// Ok fails the test if an err is not nil. | ||
func Ok(tb testing.TB, err error) { | ||
if err != nil { | ||
_, file, line, _ := runtime.Caller(1) | ||
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error()) | ||
tb.FailNow() | ||
} | ||
} | ||
|
||
// NotOk fails the test if an err is nil. | ||
func NotOk(tb testing.TB, err error) { | ||
if err == nil { | ||
_, file, line, _ := runtime.Caller(1) | ||
fmt.Printf("\033[31m%s:%d: expected error, got nothing \033[39m\n\n", filepath.Base(file), line) | ||
tb.FailNow() | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
osrg/hooks ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated