Exemplo n.º 1
0
        public static void Add(Entry e)
        {
            lock (All) All.Add(e);

            var dir  = Path.GetDirectoryName(e.Path);
            var name = Path.GetFileName(e.Path);

            if (!Directory.Exists(dir))
            {
                throw new DirectoryNotFoundException("Watch.FileLines: Directory not found: " + dir);
            }
            if (!File.Exists(e.Path))
            {
                throw new FileNotFoundException("Watch.FileLines: File not found: " + e.Path, e.Path);
            }

            if (e.Watch)
            {
                var fsw = new FileSystemWatcher(dir, name)
                {
                    IncludeSubdirectories = false,
                    NotifyFilter          = NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.FileName,
                };
                fsw.Created            += delegate { lock (e.Mutex) { e.LastMeta = FileMetaSnapshot.From(e.Path); Refresh(e); } };
                fsw.Changed            += delegate { lock (e.Mutex) { e.LastMeta = FileMetaSnapshot.From(e.Path); Refresh(e); } };
                fsw.Error              += delegate { lock (e.Mutex) { e.LastMeta = FileMetaSnapshot.From(e.Path); Refresh(e); } };
                fsw.Deleted            += delegate { lock (e.Mutex) { e.LastMeta = FileMetaSnapshot.From(e.Path); Refresh(e); } };
                fsw.Renamed            += delegate { lock (e.Mutex) { e.LastMeta = FileMetaSnapshot.From(e.Path); Refresh(e); } };
                fsw.EnableRaisingEvents = true;
            }

            Refresh(e);
        }
Exemplo n.º 2
0
        static bool PollFileMetaChanged(Entry e)           // FIXME: Rename to better indicate side effects?!?
        {
            var  current = FileMetaSnapshot.From(e.Path);
            bool changes = false;

            changes    = e.LastMeta != current;
            e.LastMeta = current;
            return(changes);
        }
Exemplo n.º 3
0
            public Entry(string path, bool watch, Action <IEnumerable <string> > onChanged, Action <Exception> onError)
            {
                Pending  = false;
                LastMeta = FileMetaSnapshot.From(path);

                Path      = path;
                Watch     = watch;
                OnChanged = onChanged;
                OnError   = onError;
            }
        public static FileMetaSnapshot From(string path)
        {
            var fi       = new FileInfo(path);
            var snapshot = new FileMetaSnapshot()
            {
                LastMod = fi.Exists ? fi.LastWriteTimeUtc : new DateTime(1971, 1, 1),
                Size    = fi.Exists ? fi.Length : 0,
            };

            return(snapshot);
        }