CarotDav_decompile/Rei.Fs/AutoSplitFs.cs

1071 lines
36 KiB
C#
Raw Normal View History

// Decompiled with JetBrains decompiler
// Type: Rei.Fs.AutoSplitFs
// 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 System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
namespace Rei.Fs
{
public class AutoSplitFs : FsBase
{
private FsBase basefs;
private string EscapeString;
public long SplitSize;
public AutoSplitFs(FsBase @base, long size, string escape)
: base(@base.ConnectionSetting)
{
if (size <= 0L)
throw new ArgumentOutOfRangeException(nameof (size));
this.basefs = @base;
this.SplitSize = size;
this.EscapeString = escape;
this.basefs.LogMessage += new EventHandler<LogMessageEventArgs>(this.InnerOnLogMessage);
}
private void InnerOnLogMessage(object sender, LogMessageEventArgs e)
{
this.OnLogMessage(e.Message);
}
public override SecureType LastSecureType
{
get
{
return this.basefs.LastSecureType;
}
}
public override string ClientName
{
get
{
return this.basefs.ClientName;
}
set
{
this.basefs.ClientName = value;
}
}
public override string GroupName
{
get
{
return this.basefs.GroupName;
}
set
{
this.basefs.GroupName = value;
}
}
public override string UserName
{
get
{
return this.basefs.UserName;
}
}
public override string Password
{
get
{
return this.basefs.Password;
}
}
public override FsAbility Ability
{
get
{
return (this.basefs.Ability | FsAbility.PartialRead | FsAbility.PartialWrite) & ~FsAbility.Lock;
}
}
public override void ResetConnection()
{
this.basefs.ResetConnection();
base.ResetConnection();
}
public override ResourceInfo GetInfo(Uri targeturi)
{
ResourceInfo info = this.basefs.GetInfo(targeturi);
if (info.IsCollection)
{
info.Id = (ResourceId) new AutoSplitFs.SplitId(info.Id, this);
return info;
}
AutoSplitFs.SplitId splitId = new AutoSplitFs.SplitId(info.Id, this);
ResourceInfo[] parentInfoAndEntries = this.GetParentInfoAndEntries((ResourceId) splitId);
int num1 = 1;
int num2 = checked (parentInfoAndEntries.Length - 1);
int index = num1;
while (index <= num2)
{
if ((ResourceId) splitId == parentInfoAndEntries[index].Id)
return parentInfoAndEntries[index];
checked { ++index; }
}
throw new RemoteResourceNotFoundException(targeturi);
}
public override ResourceInfo GetInfo(ResourceId id)
{
AutoSplitFs.SplitId splitId = (AutoSplitFs.SplitId) id;
if (splitId.IsCollection)
{
ResourceInfo info = this.basefs.GetInfo(splitId.BaseId);
if (info.IsCollection)
{
info.Id = (ResourceId) new AutoSplitFs.SplitId(info.Id, this);
return info;
}
}
ResourceInfo[] parentInfoAndEntries = this.GetParentInfoAndEntries((ResourceId) splitId);
int num1 = 1;
int num2 = checked (parentInfoAndEntries.Length - 1);
int index = num1;
while (index <= num2)
{
if ((ResourceId) splitId == parentInfoAndEntries[index].Id)
return parentInfoAndEntries[index];
checked { ++index; }
}
throw new RemoteResourceNotFoundException(splitId.Uri);
}
public override ResourceInfo GetInfo(ResourceId parentid, string name)
{
AutoSplitFs.SplitId splitId = (AutoSplitFs.SplitId) parentid;
ResourceInfo[] infoAndEntries = this.GetInfoAndEntries((ResourceId) splitId);
Uri uri = UriUtil.CombineName(splitId.Uri, name);
Uri u1 = UriUtil.AddLastSlash(uri);
int num1 = 1;
int num2 = checked (infoAndEntries.Length - 1);
int index = num1;
while (index <= num2)
{
if (UriUtil.UriEquals(uri, infoAndEntries[index].Id.Uri, this.basefs.IsCaseSensitiveFs) || UriUtil.UriEquals(u1, infoAndEntries[index].Id.Uri, this.basefs.IsCaseSensitiveFs))
return infoAndEntries[index];
checked { ++index; }
}
throw new RemoteResourceNotFoundException(uri);
}
public override ResourceInfo[] GetEntries(ResourceId id)
{
return this.InnerCombineList(this.basefs.GetEntries(((AutoSplitFs.SplitId) id).BaseId));
}
public override ResourceInfo[] GetInfoAndEntries(Uri targeturi)
{
ResourceInfo[] infoAndEntries = this.basefs.GetInfoAndEntries(targeturi);
if (infoAndEntries[0].IsCollection)
return this.InnerCombineList(infoAndEntries);
ResourceId id = infoAndEntries[0].Id;
ResourceInfo[] resourceInfoArray = this.InnerCombineList(this.basefs.GetParentInfoAndEntries(id));
int num1 = 1;
int num2 = checked (resourceInfoArray.Length - 1);
int index = num1;
while (index <= num2)
{
if (id == resourceInfoArray[index].Id)
return new ResourceInfo[1]
{
resourceInfoArray[index]
};
checked { ++index; }
}
throw new RemoteResourceNotFoundException(targeturi);
}
public override ResourceInfo[] GetInfoAndEntries(ResourceId id)
{
AutoSplitFs.SplitId splitId = (AutoSplitFs.SplitId) id;
if (splitId.IsCollection)
return this.InnerCombineList(this.basefs.GetInfoAndEntries(splitId.BaseId));
return new ResourceInfo[1]
{
this.GetInfo((ResourceId) splitId)
};
}
public override ResourceInfo[] GetParentInfoAndEntries(ResourceId childid)
{
return this.InnerCombineList(this.basefs.GetParentInfoAndEntries(((AutoSplitFs.SplitId) childid).BaseId));
}
private ResourceInfo[] InnerCombineList(ResourceInfo[] ris)
{
List<ResourceInfo> resourceInfoList = new List<ResourceInfo>();
List<int> intList1 = new List<int>();
List<int> intList2 = new List<int>();
int num1 = 0;
int num2 = checked (ris.Length - 1);
int index1 = num1;
while (index1 <= num2)
{
if (ris[index1].IsCollection)
{
ris[index1].Id = (ResourceId) new AutoSplitFs.SplitId(ris[index1].Id, this);
resourceInfoList.Add(ris[index1]);
}
else
{
string lastName1 = UriUtil.GetLastName(ris[index1].Id.Uri);
int num3 = lastName1.LastIndexOf(this.EscapeString);
int result;
if (num3 >= 0 && checked (num3 + this.EscapeString.Length) < lastName1.Length && (int.TryParse(lastName1.Substring(checked (num3 + this.EscapeString.Length)), out result) && result >= 1))
{
intList1.Add(index1);
}
else
{
ResourceInfo ri = ris[index1];
string lastName2 = UriUtil.GetLastName(ri.Id.Uri);
ri.Id = (ResourceId) new AutoSplitFs.SplitId(ri.Id, this);
result = 1;
label_6:
string Right = lastName2 + this.EscapeString + result.ToString();
int num4 = 0;
int num5 = checked (ris.Length - 1);
int index2 = num4;
while (index2 <= num5)
{
if (!ris[index2].IsCollection && Operators.CompareString(UriUtil.GetLastName(ris[index2].Id.Uri), Right, false) == 0)
{
if (ris[index2].HasError)
ri.HasError = true;
if (ri.Size >= 0L)
{
if (ris[index2].Size < 0L)
{
ri.Size = -1L;
}
else
{
ResourceInfo resourceInfo = ri;
resourceInfo.Size = checked (resourceInfo.Size + ris[index2].Size);
}
}
intList2.Add(index2);
checked { ++result; }
goto label_6;
}
else
checked { ++index2; }
}
if (result > 1)
ri.IsSplitted = true;
resourceInfoList.Add(ri);
}
}
checked { ++index1; }
}
int num6 = 0;
int num7 = checked (intList2.Count - 1);
int index3 = num6;
while (index3 <= num7)
{
int index2 = intList1.IndexOf(intList2[index3]);
if (index2 >= 0)
intList1.RemoveAt(index2);
checked { ++index3; }
}
int num8 = 0;
int num9 = checked (intList1.Count - 1);
int index4 = num8;
while (index4 <= num9)
{
ResourceInfo ri = ris[intList1[index4]];
ri.Id = (ResourceId) new AutoSplitFs.SplitId(ri.Id, this);
ri.IsMinor = true;
resourceInfoList.Add(ri);
checked { ++index4; }
}
ris = resourceInfoList.ToArray();
return ris;
}
public override ReturnedInfo CreateCollection(ResourceId parentid, string name)
{
ReturnedInfo collection = this.basefs.CreateCollection(((AutoSplitFs.SplitId) parentid).BaseId, name);
ResourceInfo resourceInfo1 = collection as ResourceInfo;
if (resourceInfo1 == null)
return (ReturnedInfo) new AutoSplitFs.SplitId(collection as ResourceId, this);
ResourceId resourceId = (ResourceId) new AutoSplitFs.SplitId(resourceInfo1.Id, this);
ResourceInfo resourceInfo2 = (ResourceInfo) resourceInfo1.Clone();
resourceInfo1.Id = resourceId;
return (ReturnedInfo) resourceInfo1;
}
public override void Delete(ResourceId id)
{
AutoSplitFs.SplitId splitId = (AutoSplitFs.SplitId) id;
ResourceInfo resourceInfo = (ResourceInfo) null;
ResourceInfo[] parentInfoAndEntries = this.basefs.GetParentInfoAndEntries(splitId.BaseId);
int num1 = 1;
int num2 = checked (parentInfoAndEntries.Length - 1);
int index1 = num1;
while (index1 <= num2)
{
if (parentInfoAndEntries[index1].Id == splitId.BaseId)
{
resourceInfo = parentInfoAndEntries[index1];
break;
}
checked { ++index1; }
}
if (resourceInfo == null)
throw new RemoteResourceNotFoundException(splitId.Uri);
this.basefs.Delete(resourceInfo.Id);
if (resourceInfo.IsCollection)
return;
try
{
int num3 = 1;
string name = splitId.Name;
label_9:
Uri u2 = UriUtil.CombineName(splitId.Parent, name + this.EscapeString + num3.ToString());
int num4 = 1;
int num5 = checked (parentInfoAndEntries.Length - 1);
int index2 = num4;
while (index2 <= num5)
{
if (!parentInfoAndEntries[index2].IsCollection && UriUtil.UriEquals(parentInfoAndEntries[index2].Id.Uri, u2, this.basefs.IsCaseSensitiveFs))
{
this.basefs.Delete(parentInfoAndEntries[index2].Id);
checked { ++num3; }
goto label_9;
}
else
checked { ++index2; }
}
}
catch (Exception ex)
{
ProjectData.SetProjectError(ex);
Exception innerexception = ex;
throw new InconsistencyDetectedException(splitId.Uri, "Exception occurred in undivisable operation.", innerexception);
}
}
public override ReturnedInfo Move(ResourceId id, ResourceId newparentid, string newname, bool overwrite)
{
AutoSplitFs.SplitId splitId1 = (AutoSplitFs.SplitId) id;
AutoSplitFs.SplitId splitId2 = (AutoSplitFs.SplitId) newparentid;
ResourceId newparentid1 = (object) newparentid != null ? splitId2.BaseId : (ResourceId) null;
ResourceInfo resourceInfo = (ResourceInfo) null;
ResourceInfo[] parentInfoAndEntries = this.basefs.GetParentInfoAndEntries(splitId1.BaseId);
int num1 = 1;
int num2 = checked (parentInfoAndEntries.Length - 1);
int index1 = num1;
while (index1 <= num2)
{
if (parentInfoAndEntries[index1].Id == splitId1.BaseId)
{
resourceInfo = parentInfoAndEntries[index1];
break;
}
checked { ++index1; }
}
if (resourceInfo == null)
throw new RemoteResourceNotFoundException(splitId1.Uri);
ReturnedInfo returnedInfo = this.basefs.Move(resourceInfo.Id, newparentid1, newname, overwrite);
AutoSplitFs.SplitId splitId3 = !(returnedInfo is ResourceInfo) ? new AutoSplitFs.SplitId((ResourceId) returnedInfo, this) : new AutoSplitFs.SplitId(((ResourceInfo) returnedInfo).Id, this);
if (!resourceInfo.IsCollection)
{
try
{
int num3 = 1;
string name = splitId1.Name;
label_9:
Uri u2 = UriUtil.CombineName(splitId1.Parent, name + this.EscapeString + num3.ToString());
int num4 = 1;
int num5 = checked (parentInfoAndEntries.Length - 1);
int index2 = num4;
while (index2 <= num5)
{
if (!parentInfoAndEntries[index2].IsCollection && UriUtil.UriEquals(parentInfoAndEntries[index2].Id.Uri, u2, this.basefs.IsCaseSensitiveFs))
{
string newname1 = newname != null ? newname + this.EscapeString + num3.ToString() : (string) null;
this.basefs.Move(parentInfoAndEntries[index2].Id, newparentid1, newname1, overwrite);
checked { ++num3; }
goto label_9;
}
else
checked { ++index2; }
}
}
catch (Exception ex)
{
ProjectData.SetProjectError(ex);
Exception innerexception = ex;
throw new InconsistencyDetectedException(splitId1.Uri, "Exception occurred in undivisable operation.", innerexception);
}
}
return (ReturnedInfo) splitId3;
}
public override ReturnedInfo Copy(ResourceId id, ResourceId newparentid, string newname, bool overwrite)
{
AutoSplitFs.SplitId splitId1 = (AutoSplitFs.SplitId) id;
AutoSplitFs.SplitId splitId2 = (AutoSplitFs.SplitId) newparentid;
ResourceId newparentid1 = (object) newparentid != null ? splitId2.BaseId : (ResourceId) null;
ResourceInfo resourceInfo = (ResourceInfo) null;
ResourceInfo[] parentInfoAndEntries = this.basefs.GetParentInfoAndEntries(splitId1.BaseId);
int num1 = 1;
int num2 = checked (parentInfoAndEntries.Length - 1);
int index1 = num1;
while (index1 <= num2)
{
if (parentInfoAndEntries[index1].Id == splitId1.BaseId)
{
resourceInfo = parentInfoAndEntries[index1];
break;
}
checked { ++index1; }
}
if (resourceInfo == null)
throw new RemoteResourceNotFoundException(splitId1.Uri);
ReturnedInfo returnedInfo = this.basefs.Copy(resourceInfo.Id, newparentid1, newname, overwrite);
AutoSplitFs.SplitId splitId3 = !(returnedInfo is ResourceInfo) ? new AutoSplitFs.SplitId((ResourceId) returnedInfo, this) : new AutoSplitFs.SplitId(((ResourceInfo) returnedInfo).Id, this);
if (!resourceInfo.IsCollection)
{
try
{
int num3 = 1;
string name = splitId1.Name;
label_9:
Uri u2 = UriUtil.CombineName(splitId1.Parent, name + this.EscapeString + num3.ToString());
int num4 = 1;
int num5 = checked (parentInfoAndEntries.Length - 1);
int index2 = num4;
while (index2 <= num5)
{
if (!parentInfoAndEntries[index2].IsCollection && UriUtil.UriEquals(parentInfoAndEntries[index2].Id.Uri, u2, this.basefs.IsCaseSensitiveFs))
{
string newname1 = newname != null ? newname + this.EscapeString + num3.ToString() : (string) null;
this.basefs.Copy(parentInfoAndEntries[index2].Id, newparentid1, newname1, overwrite);
checked { ++num3; }
goto label_9;
}
else
checked { ++index2; }
}
}
catch (Exception ex)
{
ProjectData.SetProjectError(ex);
Exception innerexception = ex;
throw new InconsistencyDetectedException(splitId1.Uri, "Exception occurred in undivisable operation.", innerexception);
}
}
return (ReturnedInfo) splitId3;
}
public override ReturnedInfo SetTimes(ResourceId id, DateTime lastmodifiedtime, DateTime creationtime, DateTime lastaccesstime)
{
return this.basefs.SetTimes(((AutoSplitFs.SplitId) id).BaseId, lastmodifiedtime, creationtime, lastaccesstime);
}
public override ReturnedInfo SetResourceInfo(ResourceInfo info, ResourceInfo original)
{
ResourceInfo resourceInfo = (ResourceInfo) original.Clone();
resourceInfo.Id = ((AutoSplitFs.SplitId) resourceInfo.Id).BaseId;
return this.basefs.SetResourceInfo(info, original);
}
public override Stream OpenRead(ResourceId id, ref long start, ref long length)
{
AutoSplitFs.SplitId id1 = (AutoSplitFs.SplitId) id;
if (start < 0L)
throw new ArgumentOutOfRangeException(nameof (start));
if (length == 0L)
throw new ArgumentOutOfRangeException(nameof (length));
return (Stream) new AutoSplitFs.CombinedReadStream(this, id1, ref start, ref length);
}
public override Stream OpenWrite(ResourceId parentid, string name, long length)
{
AutoSplitFs.SplitId parentid1 = (AutoSplitFs.SplitId) parentid;
int num = name.IndexOf(this.EscapeString);
int result;
if (num >= 0 && checked (num + this.EscapeString.Length) < name.Length && (int.TryParse(name.Substring(checked (num + this.EscapeString.Length)), out result) && result >= 1))
throw new ArgumentException("Invalid name");
return (Stream) new AutoSplitFs.CombinedWriteStream(this, parentid1, name, length, this.SplitSize);
}
public override Stream OpenWriteOver(ResourceId id, long length)
{
AutoSplitFs.SplitId splitId = (AutoSplitFs.SplitId) id;
int num = splitId.Name.IndexOf(this.EscapeString);
int result;
if (num >= 0 && checked (num + this.EscapeString.Length) < splitId.Name.Length && (int.TryParse(splitId.Name.Substring(checked (num + this.EscapeString.Length)), out result) && result >= 1))
throw new ArgumentException("Invalid name");
AutoSplitFs.SplitId id1 = splitId;
long start = 0;
long splitSize = this.SplitSize;
return (Stream) new AutoSplitFs.CombinedWriteStream(this, id1, ref start, ref length, splitSize);
}
public override Stream OpenWriteResume(ResourceId id, ref long start, ref long length)
{
AutoSplitFs.SplitId id1 = (AutoSplitFs.SplitId) id;
if (start < 0L)
throw new ArgumentOutOfRangeException(nameof (start));
return (Stream) new AutoSplitFs.CombinedWriteStream(this, id1, ref start, ref length, this.SplitSize);
}
public override ReturnedInfo CloseWrite(Stream st)
{
st.Close();
return (ReturnedInfo) ((AutoSplitFs.CombinedWriteStream) st).Id;
}
public override string GetLock(ResourceId id, string owner, int timeoutseconds, FsBase.LockScope scope)
{
throw new NotImplementedException();
}
public override void ReleaseLock(ResourceId id, string locktoken)
{
throw new NotImplementedException();
}
private class CombinedReadStream : Stream
{
private AutoSplitFs pFs;
private AutoSplitFs.SplitId pId;
private int pSplitIndex;
private long pLength;
private long pPosition;
private Stream pCurrentStream;
public CombinedReadStream(AutoSplitFs fs, AutoSplitFs.SplitId id, ref long start, ref long length)
{
this.pFs = fs;
this.pSplitIndex = 0;
long num1 = 0;
this.pId = id;
ResourceId id1 = this.pId.BaseId;
if (start > 0L)
{
ResourceInfo resourceInfo = (ResourceInfo) null;
ResourceInfo[] parentInfoAndEntries = this.pFs.basefs.GetParentInfoAndEntries(this.pId.BaseId);
int num2 = 1;
int num3 = checked (parentInfoAndEntries.Length - 1);
int index1 = num2;
while (index1 <= num3)
{
if (parentInfoAndEntries[index1].Id == this.pId.BaseId)
{
resourceInfo = parentInfoAndEntries[index1];
break;
}
checked { ++index1; }
}
if (resourceInfo == null)
throw new RemoteResourceNotFoundException(this.pId.Uri);
if (resourceInfo.Size >= 0L && start >= resourceInfo.Size)
{
this.pSplitIndex = checked (this.pSplitIndex + 1);
checked { num1 += resourceInfo.Size; }
string name = this.pId.Name;
label_10:
Uri u2 = UriUtil.CombineName(this.pId.Parent, name + this.pFs.EscapeString + this.pSplitIndex.ToString());
int num4 = 1;
int num5 = checked (parentInfoAndEntries.Length - 1);
int index2 = num4;
while (index2 <= num5)
{
if (!parentInfoAndEntries[index2].IsCollection && UriUtil.UriEquals(parentInfoAndEntries[index2].Id.Uri, u2, this.pFs.basefs.IsCaseSensitiveFs))
{
if (parentInfoAndEntries[index2].Size >= 0L && start >= checked (num1 + parentInfoAndEntries[index2].Size))
{
this.pSplitIndex = checked (this.pSplitIndex + 1);
checked { num1 += parentInfoAndEntries[index2].Size; }
goto label_10;
}
else
{
id1 = parentInfoAndEntries[index2].Id;
goto label_18;
}
}
else
checked { ++index2; }
}
throw new InvalidRangeException();
}
}
label_18:
long start1 = 0;
if ((this.pFs.basefs.Ability & FsAbility.PartialRead) != FsAbility.None)
start1 = checked (start - num1);
long length1 = -1;
this.pCurrentStream = this.pFs.basefs.OpenRead(id1, ref start1, ref length1);
if (length >= 0L)
length = checked (length + start - num1 - start1);
start = checked (start1 + num1);
if (length >= 0L)
{
if (length1 > length)
length = length1;
if (length1 < 0L)
length = -1L;
if (length1 >= 0L && length > length1)
length = -1L;
}
this.pPosition = 0L;
this.pLength = length;
}
public override bool CanRead
{
get
{
return true;
}
}
public override bool CanSeek
{
get
{
return false;
}
}
public override bool CanWrite
{
get
{
return false;
}
}
public override long Length
{
get
{
return this.pLength;
}
}
public override long Position
{
get
{
return this.pPosition;
}
set
{
throw new NotSupportedException();
}
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public override void Flush()
{
if (this.pCurrentStream == null)
return;
this.pCurrentStream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
if (this.pCurrentStream == null)
throw new ObjectDisposedException(this.GetType().FullName);
if (this.pLength >= 0L && checked (this.pPosition + (long) count) > this.pLength)
count = checked ((int) (this.pLength - this.pPosition));
if (count < 0)
throw new ArgumentOutOfRangeException(nameof (count));
if (count == 0)
return 0;
int num1;
int num2;
while (true)
{
num1 = this.pCurrentStream.Read(buffer, offset, count);
if (num1 <= 0)
{
this.pCurrentStream.Close();
this.pSplitIndex = checked (this.pSplitIndex + 1);
Uri targeturi = UriUtil.CombineName(this.pId.Parent, this.pId.Name + this.pFs.EscapeString + this.pSplitIndex.ToString());
try
{
this.pCurrentStream = this.pFs.basefs.OpenRead(targeturi);
}
catch (RemoteResourceNotFoundException ex)
{
ProjectData.SetProjectError((Exception) ex);
RemoteResourceNotFoundException notFoundException = ex;
if (this.pLength >= 0L)
throw new InvalidRangeException((Exception) notFoundException);
num2 = 0;
ProjectData.ClearProjectError();
goto label_16;
}
}
else
break;
}
this.pPosition = checked (this.pPosition + (long) num1);
return num1;
label_16:
return num2;
}
protected override void Dispose(bool disposing)
{
try
{
if (!disposing || this.pCurrentStream == null)
return;
this.pCurrentStream.Close();
this.pCurrentStream = (Stream) null;
}
finally
{
base.Dispose(disposing);
}
}
}
private class CombinedWriteStream : Stream
{
private AutoSplitFs pFs;
public AutoSplitFs.SplitId pId;
private string pName;
private ResourceId pParentId;
private int pSplitIndex;
private long pSplitSize;
private long pLength;
private long pPosition;
private long pCurrentLength;
private long pCurrentPosition;
private Stream pCurrentStream;
public CombinedWriteStream(AutoSplitFs fs, AutoSplitFs.SplitId parentid, string name, long length, long splitsize)
{
this.pFs = fs;
this.pParentId = parentid.BaseId;
this.pName = name;
this.pSplitSize = splitsize;
this.pSplitIndex = 0;
this.pId = (AutoSplitFs.SplitId) null;
this.pPosition = 0L;
this.pLength = length;
this.DeleteFollowingFiles(this.pFs.basefs.GetEntries(this.pParentId), 0);
this.pCurrentLength = this.pLength >= 0L ? (checked (this.pLength - this.pPosition) > this.pSplitSize ? this.pSplitSize : checked (this.pLength - this.pPosition)) : -1L;
this.pCurrentStream = this.pFs.basefs.OpenWrite(this.pParentId, this.pName, this.pCurrentLength);
this.pCurrentPosition = 0L;
}
public CombinedWriteStream(AutoSplitFs fs, AutoSplitFs.SplitId id, ref long start, ref long length, long splitsize)
{
this.pFs = fs;
this.pName = UriUtil.GetLastName(id.BaseId.Uri);
this.pSplitSize = splitsize;
ResourceInfo[] parentInfoAndEntries = this.pFs.basefs.GetParentInfoAndEntries(id.BaseId);
this.pParentId = parentInfoAndEntries[0].Id;
ResourceInfo resourceInfo = (ResourceInfo) null;
int num1 = 1;
int num2 = checked (parentInfoAndEntries.Length - 1);
int index1 = num1;
while (index1 <= num2)
{
if (parentInfoAndEntries[index1].Id == id.BaseId)
{
resourceInfo = parentInfoAndEntries[index1];
break;
}
checked { ++index1; }
}
if (resourceInfo == null)
throw new RemoteResourceNotFoundException(id.Uri);
long num3 = 0;
this.pSplitIndex = 0;
if (resourceInfo.Size >= 0L && start >= checked (resourceInfo.Size + num3))
{
if (resourceInfo.Size != 0L)
{
checked { num3 += resourceInfo.Size; }
this.pId = new AutoSplitFs.SplitId(resourceInfo.Id, this.pFs);
this.pSplitIndex = 1;
}
int num4 = 1;
label_11:
Uri u2 = UriUtil.CombineName(this.pParentId.Uri, this.pName + this.pFs.EscapeString + num4.ToString());
int num5 = 1;
int num6 = checked (parentInfoAndEntries.Length - 1);
int index2 = num5;
while (index2 <= num6)
{
if (!parentInfoAndEntries[index2].IsCollection && UriUtil.UriEquals(parentInfoAndEntries[index2].Id.Uri, u2, fs.IsCaseSensitiveFs))
{
if (parentInfoAndEntries[index2].Size != 0L)
{
if (this.pId == null)
this.pId = new AutoSplitFs.SplitId(resourceInfo.Id, this.pFs);
if (parentInfoAndEntries[index2].Size < 0L || start < checked (parentInfoAndEntries[index2].Size + num3))
{
this.pSplitIndex = num4;
break;
}
checked { num3 += parentInfoAndEntries[index2].Size; }
this.pSplitIndex = checked (num4 + 1);
}
checked { ++num4; }
goto label_11;
}
else
checked { ++index2; }
}
}
if (length >= 0L)
length = checked (length + start - num3);
start = num3;
this.pPosition = 0L;
this.pLength = length;
this.DeleteFollowingFiles(parentInfoAndEntries, 1);
this.pCurrentLength = this.pLength >= 0L ? (checked (this.pLength - this.pPosition) > this.pSplitSize ? this.pSplitSize : checked (length - this.pPosition)) : -1L;
this.pCurrentStream = this.pSplitIndex != 0 ? this.pFs.basefs.OpenWrite(this.pParentId, this.pName + this.pFs.EscapeString + this.pSplitIndex.ToString(), this.pCurrentLength) : this.pFs.basefs.OpenWrite(this.pParentId, this.pName, this.pCurrentLength);
this.pCurrentPosition = 0L;
}
public override bool CanRead
{
get
{
return false;
}
}
public override bool CanSeek
{
get
{
return false;
}
}
public override bool CanWrite
{
get
{
return true;
}
}
public override long Length
{
get
{
return this.pLength;
}
}
public override long Position
{
get
{
return this.pPosition;
}
set
{
throw new NotSupportedException();
}
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public override void Flush()
{
if (this.pCurrentStream == null)
return;
this.pCurrentStream.Flush();
}
public override void Write(byte[] buffer, int offset, int count)
{
if (this.pCurrentStream == null)
throw new ObjectDisposedException(this.GetType().FullName);
if (count < 0)
throw new ArgumentOutOfRangeException(nameof (count));
if (offset < 0)
throw new ArgumentOutOfRangeException(nameof (offset));
if (this.pLength >= 0L && checked (this.pPosition + (long) count) > this.pLength)
throw new ArgumentException("stream overflow", nameof (count));
while (count != 0)
{
if (this.pCurrentPosition == this.pCurrentLength || this.pCurrentLength < 0L && this.pCurrentPosition == this.pSplitSize)
{
Stream pCurrentStream = this.pCurrentStream;
this.pCurrentStream = (Stream) null;
pCurrentStream.Close();
ReturnedInfo returnedInfo = this.pFs.basefs.CloseWrite(pCurrentStream);
ResourceId baseid = !(returnedInfo is ResourceInfo) ? (ResourceId) returnedInfo : ((ResourceInfo) returnedInfo).Id;
if (this.pId == null)
this.pId = new AutoSplitFs.SplitId(baseid, this.pFs);
this.pSplitIndex = checked (this.pSplitIndex + 1);
this.pCurrentLength = this.pLength >= 0L ? (checked (this.pLength - this.pPosition) > this.pSplitSize ? this.pSplitSize : checked (this.pLength - this.pPosition)) : -1L;
this.pCurrentStream = this.pFs.basefs.OpenWrite(this.pParentId, this.pName + this.pFs.EscapeString + this.pSplitIndex.ToString(), this.pCurrentLength);
this.pCurrentPosition = 0L;
}
int count1 = count;
if (this.pCurrentLength < 0L)
{
if ((long) count1 > checked (this.pSplitSize - this.pCurrentPosition))
count1 = checked ((int) (this.pSplitSize - this.pCurrentPosition));
}
else if ((long) count1 > checked (this.pCurrentLength - this.pCurrentPosition))
count1 = checked ((int) (this.pCurrentLength - this.pCurrentPosition));
this.pCurrentStream.Write(buffer, offset, count1);
checked { count -= count1; }
checked { offset += count1; }
this.pCurrentPosition = checked (this.pCurrentPosition + (long) count1);
this.pPosition = checked (this.pPosition + (long) count1);
}
}
protected override void Dispose(bool disposing)
{
try
{
if (!disposing || this.pCurrentStream == null)
return;
Stream pCurrentStream = this.pCurrentStream;
this.pCurrentStream = (Stream) null;
pCurrentStream.Close();
ReturnedInfo returnedInfo = this.pFs.basefs.CloseWrite(pCurrentStream);
ResourceId baseid = !(returnedInfo is ResourceInfo) ? (ResourceId) returnedInfo : ((ResourceInfo) returnedInfo).Id;
if (this.pId != null)
return;
this.pId = new AutoSplitFs.SplitId(baseid, this.pFs);
}
finally
{
base.Dispose(disposing);
}
}
public ResourceId Id
{
get
{
return (ResourceId) this.pId;
}
}
private void DeleteFollowingFiles(ResourceInfo[] ris, int startindex)
{
string str = this.pName + this.pFs.EscapeString;
int num1 = startindex;
int num2 = checked (ris.Length - 1);
int index = num1;
while (index <= num2)
{
if (!ris[index].IsCollection)
{
string lastName = UriUtil.GetLastName(ris[index].Id.Uri);
int result;
if (lastName.StartsWith(str) && int.TryParse(lastName.Substring(str.Length), NumberStyles.None, (IFormatProvider) null, out result))
{
if (result > this.pSplitIndex)
{
try
{
this.pFs.basefs.Delete(ris[index].Id);
}
catch (RemoteResourceNotFoundException ex)
{
ProjectData.SetProjectError((Exception) ex);
ProjectData.ClearProjectError();
}
}
}
}
checked { ++index; }
}
}
}
[Serializable]
public class SplitId : ResourceId
{
private ResourceId pBaseId;
private List<ResourceId> pInnerIdList;
public SplitId(ResourceId baseid, AutoSplitFs fs)
: base(baseid.Uri, (FsBase) fs)
{
this.pInnerIdList = new List<ResourceId>();
this.pBaseId = baseid;
this.pInnerIdList.Add(baseid);
}
public ResourceId BaseId
{
get
{
return this.pBaseId;
}
}
public string Name
{
get
{
return UriUtil.GetLastName(this.pBaseId.Uri);
}
}
public Uri Parent
{
get
{
return UriUtil.GetParent(this.pBaseId.Uri);
}
}
}
}
}