2005-07-05 Gabor Kelemen <kelemeng@gnome.hu>
[beagle.git] / beagled / FileAttributesStore_ExtendedAttribute.cs
blob9d6b6e886ca0d7575c7c60f090fae0732dc1100d
1 //
2 // FileAttributesStore_ExtendedAttribute.cs
3 //
4 // Copyright (C) 2004 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.IO;
30 using Beagle.Util;
32 namespace Beagle.Daemon {
34 public class FileAttributesStore_ExtendedAttribute : IFileAttributesStore {
36 public static bool Disable = false;
38 private string index_fingerprint;
40 public FileAttributesStore_ExtendedAttribute (string index_fingerprint)
42 this.index_fingerprint = index_fingerprint;
45 const int EA_VERSION = 1;
47 // FIXME: We should probably serialize the data into a lump and attach
48 // it to just one EA. The current method has an inherent race condition:
49 // if the file changes out from under us mid-Read or mid-Write, all sorts
50 // of weirdness could ensue.
52 const string fingerprint_attr = "Fingerprint";
53 const string unique_id_attr = "Uid";
54 const string last_mtime_attr = "MTime";
55 const string last_indexed_attr = "IndexTime";
56 const string filter_attr = "Filter";
58 public FileAttributes Read (string path)
60 if (Disable)
61 return null;
63 try {
64 string tmp;
65 tmp = ExtendedAttribute.Get (path, fingerprint_attr);
66 if (tmp == null
67 || int.Parse (tmp.Substring (0, 2)) != EA_VERSION
68 || (index_fingerprint != null && tmp.Substring (3) != index_fingerprint))
69 return null;
71 FileAttributes attr = new FileAttributes ();
73 string uid_str = ExtendedAttribute.Get (path, unique_id_attr);
74 attr.UniqueId = GuidFu.FromShortString (uid_str);
76 attr.Path = path;
77 attr.LastWriteTime = StringFu.StringToDateTime (ExtendedAttribute.Get (path, last_mtime_attr));
79 attr.LastIndexedTime = StringFu.StringToDateTime (ExtendedAttribute.Get (path, last_indexed_attr));
80 tmp = ExtendedAttribute.Get (path, filter_attr);
81 if (tmp != null) {
82 attr.FilterVersion = int.Parse (tmp.Substring (0, 3));
83 attr.FilterName = tmp.Substring (4);
86 return attr;
88 } catch (Exception e) {
89 //Logger.Log.Debug ("Caught exception reading EAs from {0}", path);
90 //Logger.Log.Debug (e);
91 // FIXME: Do something smarter with the exception.
92 return null;
96 public bool Write (FileAttributes attr)
98 if (Disable)
99 return false;
101 try {
102 string tmp;
104 if (index_fingerprint != null) {
105 tmp = String.Format ("{0:00} {1}", EA_VERSION, index_fingerprint);
106 ExtendedAttribute.Set (attr.Path, fingerprint_attr, tmp);
109 ExtendedAttribute.Set (attr.Path, unique_id_attr, GuidFu.ToShortString (attr.UniqueId));
110 ExtendedAttribute.Set (attr.Path, last_mtime_attr,
111 StringFu.DateTimeToString (attr.LastWriteTime));
113 ExtendedAttribute.Set (attr.Path, last_indexed_attr,
114 StringFu.DateTimeToString (attr.LastIndexedTime));
115 if (attr.HasFilterInfo)
116 ExtendedAttribute.Set (attr.Path, filter_attr,
117 String.Format ("{0:000} {1}", attr.FilterVersion, attr.FilterName));
119 return true;
120 } catch (IOException e) {
121 // An IOException here probably means that we don't have the right
122 // permissions to set the EAs. We just fail silently and return false rather
123 // than spewing a bunch of scary exceptions.
124 return false;
125 } catch (Exception e) {
126 Logger.Log.Debug ("Caught exception writing EAs to {0}", attr.Path);
127 Logger.Log.Debug (e);
128 // FIXME: Do something smarter with the exception.
129 return false;
133 public void Drop (string path)
135 if (Disable)
136 return;
138 try {
139 ExtendedAttribute.Remove (path, fingerprint_attr);
140 ExtendedAttribute.Remove (path, unique_id_attr);
141 ExtendedAttribute.Remove (path, last_mtime_attr);
142 ExtendedAttribute.Remove (path, last_indexed_attr);
143 ExtendedAttribute.Remove (path, filter_attr);
145 } catch (Exception e) {
146 // FIXME: Do something smarter with the exception.