private void EndDownloadFile(object[] args) { StorageFile file = (StorageFile)args[0]; OpFile commonFile = null; if (FileMap.SafeTryGetValue(file.HashID, out commonFile)) { commonFile.Downloaded = true; } // interface list box would be watching if file is transferring, will catch completed update }
string Transfers_DataFileRequest(ulong key, FileDetails details) { ulong hashID = BitConverter.ToUInt64(details.Hash, 0); OpFile file = null; if (FileMap.SafeTryGetValue(hashID, out file)) { if (details.Size == file.Size && Utilities.MemCompare(details.Hash, file.Hash)) { return(GetFilePath(hashID)); } } return(null); }
bool Transfers_DataFileSearch(ulong key, FileDetails details) { ulong hashID = BitConverter.ToUInt64(details.Hash, 0); OpFile file = null; if (FileMap.SafeTryGetValue(hashID, out file)) { if (details.Size == file.Size && Utilities.MemCompare(details.Hash, file.Hash)) { return(true); } } return(false); }
private void UnloadHeaderFile(string path, byte[] key) { try { if (!File.Exists(path)) { return; } using (TaggedStream filex = new TaggedStream(path, Network.Protocol)) using (IVCryptoStream crypto = IVCryptoStream.Load(filex, key)) { PacketStream stream = new PacketStream(crypto, Protocol, FileAccess.Read); G2Header header = null; while (stream.ReadPacket(ref header)) { if (header.Name == StoragePacket.File) { StorageFile packet = StorageFile.Decode(header); if (packet == null) { continue; } OpFile commonFile = null; if (!FileMap.SafeTryGetValue(packet.HashID, out commonFile)) { continue; } commonFile.DeRef(); } } } } catch (Exception ex) { Core.Network.UpdateLog("Storage", "Error loading files " + ex.Message); } }
void HashFile(HashPack pack) { // three steps, hash file, encrypt file, hash encrypted file try { OpFile file = null; StorageFile info = pack.File.Info.Clone(); // remove old references from local file OpFile commonFile = null; if (FileMap.SafeTryGetValue(pack.File.Info.HashID, out commonFile)) { commonFile.DeRef(); //crit test } if (!File.Exists(pack.Path)) { return; } // do public hash Utilities.ShaHashFile(pack.Path, ref info.InternalHash, ref info.InternalSize); info.InternalHashID = BitConverter.ToUInt64(info.InternalHash, 0); // if file exists in public map, use key for that file OpFile internalFile = null; InternalFileMap.SafeTryGetValue(info.InternalHashID, out internalFile); if (internalFile != null) { file = internalFile; file.References++; // if file already encrypted in our system, continue if (File.Exists(GetFilePath(info.HashID))) { info.Size = file.Size; info.FileKey = file.Key; info.Hash = file.Hash; info.HashID = file.HashID; if (!Utilities.MemCompare(file.Hash, pack.File.Info.Hash)) { ReviseFile(pack, info); } return; } } // file key is opID and public hash xor'd so that files won't be duplicated on the network // apply special compartment key here as well, xor again RijndaelManaged crypt = Utilities.CommonFileKey(Core.User.Settings.OpKey, info.InternalHash); info.FileKey = crypt.Key; // encrypt file to temp dir string tempPath = Core.GetTempPath(); Utilities.EncryptTagFile(pack.Path, tempPath, crypt, Core.Network.Protocol, ref info.Hash, ref info.Size); info.HashID = BitConverter.ToUInt64(info.Hash, 0); // move to official path string path = GetFilePath(info.HashID); if (!File.Exists(path)) { File.Move(tempPath, path); } // if we dont have record of file make one if (file == null) { file = new OpFile(info); file.References++; FileMap.SafeAdd(info.HashID, file); InternalFileMap.SafeAdd(info.InternalHashID, file); } // else, record already made, just needed to put the actual file in the system else { Debug.Assert(info.HashID == file.HashID); } // if hash is different than previous mark as modified if (!Utilities.MemCompare(file.Hash, pack.File.Info.Hash)) { ReviseFile(pack, info); } } catch (Exception ex) { /*rotate file to back of queue * lock (HashQueue) * if (HashQueue.Count > 1) * HashQueue.Enqueue(HashQueue.Dequeue());*/ Core.Network.UpdateLog("Storage", "Hash thread: " + ex.Message); } }
private void LoadHeaderFile(string path, OpStorage storage, bool reload, bool working) { try { if (!File.Exists(path)) { return; } bool cached = Network.Routing.InCacheArea(storage.UserID); bool local = false; byte[] key = working ? LocalFileKey : storage.File.Header.FileKey; using (TaggedStream filex = new TaggedStream(path, Network.Protocol)) using (IVCryptoStream crypto = IVCryptoStream.Load(filex, key)) { PacketStream stream = new PacketStream(crypto, Protocol, FileAccess.Read); G2Header header = null; ulong currentUID = 0; while (stream.ReadPacket(ref header)) { if (!working && header.Name == StoragePacket.Root) { StorageRoot packet = StorageRoot.Decode(header); local = Core.UserID == storage.UserID || GetHigherRegion(Core.UserID, packet.ProjectID).Contains(storage.UserID) || Trust.GetDownlinkIDs(Core.UserID, packet.ProjectID, 1).Contains(storage.UserID); } if (header.Name == StoragePacket.File) { StorageFile packet = StorageFile.Decode(header); if (packet == null) { continue; } bool historyFile = true; if (packet.UID != currentUID) { historyFile = false; currentUID = packet.UID; } OpFile file = null; if (!FileMap.SafeTryGetValue(packet.HashID, out file)) { file = new OpFile(packet); FileMap.SafeAdd(packet.HashID, file); } InternalFileMap.SafeAdd(packet.InternalHashID, file); if (!reload) { file.References++; } if (!working) // if one ref is public, then whole file is marked public { file.Working = false; } if (packet.HashID == 0 || packet.InternalHash == null) { Debug.Assert(false); continue; } string filepath = GetFilePath(packet.HashID); file.Downloaded = File.Exists(filepath); if (Loading && file.Downloaded && !ReferencedPaths.Contains(filepath)) { ReferencedPaths.Add(filepath); } if (!file.Downloaded) { // if in local range only store latest if (local && !historyFile) { DownloadFile(storage.UserID, packet); } // if storage is in cache range, download all files else if (Network.Established && cached) { DownloadFile(storage.UserID, packet); } } // on link update, if in local range, get latest files // (handled by location update, when it sees a new version of storage component is available) } } } } catch (Exception ex) { Core.Network.UpdateLog("Storage", "Error loading files " + ex.Message); } }
private void LoadHeaderFile(string path, OpStorage storage, bool reload, bool working) { try { if (!File.Exists(path)) return; bool cached = Network.Routing.InCacheArea(storage.UserID); bool local = false; byte[] key = working ? LocalFileKey : storage.File.Header.FileKey; using (TaggedStream filex = new TaggedStream(path, Network.Protocol)) using (IVCryptoStream crypto = IVCryptoStream.Load(filex, key)) { PacketStream stream = new PacketStream(crypto, Protocol, FileAccess.Read); G2Header header = null; ulong currentUID = 0; while (stream.ReadPacket(ref header)) { if (!working && header.Name == StoragePacket.Root) { StorageRoot packet = StorageRoot.Decode(header); local = Core.UserID == storage.UserID || GetHigherRegion(Core.UserID, packet.ProjectID).Contains(storage.UserID) || Trust.GetDownlinkIDs(Core.UserID, packet.ProjectID, 1).Contains(storage.UserID); } if (header.Name == StoragePacket.File) { StorageFile packet = StorageFile.Decode(header); if (packet == null) continue; bool historyFile = true; if (packet.UID != currentUID) { historyFile = false; currentUID = packet.UID; } OpFile file = null; if (!FileMap.SafeTryGetValue(packet.HashID, out file)) { file = new OpFile(packet); FileMap.SafeAdd(packet.HashID, file); } InternalFileMap.SafeAdd(packet.InternalHashID, file); if (!reload) file.References++; if (!working) // if one ref is public, then whole file is marked public file.Working = false; if (packet.HashID == 0 || packet.InternalHash == null) { Debug.Assert(false); continue; } string filepath = GetFilePath(packet.HashID); file.Downloaded = File.Exists(filepath); if (Loading && file.Downloaded && !ReferencedPaths.Contains(filepath)) ReferencedPaths.Add(filepath); if (!file.Downloaded) { // if in local range only store latest if (local && !historyFile) DownloadFile(storage.UserID, packet); // if storage is in cache range, download all files else if (Network.Established && cached) DownloadFile(storage.UserID, packet); } // on link update, if in local range, get latest files // (handled by location update, when it sees a new version of storage component is available) } } } } catch (Exception ex) { Core.Network.UpdateLog("Storage", "Error loading files " + ex.Message); } }
void HashFile(HashPack pack) { // three steps, hash file, encrypt file, hash encrypted file try { OpFile file = null; StorageFile info = pack.File.Info.Clone(); // remove old references from local file OpFile commonFile = null; if (FileMap.SafeTryGetValue(pack.File.Info.HashID, out commonFile)) commonFile.DeRef(); //crit test if (!File.Exists(pack.Path)) return; // do public hash Utilities.ShaHashFile(pack.Path, ref info.InternalHash, ref info.InternalSize); info.InternalHashID = BitConverter.ToUInt64(info.InternalHash, 0); // if file exists in public map, use key for that file OpFile internalFile = null; InternalFileMap.SafeTryGetValue(info.InternalHashID, out internalFile); if (internalFile != null) { file = internalFile; file.References++; // if file already encrypted in our system, continue if (File.Exists(GetFilePath(info.HashID))) { info.Size = file.Size; info.FileKey = file.Key; info.Hash = file.Hash; info.HashID = file.HashID; if (!Utilities.MemCompare(file.Hash, pack.File.Info.Hash)) ReviseFile(pack, info); return; } } // file key is opID and public hash xor'd so that files won't be duplicated on the network // apply special compartment key here as well, xor again RijndaelManaged crypt = Utilities.CommonFileKey(Core.User.Settings.OpKey, info.InternalHash); info.FileKey = crypt.Key; // encrypt file to temp dir string tempPath = Core.GetTempPath(); Utilities.EncryptTagFile(pack.Path, tempPath, crypt, Core.Network.Protocol, ref info.Hash, ref info.Size); info.HashID = BitConverter.ToUInt64(info.Hash, 0); // move to official path string path = GetFilePath(info.HashID); if (!File.Exists(path)) File.Move(tempPath, path); // if we dont have record of file make one if (file == null) { file = new OpFile(info); file.References++; FileMap.SafeAdd(info.HashID, file); InternalFileMap.SafeAdd(info.InternalHashID, file); } // else, record already made, just needed to put the actual file in the system else { Debug.Assert(info.HashID == file.HashID); } // if hash is different than previous mark as modified if (!Utilities.MemCompare(file.Hash, pack.File.Info.Hash)) ReviseFile(pack, info); } catch (Exception ex) { /*rotate file to back of queue lock (HashQueue) if (HashQueue.Count > 1) HashQueue.Enqueue(HashQueue.Dequeue());*/ Core.Network.UpdateLog("Storage", "Hash thread: " + ex.Message); } }