cvsimport
[beagle.git] / beagled / FileSystemQueryable / TreeCrawlTask.cs
blob8e943997a31991bc95e3843bb011d4d1849ba8e9
1 //
2 // TreeCrawlTask.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 TreeCrawlTask : Scheduler.Task {
38 public delegate void Handler (DirectoryModel parent, string name);
40 private object big_lock = new object ();
41 private bool is_active = false;
42 private Handler handler;
43 private Queue to_be_crawled = new Queue ();
45 public TreeCrawlTask (Handler handler)
47 this.handler = handler;
48 this.Tag = "Tree Crawler";
49 this.Priority = Scheduler.Priority.Delayed;
52 public bool IsActive {
53 get { lock (big_lock) return is_active; }
56 // Returns 'true' if the queue was empty before adding
57 // this item.
58 public bool Add (DirectoryModel dir)
60 lock (big_lock) {
61 bool was_empty;
62 was_empty = (to_be_crawled.Count == 0);
64 if (!was_empty && to_be_crawled.Contains (dir))
65 return false;
67 to_be_crawled.Enqueue (dir);
68 Description = String.Format ("Pending directories: {0}", to_be_crawled.Count);
69 return was_empty;
73 override protected void DoTaskReal ()
75 DirectoryModel dir;
77 lock (big_lock) {
78 if (to_be_crawled.Count == 0) {
79 DoneCrawling ();
80 return;
82 dir = to_be_crawled.Dequeue () as DirectoryModel;
83 Log.Debug ("Running tree crawl task");
84 is_active = true;
87 LuceneQueryable queryable = (LuceneQueryable) Source;
89 if (dir.IsAttached) {
90 if (FileSystemQueryable.Debug)
91 Logger.Log.Debug ("Scanning '{0}' for subdirectories", dir.FullName);
93 try {
94 foreach (string name in DirectoryWalker.GetDirectoryNames (dir.FullName)) {
95 string path;
96 path = Path.Combine (dir.FullName, name);
97 if (!FileSystem.IsSpecialFile (path))
98 handler (dir, name);
100 } catch (DirectoryNotFoundException ex) {
101 Logger.Log.Debug ("Couldn't scan '{0}' for subdirectories", dir.FullName);
105 lock (big_lock) {
106 if (to_be_crawled.Count != 0)
107 Reschedule = true;
108 else
109 DoneCrawling ();
113 private void DoneCrawling ()
115 Log.Debug ("Done crawling directory tree!!!");
116 is_active = false;