//Створення папки вручну protected DirElement(DirElement parent, SafeStreamAccess dataFileStream, string Name, Object addElementLocker, Object changeElementsLocker, Bitmap Icon = null, SafeStreamAccess.ProgressCallback Progress = null) : this(addElementLocker, changeElementsLocker) { lock (_addElementLocker) { lock (dataFileStream.writeLock) { header = new Header(parent.header.headersFileStream, parent.header.AES, ElementType.Dir); this.dataFileStream = dataFileStream; byte[] icon = GetIconBytes(Icon); UInt32 iconSize = icon == null ? 0 : (UInt32)icon.Length; UInt64 iconStartPos = dataFileStream.GetFreeSpaceStartPos(Crypto.GetMod16(iconSize)); //Вибираємо місце куди писати іконку AesCryptoServiceProvider AES = GetFileAES(_IconIV); if ((icon != null) && (iconSize > 0)) { dataFileStream.WriteEncrypt((long)iconStartPos, icon, AES); } _Name = Name; _ID = GenID(); _ParentID = parent.ID; _IconStartPos = iconStartPos; _IconSize = iconSize; _PHash = GetPHash(Icon); Parent = parent; SaveInf(); } } }
//Шукає в сортованому по ID списку private DirElement FindParentByID(List <DirElement> dirs, UInt64 ParentID) { var dir = new DirElement(ParentID); int index = dirs.BinarySearch(dir, new IDComparer()); return(index >= 0 ? dirs[index] : null); }
public DirElement CreateDir(string Name, Bitmap Icon) { int index; lock (_changeElementsLocker) { if (FindByName(_Elements, Name, out index) != null) { throw new DuplicatesFileNameException("Не можна створити папку. Файл з таким ім’ям вже є."); } } DirElement dir; try { dir = new DirElement(this, dataFileStream, Name, _addElementLocker, _changeElementsLocker, Icon); } catch { throw new DataWasNotWrittenException("Іконка папки не записалась."); } return(dir); }
private void ChangeParent(DirElement NewParent, bool withWrite = false) { if (NewParent == null) { return; } lock (_changeElementsLocker) { if (NewParent.FileExists(_Name)) { return; } if (_Parent != null) { (_Parent as DirElement).RemoveElementFromElementsList(this); } bool writeToFile = _ParentID != NewParent.ID || withWrite; _Parent = NewParent; _ParentID = NewParent.ID; (_Parent as DirElement).InsertElementToElementsList(this); if (writeToFile) { SaveInf(); } } }
//Створення файлу вручну public FileElement(DirElement parent, Header parentHeader, SafeStreamAccess dataFileStream, string Name, Stream fileStream, bool isCompressed, Object addElementLocker, Object changeElementsLocker, Bitmap Icon = null, SafeStreamAccess.ProgressCallback Progress = null) : base(addElementLocker, changeElementsLocker) { lock (_addElementLocker) { UInt64 fileSize = (UInt64)fileStream.Length; byte[] icon = GetIconBytes(Icon); UInt32 iconSize = icon == null ? 0 : (UInt32)icon.Length; UInt64 fileStartPos = dataFileStream.GetFreeSpaceStartPos(Crypto.GetMod16(fileSize)); //Вибираємо місце куди писати файл UInt64 iconStartPos = dataFileStream.GetFreeSpaceStartPos(Crypto.GetMod16(iconSize)); //Вибираємо місце куди писати іконку iconStartPos = (iconStartPos == fileStartPos) ? iconStartPos += Crypto.GetMod16(fileSize) : iconStartPos; lock (dataFileStream.writeLock) { header = new Header(parentHeader.headersFileStream, parentHeader.AES, ElementType.File); this.dataFileStream = dataFileStream; _Name = Name; _ParentID = parent.ID; _FileStartPos = fileStartPos; _FileSize = fileSize; _IconStartPos = iconStartPos; _IconSize = iconSize; _IsCompressed = isCompressed; _Hash = new byte[16]; CryptoRandom.GetBytes(_Hash); _PHash = GetPHash(Icon); SaveInf(); _Exists = false; } AesCryptoServiceProvider AES = GetFileAES(_FileIV); if (fileSize > 0) { dataFileStream.WriteEncrypt((long)fileStartPos, fileStream, AES, out _Hash, Progress); } if ((icon != null) && (iconSize > 0)) { AES.IV = _IconIV; dataFileStream.WriteEncrypt((long)iconStartPos, icon, AES); } //Закидаємо файл в потрібну папку і записуємо зміни _Exists = true; ChangeParent(parent, true); } }
private void ChangeParent(DirElement NewParent) { if ((NewParent == null) || (NewParent == this)) { return; } lock (_changeElementsLocker) { //bool writeToFile = ((_Parent != NewParent) && (NewParent.ID != _ParentID)); if (FindByName(NewParent._Elements, _Name) != null) { return; } if (FindSubDirByID(NewParent.ID) != null) { throw new RecursiveFolderAttachmentException("Невірна вкладеність папок"); } int index; if (_Parent != null) { if (FindByName((_Parent as DirElement)._Elements, _Name, out index) != null) { (_Parent as DirElement)._Elements.RemoveAt(index); } } bool writeToFile = _ParentID != NewParent.ID; _Parent = NewParent; _ParentID = NewParent.ID; if (FindByName((_Parent as DirElement)._Elements, _Name, out index) == null) { (_Parent as DirElement)._Elements.Insert(index, this); } else { throw new DuplicatesFileNameException("Елемент з такою назвою в цьому списку вже є!"); } if (writeToFile) { SaveInf(); } } }
private void FillParents(List <DirElement> dirsList, List <Element> elementList) { dirsList.Sort(new IDComparer()); foreach (var element in elementList) { DirElement parent = FindParentByID(dirsList, element.ParentID); try { element.Parent = parent != null ? parent : this; } catch { } } }