github.com/FabianKramm/notify@v0.9.3-0.20210719135015-4705c29227a1/event_readdcw.go (about)

     1  // Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
     2  // Use of this source code is governed by the MIT license that can be
     3  // found in the LICENSE file.
     4  
     5  // +build windows
     6  
     7  package notify
     8  
     9  import (
    10  	"os"
    11  	"path/filepath"
    12  	"syscall"
    13  )
    14  
    15  // Platform independent event values.
    16  const (
    17  	osSpecificCreate Event = 1 << (20 + iota)
    18  	osSpecificRemove
    19  	osSpecificWrite
    20  	osSpecificRename
    21  	// recursive is used to distinguish recursive eventsets from non-recursive ones
    22  	recursive
    23  	// omit is used for dispatching internal events; only those events are sent
    24  	// for which both the event and the watchpoint has omit in theirs event sets.
    25  	omit
    26  	// dirmarker TODO(pknap)
    27  	dirmarker
    28  )
    29  
    30  // ReadDirectoryChangesW filters
    31  // On Windows the following events can be passed to Watch. A different set of
    32  // events (see actions below) are received on the channel passed to Watch.
    33  // For more information refer to
    34  // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365465(v=vs.85).aspx
    35  const (
    36  	FileNotifyChangeFileName   = Event(syscall.FILE_NOTIFY_CHANGE_FILE_NAME)
    37  	FileNotifyChangeDirName    = Event(syscall.FILE_NOTIFY_CHANGE_DIR_NAME)
    38  	FileNotifyChangeAttributes = Event(syscall.FILE_NOTIFY_CHANGE_ATTRIBUTES)
    39  	FileNotifyChangeSize       = Event(syscall.FILE_NOTIFY_CHANGE_SIZE)
    40  	FileNotifyChangeLastWrite  = Event(syscall.FILE_NOTIFY_CHANGE_LAST_WRITE)
    41  	FileNotifyChangeLastAccess = Event(syscall.FILE_NOTIFY_CHANGE_LAST_ACCESS)
    42  	FileNotifyChangeCreation   = Event(syscall.FILE_NOTIFY_CHANGE_CREATION)
    43  	FileNotifyChangeSecurity   = Event(syscallFileNotifyChangeSecurity)
    44  )
    45  
    46  const (
    47  	fileNotifyChangeAll      = 0x17f // logical sum of all FileNotifyChange* events.
    48  	fileNotifyChangeModified = fileNotifyChangeAll &^ (FileNotifyChangeFileName | FileNotifyChangeDirName)
    49  )
    50  
    51  // according to: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365465(v=vs.85).aspx
    52  // this flag should be declared in: http://golang.org/src/pkg/syscall/ztypes_windows.go
    53  const syscallFileNotifyChangeSecurity = 0x00000100
    54  
    55  // ReadDirectoryChangesW actions
    56  // The following events are returned on the channel passed to Watch, but cannot
    57  // be passed to Watch itself (see filters above). You can find a table showing
    58  // the relation between actions and filteres at
    59  // https://github.com/rjeczalik/notify/issues/10#issuecomment-66179535
    60  // The msdn documentation on actions is part of
    61  // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364391(v=vs.85).aspx
    62  const (
    63  	FileActionAdded          = Event(syscall.FILE_ACTION_ADDED) << 12
    64  	FileActionRemoved        = Event(syscall.FILE_ACTION_REMOVED) << 12
    65  	FileActionModified       = Event(syscall.FILE_ACTION_MODIFIED) << 14
    66  	FileActionRenamedOldName = Event(syscall.FILE_ACTION_RENAMED_OLD_NAME) << 15
    67  	FileActionRenamedNewName = Event(syscall.FILE_ACTION_RENAMED_NEW_NAME) << 16
    68  )
    69  
    70  const fileActionAll = 0x7f000 // logical sum of all FileAction* events.
    71  
    72  var osestr = map[Event]string{
    73  	FileNotifyChangeFileName:   "notify.FileNotifyChangeFileName",
    74  	FileNotifyChangeDirName:    "notify.FileNotifyChangeDirName",
    75  	FileNotifyChangeAttributes: "notify.FileNotifyChangeAttributes",
    76  	FileNotifyChangeSize:       "notify.FileNotifyChangeSize",
    77  	FileNotifyChangeLastWrite:  "notify.FileNotifyChangeLastWrite",
    78  	FileNotifyChangeLastAccess: "notify.FileNotifyChangeLastAccess",
    79  	FileNotifyChangeCreation:   "notify.FileNotifyChangeCreation",
    80  	FileNotifyChangeSecurity:   "notify.FileNotifyChangeSecurity",
    81  
    82  	FileActionAdded:          "notify.FileActionAdded",
    83  	FileActionRemoved:        "notify.FileActionRemoved",
    84  	FileActionModified:       "notify.FileActionModified",
    85  	FileActionRenamedOldName: "notify.FileActionRenamedOldName",
    86  	FileActionRenamedNewName: "notify.FileActionRenamedNewName",
    87  }
    88  
    89  const (
    90  	fTypeUnknown uint8 = iota
    91  	fTypeFile
    92  	fTypeDirectory
    93  )
    94  
    95  // TODO(ppknap) : doc.
    96  type event struct {
    97  	pathw  []uint16
    98  	name   string
    99  	ftype  uint8
   100  	action uint32
   101  	filter uint32
   102  	e      Event
   103  }
   104  
   105  func (e *event) Event() Event     { return e.e }
   106  func (e *event) Path() string     { return filepath.Join(syscall.UTF16ToString(e.pathw), e.name) }
   107  func (e *event) Sys() interface{} { return e.ftype }
   108  
   109  func (e *event) isDir() (bool, error) {
   110  	if e.ftype != fTypeUnknown {
   111  		return e.ftype == fTypeDirectory, nil
   112  	}
   113  	fi, err := os.Stat(e.Path())
   114  	if err != nil {
   115  		return false, err
   116  	}
   117  	return fi.IsDir(), nil
   118  }