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 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
58 public bool Add (DirectoryModel dir
)
62 was_empty
= (to_be_crawled
.Count
== 0);
64 if (!was_empty
&& to_be_crawled
.Contains (dir
))
67 to_be_crawled
.Enqueue (dir
);
68 Description
= String
.Format ("Pending directories: {0}", to_be_crawled
.Count
);
73 override protected void DoTaskReal ()
78 if (to_be_crawled
.Count
== 0) {
82 dir
= to_be_crawled
.Dequeue () as DirectoryModel
;
87 if (FileSystemQueryable
.Debug
)
88 Logger
.Log
.Debug ("Scanning '{0}' for subdirectories", dir
.FullName
);
91 foreach (string name
in DirectoryWalker
.GetDirectoryNames (dir
.FullName
))
93 } catch (DirectoryNotFoundException ex
) {
94 Logger
.Log
.Debug ("Couldn't scan '{0}' for subdirectories", dir
.FullName
);
99 if (to_be_crawled
.Count
!= 0)