Skip to content

Commit

Permalink
Protect folders Ships/[VAB|SPH] from deletion.
Browse files Browse the repository at this point in the history
Throw a DirectoryNotFoundKraken if trying to write a file and part of its
path is missing (usually Ships and/or VAB/SPH, see #3 and KSP-CKAN#5).
Should be catched in cmdline and gui.
  • Loading branch information
Ormira committed Dec 27, 2014
1 parent 095c578 commit e1b49c4
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions ModuleInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,13 +584,20 @@ internal static void CopyZipEntry(ZipFile zipfile, ZipEntry entry, string fullPa
// overwite files, as it ensures deletiion on rollback.
file_transaction.Snapshot(fullPath);

// It's a file! Prepare the streams
using (Stream zipStream = zipfile.GetInputStream(entry))
using (FileStream writer = File.Create(fullPath))
try
{
// 4k is the block size on practically every disk and OS.
byte[] buffer = new byte[4096];
StreamUtils.Copy(zipStream, writer, buffer);
// It's a file! Prepare the streams
using (Stream zipStream = zipfile.GetInputStream(entry))
using (FileStream writer = File.Create(fullPath))
{
// 4k is the block size on practically every disk and OS.
byte[] buffer = new byte[4096];
StreamUtils.Copy(zipStream, writer, buffer);
}
}
catch (DirectoryNotFoundException ex)
{
throw new DirectoryNotFoundKraken("", ex.Message, ex);
}
}
}
Expand Down Expand Up @@ -707,6 +714,12 @@ private void Uninstall(string modName)
{
if (!Directory.EnumerateFileSystemEntries(directory).Any())
{
// Skip Ships/VAB ans Ships/SPH
if (directory == KSPPathUtils.ToAbsolute("VAB", ksp.Ships())
|| directory == KSPPathUtils.ToAbsolute("SPH", ksp.Ships()))
{
continue;
}

// We *don't* use our file_transaction to delete files here, because
// it fails if the system's temp directory is on a different device
Expand Down

0 comments on commit e1b49c4

Please sign in to comment.