vendor/github.com/fsnotify/fsnotify/fsnotify.go
changeset 265 05c40b36d3b2
parent 260 445e01aede7e
equal deleted inserted replaced
264:8f478162d991 265:05c40b36d3b2
     1 // Copyright 2012 The Go Authors. All rights reserved.
       
     2 // Use of this source code is governed by a BSD-style
       
     3 // license that can be found in the LICENSE file.
       
     4 
       
     5 //go:build !plan9
     1 //go:build !plan9
     6 // +build !plan9
     2 // +build !plan9
     7 
     3 
     8 // Package fsnotify provides a platform-independent interface for file system notifications.
     4 // Package fsnotify provides a cross-platform interface for file system
       
     5 // notifications.
     9 package fsnotify
     6 package fsnotify
    10 
     7 
    11 import (
     8 import (
    12 	"bytes"
       
    13 	"errors"
     9 	"errors"
    14 	"fmt"
    10 	"fmt"
       
    11 	"strings"
    15 )
    12 )
    16 
    13 
    17 // Event represents a single file system notification.
    14 // Event represents a file system notification.
    18 type Event struct {
    15 type Event struct {
    19 	Name string // Relative path to the file or directory.
    16 	// Path to the file or directory.
    20 	Op   Op     // File operation that triggered the event.
    17 	//
       
    18 	// Paths are relative to the input; for example with Add("dir") the Name
       
    19 	// will be set to "dir/file" if you create that file, but if you use
       
    20 	// Add("/path/to/dir") it will be "/path/to/dir/file".
       
    21 	Name string
       
    22 
       
    23 	// File operation that triggered the event.
       
    24 	//
       
    25 	// This is a bitmask and some systems may send multiple operations at once.
       
    26 	// Use the Event.Has() method instead of comparing with ==.
       
    27 	Op Op
    21 }
    28 }
    22 
    29 
    23 // Op describes a set of file operations.
    30 // Op describes a set of file operations.
    24 type Op uint32
    31 type Op uint32
    25 
    32 
    26 // These are the generalized file operations that can trigger a notification.
    33 // The operations fsnotify can trigger; see the documentation on [Watcher] for a
       
    34 // full description, and check them with [Event.Has].
    27 const (
    35 const (
    28 	Create Op = 1 << iota
    36 	Create Op = 1 << iota
    29 	Write
    37 	Write
    30 	Remove
    38 	Remove
    31 	Rename
    39 	Rename
    32 	Chmod
    40 	Chmod
    33 )
    41 )
    34 
    42 
       
    43 // Common errors that can be reported by a watcher
       
    44 var (
       
    45 	ErrNonExistentWatch = errors.New("can't remove non-existent watcher")
       
    46 	ErrEventOverflow    = errors.New("fsnotify queue overflow")
       
    47 )
       
    48 
    35 func (op Op) String() string {
    49 func (op Op) String() string {
    36 	// Use a buffer for efficient string concatenation
    50 	var b strings.Builder
    37 	var buffer bytes.Buffer
    51 	if op.Has(Create) {
    38 
    52 		b.WriteString("|CREATE")
    39 	if op&Create == Create {
       
    40 		buffer.WriteString("|CREATE")
       
    41 	}
    53 	}
    42 	if op&Remove == Remove {
    54 	if op.Has(Remove) {
    43 		buffer.WriteString("|REMOVE")
    55 		b.WriteString("|REMOVE")
    44 	}
    56 	}
    45 	if op&Write == Write {
    57 	if op.Has(Write) {
    46 		buffer.WriteString("|WRITE")
    58 		b.WriteString("|WRITE")
    47 	}
    59 	}
    48 	if op&Rename == Rename {
    60 	if op.Has(Rename) {
    49 		buffer.WriteString("|RENAME")
    61 		b.WriteString("|RENAME")
    50 	}
    62 	}
    51 	if op&Chmod == Chmod {
    63 	if op.Has(Chmod) {
    52 		buffer.WriteString("|CHMOD")
    64 		b.WriteString("|CHMOD")
    53 	}
    65 	}
    54 	if buffer.Len() == 0 {
    66 	if b.Len() == 0 {
    55 		return ""
    67 		return "[no events]"
    56 	}
    68 	}
    57 	return buffer.String()[1:] // Strip leading pipe
    69 	return b.String()[1:]
    58 }
    70 }
    59 
    71 
    60 // String returns a string representation of the event in the form
    72 // Has reports if this operation has the given operation.
    61 // "file: REMOVE|WRITE|..."
    73 func (o Op) Has(h Op) bool { return o&h == h }
       
    74 
       
    75 // Has reports if this event has the given operation.
       
    76 func (e Event) Has(op Op) bool { return e.Op.Has(op) }
       
    77 
       
    78 // String returns a string representation of the event with their path.
    62 func (e Event) String() string {
    79 func (e Event) String() string {
    63 	return fmt.Sprintf("%q: %s", e.Name, e.Op.String())
    80 	return fmt.Sprintf("%-13s %q", e.Op.String(), e.Name)
    64 }
    81 }
    65 
       
    66 // Common errors that can be reported by a watcher
       
    67 var (
       
    68 	ErrEventOverflow = errors.New("fsnotify queue overflow")
       
    69 )