49 lines
2.4 KiB
C#
49 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
|
|
// Qualification uses disposable fixtures, never the installed application lock.
|
|
public static class CompanionFileCopyPreflight
|
|
{
|
|
public static FileStream AcquireAgentGuard(string stateDirectory, int session)
|
|
{
|
|
Directory.CreateDirectory(stateDirectory);
|
|
// Same lock as TrayApplication.Run prevents a shortcut/sign-in restart
|
|
// while an explicitly authorised installer is replacing the payload.
|
|
return File.Open(Path.Combine(stateDirectory, "agent-" + session + ".lock"),
|
|
FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
|
|
}
|
|
|
|
public static void Verify(string home, IEnumerable<string> files, Action<string> log)
|
|
{
|
|
string root = Path.GetFullPath(home).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
|
|
foreach (string relative in files)
|
|
{
|
|
string path = Path.GetFullPath(Path.Combine(root, relative));
|
|
if (!path.StartsWith(root, StringComparison.OrdinalIgnoreCase))
|
|
throw new InvalidDataException("Payload path escapes the installation directory.");
|
|
try
|
|
{
|
|
try { File.GetAttributes(path); }
|
|
catch (FileNotFoundException) { continue; }
|
|
catch (DirectoryNotFoundException) { continue; }
|
|
// OPEN_EXISTING: check replacement access without writing bytes,
|
|
// truncating, renaming, deleting or changing permissions.
|
|
using (File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) { }
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (!(ex is IOException) && !(ex is UnauthorizedAccessException)) throw;
|
|
int error = Marshal.GetHRForException(ex) & 0xffff;
|
|
log("Payload replacement preflight failed: " + path + "; Windows error=" + error);
|
|
throw new IOException("Cannot replace Companion file: " + path +
|
|
" (Windows error " + error + "). Exit the Companion tray app and close Word, then retry. " +
|
|
"If it persists, retain the setup log. No registration or MSI changes were attempted; " +
|
|
"do not delete the installation folder or change permissions.", ex);
|
|
}
|
|
}
|
|
log("Existing payload passed non-writing replacement preflight.");
|
|
}
|
|
}
|