File: PathFinder.cs

package info (click to toggle)
beagle 0.2.12-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,204 kB
  • ctags: 16,188
  • sloc: cs: 91,628; sh: 27,627; ansic: 10,646; makefile: 2,248; xml: 15
file content (207 lines) | stat: -rw-r--r-- 6,592 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// PathFinder.cs
//
// Copyright (C) 2004 Novell, Inc.
//

//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//


using System;
using System.IO;

namespace Beagle.Util {

	public class PathFinder {

		private PathFinder () { }

		static public string[] Paths {
			get {
				string env_var = Environment.GetEnvironmentVariable ("PATH");
				return env_var.Split (':');
			}
		}

		static public string PkgLibDir {
			get { return ExternalStringsHack.PkgLibDir; }
		}

		static public string FilterDir {
			get { return Path.Combine (PkgLibDir, "Filters"); }
		}

		static public string BackendDir {
			get { return Path.Combine (PkgLibDir, "Backends"); }
		}

		static private string LocalStateDir {
			get { return ExternalStringsHack.LocalStateDir; }
		}

		static public string SystemDir {
			get { return Path.Combine (Path.Combine (LocalStateDir, "cache"), "beagle"); }
		}

		static public string SystemIndexesDir {
			get { return Path.Combine (SystemDir, "indexes"); }
		}

		// The user's personal files are under this and their dotfiles are in it.
		// It is usually found via HOME, but that can be overridden by setting
		// BEAGLE_HOME
		static string home_dir;
		static object home_dir_lock = new object ();
		static public string HomeDir {
			get {
				lock (home_dir_lock) {
					if (home_dir == null) {
						home_dir = Environment.GetEnvironmentVariable ("BEAGLE_HOME");
						if (home_dir == null)
							home_dir = Environment.GetEnvironmentVariable ("HOME");
						if (home_dir == null)
							throw new Exception ("Couldn't get HOME or BEAGLE_HOME");
						if (home_dir.EndsWith ("/"))
							home_dir = home_dir.Remove (home_dir.Length - 1, 1);
						home_dir = Path.GetFullPath (home_dir);
						if (! Directory.Exists (home_dir))
							throw new Exception ("Home directory '"+home_dir+"' doesn't exist");
					}
				}
				
				return home_dir;
			}

			// You probably don't want to do this.
			set { lock (home_dir_lock) { home_dir = value; } }
		}

		// The storage directory is the place where beagle stores its private data.
		// Fun fact #1: By default this is ~/.beagle
		// Fun fact #2: It can be overridden by setting BEAGLE_STORAGE
		static string storage_dir;
		static object storage_dir_lock = new object ();
		static public string StorageDir {
			get {
				lock (storage_dir_lock) {
					if (storage_dir == null) {
						storage_dir = Environment.GetEnvironmentVariable ("BEAGLE_STORAGE");

						if (storage_dir == null)
							storage_dir = Path.Combine (HomeDir, ".beagle");
						else {
							if (storage_dir.EndsWith ("/"))
								storage_dir = storage_dir.Remove (storage_dir.Length - 1, 1);
							storage_dir = Path.GetFullPath (storage_dir);
						}

						if (! Directory.Exists (storage_dir)) {
							Directory.CreateDirectory (storage_dir);
							// Make sure that the directory is only
							// readable by the owner.
							Mono.Unix.Native.Syscall.chmod (storage_dir, (Mono.Unix.Native.FilePermissions) 448); // 448 == 0700
						}
					}
				}

				return storage_dir;
			}
		}

		static string remote_storage_dir;
		static public string GetRemoteStorageDir (bool create)
		{
			if (remote_storage_dir == null) {
				if ((! SystemInformation.IsPathOnBlockDevice (PathFinder.StorageDir) && Conf.Daemon.IndexSynchronization) ||
				    Environment.GetEnvironmentVariable ("BEAGLE_SYNCHRONIZE_LOCALLY") != null) {
					string index_pointer = Path.Combine (StorageDir, "remote_storage_dir");

					if (File.Exists (index_pointer)) {
						StreamReader r = new StreamReader (index_pointer);
						remote_storage_dir = r.ReadLine ();
						r.Close ();

						if (!Directory.Exists (remote_storage_dir))
							remote_storage_dir = null;
					}

					if (create) {
						if (remote_storage_dir == null) {
							do {
								string p = String.Format ("beagle-{0}-{1}", Environment.GetEnvironmentVariable ("USER"),
											  Guid.NewGuid ().ToString ());

								remote_storage_dir = Path.Combine (Path.GetTempPath (), p);
							} while (Directory.Exists (remote_storage_dir) || File.Exists (remote_storage_dir));

							StreamWriter w = new StreamWriter (index_pointer);
							w.WriteLine (remote_storage_dir);
							w.Close ();


						}

						if (! Directory.Exists (remote_storage_dir)) {
							Directory.CreateDirectory (remote_storage_dir);
							// Make sure that the directory is only
							// readable by the owner.
							Mono.Unix.Native.Syscall.chmod (remote_storage_dir, (Mono.Unix.Native.FilePermissions) 448); // 448 == 0700
						}
					}
				} else
					remote_storage_dir = StorageDir;
			}

			return remote_storage_dir;
		}

		// The directory where beagle stores its indexes
		// Fun fact #1: It will be synchronized locally if PathFinder.HomeDir
		// is on a non-block device, or if BEAGLE_SYNCHRONIZE_LOCALLY is set.
		static string index_dir;
		static public string IndexDir {
			get { 
				if (index_dir == null) {
					index_dir = Path.Combine (GetRemoteStorageDir (true), "Indexes");
					
					if (! Directory.Exists (index_dir)) {
						Directory.CreateDirectory (index_dir);

						// Make sure that the directory is only readable by the owner. 
						// Required when using index synchronization as then it resides in /tmp
						Mono.Unix.Native.Syscall.chmod (index_dir, (Mono.Unix.Native.FilePermissions) 448); // 448 == 0700
					}
				}
				
				return index_dir;
			}
		}

		static public string LogDir {
			get {
				string dir = Path.Combine (StorageDir, "Log");
				if (! Directory.Exists (dir))
					Directory.CreateDirectory (dir);
				return dir;
			}
		}
	}
}