~ubuntu-branches/ubuntu/lucid/beagle/lucid

« back to all changes in this revision

Viewing changes to beagled/FileAttributesStore_ExtendedAttribute.cs

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2007-12-24 17:25:05 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: james.westby@ubuntu.com-20071224172505-a5wsz6kdesm45vre
Tags: 0.3.1-2ubuntu1
* Merge from Debian unstable, remaining Ubuntu changes:
  - debian/control:
    + Rename iceweasel-beagle and icedove-beagle to mozilla-beagle and
      thunderbird-beagle, change the Recommends to Depends and update
      them accordingly.
    + Suggest schedutils for beagle.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
        
34
34
        public class FileAttributesStore_ExtendedAttribute : IFileAttributesStore {
35
35
 
 
36
                // Version history:
 
37
                // 1: Original
 
38
                // 2: Replace LastIndexedTime with LastAttrTime
 
39
                // 3: Store EA's using a CSV format
 
40
                //    Format: "Version[2] Fingerprint,Uid,LastWriteTime,LastAttrTime,FilterVersion[3] FilterName"
 
41
                private const int EA_VERSION = 3;
 
42
 
36
43
                public static bool Disable = false;
37
44
 
38
45
                private string index_fingerprint;
39
 
                
 
46
 
40
47
                public FileAttributesStore_ExtendedAttribute (string index_fingerprint)
41
48
                {
42
49
                        this.index_fingerprint = index_fingerprint;
43
50
                }
44
51
 
45
 
                // Version history:
46
 
                // 1: Original
47
 
                // 2: Replace LastIndexedTime with LastAttrTime
48
 
                const int EA_VERSION = 2;
49
 
 
50
 
                // FIXME: We should probably serialize the data into a lump and attach
51
 
                // it to just one EA.  The current method has an inherent race condition:
52
 
                // if the file changes out from under us mid-Read or mid-Write, all sorts
53
 
                // of weirdness could ensue.
54
 
 
55
 
                const string fingerprint_attr = "Fingerprint";
56
 
                const string unique_id_attr = "Uid";
57
 
                const string last_mtime_attr = "MTime";
58
 
                const string last_attrtime_attr = "AttrTime";
59
 
                const string filter_attr = "Filter";
60
 
 
61
52
                public FileAttributes Read (string path)
62
53
                {
63
54
                        if (Disable)
64
55
                                return null;
65
56
 
66
57
                        try {
67
 
                                string tmp;
68
 
                                tmp = ExtendedAttribute.Get (path, fingerprint_attr);
69
 
                                if (tmp == null 
70
 
                                    || int.Parse (tmp.Substring (0, 2)) != EA_VERSION
71
 
                                    || (index_fingerprint != null && tmp.Substring (3) != index_fingerprint))
 
58
                                string tmp = ExtendedAttribute.Get (path);
 
59
 
 
60
                                if (tmp == null)
 
61
                                        return null;
 
62
 
 
63
                                string[] csv = tmp.Split (',');
 
64
 
 
65
                                if (int.Parse (csv [0].Substring (0, 2)) != EA_VERSION
 
66
                                    || (index_fingerprint != null && csv [0].Substring (3) != index_fingerprint))
72
67
                                        return null;
73
68
 
74
69
                                FileAttributes attr = new FileAttributes ();
75
 
                                
76
 
                                string uid_str = ExtendedAttribute.Get (path, unique_id_attr);
77
 
                                attr.UniqueId = GuidFu.FromShortString (uid_str);
78
 
 
 
70
                                attr.UniqueId = GuidFu.FromShortString (csv [1]);
79
71
                                attr.Path = path;
80
 
                                attr.LastWriteTime = StringFu.StringToDateTime (ExtendedAttribute.Get (path, last_mtime_attr));
81
 
                                
82
 
                                attr.LastAttrTime = StringFu.StringToDateTime (ExtendedAttribute.Get (path, last_attrtime_attr));
 
72
                                attr.LastWriteTime = StringFu.StringToDateTime (csv [2]);
 
73
                                attr.LastAttrTime = StringFu.StringToDateTime (csv [3]);
83
74
 
84
 
                                tmp = ExtendedAttribute.Get (path, filter_attr);
85
 
                                if (tmp != null) {
86
 
                                        attr.FilterVersion = int.Parse (tmp.Substring (0, 3));
87
 
                                        attr.FilterName = tmp.Substring (4);
 
75
                                if (! String.IsNullOrEmpty (csv [4])) {
 
76
                                        attr.FilterVersion = int.Parse (csv [4].Substring (0, 3));
 
77
                                        attr.FilterName = csv [4].Substring (4);
88
78
                                }
89
79
                                
90
80
                                return attr;
103
93
                                return false;
104
94
 
105
95
                        try {
106
 
                                string tmp;
107
 
                                
108
 
                                tmp = String.Format ("{0:00} {1}", EA_VERSION, index_fingerprint);
109
 
                                ExtendedAttribute.Set (attr.Path, fingerprint_attr, tmp);
110
 
 
111
 
                                // Try to read the EA we just set.  If we
112
 
                                // can't, they won't be much use to us --- so
113
 
                                // just return false.
114
 
                                string what_we_just_wrote;
115
 
                                try {
116
 
                                        what_we_just_wrote = ExtendedAttribute.Get (attr.Path, fingerprint_attr);
117
 
                                } catch (Exception ex) {
118
 
                                        return false;
119
 
                                }
120
 
                                if (what_we_just_wrote != tmp)
121
 
                                        return false;
122
 
 
123
 
                                ExtendedAttribute.Set (attr.Path, unique_id_attr, GuidFu.ToShortString (attr.UniqueId));
124
 
                                ExtendedAttribute.Set (attr.Path, last_mtime_attr,
125
 
                                                       StringFu.DateTimeToString (attr.LastWriteTime));
126
 
 
127
 
                                if (attr.HasFilterInfo) {
128
 
                                        tmp = String.Format ("{0:000} {1}", attr.FilterVersion, attr.FilterName);
129
 
                                        ExtendedAttribute.Set (attr.Path, filter_attr, tmp);
130
 
                                }
131
 
 
132
 
                                // This has to be the last thing we write out, to get LastAttrTime as close
133
 
                                // to the ctime as possible.
 
96
                                if (ExtendedAttribute.OldExists (attr.Path, "Fingerprint"))
 
97
                                        DropObsoleteAttributes (attr.Path);
 
98
 
 
99
                                string fingerprint = String.Format ("{0:00} {1}", EA_VERSION, index_fingerprint);
 
100
                                string uid = GuidFu.ToShortString (attr.UniqueId);
 
101
                                string mtime = StringFu.DateTimeToString (attr.LastWriteTime);
 
102
 
 
103
                                string filter = String.Empty;
 
104
 
 
105
                                if (attr.HasFilterInfo)
 
106
                                        filter = String.Format ("{0:000} {1}", attr.FilterVersion, attr.FilterName);
 
107
 
134
108
                                attr.LastAttrTime = DateTime.UtcNow;
135
 
                                ExtendedAttribute.Set (attr.Path, last_attrtime_attr,
136
 
                                                       StringFu.DateTimeToString (attr.LastAttrTime));
137
 
                                
 
109
                                string attrtime = StringFu.DateTimeToString (attr.LastAttrTime);
 
110
 
 
111
                                string [] csv = {fingerprint, uid, mtime, attrtime, filter};
 
112
                                ExtendedAttribute.Set (attr.Path, String.Join (",", csv));
 
113
                                                        
138
114
                                return true;
139
115
                        } catch (IOException e) {
140
116
                                // An IOException here probably means that we don't have the right
143
119
                                //Logger.Log.Debug (e);
144
120
                                return false;
145
121
                        } catch (Exception e) {
146
 
                                Logger.Log.Debug (e, "Caught exception writing EAs to {0}", attr.Path);
 
122
                                //Logger.Log.Debug (e, "Caught exception writing EAs to {0}", attr.Path);
147
123
                                // FIXME: Do something smarter with the exception.
148
124
                                return false;
149
125
                        }
155
131
                                return;
156
132
 
157
133
                        try {
158
 
                                ExtendedAttribute.Remove (path, fingerprint_attr);
159
 
                                ExtendedAttribute.Remove (path, unique_id_attr);
160
 
                                ExtendedAttribute.Remove (path, last_mtime_attr);
161
 
                                ExtendedAttribute.Remove (path, last_attrtime_attr);
162
 
                                ExtendedAttribute.Remove (path, filter_attr);
163
 
 
164
 
                        } catch (Exception e) {
 
134
                                ExtendedAttribute.Remove (path);
 
135
                        } catch {
165
136
                                // FIXME: Do something smarter with the exception.
166
137
                        }
167
138
                }
168
139
 
 
140
                // IMPORTANT: Remove this post 0.3.3 release!
 
141
                private void DropObsoleteAttributes (string path)
 
142
                {
 
143
                        try {
 
144
                                ExtendedAttribute.RemoveOld (path, "Fingerprint");
 
145
                                ExtendedAttribute.RemoveOld (path, "Uid");
 
146
                                ExtendedAttribute.RemoveOld (path, "MTime");
 
147
                                ExtendedAttribute.RemoveOld (path, "AttrTime");
 
148
                                ExtendedAttribute.RemoveOld (path, "Filter");
 
149
 
 
150
                                // And some others from Joe's favorite list :-)
 
151
                                ExtendedAttribute.RemoveOld (path, "Name");
 
152
                                ExtendedAttribute.RemoveOld (path, "IndexTime");
 
153
                        } catch { }
 
154
                }
 
155
 
169
156
                // There are no transactions for EAs
170
157
                
171
158
                public void BeginTransaction ()