2 // FileAttributesStore.cs
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
31 namespace Beagle
.Daemon
{
33 public class FileAttributesStore
{
35 private IFileAttributesStore ifas
;
37 public FileAttributesStore (IFileAttributesStore ifas
)
42 public FileAttributes
Read (string path
)
45 return ifas
.Read (path
);
49 public FileAttributes
ReadOrCreate (string path
, Guid unique_id
, out bool created
)
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.
58 || (unique_id
!= Guid
.Empty
&& unique_id
!= attr
.UniqueId
)) {
59 attr
= new FileAttributes ();
60 attr
.UniqueId
= unique_id
;
62 // First drop the old attribute
65 // Now add the new attribute
73 public FileAttributes
ReadOrCreate (string path
, Guid unique_id
)
76 return ReadOrCreate (path
, unique_id
, out dummy
);
79 public FileAttributes
ReadOrCreate (string path
)
81 return ReadOrCreate (path
, Guid
.NewGuid ());
84 public bool Write (FileAttributes attr
)
87 attr
.LastAttrTime
= DateTime
.UtcNow
;
88 return ifas
.Write (attr
);
92 public void Drop (string path
)
99 public void BeginTransaction ()
102 ifas
.BeginTransaction ();
106 public void CommitTransaction ()
109 ifas
.CommitTransaction ();
112 //////////////////////////////////////////////////////////
114 public bool IsUpToDate (string path
, Filter filter
)
120 // If there are no attributes set on the file, there is no
121 // way that we can be up-to-date.
125 // Note that when filter is set to null, we ignore
126 // any existing filter data. That might not be the
127 // expected behavior...
128 if (filter
!= null) {
130 if (! attr
.HasFilterInfo
)
133 if (attr
.FilterName
!= filter
.Name
)
136 // FIXME: Obviously we want to reindex if
137 // attr.FilterVersion < filter.Version.
138 // But what if the filter we would use is older
139 // than the one that was previously used?
140 if (attr
.FilterVersion
!= filter
.Version
)
144 if (FileSystem
.GetLastWriteTimeUtc (path
) > attr
.LastWriteTime
)
150 public bool IsUpToDate (string path
)
152 return IsUpToDate (path
, null);
155 //////////////////////////////////////////////////////////
157 // A convenience routine.
158 public void AttachLastWriteTime (string path
, DateTime mtime
)
160 FileAttributes attr
= ReadOrCreate (path
);
161 attr
.LastWriteTime
= mtime
;
163 Logger
.Log
.Warn ("Couldn't store file attributes for {0}", path
);