~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins.Setup/Mono.Addins.Setup/AddinPackage.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// AddinPackage.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
using System.Collections;
 
31
using System.IO;
 
32
using System.Xml;
 
33
using System.Xml.Serialization;
 
34
using System.Reflection;
 
35
using System.Diagnostics;
 
36
using System.Collections.Specialized;
 
37
using System.Net;
 
38
 
 
39
using ICSharpCode.SharpZipLib.Zip;
 
40
using Mono.Addins;
 
41
using Mono.Addins.Description;
 
42
using System.Collections.Generic;
 
43
using System.Linq;
 
44
 
 
45
namespace Mono.Addins.Setup
 
46
{
 
47
        internal class AddinPackage: Package
 
48
        {
 
49
                AddinInfo info;
 
50
                string packFile;
 
51
                string url;
 
52
                string tempFolder;
 
53
                bool disablingOnUninstall;
 
54
                bool uninstallingLoaded;
 
55
                string configFile;
 
56
                bool installed;
 
57
                Addin iaddin;
 
58
                
 
59
                public AddinHeader Addin {
 
60
                        get { return info; }
 
61
                }
 
62
                
 
63
                public override string Name {
 
64
                        get { return info.Name + " v" + info.Version; }
 
65
                }
 
66
                
 
67
                public static AddinPackage PackageFromRepository (AddinRepositoryEntry repAddin)
 
68
                {
 
69
                        AddinPackage pack = new AddinPackage ();
 
70
                        pack.info = (AddinInfo) repAddin.Addin;
 
71
                        pack.url = new Uri (new Uri (repAddin.RepositoryUrl), repAddin.Url).ToString ();
 
72
                        return pack;
 
73
                }
 
74
                
 
75
                public static AddinPackage PackageFromFile (string file)
 
76
                {
 
77
                        AddinPackage pack = new AddinPackage ();
 
78
                        pack.info = ReadAddinInfo (file);
 
79
                        pack.packFile = file;
 
80
                        return pack;
 
81
                }
 
82
                
 
83
                public static AddinPackage FromInstalledAddin (Addin sinfo)
 
84
                {
 
85
                        AddinPackage pack = new AddinPackage ();
 
86
                        pack.info = AddinInfo.ReadFromDescription (sinfo.Description);
 
87
                        return pack;
 
88
                }
 
89
                
 
90
                static AddinInfo ReadAddinInfo (string file)
 
91
                {
 
92
                        ZipFile zfile = new ZipFile (file);
 
93
                        foreach (ZipEntry ze in zfile) {
 
94
                                if (ze.Name == "addin.info") {
 
95
                                        using (Stream s = zfile.GetInputStream (ze)) {
 
96
                                                return AddinInfo.ReadFromAddinFile (new StreamReader (s));
 
97
                                        }
 
98
                                }
 
99
                        }
 
100
                        throw new InstallException ("Addin configuration file not found in package.");
 
101
                }
 
102
                
 
103
                internal override bool IsUpgradeOf (Package p)
 
104
                {
 
105
                        AddinPackage ap = p as AddinPackage;
 
106
                        if (ap == null) return false;
 
107
                        return info.SupportsVersion (ap.info.Version);
 
108
                }
 
109
                
 
110
                public override bool Equals (object ob)
 
111
                {
 
112
                        AddinPackage ap = ob as AddinPackage;
 
113
                        if (ap == null) return false;
 
114
                        return ap.info.Id == info.Id && ap.info.Version == info.Version;
 
115
                }
 
116
                
 
117
                public override int GetHashCode ()
 
118
                {
 
119
                        return (info.Id + info.Version).GetHashCode ();
 
120
                }
 
121
                
 
122
                internal override void PrepareInstall (IProgressMonitor monitor, AddinStore service)
 
123
                {
 
124
                        if (service.Registry.IsRegisteredForUninstall (info.Id))
 
125
                                throw new InstallException ("The addin " + info.Name + " v" + info.Version + " is scheduled for uninstallation. Please restart the application before trying to install it again.");
 
126
                        if (service.Registry.GetAddin (Mono.Addins.Addin.GetFullId (info.Namespace, info.Id, info.Version), true) != null)
 
127
                                throw new InstallException ("The addin " + info.Name + " v" + info.Version + " is already installed.");
 
128
                                                
 
129
                        if (url != null)
 
130
                                packFile = service.DownloadFile (monitor, url);
 
131
                        
 
132
                        tempFolder = CreateTempFolder ();
 
133
 
 
134
                        // Extract the files                    
 
135
                        using (FileStream fs = new FileStream (packFile, FileMode.Open, FileAccess.Read)) {
 
136
                                ZipFile zip = new ZipFile (fs);
 
137
                                foreach (ZipEntry entry in zip) {
 
138
                                        string path = Path.Combine (tempFolder, entry.Name);
 
139
                                        string dir = Path.GetDirectoryName (path);
 
140
                                        if (!Directory.Exists (dir))
 
141
                                                Directory.CreateDirectory (dir);
 
142
                                                
 
143
                                        byte[] buffer = new byte [8192];
 
144
                                        int n=0;
 
145
                                        Stream inStream = zip.GetInputStream (entry);
 
146
                                        Stream outStream = null;
 
147
                                        try {
 
148
                                                outStream = File.Create (path);
 
149
                                                while ((n = inStream.Read (buffer, 0, buffer.Length)) > 0)
 
150
                                                        outStream.Write (buffer, 0, n);
 
151
                                        } finally {
 
152
                                                inStream.Close ();
 
153
                                                if (outStream != null)
 
154
                                                        outStream.Close ();
 
155
                                        }
 
156
                                }
 
157
                        }
 
158
                        
 
159
                        foreach (string s in Directory.GetFiles (tempFolder)) {
 
160
                                if (Path.GetFileName (s) == "addin.info") {
 
161
                                        configFile = s;
 
162
                                        break;
 
163
                                }
 
164
                        }
 
165
 
 
166
                        if (configFile == null)
 
167
                                throw new InstallException ("Add-in information file not found in package.");
 
168
                }
 
169
                
 
170
                internal override void CommitInstall (IProgressMonitor monitor, AddinStore service)
 
171
                {
 
172
                        service.RegisterAddin (monitor, info, tempFolder);
 
173
                        installed = true;
 
174
                }
 
175
                
 
176
                internal override void RollbackInstall (IProgressMonitor monitor, AddinStore service)
 
177
                {
 
178
                        if (installed) {
 
179
                                iaddin = service.Registry.GetAddin (info.Id);
 
180
                                if (iaddin != null)
 
181
                                        CommitUninstall (monitor, service);
 
182
                        }
 
183
                }
 
184
                
 
185
                internal override void EndInstall (IProgressMonitor monitor, AddinStore service)
 
186
                {
 
187
                        if (url != null && packFile != null)
 
188
                                File.Delete (packFile);
 
189
                        if (tempFolder != null)
 
190
                                Directory.Delete (tempFolder, true);
 
191
                }
 
192
                
 
193
                internal override void Resolve (IProgressMonitor monitor, AddinStore service, PackageCollection toInstall, PackageCollection toUninstall, PackageCollection installedRequired, DependencyCollection unresolved)
 
194
                {
 
195
                        Addin ia = service.Registry.GetAddin (Mono.Addins.Addin.GetIdName (info.Id));
 
196
                        
 
197
                        if (ia != null) {
 
198
                                Package p = AddinPackage.FromInstalledAddin (ia);
 
199
                                if (!toUninstall.Contains (p))
 
200
                                        toUninstall.Add (p);
 
201
                                        
 
202
                                if (!info.SupportsVersion (ia.Version)) {
 
203
                                
 
204
                                        // This addin breaks the api of the currently installed one,
 
205
                                        // it has to be removed, together with all dependencies
 
206
                                        
 
207
                                        Addin[] ainfos = service.GetDependentAddins (info.Id, true);
 
208
                                        foreach (Addin ainfo in ainfos) {
 
209
                                                p = AddinPackage.FromInstalledAddin (ainfo);
 
210
                                                if (!toUninstall.Contains (p))
 
211
                                                        toUninstall.Add (p);
 
212
                                        }
 
213
                                }
 
214
                        }
 
215
                        
 
216
                        foreach (Dependency dep in info.Dependencies) {
 
217
                                service.ResolveDependency (monitor, dep, this, toInstall, toUninstall, installedRequired, unresolved);
 
218
                        }
 
219
                }
 
220
                
 
221
                internal override void PrepareUninstall (IProgressMonitor monitor, AddinStore service)
 
222
                {
 
223
                        iaddin = service.Registry.GetAddin (info.Id, true);
 
224
                        if (iaddin == null)
 
225
                                throw new InstallException (string.Format ("The add-in '{0}' is not installed.", info.Name));
 
226
 
 
227
                        AddinDescription conf = iaddin.Description;
 
228
                        
 
229
                        if (!File.Exists (iaddin.AddinFile)) {
 
230
                                monitor.ReportWarning (string.Format ("The add-in '{0}' is scheduled for uninstalling, but the add-in file could not be found.", info.Name));
 
231
                                return;
 
232
                        }
 
233
                        
 
234
                        // The add-in is a core application add-in. It can't be uninstalled, so it will be disabled.
 
235
                        if (!service.IsUserAddin (iaddin.AddinFile)) {
 
236
                                disablingOnUninstall = true;
 
237
                                return;
 
238
                        }
 
239
                        
 
240
                        // If the add-in assemblies are loaded, or if there is any file with a write lock, delay the uninstallation
 
241
                        HashSet<string> files = new HashSet<string> (GetInstalledFiles (conf));
 
242
                        if (AddinManager.CheckAssembliesLoaded (files) || files.Any (f => HasWriteLock (f))) {
 
243
                                uninstallingLoaded = true;
 
244
                                return;
 
245
                        }
 
246
                        
 
247
                        if (!service.HasWriteAccess (iaddin.AddinFile))
 
248
                                throw new InstallException (AddinStore.GetUninstallErrorNoRoot (info));
 
249
 
 
250
                        foreach (string path in GetInstalledFiles (conf)) {
 
251
                                if (!service.HasWriteAccess (path))
 
252
                                        throw new InstallException (AddinStore.GetUninstallErrorNoRoot (info));
 
253
                        }
 
254
                        
 
255
                        tempFolder = CreateTempFolder ();
 
256
                        CopyAddinFiles (monitor, conf, iaddin.AddinFile, tempFolder);
 
257
                }
 
258
                
 
259
                bool HasWriteLock (string file)
 
260
                {
 
261
                        if (!File.Exists (file))
 
262
                                return false;
 
263
                        try {
 
264
                                File.OpenWrite (file).Close ();
 
265
                                return false;
 
266
                        } catch {
 
267
                                return true;
 
268
                        }
 
269
                }
 
270
                
 
271
                IEnumerable<string> GetInstalledFiles (AddinDescription conf)
 
272
                {
 
273
                        string basePath = Path.GetDirectoryName (conf.AddinFile);
 
274
                        foreach (string relPath in conf.AllFiles) {
 
275
                                string afile = Path.Combine (basePath, relPath);
 
276
                                if (File.Exists (afile))
 
277
                                        yield return afile;
 
278
                        }
 
279
                        foreach (var p in conf.Properties) {
 
280
                                string file;
 
281
                                try {
 
282
                                        file = Path.Combine (basePath, p.Value);
 
283
                                        if (!File.Exists (file))
 
284
                                                file = null;
 
285
                                } catch {
 
286
                                        file = null;
 
287
                                }
 
288
                                if (file != null)
 
289
                                        yield return file;
 
290
                        }
 
291
                }
 
292
                
 
293
                internal override void CommitUninstall (IProgressMonitor monitor, AddinStore service)
 
294
                {
 
295
                        if (disablingOnUninstall) {
 
296
                                disablingOnUninstall = false;
 
297
                                service.Registry.DisableAddin (info.Id);
 
298
                                return;
 
299
                        }
 
300
                        
 
301
                        AddinDescription conf = iaddin.Description;
 
302
                        
 
303
                        string basePath = Path.GetDirectoryName (conf.AddinFile);
 
304
                        
 
305
                        if (uninstallingLoaded) {
 
306
                                List<string> files = new List<string> ();
 
307
                                files.Add (iaddin.AddinFile);
 
308
                                foreach (string f in GetInstalledFiles (conf))
 
309
                                        files.Add (f);
 
310
                                service.Registry.RegisterForUninstall (info.Id, files);
 
311
                                return;
 
312
                        }
 
313
                        
 
314
                        if (tempFolder == null)
 
315
                                return;
 
316
 
 
317
                        monitor.Log.WriteLine ("Uninstalling " + info.Name + " v" + info.Version);
 
318
                        
 
319
                        foreach (string path in GetInstalledFiles (conf))
 
320
                                File.Delete (path);
 
321
                        
 
322
                        File.Delete (iaddin.AddinFile);
 
323
                        
 
324
                        RecDeleteDir (monitor, basePath);
 
325
                        
 
326
                        monitor.Log.WriteLine ("Done");
 
327
                }
 
328
                
 
329
                void RecDeleteDir (IProgressMonitor monitor, string path)
 
330
                {
 
331
                        if (Directory.GetFiles (path).Length != 0)
 
332
                                return;
 
333
                
 
334
                        foreach (string dir in Directory.GetDirectories (path))
 
335
                                RecDeleteDir (monitor, dir);
 
336
 
 
337
                        try {
 
338
                                Directory.Delete (path);
 
339
                        } catch {
 
340
                                monitor.ReportWarning ("Directory " + path + " could not be deleted.");
 
341
                        }
 
342
                }
 
343
                
 
344
                internal override void RollbackUninstall (IProgressMonitor monitor, AddinStore service)
 
345
                {
 
346
                        disablingOnUninstall = false;
 
347
                        if (tempFolder != null) {
 
348
                                AddinDescription conf = iaddin.Description;
 
349
                                string configFile = Path.Combine (tempFolder, Path.GetFileName (iaddin.AddinFile));
 
350
                                
 
351
                                string addinDir = Path.GetDirectoryName (iaddin.AddinFile);
 
352
                                CopyAddinFiles (monitor, conf, configFile, addinDir);
 
353
                        }
 
354
                }
 
355
                
 
356
                internal override void EndUninstall (IProgressMonitor monitor, AddinStore service)
 
357
                {
 
358
                        if (tempFolder != null)
 
359
                                Directory.Delete (tempFolder, true);
 
360
                        tempFolder = null;
 
361
                }
 
362
                
 
363
                void CopyAddinFiles (IProgressMonitor monitor, AddinDescription conf, string configFile, string destPath)
 
364
                {
 
365
                        if (!Directory.Exists (destPath))
 
366
                                Directory.CreateDirectory (destPath);
 
367
                        
 
368
                        string dfile = Path.Combine (destPath, Path.GetFileName (configFile));
 
369
                        if (File.Exists (dfile))
 
370
                                File.Delete (dfile);
 
371
                                
 
372
                        File.Copy (configFile, dfile);
 
373
                        
 
374
                        string basePath = Path.GetDirectoryName (configFile);
 
375
                        
 
376
                        foreach (string relPath in conf.AllFiles) {
 
377
                                string path = Path.Combine (basePath, relPath);
 
378
                                if (!File.Exists (path))
 
379
                                        continue;
 
380
                                
 
381
                                string destf = Path.Combine (destPath, Path.GetDirectoryName (relPath));
 
382
                                if (!Directory.Exists (destf))
 
383
                                        Directory.CreateDirectory (destf);
 
384
                                        
 
385
                                dfile = Path.Combine (destPath, relPath);
 
386
                                if (File.Exists (dfile))
 
387
                                        File.Delete (dfile);
 
388
 
 
389
                                File.Copy (path, dfile);
 
390
                        }
 
391
                }
 
392
                
 
393
                
 
394
        }
 
395
}