CarotDav_decompile/Rei.Fs/IO/FileSystem.cs

483 lines
18 KiB
C#
Raw Normal View History

// Decompiled with JetBrains decompiler
// Type: Rei.Fs.IO.FileSystem
// Assembly: Rei.Fs, Version=1.13.2.8796, Culture=neutral, PublicKeyToken=null
// MVID: D8B08A8B-697C-4439-9CFF-1BE4EE46F7B0
// Assembly location: F:\Eigene Dateien\Dropbox\portable Collection\Progs\CarotDAV\Rei.Fs.dll
using Microsoft.VisualBasic.CompilerServices;
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Rei.Fs.IO
{
[StandardModule]
public sealed class FileSystem
{
private static DateTime win32timeorigin = new DateTime(1601, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static string CombinePath(string path1, string path2)
{
if (path1.EndsWith("\\"))
{
if (path2.StartsWith("\\"))
return path1 + path2.Substring(1);
return path1 + path2;
}
if (path2.StartsWith("\\"))
return path1 + path2;
return path1 + "\\" + path2;
}
public static string GetFilename(string path)
{
while (path.EndsWith("\\"))
path = path.Substring(0, checked (path.Length - 1));
int num = path.LastIndexOf("\\");
if (num < 0)
return path;
return path.Substring(checked (num + 1));
}
public static string GetDirectoryName(string path)
{
while (path.EndsWith("\\"))
path = path.Substring(0, checked (path.Length - 1));
int length = path.LastIndexOf("\\");
if (length < 0)
return path;
return path.Substring(0, length);
}
public static string NormalizePath(string path)
{
if (path.StartsWith("\\\\?\\"))
return path;
string str1 = path;
if (str1.Length >= 248)
;
string str2 = str1.Replace("/", "\\");
if (str2.StartsWith("\\\\"))
path = "\\\\?\\UNC\\" + str2.Substring(1);
else if (path.Length > 3 && Operators.CompareString(Conversions.ToString(path[1]), ":", false) == 0 && Operators.CompareString(Conversions.ToString(path[2]), "\\", false) == 0)
path = "\\\\?\\" + str2.Replace("/", "\\");
else if (Operators.CompareString(Conversions.ToString(path[0]), "\\", false) == 0)
path = "\\\\?\\" + str2.Replace("/", "\\");
return path;
}
public static System.Runtime.InteropServices.ComTypes.FILETIME ToFileTime(DateTime time)
{
byte[] bytes = BitConverter.GetBytes(DateTime.Compare(time, FileSystem.win32timeorigin) >= 0 ? time.ToFileTime() : 0L);
System.Runtime.InteropServices.ComTypes.FILETIME filetime;
filetime.dwHighDateTime = BitConverter.ToInt32(bytes, 4);
filetime.dwLowDateTime = BitConverter.ToInt32(bytes, 0);
return filetime;
}
private static void RaiseError(string path)
{
FileSystem.RaiseError(path, Marshal.GetLastWin32Error());
}
private static void RaiseError(string path, int code)
{
switch (code)
{
case 2:
throw new FileNotFoundException();
case 3:
throw new DirectoryNotFoundException();
case 5:
throw new UnauthorizedAccessException();
case 15:
throw new DriveNotFoundException();
default:
throw new IOException(new Win32Exception(code).Message);
}
}
public static FileInfo GetFileInfo(string path)
{
FileInfo info = (FileInfo) null;
if (!FileSystem.TryGetFileInfo(path, ref info))
FileSystem.RaiseError(path);
return info;
}
public static bool TryGetFileInfo(string path, ref FileInfo info)
{
string path1 = FileSystem.NormalizePath(path);
Win32Native.WIN32_FILE_ATTRIBUTE_DATA fileData;
if (!Win32Native.GetFileAttributesEx(FileSystem.NormalizePath(path), Win32Native.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, ref fileData))
{
switch (Marshal.GetLastWin32Error())
{
case 2:
case 3:
case 50:
case 67:
return false;
default:
throw new ArgumentOutOfRangeException("TryGetFileInfo error path:" + path + " error:" + Conversions.ToString(Marshal.GetLastWin32Error()));
}
}
else
{
info = new FileInfo(path1, fileData);
return true;
}
}
public static bool FileOrDirectoryExists(string path, ref bool isdirectory)
{
Win32Native.ErrorModeType uMode = Win32Native.SetErrorMode(Win32Native.ErrorModeType.SEM_FAILCRITICALERRORS);
Win32Native.WIN32_FILE_ATTRIBUTE_DATA fileData;
bool fileAttributesEx;
try
{
fileAttributesEx = Win32Native.GetFileAttributesEx(FileSystem.NormalizePath(path), Win32Native.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, ref fileData);
}
finally
{
int num = (int) Win32Native.SetErrorMode(uMode);
}
if (!fileAttributesEx)
{
int lastWin32Error = Marshal.GetLastWin32Error();
switch (lastWin32Error)
{
case 2:
case 3:
break;
default:
throw new ArgumentOutOfRangeException(path + " " + lastWin32Error.ToString());
}
}
else
isdirectory = (fileData.dwFileAttributes & FileAttributes.Directory) != (FileAttributes) 0;
return fileAttributesEx;
}
public static bool FileExists(string path)
{
Win32Native.ErrorModeType uMode = Win32Native.SetErrorMode(Win32Native.ErrorModeType.SEM_FAILCRITICALERRORS);
Win32Native.WIN32_FILE_ATTRIBUTE_DATA fileData;
bool fileAttributesEx;
try
{
fileAttributesEx = Win32Native.GetFileAttributesEx(FileSystem.NormalizePath(path), Win32Native.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, ref fileData);
}
finally
{
int num = (int) Win32Native.SetErrorMode(uMode);
}
if (!fileAttributesEx)
{
switch (Marshal.GetLastWin32Error())
{
case 2:
case 3:
break;
default:
throw new ArgumentOutOfRangeException(path);
}
}
return fileAttributesEx && (fileData.dwFileAttributes & FileAttributes.Directory) == (FileAttributes) 0;
}
public static bool DirectoryExists(string path)
{
Win32Native.ErrorModeType uMode = Win32Native.SetErrorMode(Win32Native.ErrorModeType.SEM_FAILCRITICALERRORS);
Win32Native.WIN32_FILE_ATTRIBUTE_DATA fileData;
bool fileAttributesEx;
try
{
fileAttributesEx = Win32Native.GetFileAttributesEx(FileSystem.NormalizePath(path), Win32Native.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, ref fileData);
}
finally
{
int num = (int) Win32Native.SetErrorMode(uMode);
}
if (!fileAttributesEx)
{
switch (Marshal.GetLastWin32Error())
{
case 2:
case 3:
break;
default:
throw new ArgumentOutOfRangeException(path);
}
}
return fileAttributesEx && (fileData.dwFileAttributes & FileAttributes.Directory) != (FileAttributes) 0;
}
public static string[] GetNetworkShare(string host)
{
List<string> stringList = new List<string>();
int entriesread = 0;
int totalentries = 0;
int resume_handle = 0;
int num1 = Marshal.SizeOf(typeof (Win32Native.SHARE_INFO_0));
IntPtr zero = IntPtr.Zero;
int hresult = Win32Native.NetShareEnum(new StringBuilder(host), 0, ref zero, uint.MaxValue, ref entriesread, ref totalentries, ref resume_handle);
if (hresult != 0)
throw new IOException("unknown error", hresult);
try
{
IntPtr ptr = zero;
int num2 = 0;
int num3 = checked (entriesread - 1);
int num4 = num2;
while (num4 <= num3)
{
object structure = Marshal.PtrToStructure(ptr, typeof (Win32Native.SHARE_INFO_0));
Win32Native.SHARE_INFO_0 shareInfo0_1;
Win32Native.SHARE_INFO_0 shareInfo0_2 = structure != null ? (Win32Native.SHARE_INFO_0) structure : shareInfo0_1;
stringList.Add(shareInfo0_2.shi0_netname);
ptr = new IntPtr(checked (ptr.ToInt32() + num1));
checked { ++num4; }
}
}
finally
{
Win32Native.NetApiBufferFree(zero);
}
return stringList.ToArray();
}
public static SafeFileHandle CreateFile(string path, FileAccess access, FileShare share, FileMode mode)
{
return FileSystem.CreateFile(path, access, share, mode, 128U);
}
public static SafeFileHandle CreateFile(string path, FileAccess access, FileShare share, FileMode mode, uint dwFlagsAndAttributes)
{
SafeFileHandle file;
if (mode == FileMode.Append)
{
file = Win32Native.CreateFile(FileSystem.NormalizePath(path), checked ((uint) access), checked ((uint) share), IntPtr.Zero, 4U, dwFlagsAndAttributes | 268435456U, IntPtr.Zero);
if (!file.IsInvalid)
{
int num = (int) Win32Native.SetFilePointer(file, 0, IntPtr.Zero, Win32Native.MoveMethodType.FILE_END);
}
}
else
{
uint dwDesiredAccess;
if (access == FileAccess.Read)
dwDesiredAccess = 2147483648U;
else if (access == FileAccess.Write)
dwDesiredAccess = 1073741824U;
else if (access == FileAccess.ReadWrite)
dwDesiredAccess = 268435456U;
file = Win32Native.CreateFile(FileSystem.NormalizePath(path), dwDesiredAccess, checked ((uint) share), IntPtr.Zero, checked ((uint) mode), dwFlagsAndAttributes, IntPtr.Zero);
}
if (file.IsInvalid)
FileSystem.RaiseError(path);
return file;
}
public static Stream OpenRead(string path)
{
return (Stream) new FileStream(FileSystem.CreateFile(path, FileAccess.Read, FileShare.Read, FileMode.Open), FileAccess.Read);
}
public static void SetFileTime(string path, DateTime creationtime, DateTime accesstime, DateTime writetime)
{
if (DateTime.Compare(creationtime, DateTime.MinValue) == 0 && DateTime.Compare(accesstime, DateTime.MinValue) == 0 && DateTime.Compare(writetime, DateTime.MinValue) == 0)
return;
while (path.EndsWith("\\"))
path = path.Substring(0, checked (path.Length - 1));
using (SafeFileHandle file = Win32Native.CreateFile(FileSystem.NormalizePath(path), 256U, 0U, IntPtr.Zero, 3U, 33554432U, IntPtr.Zero))
{
if (file.IsInvalid)
FileSystem.RaiseError(path);
System.Runtime.InteropServices.ComTypes.FILETIME lpCreationTime = new System.Runtime.InteropServices.ComTypes.FILETIME();
System.Runtime.InteropServices.ComTypes.FILETIME lpLastAccessTime = new System.Runtime.InteropServices.ComTypes.FILETIME();
System.Runtime.InteropServices.ComTypes.FILETIME lpLastWriteTime = new System.Runtime.InteropServices.ComTypes.FILETIME();
if (DateTime.Compare(creationtime, DateTime.MinValue) > 0)
lpCreationTime = FileSystem.ToFileTime(creationtime);
if (DateTime.Compare(accesstime, DateTime.MinValue) > 0)
lpLastAccessTime = FileSystem.ToFileTime(accesstime);
if (DateTime.Compare(writetime, DateTime.MinValue) > 0)
lpLastWriteTime = FileSystem.ToFileTime(writetime);
if (Win32Native.SetFileTime(file, ref lpCreationTime, ref lpLastAccessTime, ref lpLastWriteTime))
return;
FileSystem.RaiseError(path);
}
}
public static FileInfo[] ListDirectoryEntries(string directorypath)
{
if (string.IsNullOrEmpty(directorypath))
throw new ArgumentNullException();
directorypath = directorypath.EndsWith("\\") || directorypath.EndsWith("/") ? FileSystem.CombinePath(directorypath, "*") : FileSystem.CombinePath(directorypath, "\\*");
return FileSystem.FindFiles(directorypath);
}
public static FileInfo[] FindFiles(string path)
{
List<FileInfo> fileInfoList = new List<FileInfo>();
Win32Native.WIN32_FIND_DATA lpFindFileData = new Win32Native.WIN32_FIND_DATA();
int length = path.LastIndexOf("\\");
int num = path.LastIndexOf("/");
if (num > length)
length = num;
string path1 = length >= 0 ? path.Substring(0, length) : Environment.CurrentDirectory;
using (SafeFindHandle firstFile = Win32Native.FindFirstFile(FileSystem.NormalizePath(path), out lpFindFileData))
{
if (firstFile.IsInvalid)
FileSystem.RaiseError(path);
do
{
if (Operators.CompareString(lpFindFileData.cFileName, ".", false) != 0 && Operators.CompareString(lpFindFileData.cFileName, "..", false) != 0)
fileInfoList.Add(new FileInfo(FileSystem.CombinePath(path1, lpFindFileData.cFileName), lpFindFileData));
}
while (Win32Native.FindNextFile(firstFile, out lpFindFileData));
int lastWin32Error = Marshal.GetLastWin32Error();
if (lastWin32Error != 18)
FileSystem.RaiseError(path, lastWin32Error);
}
return fileInfoList.ToArray();
}
public static void CreateDirectory(string path)
{
if (Win32Native.CreateDirectory(FileSystem.NormalizePath(path), IntPtr.Zero))
return;
FileSystem.RaiseError(path);
}
public static void DeleteFile(string path, bool recursive)
{
string str1 = FileSystem.NormalizePath(path);
bool isdirectory;
if (!FileSystem.FileOrDirectoryExists(str1, ref isdirectory))
throw new FileNotFoundException();
if (!isdirectory)
{
if (Win32Native.DeleteFile(str1))
return;
FileSystem.RaiseError(str1);
}
else if (!recursive)
{
if (Win32Native.RemoveDirectory(str1))
return;
FileSystem.RaiseError(str1);
}
else
{
List<string> stringList = new List<string>();
stringList.Add(str1);
while (stringList.Count > 0)
{
string str2 = stringList[checked (stringList.Count - 1)];
FileInfo[] fileInfoArray = FileSystem.ListDirectoryEntries(str2);
if (fileInfoArray.Length == 0)
{
if (!Win32Native.RemoveDirectory(str2))
FileSystem.RaiseError(str2);
stringList.RemoveAt(checked (stringList.Count - 1));
}
else
{
int num1 = 0;
int num2 = checked (fileInfoArray.Length - 1);
int index = num1;
while (index <= num2)
{
if (fileInfoArray[index].IsDirectory)
stringList.Add(FileSystem.NormalizePath(FileSystem.CombinePath(str2, fileInfoArray[index].FileName)));
else if (!Win32Native.DeleteFile(FileSystem.NormalizePath(FileSystem.CombinePath(str2, fileInfoArray[index].FileName))))
FileSystem.RaiseError(str2);
checked { ++index; }
}
}
}
}
}
public static void MoveFile(string path1, string path2, bool overwrite)
{
if (!overwrite ? Win32Native.MoveFileEx(FileSystem.NormalizePath(path1), FileSystem.NormalizePath(path2), Win32Native.MoveFileFlagType.MOVEFILE_COPY_ALLOWED) : Win32Native.MoveFileEx(FileSystem.NormalizePath(path1), FileSystem.NormalizePath(path2), Win32Native.MoveFileFlagType.MOVEFILE_REPLACE_EXISTING | Win32Native.MoveFileFlagType.MOVEFILE_COPY_ALLOWED))
return;
int lastWin32Error = Marshal.GetLastWin32Error();
FileSystem.RaiseError(path1, lastWin32Error);
}
public static void CopyFile(string path1, string path2, bool overwrite)
{
string str1 = FileSystem.NormalizePath(path1);
string str2 = FileSystem.NormalizePath(path2);
bool isdirectory1;
if (!FileSystem.FileOrDirectoryExists(str1, ref isdirectory1))
throw new FileNotFoundException();
bool isdirectory2;
if (!overwrite && FileSystem.FileOrDirectoryExists(str2, ref isdirectory2))
FileSystem.RaiseError(str2, 183);
if (!isdirectory1)
{
if (Win32Native.CopyFile(str1, str2, !overwrite))
return;
FileSystem.RaiseError(str1);
}
else
{
List<string[]> strArrayList = new List<string[]>();
strArrayList.Add(new string[3]{ str1, str2, "d" });
int index1 = 0;
while (index1 < strArrayList.Count)
{
string[] strArray = strArrayList[index1];
if (Operators.CompareString(strArray[2], "d", false) == 0)
{
FileInfo[] fileInfoArray = FileSystem.ListDirectoryEntries(strArray[0]);
int num1 = 0;
int num2 = checked (fileInfoArray.Length - 1);
int index2 = num1;
while (index2 <= num2)
{
if (fileInfoArray[index2].IsDirectory)
strArrayList.Add(new string[3]
{
FileSystem.NormalizePath(FileSystem.CombinePath(strArray[0], fileInfoArray[index2].FileName)),
FileSystem.NormalizePath(FileSystem.CombinePath(strArray[1], fileInfoArray[index2].FileName)),
"d"
});
else
strArrayList.Add(new string[3]
{
FileSystem.NormalizePath(FileSystem.CombinePath(strArray[0], fileInfoArray[index2].FileName)),
FileSystem.NormalizePath(FileSystem.CombinePath(strArray[1], fileInfoArray[index2].FileName)),
"f"
});
checked { ++index2; }
}
}
checked { ++index1; }
}
int index3 = 0;
while (index3 < strArrayList.Count)
{
string[] strArray = strArrayList[index3];
if (Operators.CompareString(strArray[2], "d", false) == 0)
{
if (!Win32Native.CreateDirectory(strArray[1], IntPtr.Zero))
FileSystem.RaiseError(strArray[1]);
}
else if (!Win32Native.CopyFile(strArray[0], strArray[1], !overwrite))
FileSystem.RaiseError(strArray[0]);
checked { ++index3; }
}
}
}
}
}