Пример #1
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);
            }
        }