Yet another. Init the gobject type system.
[beagle.git] / Util / PathFinder.cs
blob46489e28bd2fdc61acaf3a104671c1a4cd03a3fd
1 //
2 // PathFinder.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.
28 using System;
29 using System.IO;
31 namespace Beagle.Util {
33 public class PathFinder {
35 private PathFinder () { }
37 static public string PkgLibDir {
38 get { return ExternalStringsHack.PkgLibDir; }
41 static public string FilterDir {
42 get { return Path.Combine (PkgLibDir, "Filters"); }
45 static public string BackendDir {
46 get { return Path.Combine (PkgLibDir, "Backends"); }
49 static private string LocalStateDir {
50 get { return ExternalStringsHack.LocalStateDir; }
53 static public string SystemDir {
54 get { return Path.Combine (Path.Combine (LocalStateDir, "cache"), "beagle"); }
57 static public string SystemIndexesDir {
58 get { return Path.Combine (SystemDir, "indexes"); }
61 // The user's personal files are under this and their dotfiles are in it.
62 // It is usually found via HOME, but that can be overridden by setting
63 // BEAGLE_HOME
64 static string home_dir;
65 static object home_dir_lock = new object ();
66 static public string HomeDir {
67 get {
68 lock (home_dir_lock) {
69 if (home_dir == null) {
70 home_dir = Environment.GetEnvironmentVariable ("BEAGLE_HOME");
71 if (home_dir == null)
72 home_dir = Environment.GetEnvironmentVariable ("HOME");
73 if (home_dir == null)
74 throw new Exception ("Couldn't get HOME or BEAGLE_HOME");
75 if (home_dir.EndsWith ("/"))
76 home_dir = home_dir.Remove (home_dir.Length - 1, 1);
77 home_dir = Path.GetFullPath (home_dir);
78 if (! Directory.Exists (home_dir))
79 throw new Exception ("Home directory '"+home_dir+"' doesn't exist");
83 return home_dir;
86 // You probably don't want to do this.
87 set { lock (home_dir_lock) { home_dir = value; } }
90 // The storage directory is the place where beagle stores its private data.
91 // Fun fact #1: By default this is ~/.beagle
92 // Fun fact #2: It can be overridden by setting BEAGLE_STORAGE
93 static string storage_dir;
94 static object storage_dir_lock = new object ();
95 static public string StorageDir {
96 get {
97 lock (storage_dir_lock) {
98 if (storage_dir == null) {
99 storage_dir = Environment.GetEnvironmentVariable ("BEAGLE_STORAGE");
101 if (storage_dir == null)
102 storage_dir = Path.Combine (HomeDir, ".beagle");
103 else {
104 if (storage_dir.EndsWith ("/"))
105 storage_dir = storage_dir.Remove (storage_dir.Length - 1, 1);
106 storage_dir = Path.GetFullPath (storage_dir);
109 if (! Directory.Exists (storage_dir)) {
110 Directory.CreateDirectory (storage_dir);
111 // Make sure that the directory is only
112 // readable by the owner.
113 Mono.Unix.Native.Syscall.chmod (storage_dir, (Mono.Unix.Native.FilePermissions) 448); // 448 == 0700
118 return storage_dir;
122 static string remote_storage_dir;
123 static public string GetRemoteStorageDir (bool create)
125 if (remote_storage_dir == null) {
126 if ((! SystemInformation.IsPathOnBlockDevice (PathFinder.StorageDir) && Conf.Daemon.IndexSynchronization) ||
127 Environment.GetEnvironmentVariable ("BEAGLE_SYNCHRONIZE_LOCALLY") != null) {
128 string index_pointer = Path.Combine (StorageDir, "remote_storage_dir");
130 if (File.Exists (index_pointer)) {
131 StreamReader r = new StreamReader (index_pointer);
132 remote_storage_dir = r.ReadLine ();
133 r.Close ();
135 if (!Directory.Exists (remote_storage_dir))
136 remote_storage_dir = null;
139 if (create) {
140 if (remote_storage_dir == null) {
141 do {
142 string p = String.Format ("beagle-{0}-{1}", Environment.GetEnvironmentVariable ("USER"),
143 Guid.NewGuid ().ToString ());
145 remote_storage_dir = Path.Combine (Path.GetTempPath (), p);
146 } while (Directory.Exists (remote_storage_dir) || File.Exists (remote_storage_dir));
148 StreamWriter w = new StreamWriter (index_pointer);
149 w.WriteLine (remote_storage_dir);
150 w.Close ();
155 if (! Directory.Exists (remote_storage_dir)) {
156 Directory.CreateDirectory (remote_storage_dir);
157 // Make sure that the directory is only
158 // readable by the owner.
159 Mono.Unix.Native.Syscall.chmod (remote_storage_dir, (Mono.Unix.Native.FilePermissions) 448); // 448 == 0700
162 } else
163 remote_storage_dir = StorageDir;
166 return remote_storage_dir;
169 // The directory where beagle stores its indexes
170 // Fun fact #1: It will be synchronized locally if PathFinder.HomeDir
171 // is on a non-block device, or if BEAGLE_SYNCHRONIZE_LOCALLY is set.
172 static string index_dir;
173 static public string IndexDir {
174 get {
175 if (index_dir == null) {
176 index_dir = Path.Combine (GetRemoteStorageDir (true), "Indexes");
178 if (! Directory.Exists (index_dir)) {
179 Directory.CreateDirectory (index_dir);
181 // Make sure that the directory is only readable by the owner.
182 // Required when using index synchronization as then it resides in /tmp
183 Mono.Unix.Native.Syscall.chmod (index_dir, (Mono.Unix.Native.FilePermissions) 448); // 448 == 0700
187 return index_dir;
191 static public string LogDir {
192 get {
193 string dir = Path.Combine (StorageDir, "Log");
194 if (! Directory.Exists (dir))
195 Directory.CreateDirectory (dir);
196 return dir;