Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AddinPackage.cs « Mono.Addins.Setup « Mono.Addins.Setup - github.com/mono/mono-addins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c18b1d920c6fc7e6f341166eb377737cbc0002d8 (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//
// AddinPackage.cs
//
// Author:
//   Lluis Sanchez Gual
//
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
//
// 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.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Specialized;
using System.Net;

using ICSharpCode.SharpZipLib.Zip;
using Mono.Addins;
using Mono.Addins.Description;
using System.Collections.Generic;
using System.Linq;

namespace Mono.Addins.Setup
{
	internal class AddinPackage: Package
	{
		AddinInfo info;
		string packFile;
		string url;
		string tempFolder;
		bool disablingOnUninstall;
		bool uninstallingLoaded;
		string configFile;
		bool installed;
		Addin iaddin;
		
		public AddinHeader Addin {
			get { return info; }
		}
		
		public override string Name {
			get { return info.Name + " v" + info.Version; }
		}
		
		public static AddinPackage PackageFromRepository (AddinRepositoryEntry repAddin)
		{
			AddinPackage pack = new AddinPackage ();
			pack.info = (AddinInfo) repAddin.Addin;
			pack.url = new Uri (new Uri (repAddin.RepositoryUrl), repAddin.Url).ToString ();
			return pack;
		}
		
		public static AddinPackage PackageFromFile (string file)
		{
			AddinPackage pack = new AddinPackage ();
			pack.info = ReadAddinInfo (file);
			pack.packFile = file;
			return pack;
		}
		
		public static AddinPackage FromInstalledAddin (Addin sinfo)
		{
			AddinPackage pack = new AddinPackage ();
			pack.info = AddinInfo.ReadFromDescription (sinfo.Description);
			return pack;
		}
		
		static AddinInfo ReadAddinInfo (string file)
		{
			ZipFile zfile = new ZipFile (file);
			foreach (ZipEntry ze in zfile) {
				if (ze.Name == "addin.info") {
					using (Stream s = zfile.GetInputStream (ze)) {
						return AddinInfo.ReadFromAddinFile (new StreamReader (s));
					}
				}
			}
			throw new InstallException ("Addin configuration file not found in package.");
		}
		
		internal override bool IsUpgradeOf (Package p)
		{
			AddinPackage ap = p as AddinPackage;
			if (ap == null) return false;
			return info.SupportsVersion (ap.info.Version);
		}
		
		public override bool Equals (object ob)
		{
			AddinPackage ap = ob as AddinPackage;
			if (ap == null) return false;
			return ap.info.Id == info.Id && ap.info.Version == info.Version;
		}
		
		public override int GetHashCode ()
		{
			return (info.Id + info.Version).GetHashCode ();
		}
		
		internal override void PrepareInstall (IProgressMonitor monitor, AddinStore service)
		{
			if (service.Registry.IsRegisteredForUninstall (info.Id))
				throw new InstallException ("The addin " + info.Name + " v" + info.Version + " is scheduled for uninstallation. Please restart the application before trying to install it again.");
			if (service.Registry.GetAddin (Mono.Addins.Addin.GetFullId (info.Namespace, info.Id, info.Version), true) != null)
				throw new InstallException ("The addin " + info.Name + " v" + info.Version + " is already installed.");
						
			if (url != null)
				packFile = service.DownloadFile (monitor, url);
			
			tempFolder = CreateTempFolder ();

			// Extract the files			
			using (FileStream fs = new FileStream (packFile, FileMode.Open, FileAccess.Read)) {
				ZipFile zip = new ZipFile (fs);
				foreach (ZipEntry entry in zip) {
					string path = Path.Combine (tempFolder, entry.Name);
					string dir = Path.GetDirectoryName (path);
					if (!Directory.Exists (dir))
						Directory.CreateDirectory (dir);
						
					byte[] buffer = new byte [8192];
					int n=0;
					Stream inStream = zip.GetInputStream (entry);
					Stream outStream = null;
					try {
						outStream = File.Create (path);
						while ((n = inStream.Read (buffer, 0, buffer.Length)) > 0)
							outStream.Write (buffer, 0, n);
					} finally {
						inStream.Close ();
						if (outStream != null)
							outStream.Close ();
					}
				}
			}
			
			foreach (string s in Directory.GetFiles (tempFolder)) {
				if (Path.GetFileName (s) == "addin.info") {
					configFile = s;
					break;
				}
			}

			if (configFile == null)
				throw new InstallException ("Add-in information file not found in package.");
		}
		
		internal override void CommitInstall (IProgressMonitor monitor, AddinStore service)
		{
			service.RegisterAddin (monitor, info, tempFolder);
			installed = true;
		}
		
		internal override void RollbackInstall (IProgressMonitor monitor, AddinStore service)
		{
			if (installed) {
				iaddin = service.Registry.GetAddin (info.Id);
				if (iaddin != null)
					CommitUninstall (monitor, service);
			}
		}
		
		internal override void EndInstall (IProgressMonitor monitor, AddinStore service)
		{
			if (url != null && packFile != null)
				File.Delete (packFile);
			if (tempFolder != null)
				Directory.Delete (tempFolder, true);
		}
		
		internal override void Resolve (IProgressMonitor monitor, AddinStore service, PackageCollection toInstall, PackageCollection toUninstall, PackageCollection installedRequired, DependencyCollection unresolved)
		{
			Addin ia = service.Registry.GetAddin (Mono.Addins.Addin.GetIdName (info.Id));
			
			if (ia != null) {
				Package p = AddinPackage.FromInstalledAddin (ia);
				if (!toUninstall.Contains (p))
					toUninstall.Add (p);
					
				if (!info.SupportsVersion (ia.Version)) {
				
					// This addin breaks the api of the currently installed one,
					// it has to be removed, together with all dependencies
					
					Addin[] ainfos = service.GetDependentAddins (info.Id, true);
					foreach (Addin ainfo in ainfos) {
						p = AddinPackage.FromInstalledAddin (ainfo);
						if (!toUninstall.Contains (p))
							toUninstall.Add (p);
					}
				}
			}
			
			foreach (Dependency dep in info.Dependencies) {
				service.ResolveDependency (monitor, dep, this, toInstall, toUninstall, installedRequired, unresolved);
			}
		}
		
		internal override void PrepareUninstall (IProgressMonitor monitor, AddinStore service)
		{
			iaddin = service.Registry.GetAddin (info.Id, true);
			if (iaddin == null)
				throw new InstallException (string.Format ("The add-in '{0}' is not installed.", info.Name));

			AddinDescription conf = iaddin.Description;
			
			if (!File.Exists (iaddin.AddinFile)) {
				monitor.ReportWarning (string.Format ("The add-in '{0}' is scheduled for uninstalling, but the add-in file could not be found.", info.Name));
				return;
			}
			
			// The add-in is a core application add-in. It can't be uninstalled, so it will be disabled.
			if (!service.IsUserAddin (iaddin.AddinFile)) {
				disablingOnUninstall = true;
				return;
			}
			
			// If the add-in assemblies are loaded, or if there is any file with a write lock, delay the uninstallation
			HashSet<string> files = new HashSet<string> (GetInstalledFiles (conf));
			if (AddinManager.CheckAssembliesLoaded (files) || files.Any (f => HasWriteLock (f))) {
				uninstallingLoaded = true;
				return;
			}
			
			if (!service.HasWriteAccess (iaddin.AddinFile))
				throw new InstallException (AddinStore.GetUninstallErrorNoRoot (info));

			foreach (string path in GetInstalledFiles (conf)) {
				if (!service.HasWriteAccess (path))
					throw new InstallException (AddinStore.GetUninstallErrorNoRoot (info));
			}
			
			tempFolder = CreateTempFolder ();
			CopyAddinFiles (monitor, conf, iaddin.AddinFile, tempFolder);
		}
		
		bool HasWriteLock (string file)
		{
			if (!File.Exists (file))
				return false;
			try {
				File.OpenWrite (file).Close ();
				return false;
			} catch {
				return true;
			}
		}
		
		IEnumerable<string> GetInstalledFiles (AddinDescription conf)
		{
			string basePath = Path.GetDirectoryName (conf.AddinFile);
			foreach (string relPath in conf.AllFiles) {
				string afile = Path.Combine (basePath, relPath);
				if (File.Exists (afile))
					yield return afile;
			}
			foreach (var p in conf.Properties) {
				string file;
				try {
					file = Path.Combine (basePath, p.Value);
					if (!File.Exists (file))
						file = null;
				} catch {
					file = null;
				}
				if (file != null)
					yield return file;
			}
		}
		
		internal override void CommitUninstall (IProgressMonitor monitor, AddinStore service)
		{
			if (disablingOnUninstall) {
				disablingOnUninstall = false;
				service.Registry.DisableAddin (info.Id);
				return;
			}
			
			AddinDescription conf = iaddin.Description;
			
			string basePath = Path.GetDirectoryName (conf.AddinFile);
			
			if (uninstallingLoaded) {
				List<string> files = new List<string> ();
				files.Add (iaddin.AddinFile);
				foreach (string f in GetInstalledFiles (conf))
					files.Add (f);
				service.Registry.RegisterForUninstall (info.Id, files);
				return;
			}
			
			if (tempFolder == null)
				return;

			monitor.Log.WriteLine ("Uninstalling " + info.Name + " v" + info.Version);
			
			foreach (string path in GetInstalledFiles (conf))
				File.Delete (path);
			
			File.Delete (iaddin.AddinFile);
			
			RecDeleteDir (monitor, basePath);
			
			monitor.Log.WriteLine ("Done");
		}
		
		void RecDeleteDir (IProgressMonitor monitor, string path)
		{
			if (Directory.GetFiles (path).Length != 0)
				return;
		
			foreach (string dir in Directory.GetDirectories (path))
				RecDeleteDir (monitor, dir);

			try {
				Directory.Delete (path);
			} catch {
				monitor.ReportWarning ("Directory " + path + " could not be deleted.");
			}
		}
		
		internal override void RollbackUninstall (IProgressMonitor monitor, AddinStore service)
		{
			disablingOnUninstall = false;
			if (tempFolder != null) {
				AddinDescription conf = iaddin.Description;
				string configFile = Path.Combine (tempFolder, Path.GetFileName (iaddin.AddinFile));
				
				string addinDir = Path.GetDirectoryName (iaddin.AddinFile);
				CopyAddinFiles (monitor, conf, configFile, addinDir);
			}
		}
		
		internal override void EndUninstall (IProgressMonitor monitor, AddinStore service)
		{
			if (tempFolder != null)
				Directory.Delete (tempFolder, true);
			tempFolder = null;
		}
		
		void CopyAddinFiles (IProgressMonitor monitor, AddinDescription conf, string configFile, string destPath)
		{
			if (!Directory.Exists (destPath))
				Directory.CreateDirectory (destPath);
			
			string dfile = Path.Combine (destPath, Path.GetFileName (configFile));
			if (File.Exists (dfile))
				File.Delete (dfile);
				
			File.Copy (configFile, dfile);
			
			string basePath = Path.GetDirectoryName (configFile);
			
			foreach (string relPath in conf.AllFiles) {
				string path = Path.Combine (basePath, relPath);
				if (!File.Exists (path))
					continue;
				
				string destf = Path.Combine (destPath, Path.GetDirectoryName (relPath));
				if (!Directory.Exists (destf))
					Directory.CreateDirectory (destf);
					
				dfile = Path.Combine (destPath, relPath);
				if (File.Exists (dfile))
					File.Delete (dfile);

				File.Copy (path, dfile);
			}
		}
		
		
	}
}