示例#1
0
        public override void ClearCache()
        {
            _IsReady = false;
            pathlist.Clear();

            var job = JobControler.CreateNewJob();

            job.DisplayName = "Initialize CarotCrypt";
            job.ProgressStr = "Initialize...";
            JobControler.Run(job, async(j) =>
            {
                job.Progress = -1;

                job.ProgressStr = "waiting for base system...";
                while (!RemoteServerFactory.ServerList[_dependService].IsReady)
                {
                    await Task.Delay(1000, j.Ct).ConfigureAwait(false);
                }

                job.ProgressStr = "loading...";
                var host        = await RemoteServerFactory.PathToItem(cryptRootPath, ReloadType.Reload).ConfigureAwait(false);
                if (host == null)
                {
                    return;
                }
                var root = new CarotCryptSystemItem(this, host, null);
                pathlist.AddOrUpdate("", (k) => root, (k, v) => root);
                await EnsureItem("", 1).ConfigureAwait(false);
                _IsReady = true;

                job.Progress    = 1;
                job.ProgressStr = "done.";
            });
        }
示例#2
0
        public async override Task <bool> Add()
        {
            var picker = new TSviewCloud.FormTreeSelect
            {
                Text = "Select encrypt root folder"
            };

            if (picker.ShowDialog() != DialogResult.OK)
            {
                return(false);
            }
            if (picker.SelectedItem == null)
            {
                return(false);
            }
            if (picker.SelectedItem.ItemType == RemoteItemType.File)
            {
                return(false);
            }

            var pass = new FormInputPass();

            if (pass.ShowDialog() != DialogResult.OK)
            {
                return(false);
            }
            CryptCarot = new CryptCarotDAV(pass.CryptNameHeader)
            {
                Password = pass.Password
            };
            DrivePassword   = pass.Password;
            cryptNameHeader = pass.CryptNameHeader;

            cryptRootPath  = picker.SelectedItem.FullPath;
            _dependService = picker.SelectedItem.Server;
            var root = new CarotCryptSystemItem(this, picker.SelectedItem, null);

            pathlist.AddOrUpdate("", (k) => root, (k, v) => root);
            await EnsureItem("", 1).ConfigureAwait(false);

            _IsReady = true;
            TSviewCloudConfig.Config.Log.LogOut("[Add] CarotCryptSystem {0} as {1}", cryptRootPath, Name);
            return(true);
        }
示例#3
0
        public override Job <IRemoteItem> MakeFolder(string foldername, IRemoteItem remoteTarget, bool WeekDepend = false, params Job[] parentJob)
        {
            if (parentJob?.Any(x => x?.IsCanceled ?? false) ?? false)
            {
                return(null);
            }

            try
            {
                var check = CheckUpload(remoteTarget, foldername, null, WeekDepend, parentJob);
                if (check != null)
                {
                    WeekDepend = false;
                    parentJob  = new[] { check };
                }
            }
            catch
            {
                var mkjob = JobControler.CreateNewJob <IRemoteItem>(
                    type: JobClass.RemoteOperation,
                    depends: parentJob);
                mkjob.WeekDepend  = WeekDepend;
                mkjob.ForceHidden = true;
                JobControler.Run <IRemoteItem>(mkjob, (j) =>
                {
                    j.Result = remoteTarget.Children.Where(x => x.Name == foldername).FirstOrDefault();
                });
                return(mkjob);
            }

            TSviewCloudConfig.Config.Log.LogOut("[MakeFolder(CarotCryptSystem)] " + foldername);

            var parent     = pathlist[(remoteTarget.ID == cryptRootPath)? "": remoteTarget.ID];
            var orgmakejob = parent.orgItem.MakeFolder(CryptCarot.EncryptFilename(foldername), WeekDepend, parentJob);

            var job = JobControler.CreateNewJob <IRemoteItem>(
                type: JobClass.RemoteOperation,
                depends: orgmakejob);

            job.DisplayName = "Make folder : " + foldername;
            job.ProgressStr = "wait for operation.";
            var ct = job.Ct;

            JobControler.Run <IRemoteItem>(job, (j) =>
            {
                var result = j.ResultOfDepend[0];
                if (result.TryGetTarget(out var item))
                {
                    j.ProgressStr = "Make folder...";
                    j.Progress    = -1;


                    var newitem = new CarotCryptSystemItem(this, item, remoteTarget);
                    pathlist.AddOrUpdate(newitem.ID, (k) => newitem, (k, v) => newitem);

                    remoteTarget.SetChildren(remoteTarget.Children.Concat(new[] { newitem }));

                    j.Result = newitem;

                    SetUpdate(remoteTarget);
                }
                j.ProgressStr = "Done";
                j.Progress    = 1;
            });
示例#4
0
        private async Task LoadItems(string ID, int depth = 0, bool deep = false)
        {
            if (depth < 0)
            {
                return;
            }
            ID = ID ?? "";

            bool master = true;

            loadinglist.AddOrUpdate(ID, new ManualResetEventSlim(false), (k, v) =>
            {
                if (v.IsSet)
                {
                    return(new ManualResetEventSlim(false));
                }

                master = false;
                return(v);
            });

            if (!master)
            {
                while (loadinglist.TryGetValue(ID, out var tmp) && tmp != null)
                {
                    await Task.Run(() => tmp.Wait()).ConfigureAwait(false);
                }
                return;
            }

            TSviewCloudConfig.Config.Log.LogOut("[LoadItems(CarotCryptSystem)] " + ID);
            try
            {
                var orgID = (string.IsNullOrEmpty(ID)) ? cryptRootPath : ID;
                if (!orgID.StartsWith(cryptRootPath))
                {
                    throw new ArgumentException("ID is not in root path", "ID");
                }
                var orgitem = await RemoteServerFactory.PathToItem(orgID, (deep)?ReloadType.Reload : ReloadType.Cache).ConfigureAwait(false);

                if (orgitem?.Children != null && orgitem.Children?.Count() != 0)
                {
                    var ret = new List <CarotCryptSystemItem>();
                    Parallel.ForEach(
                        orgitem.Children,
                        new ParallelOptions {
                        MaxDegreeOfParallelism = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 0.75) * 1.0))
                    },
                        () => new List <CarotCryptSystemItem>(),
                        (x, state, local) =>
                    {
                        if (!x.Name.StartsWith(cryptNameHeader))
                        {
                            return(local);
                        }

                        var child = RemoteServerFactory.PathToItem(x.FullPath, (deep) ? ReloadType.Reload : ReloadType.Cache).Result;
                        if (child == null)
                        {
                            return(local);
                        }

                        var item = new CarotCryptSystemItem(this, child, pathlist[ID]);
                        pathlist.AddOrUpdate(item.ID, (k) => item, (k, v) => item);
                        local.Add(item);
                        return(local);
                    },
                        (result) =>
                    {
                        lock (ret)
                            ret.AddRange(result);
                    }
                        );
                    pathlist[ID].SetChildren(ret);
                }
                else
                {
                    pathlist[ID].SetChildren(null);
                }
            }
            finally
            {
                ManualResetEventSlim tmp2;
                while (!loadinglist.TryRemove(ID, out tmp2))
                {
                    await Task.Delay(10).ConfigureAwait(false);
                }
                tmp2.Set();
            }
            if (depth > 0)
            {
                Parallel.ForEach(pathlist[ID].Children,
                                 new ParallelOptions {
                    MaxDegreeOfParallelism = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 0.75) * 1.0))
                },
                                 (x) => { LoadItems(x.ID, depth - 1).ConfigureAwait(false); });
            }
        }
示例#5
0
        public void OnDeserialized(StreamingContext c)
        {
            TSviewCloudConfig.Config.Log.LogOut("[Restore] CarotCryptSystem {0} as {1}", cryptRootPath, Name);
            CryptCarot = new CryptCarotDAV(cryptNameHeader)
            {
                Password = DrivePassword
            };
            loadinglist = new ConcurrentDictionary <string, ManualResetEventSlim>();

            var job = JobControler.CreateNewJob();

            job.DisplayName = "CryptCarotDAV";
            job.ProgressStr = "waiting parent";

            JobControler.Run(job, async(j) =>
            {
                j.ProgressStr = "Loading...";
                j.Progress    = -1;

                try
                {
                    int waitcount = 500;
                    while (!(RemoteServerFactory.ServerList.Keys.Contains(_dependService) && RemoteServerFactory.ServerList[_dependService].IsReady))
                    {
                        if (RemoteServerFactory.ServerList.Keys.Contains(_dependService))
                        {
                            await Task.Delay(1, job.Ct).ConfigureAwait(false);
                        }
                        else
                        {
                            await Task.Delay(1000, job.Ct).ConfigureAwait(false);
                        }

                        if (waitcount-- == 0)
                        {
                            throw new FileNotFoundException("Depend Service is not ready.", _dependService);
                        }
                    }
                }
                catch
                {
                    RemoteServerFactory.Delete(this);
                    return;
                }

                if (pathlist == null)
                {
                    pathlist = new ConcurrentDictionary <string, CarotCryptSystemItem>();
                    var root = new CarotCryptSystemItem(this, await RemoteServerFactory.PathToItem(cryptRootPath).ConfigureAwait(false), null);
                    pathlist.AddOrUpdate("", (k) => root, (k, v) => root);
                    await EnsureItem("", 1).ConfigureAwait(false);
                }
                else
                {
                    Parallel.ForEach(pathlist.Values.ToArray(),
                                     new ParallelOptions {
                        MaxDegreeOfParallelism = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 0.75) * 1.0))
                    },
                                     (x) => x.FixChain(this));
                }

                j.ProgressStr = "Done";
                j.Progress    = 1;

                _IsReady = true;
            });
        }