Files
scoutfs-go/examples/count-changes/main.go
2018-10-03 11:11:27 -07:00

83 lines
1.5 KiB
Go

// Copyright (c) 2018 Versity Software, Inc.
//
// Use of this source code is governed by a BSD-3-Clause license
// that can be found in the LICENSE file in the root of the source
// tree.
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
scoutfs "github.com/versity/scoutfs-go"
)
const (
max32 = 0xffffffff
max64 = 0xffffffffffffffff
)
type server struct {
update <-chan int
lastcount int
}
func queryPopulation(basedir string, update chan<- int) error {
h, err := scoutfs.NewQuery(basedir,
scoutfs.ByMSeq(scoutfs.InodesEntry{},
scoutfs.InodesEntry{Major: max64, Minor: max32, Ino: max64}))
if err != nil {
return fmt.Errorf("scoutfs new handle: %v", err)
}
defer h.Close()
count := 0
for {
for {
qents, err := h.Next()
if err != nil {
return fmt.Errorf("scoutfs next: %v", err)
}
if len(qents) == 0 {
break
}
for _, qent := range qents {
fmt.Printf("%#v\n", qent)
count++
}
}
update <- count
time.Sleep(500 * time.Millisecond)
}
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
select {
case x, ok := <-s.update:
if ok {
s.lastcount = x
} else {
fmt.Println("Channel closed!")
os.Exit(1)
}
default:
}
fmt.Fprintf(w, "%s %d", `
<HEAD><meta HTTP-EQUIV="REFRESH" content="1"></HEAD>
<h1>SCOUTFS</h1></p>
Files/Directories updated:`, s.lastcount)
}
func main() {
update := make(chan int, 1)
s := &server{update: update}
go queryPopulation(os.Args[1], update)
log.Fatal(http.ListenAndServe(":8080", s))
}