Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / FileAttributesStore.cs
blob7c6388098bc3bb938bbe0fb1dcb619cd41d17751
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 (attr == null) {
56 attr = new FileAttributes ();
57 attr.UniqueId = unique_id;
58 attr.Path = path;
59 ifas.Write (attr);
60 created = true;
62 // FIXME: If we were able to successfully read attributes,
63 // and if attr.UniqueId != unique_id, it probably means
64 // that something bad happened. Right? Should we throw
65 // an exception?
66 return attr;
70 public FileAttributes ReadOrCreate (string path, Guid unique_id)
72 bool dummy;
73 return ReadOrCreate (path, unique_id, out dummy);
76 public FileAttributes ReadOrCreate (string path)
78 return ReadOrCreate (path, Guid.NewGuid ());
81 public bool Write (FileAttributes attr)
83 lock (ifas) {
84 attr.LastAttrTime = DateTime.Now;
85 return ifas.Write (attr);
89 public void Drop (string path)
91 lock (ifas) {
92 ifas.Drop (path);
96 public void BeginTransaction ()
98 lock (ifas)
99 ifas.BeginTransaction ();
103 public void CommitTransaction ()
105 lock (ifas)
106 ifas.CommitTransaction ();
109 //////////////////////////////////////////////////////////
111 public bool IsUpToDate (string path, Filter filter)
113 FileAttributes attr;
115 attr = Read (path);
117 // If there are no attributes set on the file, there is no
118 // way that we can be up-to-date.
119 if (attr == null)
120 return false;
122 // Note that when filter is set to null, we ignore
123 // any existing filter data. That might not be the
124 // expected behavior...
125 if (filter != null) {
127 if (! attr.HasFilterInfo)
128 return false;
130 if (attr.FilterName != filter.Name)
131 return false;
133 // FIXME: Obviously we want to reindex if
134 // attr.FilterVersion < filter.Version.
135 // But what if the filter we would use is older
136 // than the one that was previously used?
137 if (attr.FilterVersion != filter.Version)
138 return false;
141 if (FileSystem.GetLastWriteTime (path) > attr.LastWriteTime)
142 return false;
144 return true;
147 public bool IsUpToDate (string path)
149 return IsUpToDate (path, null);
152 //////////////////////////////////////////////////////////
154 // A convenience routine.
155 public void AttachLastWriteTime (string path, DateTime mtime)
157 FileAttributes attr = ReadOrCreate (path);
158 attr.LastWriteTime = mtime;
159 if (! Write (attr))
160 Logger.Log.Warn ("Couldn't store file attributes for {0}", path);