5 // Copyright (C) 2004 Novell, Inc.
9 // Permission is hereby granted, free of charge, to any person obtaining a
10 // copy of this software and associated documentation files (the "Software"),
11 // to deal in the Software without restriction, including without limitation
12 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 // and/or sell copies of the Software, and to permit persons to whom the
14 // Software is furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 // DEALINGS IN THE SOFTWARE.
29 using System
.Collections
;
35 namespace Beagle
.Daemon
.EvolutionMailDriver
{
38 public delegate void ItemAddedHandler (FileInfo file
);
40 private static bool Debug
= false;
42 ArrayList roots
= new ArrayList ();
44 Hashtable last_write_time_cache
= new Hashtable ();
46 public ItemAddedHandler MboxAddedEvent
;
47 public ItemAddedHandler SummaryAddedEvent
;
49 public MailCrawler (params string[] paths
)
51 foreach (string p
in paths
) {
52 if (Directory
.Exists (p
))
57 private bool FileIsInteresting (FileInfo file
)
62 DateTime cached_time
= new DateTime ();
63 if (last_write_time_cache
.Contains (file
.FullName
))
64 cached_time
= (DateTime
) last_write_time_cache
[file
.FullName
];
66 last_write_time_cache
[file
.FullName
] = file
.LastWriteTimeUtc
;
68 return cached_time
< file
.LastWriteTimeUtc
;
71 private void OnInotifyEvent (Inotify
.Watch watch
,
75 Inotify
.EventType type
)
80 string full_path
= Path
.Combine (path
, subitem
);
82 if ((type
& Inotify
.EventType
.Create
) != 0 && (type
& Inotify
.EventType
.IsDirectory
) != 0) {
87 if ((type
& Inotify
.EventType
.Delete
) != 0 && (type
& Inotify
.EventType
.IsDirectory
) != 0) {
92 if ((type
& Inotify
.EventType
.MovedTo
) != 0) {
93 if (subitem
== "summary") {
95 if (SummaryAddedEvent
!= null && File
.Exists (full_path
)) {
96 Logger
.Log
.Info ("Reindexing updated IMAP summary: {0}", full_path
);
97 SummaryAddedEvent (new FileInfo (full_path
));
99 } else if (Path
.GetExtension (full_path
) == ".ev-summary") {
101 string mbox_file
= Path
.ChangeExtension (full_path
, null);
102 if (MboxAddedEvent
!= null && File
.Exists (mbox_file
)) {
103 Logger
.Log
.Info ("Reindexing updated mbox: {0}", mbox_file
);
104 MboxAddedEvent (new FileInfo (mbox_file
));
110 private void Watch (string root
)
112 Queue pending
= new Queue ();
114 pending
.Enqueue (root
);
116 while (pending
.Count
> 0) {
118 string dir
= (string) pending
.Dequeue ();
120 foreach (string subdir
in DirectoryWalker
.GetDirectories (dir
)) {
121 if (Shutdown
.ShutdownRequested
)
124 if (Inotify
.Enabled
) {
125 Inotify
.Subscribe (subdir
, OnInotifyEvent
,
126 Inotify
.EventType
.Create
127 | Inotify
.EventType
.Delete
128 | Inotify
.EventType
.MovedTo
);
131 pending
.Enqueue (subdir
);
134 Stopwatch watch
= new Stopwatch ();
136 Logger
.Log
.Debug ("Starting watch on {0}", dir
);
138 foreach (string path
in DirectoryWalker
.GetItems (dir
, new DirectoryWalker
.FileFilter (IsSummary
))) {
139 if (Shutdown
.ShutdownRequested
)
142 FileInfo file
= new FileInfo (path
);
144 if (file
.Name
== "summary") {
145 if (SummaryAddedEvent
!= null && FileIsInteresting (file
))
146 SummaryAddedEvent (file
);
147 } else if (file
.Extension
== ".ev-summary") {
148 string mbox_name
= Path
.Combine (file
.DirectoryName
,
149 Path
.GetFileNameWithoutExtension (file
.Name
));
150 FileInfo mbox_file
= new FileInfo (mbox_name
);
151 if (MboxAddedEvent
!= null && FileIsInteresting (mbox_file
))
152 MboxAddedEvent (mbox_file
);
157 Logger
.Log
.Debug ("Crawled {0} in {1}", dir
, watch
);
161 private bool IsSummary (string path
, string name
)
163 if (name
== "summary")
166 if (Path
.GetExtension (name
) == ".ev-summary")
174 foreach (string root
in roots
) {
175 if (Shutdown
.ShutdownRequested
)