// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
+#if !NET_2_1
+
using System;
using System.Collections;
using System.Globalization;
static Hashtable key_to_handler = new Hashtable ();
static Hashtable dir_to_handler = new Hashtable (
new CaseInsensitiveHashCodeProvider (), new CaseInsensitiveComparer ());
- /*
- const string MACHINE_STORE_VAR = "MONO_REGISTRY_MACHINE";
- */
-
public string Dir;
Hashtable values;
string file;
bool dirty;
-
+
KeyHandler (RegistryKey rkey, string basedir)
{
if (!Directory.Exists (basedir)){
- Directory.CreateDirectory (basedir);
+ try {
+ Directory.CreateDirectory (basedir);
+ } catch (UnauthorizedAccessException){
+ throw new SecurityException ("No access to the given key");
+ }
}
Dir = basedir;
file = Path.Combine (Dir, "values.xml");
}
}
}
+ } catch (UnauthorizedAccessException){
+ values.Clear ();
+ throw new SecurityException ("No access to the given key");
} catch (Exception e){
Console.Error.WriteLine ("While loading registry key at {0}: {1}", file, e);
values.Clear ();
values [name] = Int32.Parse (se.Text);
break;
case "bytearray":
- Convert.FromBase64String (se.Text);
+ values [name] = Convert.FromBase64String (se.Text);
break;
case "string":
values [name] = se.Text;
case RegistryHive.LocalMachine:
case RegistryHive.PerformanceData:
case RegistryHive.Users:
- string machineDir = Path.Combine (MachineStore, x.ToString ());
- k = new KeyHandler (rkey, machineDir);
- dir_to_handler [machineDir] = k;
+ string machine_dir = Path.Combine (MachineStore, x.ToString ());
+ k = new KeyHandler (rkey, machine_dir);
+ dir_to_handler [machine_dir] = k;
break;
default:
throw new Exception ("Unknown RegistryHive");
public void SetValue (string name, object value, RegistryValueKind valueKind)
{
SetDirty ();
+
+ if (name == null)
+ name = string.Empty;
+
switch (valueKind){
case RegistryValueKind.String:
if (value is string){
SecurityElement se = new SecurityElement ("values");
+ // With SecurityElement.Text = value, and SecurityElement.AddAttribute(key, value)
+ // the values must be escaped prior to being assigned.
foreach (DictionaryEntry de in values){
object val = de.Value;
SecurityElement value = new SecurityElement ("value");
- value.AddAttribute ("name", (string) de.Key);
+ value.AddAttribute ("name", SecurityElement.Escape ((string) de.Key));
if (val is string){
value.AddAttribute ("type", "string");
- value.Text = (string) val;
+ value.Text = SecurityElement.Escape ((string) val);
} else if (val is int){
value.AddAttribute ("type", "int");
value.Text = val.ToString ();
value.Text = Convert.ToBase64String ((byte[]) val);
} else if (val is ExpandString){
value.AddAttribute ("type", "expand");
- value.Text = val.ToString ();
+ value.Text = SecurityElement.Escape (val.ToString ());
} else if (val is string []){
value.AddAttribute ("type", "string-array");
foreach (string ss in (string[]) val){
SecurityElement str = new SecurityElement ("string");
- str.Text = ss;
+ str.Text = SecurityElement.Escape (ss);
value.AddChild (str);
}
}
private static string MachineStore {
get {
- /*
- string machineStore = Environment.GetEnvironmentVariable (MACHINE_STORE_VAR);
- if (machineStore != null)
- return machineStore;
- */
- return UserStore;
- // return "/var/lib/mono/registry";
+ string s;
+
+ s = Environment.GetEnvironmentVariable ("MONO_REGISTRY_PATH");
+ if (s != null)
+ return s;
+ s = Environment.GetMachineConfigPath ();
+ int p = s.IndexOf ("machine.config");
+ return Path.Combine (Path.Combine (s.Substring (0, p-1), ".."), "registry");
}
}
}
RegistryKey result = self.Probe (rkey, ToUnix (keyname), writable);
if (result == null && IsWellKnownKey (rkey.Name, keyname)) {
// create the subkey even if its parent was opened read-only
- result = CreateSubKey (rkey, keyname, false);
+ result = CreateSubKey (rkey, keyname, writable);
}
return result;
}
if (throw_if_missing && !self.ValueExists (name))
- throw new ArgumentException ("the given value does not exist", "name");
+ throw new ArgumentException ("the given value does not exist");
self.RemoveValue (name);
}
// key is marked for deletion
if (!throw_if_missing)
return;
- throw new ArgumentException ("the given value does not exist", "value");
+ throw new ArgumentException ("the given value does not exist");
}
string dir = Path.Combine (self.Dir, ToUnix (keyname));
Directory.Delete (dir, true);
KeyHandler.Drop (dir);
} else if (throw_if_missing)
- throw new ArgumentException ("the given value does not exist", "value");
+ throw new ArgumentException ("the given value does not exist");
}
public string [] GetSubKeyNames (RegistryKey rkey)
}
}
}
+
+#endif // NET_2_1
+