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