Compute lucene-style scores for our hits.
[beagle.git] / Util / PathFinder.cs
blob18d1f631d61121876e605566e374abcdc630b11b
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 private 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 if (! Directory.Exists (home_dir))
78 throw new Exception ("Home directory '"+home_dir+"' doesn't exist");
82 return home_dir;
85 // You probably don't want to do this.
86 set { lock (home_dir_lock) { home_dir = value; } }
89 // The storage directory is the place where beagle stores its private data.
90 // Fun fact #1: By default this is ~/.beagle
91 // Fun fact #2: It can be overridden by setting BEAGLE_STORAGE
92 static string storage_dir;
93 static object storage_dir_lock = new object ();
94 static public string StorageDir {
95 get {
96 lock (storage_dir_lock) {
97 if (storage_dir == null) {
98 storage_dir = Environment.GetEnvironmentVariable ("BEAGLE_STORAGE");
100 if (storage_dir == null)
101 storage_dir = Path.Combine (HomeDir, ".beagle");
102 else if (storage_dir.EndsWith ("/"))
103 storage_dir = storage_dir.Remove (storage_dir.Length - 1, 1);
105 if (! Directory.Exists (storage_dir)) {
106 Directory.CreateDirectory (storage_dir);
107 // Make sure that the directory is only
108 // readable by the owner.
109 Mono.Posix.Syscall.chmod (storage_dir, (Mono.Posix.FileMode) 448); // 448 == 0700
114 return storage_dir;
118 static string remote_storage_dir;
119 static public string GetRemoteStorageDir (bool create)
121 if (remote_storage_dir == null) {
122 if ((! SystemInformation.IsPathOnBlockDevice (PathFinder.StorageDir) && Conf.Daemon.IndexSynchronization) ||
123 Environment.GetEnvironmentVariable ("BEAGLE_SYNCHRONIZE_LOCALLY") != null) {
124 string index_pointer = Path.Combine (StorageDir, "remote_storage_dir");
126 if (File.Exists (index_pointer)) {
127 StreamReader r = new StreamReader (index_pointer);
128 remote_storage_dir = r.ReadLine ();
129 r.Close ();
131 if (!Directory.Exists (remote_storage_dir))
132 remote_storage_dir = null;
135 if (create) {
136 if (remote_storage_dir == null) {
137 do {
138 string p = String.Format ("beagle-{0}-{1}", Environment.GetEnvironmentVariable ("USER"),
139 Guid.NewGuid ().ToString ());
141 remote_storage_dir = Path.Combine (Path.GetTempPath (), p);
142 } while (Directory.Exists (remote_storage_dir) || File.Exists (remote_storage_dir));
144 StreamWriter w = new StreamWriter (index_pointer);
145 w.WriteLine (remote_storage_dir);
146 w.Close ();
151 if (! Directory.Exists (remote_storage_dir)) {
152 Directory.CreateDirectory (remote_storage_dir);
153 // Make sure that the directory is only
154 // readable by the owner.
155 Mono.Posix.Syscall.chmod (remote_storage_dir, (Mono.Posix.FileMode) 448); // 448 == 0700
158 } else
159 remote_storage_dir = StorageDir;
162 return remote_storage_dir;
165 // The directory where beagle stores its indexes
166 // Fun fact #1: It will be synchronized locally if PathFinder.HomeDir
167 // is on a non-block device, or if BEAGLE_SYNCHRONIZE_LOCALLY is set.
168 static string index_dir;
169 static public string IndexDir {
170 get {
171 if (index_dir == null) {
172 index_dir = Path.Combine (GetRemoteStorageDir (true), "Indexes");
174 if (! Directory.Exists (index_dir)) {
175 Directory.CreateDirectory (index_dir);
177 // Make sure that the directory is only readable by the owner.
178 // Required when using index synchronization as then it resides in /tmp
179 Mono.Posix.Syscall.chmod (index_dir, (Mono.Posix.FileMode) 448); // 448 == 0700
183 return index_dir;
187 static public string LogDir {
188 get {
189 string dir = Path.Combine (StorageDir, "Log");
190 if (! Directory.Exists (dir))
191 Directory.CreateDirectory (dir);
192 return dir;