4 // Copyright (C) 2005 Novell, Inc.
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
28 using System
.Collections
;
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;
69 override protected void DoTaskReal ()
71 QueryableState old_state
= queryable
.State
;
72 queryable
.State
= QueryableState
.Crawling
;
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) {
94 current_dir
= queryable
.GetNextDirectoryToCrawl ();
95 if (current_dir
== null) {
96 Logger
.Log
.Debug ("Done crawling!!!!");
102 if (!current_dir
.IsAttached
) {
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.
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 ();
124 if (current_generator
!= null) {
125 Scheduler
.TaskGroup
group;
126 group = Scheduler
.NewTaskGroup ("Crawl task group", null, our_post_hook
);
129 task
= queryable
.NewAddTask (current_generator
);
130 task
.AddTaskGroup (group);