Merging from head
[beagle.git] / Util / PathFinder.cs
blobf2606c4218f818d3ce9d4a5cc518d4e5c251484f
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[] Paths {
38 get {
39 string env_var = Environment.GetEnvironmentVariable ("PATH");
40 return env_var.Split (':');
44 static public string PkgLibDir {
45 get { return ExternalStringsHack.PkgLibDir; }
48 static public string FilterDir {
49 get { return Path.Combine (PkgLibDir, "Filters"); }
52 static public string BackendDir {
53 get { return Path.Combine (PkgLibDir, "Backends"); }
56 static private string LocalStateDir {
57 get { return ExternalStringsHack.LocalStateDir; }
60 static public string SystemDir {
61 get { return Path.Combine (Path.Combine (LocalStateDir, "cache"), "beagle"); }
64 static public string SystemIndexesDir {
65 get { return Path.Combine (SystemDir, "indexes"); }
68 // The user's personal files are under this and their dotfiles are in it.
69 // It is usually found via HOME, but that can be overridden by setting
70 // BEAGLE_HOME
71 static string home_dir;
72 static object home_dir_lock = new object ();
73 static public string HomeDir {
74 get {
75 lock (home_dir_lock) {
76 if (home_dir == null) {
77 home_dir = Environment.GetEnvironmentVariable ("BEAGLE_HOME");
78 if (home_dir == null)
79 home_dir = Environment.GetEnvironmentVariable ("HOME");
80 if (home_dir == null)
81 throw new Exception ("Couldn't get HOME or BEAGLE_HOME");
82 if (home_dir.EndsWith ("/"))
83 home_dir = home_dir.Remove (home_dir.Length - 1, 1);
84 home_dir = Path.GetFullPath (home_dir);
85 if (! Directory.Exists (home_dir))
86 throw new Exception ("Home directory '"+home_dir+"' doesn't exist");
90 return home_dir;
93 // You probably don't want to do this.
94 set { lock (home_dir_lock) { home_dir = value; } }
97 // The storage directory is the place where beagle stores its private data.
98 // Fun fact #1: By default this is ~/.beagle
99 // Fun fact #2: It can be overridden by setting BEAGLE_STORAGE
100 static string storage_dir;
101 static object storage_dir_lock = new object ();
102 static public string StorageDir {
103 get {
104 lock (storage_dir_lock) {
105 if (storage_dir == null) {
106 storage_dir = Environment.GetEnvironmentVariable ("BEAGLE_STORAGE");
108 if (storage_dir == null)
109 storage_dir = Path.Combine (HomeDir, ".beagle");
110 else {
111 if (storage_dir.EndsWith ("/"))
112 storage_dir = storage_dir.Remove (storage_dir.Length - 1, 1);
113 storage_dir = Path.GetFullPath (storage_dir);
116 if (! Directory.Exists (storage_dir)) {
117 Directory.CreateDirectory (storage_dir);
118 // Make sure that the directory is only
119 // readable by the owner.
120 Mono.Unix.Native.Syscall.chmod (storage_dir, (Mono.Unix.Native.FilePermissions) 448); // 448 == 0700
125 return storage_dir;
129 static string remote_storage_dir;
130 static public string GetRemoteStorageDir (bool create)
132 if (remote_storage_dir == null) {
133 if ((! SystemInformation.IsPathOnBlockDevice (PathFinder.StorageDir) && Conf.Daemon.IndexSynchronization) ||
134 Environment.GetEnvironmentVariable ("BEAGLE_SYNCHRONIZE_LOCALLY") != null) {
135 string index_pointer = Path.Combine (StorageDir, "remote_storage_dir");
137 if (File.Exists (index_pointer)) {
138 StreamReader r = new StreamReader (index_pointer);
139 remote_storage_dir = r.ReadLine ();
140 r.Close ();
142 if (!Directory.Exists (remote_storage_dir))
143 remote_storage_dir = null;
146 if (create) {
147 if (remote_storage_dir == null) {
148 do {
149 string p = String.Format ("beagle-{0}-{1}", Environment.GetEnvironmentVariable ("USER"),
150 Guid.NewGuid ().ToString ());
152 remote_storage_dir = Path.Combine (Path.GetTempPath (), p);
153 } while (Directory.Exists (remote_storage_dir) || File.Exists (remote_storage_dir));
155 StreamWriter w = new StreamWriter (index_pointer);
156 w.WriteLine (remote_storage_dir);
157 w.Close ();
162 if (! Directory.Exists (remote_storage_dir)) {
163 Directory.CreateDirectory (remote_storage_dir);
164 // Make sure that the directory is only
165 // readable by the owner.
166 Mono.Unix.Native.Syscall.chmod (remote_storage_dir, (Mono.Unix.Native.FilePermissions) 448); // 448 == 0700
169 } else
170 remote_storage_dir = StorageDir;
173 return remote_storage_dir;
176 // The directory where beagle stores its indexes
177 // Fun fact #1: It will be synchronized locally if PathFinder.HomeDir
178 // is on a non-block device, or if BEAGLE_SYNCHRONIZE_LOCALLY is set.
179 static string index_dir;
180 static public string IndexDir {
181 get {
182 if (index_dir == null) {
183 index_dir = Path.Combine (GetRemoteStorageDir (true), "Indexes");
185 if (! Directory.Exists (index_dir)) {
186 Directory.CreateDirectory (index_dir);
188 // Make sure that the directory is only readable by the owner.
189 // Required when using index synchronization as then it resides in /tmp
190 Mono.Unix.Native.Syscall.chmod (index_dir, (Mono.Unix.Native.FilePermissions) 448); // 448 == 0700
194 return index_dir;
198 static public string LogDir {
199 get {
200 string dir = Path.Combine (StorageDir, "Log");
201 if (! Directory.Exists (dir))
202 Directory.CreateDirectory (dir);
203 return dir;