Files
Pilz.Updating/Pilz.Updating.Client/Utils.cs
2025-11-08 16:36:21 +01:00

90 lines
2.4 KiB
C#

using System.Diagnostics;
using System.Runtime.InteropServices;
using Pilz.Runtime;
namespace Pilz.Updating.Client;
public static class Utils
{
public static void CopyFiles(DirectoryInfo sourceDir, DirectoryInfo destinationDir)
{
if (!destinationDir.Exists)
destinationDir.Create();
foreach (var sFile in sourceDir.EnumerateFiles("*", SearchOption.TopDirectoryOnly))
{
var dFile = new FileInfo(Path.Combine(destinationDir.FullName, sFile.Name));
CopyFile(sFile, dFile);
}
foreach (var sDir in sourceDir.EnumerateDirectories("*", SearchOption.TopDirectoryOnly))
{
var dDir = destinationDir.CreateSubdirectory(sDir.Name);
CopyFiles(sDir, dDir);
}
}
public static void CopyFile(FileInfo sourceFile, FileInfo destinationFile)
{
var triesLeft = 1;
var srcFileName = sourceFile.FullName;
var destFileName = destinationFile.FullName;
string? oldFile = null;
while (triesLeft > 0)
{
triesLeft--;
try
{
File.Copy(srcFileName, destFileName, true);
}
catch (IOException)
{
if (triesLeft == 0 && File.Exists(destFileName))
{
oldFile = destFileName + ".old";
if (File.Exists(oldFile))
File.Delete(oldFile);
File.Move(destFileName, oldFile);
triesLeft++;
Thread.Sleep(100);
}
}
catch (Exception)
{
// Ignore
}
}
if (oldFile != null)
{
try
{
File.Delete(oldFile);
}
catch (Exception)
{
// Ignore
}
}
}
public static void MakeExecutable(string filePath)
{
var pChmod = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "chmod",
Arguments = $"+x \"{filePath}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
},
};
pChmod.Start();
pChmod.WaitForExit();
}
}