aboutsummaryrefslogtreecommitdiff
path: root/src/tools/Dtf/WiFile/WiFile.cs
blob: 1e5c80df5ea7013dc2728ef7b68508cf2d883f8e (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
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

using System;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.RegularExpressions;
using WixToolset.Dtf.WindowsInstaller;
using WixToolset.Dtf.WindowsInstaller.Package;

[assembly: AssemblyDescription("Windows Installer package file extraction and update tool")]


/// <summary>
/// Shows sample use of the InstallPackage class.
/// </summary>
public class WiFile
{
	public static void Usage(TextWriter w)
	{
		w.WriteLine("Usage: WiFile.exe package.msi /l [filename,filename2,...]");
		w.WriteLine("Usage: WiFile.exe package.msi /x [filename,filename2,...]");
		w.WriteLine("Usage: WiFile.exe package.msi /u [filename,filename2,...]");
		w.WriteLine();
		w.WriteLine("Lists (/l), extracts (/x) or updates (/u) files in an MSI or MSM.");
		w.WriteLine("Files are extracted using their source path relative to the package.");
		w.WriteLine("Specified filenames do not include paths.");
		w.WriteLine("Filenames may be a pattern such as *.exe or file?.dll");
	}

    [SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
	public static int Main(string[] args)
	{
		if(!(args.Length == 2 || args.Length == 3))
		{
			Usage(Console.Out);
			return -1;
		}

		string msiFile = args[0];

		string option = args[1].ToLowerInvariant();
		if(option.StartsWith("-", StringComparison.Ordinal)) option = "/" + option.Substring(1);

		string[] fileNames = null;
		if(args.Length == 3)
		{
			fileNames = args[2].Split(',');
		}

		try
		{
			switch(option)
			{
				case "/l":
					using(InstallPackage pkg = new InstallPackage(msiFile, DatabaseOpenMode.ReadOnly))
					{
						pkg.Message += new InstallPackageMessageHandler(Console.WriteLine);
						IEnumerable<string> fileKeys = (fileNames != null ? FindFileKeys(pkg, fileNames) : pkg.Files.Keys);

						foreach(string fileKey in fileKeys)
						{
							Console.WriteLine(pkg.Files[fileKey]);
						}
					}
					break;

				case "/x":
					using(InstallPackage pkg = new InstallPackage(msiFile, DatabaseOpenMode.ReadOnly))
					{
						pkg.Message += new InstallPackageMessageHandler(Console.WriteLine);
						ICollection<string> fileKeys = FindFileKeys(pkg, fileNames);

						pkg.ExtractFiles(fileKeys);
					}
					break;

				case "/u":
					using(InstallPackage pkg = new InstallPackage(msiFile, DatabaseOpenMode.Transact))
					{
						pkg.Message += new InstallPackageMessageHandler(Console.WriteLine);
                        ICollection<string> fileKeys = FindFileKeys(pkg, fileNames);

						pkg.UpdateFiles(fileKeys);
						pkg.Commit();
					}
					break;

				default:
                    Usage(Console.Out);
					return -1;
			}
		}
		catch(InstallerException iex)
		{
			Console.WriteLine("Error: " + iex.Message);
			return iex.ErrorCode != 0 ? iex.ErrorCode : 1;
		}
		catch(FileNotFoundException fnfex)
		{
			Console.WriteLine(fnfex.Message);
			return 2;
		}
		catch(Exception ex)
		{
			Console.WriteLine("Error: " + ex.Message);
			return 1;
		}
		return 0;
	}

	static ICollection<string> FindFileKeys(InstallPackage pkg, ICollection<string> fileNames)
	{
		List<string> fileKeys = null;
		if(fileNames != null)
		{
			fileKeys = new List<string>();
			foreach(string fileName in fileNames)
			{
				string[] foundFileKeys = null;
				if(fileName.IndexOfAny(new char[] { '*', '?' }) >= 0)
				{
					foundFileKeys = pkg.FindFiles(FilePatternToRegex(fileName));
				}
				else
				{
					foundFileKeys = pkg.FindFiles(fileName);
				}
				fileKeys.AddRange(foundFileKeys);
			}
			if(fileKeys.Count == 0)
			{
				throw new FileNotFoundException("Files not found in package.");
			}
		}
		return fileKeys;
	}

	static Regex FilePatternToRegex(string pattern)
	{
		return new Regex("^" + Regex.Escape(pattern).Replace("\\*", ".*").Replace("\\?", ".") + "$",
			RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
	}
}