* Filters/FilterPackage.cs, Filters/FilterRPM.cs,
[beagle.git] / Util / ExtendedAttribute.cs
blobd80577c122f26dbec01b231be2a7ee6a8d8b6a0f
1 //
2 // ExtendedAttribute.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all 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
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 // FIXME: This is not portable to Win32
29 using System;
30 using System.IO;
31 using System.Text;
32 using System.Runtime.InteropServices;
33 using Mono.Unix.Native;
35 namespace Beagle.Util {
37 public class ExtendedAttribute {
39 private static string AddPrefix (string name)
41 return "user.Beagle." + name;
44 static Encoding encoding = new UTF8Encoding ();
46 public static void Set (string path, string name, string value)
48 if (! FileSystem.Exists (path))
49 throw new IOException (path);
51 name = AddPrefix (name);
53 byte[] buffer = encoding.GetBytes (value);
54 int retval = Syscall.lsetxattr (path, name, buffer);
55 if (retval == -1)
56 throw new IOException ("Could not set extended attribute on " + path + ": " + Mono.Unix.Native.Stdlib.strerror (Mono.Unix.Native.Stdlib.GetLastError ()));
59 public static bool Exists (string path, string name)
61 if (! FileSystem.Exists (path))
62 throw new IOException (path);
64 name = AddPrefix (name);
66 long size = Syscall.lgetxattr (path, name, null, 0);
67 return size >= 0;
70 public static string Get (string path, string name)
72 if (! FileSystem.Exists (path))
73 throw new IOException (path);
75 name = AddPrefix (name);
77 byte[] buffer;
78 long size = Syscall.lgetxattr (path, name, out buffer);
79 if (size <= 0)
80 return null;
81 return encoding.GetString (buffer);
84 public static void Remove (string path, string name)
86 if (! FileSystem.Exists (path))
87 throw new IOException (path);
89 name = AddPrefix (name);
91 int retval = Syscall.lremovexattr (path, name);
92 if (retval != 0)
93 throw new IOException ("Could not remove extended attribute on " + path + ": " + Mono.Unix.Native.Stdlib.strerror (Mono.Unix.Native.Stdlib.GetLastError ()));
96 // Check to see if it is possible to get and set attributes on a given file.
97 public static bool Test (string path)
99 const string test_key = "__test_key__";
100 const string test_value = "__test_value__";
102 try {
103 Set (path, test_key, test_value);
104 if (Get (path, test_key) != test_value)
105 return false;
106 Remove (path, test_key);
107 return Get (path, test_key) == null;
109 } catch (Exception ex) {
110 return false;
114 private static bool ea_support_tested = false;
115 private static bool ea_supported = false;
117 public static bool Supported {
118 get {
119 if (ea_support_tested)
120 return ea_supported;
122 ea_supported = Test (PathFinder.HomeDir);
123 ea_support_tested = true;
125 return ea_supported;