示例#1
0
        public bool IsIdentical(DirectoryPicture otherSnapshot)
        {
            bool bResult = false;

            if (m_snapshot.Count > 0 && m_snapshot.Count == otherSnapshot.m_snapshot.Count)
            {
                int exact = 0;
                foreach (string name in m_snapshot.Keys)
                {
                    Win32FindData local = (Win32FindData)m_snapshot[name];
                    Win32FindData other = (Win32FindData)otherSnapshot.m_snapshot[name];
                    if (other != null)
                    {
                        if (local.Equal(other))
                        {
                            exact++;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                bResult = (exact == m_snapshot.Count);
            }
            return(bResult);
        }
示例#2
0
 public void AddSnapshoot(Guid guid, DirectoryPicture picture)
 {
     if (picture != null)
     {
         Snapshoot s = new Snapshoot();
         s.m_guid = guid;
         foreach (FileNotify2.DirectoryPicture.Win32FindData data in picture.m_snapshot.Values)
         {
             s.m_files.Add(data);
         }
         m_snapshoots.Add(s);
     }
 }
示例#3
0
        public DirectoryPicture ToDirectoryPicture(Guid guid)
        {
            DirectoryPicture result = new DirectoryPicture();

            foreach (Snapshoot ss in m_snapshoots)
            {
                if (ss.m_guid == guid)
                {
                    foreach (FileNotify2.DirectoryPicture.Win32FindData file in ss.m_files)
                    {
                        result.m_snapshot[file.cFileName] = file;
                    }
                }
            }
            return(result);
        }
示例#4
0
        public static DirectoryPicture TakeSnapshot(string directory)
        {
            //Console.WriteLine("Snapshot " + directory);
            DirectoryPicture result = new DirectoryPicture();
            IntPtr           INVALID_HANDLE_VALUE = new IntPtr(-1);
            WIN32_FIND_DATA  findData;
            IntPtr           findHandle = FindFirstFile(directory, out findData);

            if (findHandle != INVALID_HANDLE_VALUE)
            {
                do
                {
                    if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
                    {
                        result.m_snapshot[findData.cFileName] = new Win32FindData(findData);
                    }
                }while (FindNextFile(findHandle, out findData));
                FindClose(findHandle);
            }
            return(result);
        }
示例#5
0
        public void Start()
        {
            bool     exit     = false;
            Settings settings = Settings.LoadFromFile(AppDomain.CurrentDomain.BaseDirectory + "\\fileNotify2.xml");

            // Try to load script assembly
            foreach (Setting setting in settings.m_settings)
            {
                setting.m_lastExecute = DateTime.Now;
                IScript script = LoadScript(setting.m_guid.ToString() + ".dll");
                if (script != null)
                {
                    Factory.GetInstance().AddInstance(setting.m_guid, script);
                }
            }
            Snapshoots snapshoots = Snapshoots.LoadFromFile(AppDomain.CurrentDomain.BaseDirectory + "\\snapshoots.xml");

            foreach (Setting setting in settings.m_settings)
            {
                setting.m_lastPicture = snapshoots.ToDirectoryPicture(setting.m_guid);
            }
            do
            {
                // Find next directory snapshot
                DateTime now    = DateTime.Now;
                TimeSpan nextts = TimeSpan.MaxValue;
                Setting  nexts  = null;
                foreach (Setting setting in settings.m_settings)
                {
                    if (setting.m_active)
                    {
                        TimeSpan next = setting.m_cron.GetNextTime(now) - now;
                        if (next < nextts)
                        {
                            nextts = next;
                            nexts  = setting;
                        }
                    }
                }
                if (nexts != null)
                {
                    if (nexts.m_lastExecute.AddSeconds(5) > now)
                    {
                        nextts = nexts.m_cron.GetNextTime(now.AddSeconds(5)) - now;
                    }
                    // Wait before doing the snapshot
                    Console.WriteLine("Wait {0} before executin {1}", nextts, nexts.m_name);
                    if (m_exit.WaitOne(nextts, false))
                    {
                        exit = true;
                    }
                    else
                    {
                        Console.WriteLine("Execute " + nexts.m_name);
                        nexts.m_lastExecute = DateTime.Now;
                        // Do the snapshoot
                        if (string.IsNullOrEmpty(nexts.m_filter))
                        {
                            nexts.m_filter = "*";
                        }
                        DirectoryPicture picture = DirectoryPicture.TakeSnapshot(nexts.m_directory + "\\" + nexts.m_filter);

                        if (nexts.m_lastPicture != null)
                        {
                            if (!nexts.m_lastPicture.IsIdentical(picture))
                            {
                                IScript script = Factory.GetInstance().FindInstance(nexts.m_guid);
                                if (script == null && !string.IsNullOrEmpty(nexts.m_script))
                                {
                                    script = CompileScript(nexts);
                                    if (script != null)
                                    {
                                        Factory.GetInstance().AddInstance(nexts.m_guid, script);
                                    }
                                    else
                                    {
                                        Console.WriteLine("Error compiling {0}", nexts.m_name);
                                    }
                                }
                                if (script != null)
                                {
                                    script.m_directory = nexts.m_directory;
                                    // Missing files
                                    foreach (FileNotify2.DirectoryPicture.Win32FindData data in nexts.m_lastPicture.m_snapshot.Values)
                                    {
                                        if (!picture.m_snapshot.ContainsKey(data.cFileName))
                                        {
                                            script.DeletedFile(data);
                                        }
                                    }
                                    // New files
                                    foreach (FileNotify2.DirectoryPicture.Win32FindData data in picture.m_snapshot.Values)
                                    {
                                        if (!nexts.m_lastPicture.m_snapshot.ContainsKey(data.cFileName))
                                        {
                                            script.NewFile(data);
                                        }
                                    }
                                    // Changed files and identical one
                                    foreach (FileNotify2.DirectoryPicture.Win32FindData data in nexts.m_lastPicture.m_snapshot.Values)
                                    {
                                        FileNotify2.DirectoryPicture.Win32FindData data2 = picture.m_snapshot[data.cFileName] as FileNotify2.DirectoryPicture.Win32FindData;
                                        if (data2 != null)
                                        {
                                            if (data2.Equal(data))
                                            {
                                                script.IdenticalFile(data);
                                            }
                                            else
                                            {
                                                script.ChangedFile(data, data2);
                                            }
                                        }
                                    }
                                    // Identical files
                                }
                            }
                        }
                        nexts.m_lastPicture = picture;
                    }
                }
                else
                {
                    exit = true; // All rule are desactivated
                }
            }while (!exit);
            // Persist snapshoots
            snapshoots = new Snapshoots();
            foreach (Setting s in settings.m_settings)
            {
                if (s.m_persistant)
                {
                    snapshoots.AddSnapshoot(s.m_guid, s.m_lastPicture);
                }
            }
            snapshoots.SaveToFile(AppDomain.CurrentDomain.BaseDirectory + "\\snapshoots.xml");
        }