Beispiel #1
0
        public static StorageRoot Decode(G2Header header)
        {
            StorageRoot root  = new StorageRoot();
            G2Header    child = new G2Header(header.Data);

            while (G2Protocol.ReadNextChild(header, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                {
                    continue;
                }

                switch (child.Name)
                {
                case Packet_Project:
                    root.ProjectID = BitConverter.ToUInt32(child.Data, child.PayloadPos);
                    break;
                }
            }

            return(root);
        }
Beispiel #2
0
        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);
            }
        }
Beispiel #3
0
        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));
            }
        }
Beispiel #4
0
        public static StorageRoot Decode(G2Header header)
        {
            StorageRoot root = new StorageRoot();
            G2Header child = new G2Header(header.Data);

            while (G2Protocol.ReadNextChild(header, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_Project:
                        root.ProjectID = BitConverter.ToUInt32(child.Data, child.PayloadPos);
                        break;
                }
            }

            return root;
        }