cvsimport
[beagle.git] / beagled / FileAttributesStore.cs
blobbd252dba27ac40382ccfc1c16e10e316d454f747
1 //
2 // FileAttributesStore.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;
29 using Beagle.Util;
31 namespace Beagle.Daemon {
33 public class FileAttributesStore {
35 private IFileAttributesStore ifas;
37 public FileAttributesStore (IFileAttributesStore ifas)
39 this.ifas = ifas;
42 public FileAttributes Read (string path)
44 lock (ifas) {
45 return ifas.Read (path);
49 public FileAttributes ReadOrCreate (string path, Guid unique_id, out bool created)
51 lock (ifas) {
52 created = false;
54 FileAttributes attr = ifas.Read (path);
55 // If we pass in a Guid that doesn't match the one we found in the
56 // the attributes, clobber the old attributes and the old unique Guid.
57 if (attr == null
58 || (unique_id != Guid.Empty && unique_id != attr.UniqueId)) {
59 // First drop the old attribute, if there is one.
60 if (attr != null)
61 ifas.Drop (path);
63 // Now create the new attribute
64 attr = new FileAttributes ();
65 attr.UniqueId = unique_id;
66 attr.Path = path;
68 // Now add the new attribute
69 ifas.Write (attr);
70 created = true;
72 return attr;
76 public FileAttributes ReadOrCreate (string path, Guid unique_id)
78 bool dummy;
79 return ReadOrCreate (path, unique_id, out dummy);
82 public FileAttributes ReadOrCreate (string path)
84 return ReadOrCreate (path, Guid.NewGuid ());
87 public bool Write (FileAttributes attr)
89 lock (ifas) {
90 attr.LastAttrTime = DateTime.UtcNow;
91 return ifas.Write (attr);
95 public void Drop (string path)
97 lock (ifas) {
98 ifas.Drop (path);
102 public void BeginTransaction ()
104 lock (ifas)
105 ifas.BeginTransaction ();
109 public void CommitTransaction ()
111 lock (ifas)
112 ifas.CommitTransaction ();
115 //////////////////////////////////////////////////////////
117 public static bool IsUpToDate (string path, FileAttributes attr)
119 if (attr == null)
120 return false;
122 // FIXME:.Net-2.0 DateTime - Compare attr.LastWriteTime without converting to UTC
123 return (attr.LastWriteTime.ToUniversalTime () >= FileSystem.GetLastWriteTimeUtc (path));
126 public bool IsUpToDate (string path, Filter filter)
128 FileAttributes attr;
130 attr = Read (path);
132 // If there are no attributes set on the file, there is no
133 // way that we can be up-to-date.
134 if (attr == null)
135 return false;
137 // Note that when filter is set to null, we ignore
138 // any existing filter data. That might not be the
139 // expected behavior...
140 if (filter != null) {
142 if (! attr.HasFilterInfo)
143 return false;
145 if (attr.FilterName != filter.Name)
146 return false;
148 // FIXME: Obviously we want to reindex if
149 // attr.FilterVersion < filter.Version.
150 // But what if the filter we would use is older
151 // than the one that was previously used?
152 if (attr.FilterVersion != filter.Version)
153 return false;
156 return IsUpToDate (path, attr);
159 public bool IsUpToDate (string path)
161 return IsUpToDate (path, (Filter) null);
164 //////////////////////////////////////////////////////////
166 // A convenience routine.
167 public void AttachLastWriteTime (string path, DateTime mtime)
169 FileAttributes attr = ReadOrCreate (path);
170 attr.LastWriteTime = mtime;
171 if (! Write (attr))
172 Logger.Log.Warn ("Couldn't store file attributes for {0}", path);