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

            var job = JobControler.CreateNewJob();

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

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

                job.ProgressStr = "loading...";
                var host        = await RemoteServerFactory.PathToItem(cryptRootPath).ConfigureAwait(false);
                if (host == null)
                {
                    return;
                }
                var root = new RcloneCryptSystemItem(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);
            }
            Encrypter = new CryptRclone(pass.Password, pass.Salt)
            {
                IsEncryptedName = pass.FilenameEncryption,
            };
            DrivePassword      = pass.Password;
            DriveSalt          = pass.Salt;
            FilenameEncryption = pass.FilenameEncryption;

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

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

            _IsReady = true;
            TSviewCloudConfig.Config.Log.LogOut("[Add] RcloneCryptSystem {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(RcloneCryptSystem)] " + foldername);

            var parent     = pathlist[(remoteTarget.ID == cryptRootPath)? "": remoteTarget.ID];
            var dirname    = (Encrypter.IsEncryptedName) ? Encrypter.EncryptName(foldername) : foldername;
            var orgmakejob = parent.orgItem.MakeFolder(dirname, 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 RcloneCryptSystemItem(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(RcloneCryptSystem)] " + 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 <RcloneCryptSystemItem>();
                    Parallel.ForEach(
                        orgitem.Children,
                        new ParallelOptions {
                        MaxDegreeOfParallelism = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 0.75) * 1.0))
                    },
                        () => new List <RcloneCryptSystemItem>(),
                        (x, state, local) =>
                    {
                        var child = RemoteServerFactory.PathToItem(x.FullPath, (deep) ? ReloadType.Reload : ReloadType.Cache).Result;
                        if (child == null)
                        {
                            return(local);
                        }

                        try
                        {
                            var item = new RcloneCryptSystemItem(this, child, pathlist[ID]);
                            pathlist.AddOrUpdate(item.ID, (k) => item, (k, v) => item);
                            local.Add(item);
                        }
                        catch { }

                        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] RcloneCryptSystem {0} as {1}", cryptRootPath, Name);
            Encrypter = new CryptRclone(DrivePassword, DriveSalt)
            {
                IsEncryptedName = FilenameEncryption,
            };
            loadinglist = new ConcurrentDictionary <string, ManualResetEventSlim>();

            var job = JobControler.CreateNewJob();

            job.DisplayName = "CryptRclone";
            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, j.Ct).ConfigureAwait(false);
                        }
                        else
                        {
                            await Task.Delay(1000, j.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, RcloneCryptSystemItem>();
                    var root = new RcloneCryptSystemItem(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;
            });
        }