Skip to content

hwch/fmonitor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

fmonitor
========

The file monitor

Cross platform, works on:

  Windows
  Linux
  BSD
  OSX


package main

import (
        "flag"
        "fmonitor"
        "fmt"
        "os"
        "runtime"
)

func main() {
        var fpath string
        var help bool
        runtime.GOMAXPROCS(runtime.NumCPU())
        flag.StringVar(&fpath, "path", "/tmp/test.txt", "--path The path to the file you want to monitor")
        flag.BoolVar(&help, "help", false, "-h  --help Show the help information")
        flag.BoolVar(&help, "h", false, "")
        flag.Usage = func() {
                fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
                fmt.Fprintf(os.Stderr, "-h  --help: Show the help information\n"+
                        "--path: --path The path to the file you want to monitor\n")
        }
        flag.Parse()

        if help {
                flag.Usage()
                return
        }
        if _, err := os.Stat(fpath); err != nil {
                if os.IsNotExist(err) {
                        fmt.Printf("The file '%s' is not exist !\n", fpath)
                        return
                }
        }
        cmc := fmonitor.NewFileMonitor(fpath)
        defer cmc.Release()
        go cmc.Monitoring()
        for {
                fo := cmc.GetFileOp()
                switch fo.GetOpValue() {
                case fmonitor.OP_ADD:
                        fmt.Printf("The file <%s> has been added.\n", fo.GetNameValue())
                case fmonitor.OP_DEL:
                        fmt.Printf("The file <%s> has been deleted.\n", fo.GetNameValue())
                case fmonitor.OP_MOD:
                        fmt.Printf("The file <%s> has been changed.\n", fo.GetNameValue())
                default:
                        fmt.Printf("Unknown operation on the file <%s>.\n", fo.GetNameValue())
                }
        }

}

About

The file monitor

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages