(ArchiveFormat): CreateFile and CreatePath methods moved to PhysicalFileSystem.

This commit is contained in:
morkt 2018-09-19 12:09:05 +04:00
parent 5ab09b0af4
commit e7d531e214
3 changed files with 42 additions and 39 deletions

View File

@ -55,7 +55,7 @@ namespace GARbro.GUI
protected Stream CreateNewFile (string filename, bool create_path = false) protected Stream CreateNewFile (string filename, bool create_path = false)
{ {
if (create_path) if (create_path)
filename = GameRes.ArchiveFormat.CreatePath (filename); filename = GameRes.PhysicalFileSystem.CreatePath (filename);
FileMode open_mode = FileMode.CreateNew; FileMode open_mode = FileMode.CreateNew;
if (m_duplicate_action.ApplyToAll && if (m_duplicate_action.ApplyToAll &&
m_duplicate_action.Action == ExistingFileAction.Overwrite) m_duplicate_action.Action == ExistingFileAction.Overwrite)

View File

@ -49,7 +49,7 @@ namespace GameRes
public void Extract (ArcFile file, Entry entry) public void Extract (ArcFile file, Entry entry)
{ {
using (var input = OpenEntry (file, entry)) using (var input = OpenEntry (file, entry))
using (var output = CreateFile (entry.Name)) using (var output = PhysicalFileSystem.CreateFile (entry.Name))
input.CopyTo (output); input.CopyTo (output);
} }
@ -70,40 +70,6 @@ namespace GameRes
return ImageFormatDecoder.Create (input); return ImageFormatDecoder.Create (input);
} }
/// <summary>
/// Create file corresponding to <paramref name="entry"/> in current directory and open it
/// for writing. Overwrites existing file, if any.
/// </summary>
static public Stream CreateFile (string filename)
{
filename = CreatePath (filename);
return File.Create (filename);
}
static public string CreatePath (string filename)
{
string dir = Path.GetDirectoryName (filename);
if (!string.IsNullOrEmpty (dir)) // check for malformed filenames
{
string root = Path.GetPathRoot (dir);
if (!string.IsNullOrEmpty (root))
{
dir = dir.Substring (root.Length); // strip root
}
string cwd = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar;
dir = Path.GetFullPath (dir);
filename = Path.GetFileName (filename);
// check whether filename would reside within current directory
if (dir.StartsWith (cwd, StringComparison.OrdinalIgnoreCase))
{
// path looks legit, create it
Directory.CreateDirectory (dir);
filename = Path.Combine (dir, filename);
}
}
return filename;
}
/// <summary> /// <summary>
/// Create resource within stream <paramref name="file"/> containing entries from the /// Create resource within stream <paramref name="file"/> containing entries from the
/// supplied <paramref name="list"/> and applying necessary <paramref name="options"/>. /// supplied <paramref name="list"/> and applying necessary <paramref name="options"/>.

View File

@ -138,7 +138,7 @@ namespace GameRes
var info = new DirectoryInfo (CurrentDirectory); var info = new DirectoryInfo (CurrentDirectory);
foreach (var subdir in info.EnumerateDirectories()) foreach (var subdir in info.EnumerateDirectories())
{ {
if (0 != (subdir.Attributes & (FileAttributes.Hidden | FileAttributes.System))) if (0 != (subdir.Attributes & FileAttributes.System))
continue; continue;
yield return new SubDirEntry (subdir.FullName); yield return new SubDirEntry (subdir.FullName);
} }
@ -158,7 +158,7 @@ namespace GameRes
var info = new DirectoryInfo (path); var info = new DirectoryInfo (path);
foreach (var file in info.EnumerateFiles (pattern)) foreach (var file in info.EnumerateFiles (pattern))
{ {
if (0 != (file.Attributes & (FileAttributes.Hidden | FileAttributes.System))) if (0 != (file.Attributes & FileAttributes.System))
continue; continue;
yield return EntryFromFileInfo (file); yield return EntryFromFileInfo (file);
} }
@ -169,7 +169,7 @@ namespace GameRes
var info = new DirectoryInfo (CurrentDirectory); var info = new DirectoryInfo (CurrentDirectory);
foreach (var file in info.EnumerateFiles ("*", SearchOption.AllDirectories)) foreach (var file in info.EnumerateFiles ("*", SearchOption.AllDirectories))
{ {
if (0 != (file.Attributes & (FileAttributes.Hidden | FileAttributes.System))) if (0 != (file.Attributes & FileAttributes.System))
continue; continue;
yield return EntryFromFileInfo (file); yield return EntryFromFileInfo (file);
} }
@ -202,6 +202,43 @@ namespace GameRes
{ {
GC.SuppressFinalize (this); GC.SuppressFinalize (this);
} }
/// <summary>
/// Create file named <paramref name="filename"/> in current directory and open it
/// for writing. Overwrites existing file, if any.
/// </summary>
static public Stream CreateFile (string filename)
{
filename = CreatePath (filename);
return File.Create (filename);
}
/// <summary>
/// Create all directories that lead to <paramref name="filename"/>, if any.
/// </summary>
static public string CreatePath (string filename)
{
string dir = Path.GetDirectoryName (filename);
if (!string.IsNullOrEmpty (dir)) // check for malformed filenames
{
string root = Path.GetPathRoot (dir);
if (!string.IsNullOrEmpty (root))
{
dir = dir.Substring (root.Length); // strip root
}
string cwd = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar;
dir = Path.GetFullPath (dir);
filename = Path.GetFileName (filename);
// check whether filename would reside within current directory
if (dir.StartsWith (cwd, StringComparison.OrdinalIgnoreCase))
{
// path looks legit, create it
Directory.CreateDirectory (dir);
filename = Path.Combine (dir, filename);
}
}
return filename;
}
} }
public abstract class ArchiveFileSystem : IFileSystem public abstract class ArchiveFileSystem : IFileSystem