Some more fixes wrt child-indexables. Namely, fix proper handling of child indexables...
[beagle.git] / beagled / FileSystemQueryable / FileCrawlTask.cs
blob468bd9d866bc831ed199a3ce2782bbd5361acad6
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 // If our last generator is still doing stuff, just reschedule
72 // and return. This keeps us from generating more tasks until
73 // the last one we started runs to completion.
74 if ((current_generator != null && current_generator.HasNextIndexable ())
75 || current_dir != null) {
76 Reschedule = true;
77 return;
80 lock (big_lock) {
81 Log.Debug ("Running file crawl task");
82 is_active = true;
83 current_dir = queryable.GetNextDirectoryToCrawl ();
84 if (current_dir == null) {
85 Log.Debug ("Done crawling files!!!");
86 is_active = false;
87 return;
91 if (!current_dir.IsAttached) {
92 Reschedule = true;
93 return;
96 if (FileSystemQueryable.Debug)
97 Logger.Log.Debug ("Starting crawl of '{0}'", current_dir.FullName);
99 // Schedule a DirectoryIndexableGenerator
100 // for that directory, and then reschedule ourselves.
101 try {
102 current_generator = new DirectoryIndexableGenerator (queryable, current_dir);
103 } catch (DirectoryNotFoundException ex) {
104 Logger.Log.Debug ("Couldn't crawl '{0}'", current_dir.FullName);
106 // FIXME: If our attempt to crawl the directory fails, just
107 // mark it as uncrawlable and move on. This isn't optimal behavior,
108 // but works around bugs involving weird permissions for now.
109 current_dir.MarkAsUncrawlable ();
110 current_dir = null;
113 if (current_generator != null) {
114 Scheduler.TaskGroup group;
115 group = Scheduler.NewTaskGroup ("Crawl task group", null, our_post_hook);
117 Scheduler.Task task;
118 task = queryable.NewAddTask (current_generator);
119 task.AddTaskGroup (group);
120 SpawnChild (task);
123 Reschedule = true;