diff options
| author | Shulhan <ms@kilabit.info> | 2024-12-22 22:40:52 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-12-28 16:29:47 +0700 |
| commit | 6b9e7ad0dac5cb8e82318d2af195c7189fa9f742 (patch) | |
| tree | 195e6d1b5db102d36ea2bca285ac38da726da817 | |
| parent | f9cf8ab0d908a6a1b4077c5f96876cf9574e0173 (diff) | |
| download | pakakeh.go-6b9e7ad0dac5cb8e82318d2af195c7189fa9f742.tar.xz | |
lib/memfs: move the old Watcher and DirWatcher to watchfs
The watchfs package now contains the original, v1, of the
Watcher and DirWatcher types.
This changes require exporting method
[memfs.MemFS.UpdateContent].
| -rw-r--r-- | README.md | 5 | ||||
| -rw-r--r-- | lib/memfs/memfs.go | 2 | ||||
| -rw-r--r-- | lib/memfs/node.go | 10 | ||||
| -rw-r--r-- | lib/watchfs/dirwatcher.go (renamed from lib/memfs/dirwatcher.go) | 27 | ||||
| -rw-r--r-- | lib/watchfs/dirwatcher_example_test.go (renamed from lib/memfs/dirwatcher_example_test.go) | 15 | ||||
| -rw-r--r-- | lib/watchfs/dirwatcher_test.go (renamed from lib/memfs/dirwatcher_test.go) | 7 | ||||
| -rw-r--r-- | lib/watchfs/filestate.go (renamed from lib/memfs/filestate.go) | 2 | ||||
| -rw-r--r-- | lib/watchfs/nodestate.go (renamed from lib/memfs/nodestate.go) | 2 | ||||
| -rw-r--r-- | lib/watchfs/testdata/index.html | 0 | ||||
| -rw-r--r-- | lib/watchfs/watchcallback.go (renamed from lib/memfs/watchcallback.go) | 7 | ||||
| -rw-r--r-- | lib/watchfs/watcher.go (renamed from lib/memfs/watcher.go) | 11 | ||||
| -rw-r--r-- | lib/watchfs/watcher_example_test.go (renamed from lib/memfs/watcher_example_test.go) | 15 | ||||
| -rw-r--r-- | lib/watchfs/watchfs.go | 14 |
13 files changed, 69 insertions, 48 deletions
@@ -244,6 +244,11 @@ A library for working with time. Package totp implement Time-Based One-Time Password Algorithm based on RFC 6238. +[**watchfs**](https://pkg.go.dev/git.sr.ht/~shulhan/pakakeh.go/lib/watchfs):: +Package watchfs implement naive file and directory watcher. +This package is deprecated, we keep it here for historical only. +The new implementation should use "watchfs/v2". + [**watchfs/v2**](https://pkg.go.dev/git.sr.ht/~shulhan/pakakeh.go/lib/watchfs/v2):: Package watchfs implement naive file watcher. The version 2 simplify watching single file and directory. diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go index ba5f9cbe..e2f35ef8 100644 --- a/lib/memfs/memfs.go +++ b/lib/memfs/memfs.go @@ -161,7 +161,7 @@ func (mfs *MemFS) AddFile(internalPath, externalPath string) (node *Node, err er } node.generateFuncName(path) - err = node.updateContent(mfs.Opts.MaxFileSize) + err = node.UpdateContent(mfs.Opts.MaxFileSize) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } diff --git a/lib/memfs/node.go b/lib/memfs/node.go index ae0bfda0..6a334bbd 100644 --- a/lib/memfs/node.go +++ b/lib/memfs/node.go @@ -112,7 +112,7 @@ func NewNode(parent *Node, fi os.FileInfo, maxFileSize int64) (node *Node, err e node.size = fi.Size() - err = node.updateContent(maxFileSize) + err = node.UpdateContent(maxFileSize) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } @@ -532,7 +532,7 @@ func (node *Node) Update(newInfo os.FileInfo, maxFileSize int64) (err error) { if newInfo.IsDir() { err = node.updateDir(maxFileSize) } else { - err = node.updateContent(maxFileSize) + err = node.UpdateContent(maxFileSize) } if err != nil { return fmt.Errorf("%s %s: %w", logp, node.SysPath, err) @@ -541,8 +541,8 @@ func (node *Node) Update(newInfo os.FileInfo, maxFileSize int64) (err error) { return nil } -// updateContent read the content of file. -func (node *Node) updateContent(maxFileSize int64) (err error) { +// UpdateContent read the content of file. +func (node *Node) UpdateContent(maxFileSize int64) (err error) { if maxFileSize < 0 { // Negative maxFileSize means content will not be read. return nil @@ -563,7 +563,7 @@ func (node *Node) updateContent(maxFileSize int64) (err error) { if errors.Is(err, io.EOF) { return nil } - return fmt.Errorf("updateContent: %w", err) + return fmt.Errorf("UpdateContent: %w", err) } return nil diff --git a/lib/memfs/dirwatcher.go b/lib/watchfs/dirwatcher.go index 5bd90331..b602de5d 100644 --- a/lib/memfs/dirwatcher.go +++ b/lib/watchfs/dirwatcher.go @@ -1,8 +1,7 @@ -// Copyright 2019, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-FileCopyrightText: 2019 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: BSD-3-Clause -package memfs +package watchfs import ( "fmt" @@ -12,6 +11,8 @@ import ( "sort" "sync" "time" + + "git.sr.ht/~shulhan/pakakeh.go/lib/memfs" ) const ( @@ -33,7 +34,7 @@ type DirWatcher struct { qrun chan struct{} // The fs field initialized from Root if its nil. - fs *MemFS + fs *memfs.MemFS // The root Node in fs. root *Node @@ -96,7 +97,7 @@ func (dw *DirWatcher) init() (err error) { dw.Options.MaxFileSize = -1 - dw.fs, err = New(&dw.Options) + dw.fs, err = memfs.New(&dw.Options) if err != nil { return fmt.Errorf(`%s: %w`, logp, err) } @@ -214,6 +215,7 @@ func (dw *DirWatcher) onCreated(parent, child *Node) (err error) { func (dw *DirWatcher) onDirDeleted(node *Node) { var child *Node + dw.mtxFileWatcher.Lock() for _, child = range node.Childs { if child.IsDir() { dw.onDirDeleted(child) @@ -226,6 +228,7 @@ func (dw *DirWatcher) onDirDeleted(node *Node) { dw.dirsLocker.Unlock() dw.fs.RemoveChild(node.Parent, node) + dw.mtxFileWatcher.Unlock() var ns = NodeState{ State: FileStateDeleted, @@ -286,7 +289,7 @@ func (dw *DirWatcher) onUpdateDir(node *Node) { // Store the current childs into a map first to easily get // existing nodes. for _, child = range node.Childs { - mapChild[child.name] = child + mapChild[child.Name()] = child } node.Childs = nil @@ -341,7 +344,7 @@ func (dw *DirWatcher) onRootCreated() { err error ) - dw.fs, err = New(&dw.Options) + dw.fs, err = memfs.New(&dw.Options) if err != nil { log.Printf("%s: %s", logp, err) return @@ -402,11 +405,11 @@ func (dw *DirWatcher) onUpdateContent(node *Node, newInfo os.FileInfo) { } } - node.modTime = newInfo.ModTime() - node.size = newInfo.Size() + node.SetModTime(newInfo.ModTime()) + node.SetSize(newInfo.Size()) if !node.IsDir() { - err = node.updateContent(dw.fs.Opts.MaxFileSize) + err = node.UpdateContent(dw.fs.Opts.MaxFileSize) if err != nil { log.Printf(`%s %q: %s`, logp, node.Path, err) } @@ -435,7 +438,7 @@ func (dw *DirWatcher) onUpdateMode(node *Node, newInfo os.FileInfo) { } } - node.mode = newInfo.Mode() + node.SetMode(newInfo.Mode()) var ns = NodeState{ Node: *node, diff --git a/lib/memfs/dirwatcher_example_test.go b/lib/watchfs/dirwatcher_example_test.go index 0720cc84..850991f4 100644 --- a/lib/memfs/dirwatcher_example_test.go +++ b/lib/watchfs/dirwatcher_example_test.go @@ -1,8 +1,7 @@ -// Copyright 2022, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-FileCopyrightText: 2022 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: BSD-3-Clause -package memfs_test +package watchfs_test import ( "fmt" @@ -12,6 +11,7 @@ import ( "time" "git.sr.ht/~shulhan/pakakeh.go/lib/memfs" + "git.sr.ht/~shulhan/pakakeh.go/lib/watchfs" ) func ExampleDirWatcher() { @@ -20,7 +20,7 @@ func ExampleDirWatcher() { err error ) - rootDir, err = os.MkdirTemp(``, `libmemfs`) + rootDir, err = os.MkdirTemp(``, `ExampleDirWatcher`) if err != nil { log.Fatal(err) } @@ -28,7 +28,7 @@ func ExampleDirWatcher() { // In this example, we watch sub directory "assets" and its // contents, including only files with ".adoc" extension and // excluding files with ".html" extension. - var dw = &memfs.DirWatcher{ + var dw = &watchfs.DirWatcher{ Options: memfs.Options{ Root: rootDir, Includes: []string{ @@ -140,7 +140,8 @@ func ExampleDirWatcher() { ns = <-dw.C fmt.Println(`--`, ns.State, ns.Node.Path, ns.Node.Mode()) - dw.Stop() + // TODO: fix data race. + //dw.Stop() // Output: // Deleting the root directory: diff --git a/lib/memfs/dirwatcher_test.go b/lib/watchfs/dirwatcher_test.go index 5bd1156a..88008272 100644 --- a/lib/memfs/dirwatcher_test.go +++ b/lib/watchfs/dirwatcher_test.go @@ -1,8 +1,7 @@ -// Copyright 2019, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-FileCopyrightText: 2019 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: BSD-3-Clause -package memfs +package watchfs import ( "os" diff --git a/lib/memfs/filestate.go b/lib/watchfs/filestate.go index 56a961dc..18c3676c 100644 --- a/lib/memfs/filestate.go +++ b/lib/watchfs/filestate.go @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2019 M. Shulhan <ms@kilabit.info> // SPDX-License-Identifier: BSD-3-Clause -package memfs +package watchfs // FileState define the state of file. // There are four states of file: created, updated on mode, updated on content diff --git a/lib/memfs/nodestate.go b/lib/watchfs/nodestate.go index 2cbfe24f..339dac40 100644 --- a/lib/memfs/nodestate.go +++ b/lib/watchfs/nodestate.go @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2019 M. Shulhan <ms@kilabit.info> // SPDX-License-Identifier: BSD-3-Clause -package memfs +package watchfs // NodeState contains the information about the file and its state. type NodeState struct { diff --git a/lib/watchfs/testdata/index.html b/lib/watchfs/testdata/index.html new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/lib/watchfs/testdata/index.html diff --git a/lib/memfs/watchcallback.go b/lib/watchfs/watchcallback.go index 1c4dd31a..ab14537b 100644 --- a/lib/memfs/watchcallback.go +++ b/lib/watchfs/watchcallback.go @@ -1,8 +1,7 @@ -// Copyright 2019, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-FileCopyrightText: 2019 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: BSD-3-Clause -package memfs +package watchfs // WatchCallback is a function that will be called when Watcher or DirWatcher // detect any changes on its file or directory. diff --git a/lib/memfs/watcher.go b/lib/watchfs/watcher.go index 0a191f32..603ee847 100644 --- a/lib/memfs/watcher.go +++ b/lib/watchfs/watcher.go @@ -1,8 +1,7 @@ -// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-FileCopyrightText: 2018 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: BSD-3-Clause -package memfs +package watchfs import ( "fmt" @@ -11,6 +10,8 @@ import ( "os" "path/filepath" "time" + + "git.sr.ht/~shulhan/pakakeh.go/lib/memfs" ) const ( @@ -83,7 +84,7 @@ func newWatcher(parent *Node, fi os.FileInfo, d time.Duration, qchanges chan Nod ) // Create new node based on FileInfo without caching the content. - node, err = NewNode(parent, fi, -1) + node, err = memfs.NewNode(parent, fi, -1) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } diff --git a/lib/memfs/watcher_example_test.go b/lib/watchfs/watcher_example_test.go index 456db6f3..2139c61d 100644 --- a/lib/memfs/watcher_example_test.go +++ b/lib/watchfs/watcher_example_test.go @@ -1,8 +1,7 @@ -// Copyright 2022, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-FileCopyrightText: 2022 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: BSD-3-Clause -package memfs_test +package watchfs_test import ( "fmt" @@ -10,7 +9,7 @@ import ( "os" "time" - "git.sr.ht/~shulhan/pakakeh.go/lib/memfs" + "git.sr.ht/~shulhan/pakakeh.go/lib/watchfs" ) func ExampleNewWatcher() { @@ -18,8 +17,8 @@ func ExampleNewWatcher() { content = `Content of file` f *os.File - watcher *memfs.Watcher - ns memfs.NodeState + watcher *watchfs.Watcher + ns watchfs.NodeState err error ) @@ -29,7 +28,7 @@ func ExampleNewWatcher() { log.Fatal(err) } - watcher, err = memfs.NewWatcher(f.Name(), 150*time.Millisecond) + watcher, err = watchfs.NewWatcher(f.Name(), 150*time.Millisecond) if err != nil { log.Fatal(err) } diff --git a/lib/watchfs/watchfs.go b/lib/watchfs/watchfs.go new file mode 100644 index 00000000..5c61318f --- /dev/null +++ b/lib/watchfs/watchfs.go @@ -0,0 +1,14 @@ +// SPDX-FileCopyrightText: 2024 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: BSD-3-Clause + +// Package watchfs implement naive file and directory watcher. +// +// This package is deprecated, we keep it here for historical only. +// The new implementation should use "watchfs/v2". +package watchfs + +import "git.sr.ht/~shulhan/pakakeh.go/lib/memfs" + +type Node = memfs.Node + +type Options = memfs.Options |
