Remove debug statements from KCal backends.
[beagle.git] / beagled / FileSystemQueryable / FileCrawlTask.cs
blob2063f539154c3e67ca5c09a913f8aa09956d68b5
1 //
2 // FileCrawlTask.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.IO;
31 using Beagle.Daemon;
32 using Beagle.Util;
34 namespace Beagle.Daemon.FileSystemQueryable {
36 public class FileCrawlTask : Scheduler.Task {
38 object big_lock = new object ();
39 FileSystemQueryable queryable;
40 DirectoryModel current_dir = null;
41 IIndexableGenerator current_generator = null;
42 bool is_active = false;
44 Scheduler.Hook our_post_hook;
46 public FileCrawlTask (FileSystemQueryable queryable)
48 this.queryable = queryable;
49 this.Tag = "File Crawler";
50 this.Priority = Scheduler.Priority.Delayed;
52 this.our_post_hook = new Scheduler.Hook (PostCrawlHook);
55 public bool IsActive {
56 get { lock (big_lock) return is_active; }
59 private void PostCrawlHook ()
61 Logger.Log.Debug ("Done crawling '{0}'", current_dir.FullName);
63 queryable.DoneCrawlingOneDirectory (current_dir);
65 current_generator = null;
66 current_dir = null;
69 override protected void DoTaskReal ()
71 QueryableState old_state = queryable.State;
72 queryable.State = QueryableState.Crawling;
74 try {
75 DoCrawl ();
76 } finally {
77 queryable.State = old_state;
81 private void DoCrawl ()
83 // If our last generator is still doing stuff, just reschedule
84 // and return. This keeps us from generating more tasks until
85 // the last one we started runs to completion.
86 if ((current_generator != null && current_generator.HasNextIndexable ())
87 || current_dir != null) {
88 Reschedule = true;
89 return;
92 lock (big_lock) {
93 is_active = true;
94 current_dir = queryable.GetNextDirectoryToCrawl ();
95 if (current_dir == null) {
96 Logger.Log.Debug ("Done crawling!!!!");
97 is_active = false;
98 return;
102 if (!current_dir.IsAttached) {
103 Reschedule = true;
104 return;
107 if (FileSystemQueryable.Debug)
108 Logger.Log.Debug ("Starting crawl of '{0}'", current_dir.FullName);
110 // Schedule a DirectoryIndexableGenerator
111 // for that directory, and then reschedule ourselves.
112 try {
113 current_generator = new DirectoryIndexableGenerator (queryable, current_dir);
114 } catch (DirectoryNotFoundException ex) {
115 Logger.Log.Debug ("Couldn't crawl '{0}'", current_dir.FullName);
117 // FIXME: If our attempt to crawl the directory fails, just
118 // mark it as uncrawlable and move on. This isn't optimal behavior,
119 // but works around bugs involving weird permissions for now.
120 current_dir.MarkAsUncrawlable ();
121 current_dir = null;
124 if (current_generator != null) {
125 Scheduler.TaskGroup group;
126 group = Scheduler.NewTaskGroup ("Crawl task group", null, our_post_hook);
128 Scheduler.Task task;
129 task = queryable.NewAddTask (current_generator);
130 task.AddTaskGroup (group);
131 SpawnChild (task);
134 Reschedule = true;