void Cache_FileRemoved(OpVersionedFile file) { OpProfile profile = GetProfile(file.UserID); if (profile != null) { ProfileMap.SafeRemove(file.UserID); } }
void Cache_FileRemoved(OpVersionedFile file) { OpStorage storage = GetStorage(file.UserID); if (storage != null) { UnloadHeaderFile(GetFilePath(storage), storage.File.Header.FileKey); } StorageMap.SafeRemove(file.UserID); }
void Cache_FileRemoved(OpVersionedFile file) { OpPlan plan = GetPlan(file.UserID, false); if (plan == null) { return; } PlanMap.SafeRemove(file.UserID); }
private void Cache_FileAquired(OpVersionedFile file) { // get profile OpProfile prevProfile = GetProfile(file.UserID); OpProfile newProfile = new OpProfile(file); ProfileMap.SafeAdd(file.UserID, newProfile); if (file.UserID == Core.UserID) { LocalProfile = newProfile; } if ((newProfile == LocalProfile) || (prevProfile != null && prevProfile.Loaded)) { LoadProfile(newProfile.UserID); } // update subs if (Network.Established) { List <LocationData> locations = new List <LocationData>(); Trust.ProjectRoots.LockReading(delegate() { foreach (uint project in Trust.ProjectRoots.Keys) { if (newProfile.UserID == Core.UserID || Trust.IsHigher(newProfile.UserID, project)) { Trust.GetLocsBelow(Core.UserID, project, locations); } } }); Store.PublishDirect(locations, newProfile.UserID, ServiceID, 0, file.SignedHeader); } if (ProfileUpdate != null) { Core.RunInGuiThread(ProfileUpdate, newProfile); } if (Core.NewsWorthy(newProfile.UserID, 0, false)) { Core.MakeNews(ServiceIDs.Profile, "Profile updated by " + Core.GetName(newProfile.UserID), newProfile.UserID, 0, true); } }
private void Cache_FileAquired(OpVersionedFile file) { if (file.UserID != Network.Local.UserID) { return; } // only we can open the buddly list stored on the network byte[] key = Core.User.Settings.KeyPair.Decrypt(file.Header.FileKey, false); using (TaggedStream tagged = new TaggedStream(Cache.GetFilePath(file.Header), Network.Protocol)) using (IVCryptoStream crypto = IVCryptoStream.Load(tagged, key)) { BuddyList.SafeClear(); PacketStream stream = new PacketStream(crypto, Network.Protocol, FileAccess.Read); G2Header root = null; while (stream.ReadPacket(ref root)) { if (root.Name == BuddyPacket.Buddy) { OpBuddy buddy = OpBuddy.Decode(root); ulong id = Utilities.KeytoID(buddy.Key); Core.IndexKey(id, ref buddy.Key); Core.IndexName(id, buddy.Name); if (buddy.Ignored) { IgnoreList.SafeAdd(id, buddy); } else { BuddyList.SafeAdd(id, buddy); } } } } Core.RunInGuiThread(GuiUpdate); }
void Cache_FileAquired(OpVersionedFile file) { // unload old file OpStorage prevStorage = GetStorage(file.UserID); if (prevStorage != null) { string oldPath = GetFilePath(prevStorage); UnloadHeaderFile(oldPath, prevStorage.File.Header.FileKey); } OpStorage newStorage = new OpStorage(file); StorageMap.SafeAdd(file.UserID, newStorage); LoadHeaderFile(GetFilePath(newStorage), newStorage, false, false); // record changes of higher nodes for auto-integration purposes Trust.ProjectRoots.LockReading(delegate() { foreach (uint project in Trust.ProjectRoots.Keys) { List <ulong> inheritIDs = Trust.GetAutoInheritIDs(Core.UserID, project); if (Core.UserID == newStorage.UserID || inheritIDs.Contains(newStorage.UserID)) { // doesnt get called on startup because working not initialized before headers are loaded if (Working.ContainsKey(project)) { bool doSave = Working[project].RefreshHigherChanges(newStorage.UserID); if (!Loading && !SavingLocal) { Working[project].AutoIntegrate(doSave); } } } } }); // update subs - this ensures file not propagated lower until we have it (prevents flood to original poster) if (Network.Established) { List <LocationData> locations = new List <LocationData>(); Trust.ProjectRoots.LockReading(delegate() { foreach (uint project in Trust.ProjectRoots.Keys) { if (newStorage.UserID == Core.UserID || Trust.IsHigher(newStorage.UserID, project)) { Trust.GetLocsBelow(Core.UserID, project, locations); } } }); Store.PublishDirect(locations, newStorage.UserID, ServiceID, FileTypeCache, file.SignedHeader); } if (StorageUpdate != null) { Core.RunInGuiThread(StorageUpdate, newStorage); } if (Core.NewsWorthy(newStorage.UserID, 0, false)) { Core.MakeNews(ServiceIDs.Storage, "File System updated by " + Core.GetName(newStorage.UserID), newStorage.UserID, 0, false); } }
public void SaveLocal(uint project) { try { string tempPath = Core.GetTempPath(); byte[] key = Utilities.GenerateKey(Core.StrongRndGen, 256); using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, key)) { // write loaded projects WorkingStorage working = null; if (Working.ContainsKey(project)) { working = Working[project]; } if (working != null) { Protocol.WriteToFile(new StorageRoot(working.ProjectID), stream); working.WriteWorkingFile(stream, working.RootFolder, true); working.Modified = false; try { File.Delete(GetWorkingPath(project)); } catch { } } // open old file and copy entries, except for working OpStorage local = GetStorage(Core.UserID); if (local != null) { string oldPath = GetFilePath(local); if (File.Exists(oldPath)) { using (TaggedStream file = new TaggedStream(oldPath, Network.Protocol)) using (IVCryptoStream crypto = IVCryptoStream.Load(file, local.File.Header.FileKey)) { PacketStream oldStream = new PacketStream(crypto, Protocol, FileAccess.Read); bool write = false; G2Header g2header = null; while (oldStream.ReadPacket(ref g2header)) { if (g2header.Name == StoragePacket.Root) { StorageRoot root = StorageRoot.Decode(g2header); write = (root.ProjectID != project); } //copy packet right to new file if (write) //crit test { stream.Write(g2header.Data, g2header.PacketPos, g2header.PacketSize); } } } } } stream.WriteByte(0); // signal last packet stream.FlushFinalBlock(); } SavingLocal = true; // prevents auto-integrate from re-calling saveLocal OpVersionedFile vfile = Cache.UpdateLocal(tempPath, key, BitConverter.GetBytes(Core.TimeNow.ToUniversalTime().ToBinary())); SavingLocal = false; Store.PublishDirect(Core.Trust.GetLocsAbove(), Core.UserID, ServiceID, FileTypeCache, vfile.SignedHeader); } catch (Exception ex) { Core.Network.UpdateLog("Storage", "Error updating local " + ex.Message); } if (StorageUpdate != null) { Core.RunInGuiThread(StorageUpdate, GetStorage(Core.UserID)); } }
public OpStorage(OpVersionedFile file) { File = file; }
public OpProfile(OpVersionedFile file) { File = file; }
public void SaveLocal(string template, Dictionary <string, string> textFields, Dictionary <string, string> fileFields) { try { long embeddedStart = 0; string tempPath = Core.GetTempPath(); byte[] key = Utilities.GenerateKey(Core.StrongRndGen, 256); using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, key)) { int written = 0; // write template info byte[] htmlBytes = UTF8Encoding.UTF8.GetBytes(template); written += Protocol.WriteToFile(new ProfileAttachment("template", htmlBytes.Length), stream); // write fields info (convert into fields into packet list) List <byte[]> fieldPackets = new List <byte[]>(); int fieldsTotalSize = 0; if (textFields != null) { foreach (KeyValuePair <string, string> pair in textFields) { if (pair.Value == null) { continue; } ProfileField field = new ProfileField(); field.Name = pair.Key; field.Value = UTF8Encoding.UTF8.GetBytes(pair.Value); field.FieldType = ProfileFieldType.Text; byte[] packet = field.Encode(Network.Protocol); fieldPackets.Add(packet); fieldsTotalSize += packet.Length; } } if (fileFields != null) { foreach (KeyValuePair <string, string> pair in fileFields) { if (pair.Value == null) { continue; } ProfileField field = new ProfileField(); field.Name = pair.Key; field.Value = UTF8Encoding.UTF8.GetBytes(Path.GetFileName(pair.Value)); field.FieldType = ProfileFieldType.File; byte[] packet = field.Encode(Network.Protocol); fieldPackets.Add(packet); fieldsTotalSize += packet.Length; } } if (fieldsTotalSize > 0) { written += Protocol.WriteToFile(new ProfileAttachment("fields", fieldsTotalSize), stream); } // write files info if (fileFields != null) { foreach (string path in fileFields.Values) { using (FileStream file = File.OpenRead(path)) written += Protocol.WriteToFile(new ProfileAttachment("file=" + Path.GetFileName(path), file.Length), stream); } } stream.WriteByte(0); // end packets embeddedStart = written + 1; // write template bytes stream.Write(htmlBytes, 0, htmlBytes.Length); // write field bytes foreach (byte[] packet in fieldPackets) { stream.Write(packet, 0, packet.Length); } // write file bytes const int buffSize = 4096; byte[] buffer = new byte[buffSize]; if (fileFields != null) { foreach (string path in fileFields.Values) { using (FileStream file = File.OpenRead(path)) { int read = buffSize; while (read == buffSize) { read = file.Read(buffer, 0, buffSize); stream.Write(buffer, 0, read); } } } } stream.FlushFinalBlock(); } OpVersionedFile vfile = Cache.UpdateLocal(tempPath, key, BitConverter.GetBytes(embeddedStart)); Store.PublishDirect(Trust.GetLocsAbove(), Core.UserID, ServiceID, DataTypeFile, vfile.SignedHeader); } catch (Exception ex) { Network.UpdateLog("Profile", "Error updating local " + ex.Message); } }
public OpPlan(OpVersionedFile file) { File = file; }
public void SaveLocal() { try { string tempPath = Core.GetTempPath(); byte[] key = Utilities.GenerateKey(Core.StrongRndGen, 256); using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, key)) { // write dummy block if nothing to write OpPlan plan = GetPlan(Core.UserID, true); if (plan == null || plan.Blocks == null || plan.Blocks.Count == 0) { Protocol.WriteToFile(new PlanBlock(), stream); } if (plan != null) { foreach (List <PlanBlock> list in plan.Blocks.Values) { foreach (PlanBlock block in list) { Protocol.WriteToFile(block, stream); } } foreach (List <PlanGoal> list in plan.GoalMap.Values) { foreach (PlanGoal goal in list) { GetEstimate(goal, ref goal.EstCompleted, ref goal.EstTotal); Protocol.WriteToFile(goal, stream); } } foreach (List <PlanItem> list in plan.ItemMap.Values) { foreach (PlanItem item in list) { Protocol.WriteToFile(item, stream); } } } stream.WriteByte(0); // signal last packet stream.FlushFinalBlock(); } OpVersionedFile file = Cache.UpdateLocal(tempPath, key, null); Store.PublishDirect(Core.Trust.GetLocsAbove(), Core.UserID, ServiceID, DataTypeFile, file.SignedHeader); } catch (Exception ex) { Core.Network.UpdateLog("Plan", "Error updating local " + ex.Message); } }
private void Cache_FileAquired(OpVersionedFile file) { OpPlan prevPlan = GetPlan(file.UserID, false); OpPlan newPlan = new OpPlan(file); PlanMap.SafeAdd(newPlan.UserID, newPlan); if (file.UserID == Core.UserID) { LocalPlan = newPlan; } if ((newPlan == LocalPlan) || (prevPlan != null && prevPlan.Loaded)) // if loaded, reload { LoadPlan(newPlan.UserID); } // update subs if (Network.Established) { List <LocationData> locations = new List <LocationData>(); Trust.ProjectRoots.LockReading(delegate() { foreach (uint project in Trust.ProjectRoots.Keys) { if (newPlan.UserID == Core.UserID || Trust.IsHigher(newPlan.UserID, project)) { Trust.GetLocsBelow(Core.UserID, project, locations); } } }); Store.PublishDirect(locations, newPlan.UserID, ServiceID, DataTypeFile, newPlan.File.SignedHeader); } // see if we need to update our own goal estimates if (newPlan.UserID != Core.UserID && LocalPlan != null) { Trust.ProjectRoots.LockReading(delegate() { foreach (uint project in Trust.ProjectRoots.Keys) { if (Trust.IsLower(Core.UserID, newPlan.UserID, project)) // updated plan must be lower than us to have an effect { foreach (int ident in LocalPlan.GoalMap.Keys) { if (!newPlan.Loaded) { LoadPlan(newPlan.UserID); } // if updated plan part of the same goal ident, re-estimate our own goals, incorporating update's changes if (newPlan.GoalMap.ContainsKey(ident) || newPlan.ItemMap.ContainsKey(ident)) { foreach (PlanGoal goal in LocalPlan.GoalMap[ident]) { int completed = 0, total = 0; GetEstimate(goal, ref completed, ref total); if (completed != goal.EstCompleted || total != goal.EstTotal) { goal.EstCompleted = completed; goal.EstTotal = total; if (RunSaveLocal == 0) // if countdown not started, start { RunSaveLocal = SaveInterval; } } } } } } } }); } Core.RunInGuiThread(PlanUpdate, newPlan); if (Core.NewsWorthy(newPlan.UserID, 0, false)) { Core.MakeNews(ServiceIDs.Plan, "Plan updated by " + Core.GetName(newPlan.UserID), newPlan.UserID, 0, false); } }