예제 #1
0
파일: Client.cs 프로젝트: rcuvgd/FTPbox
        /// <summary>
        ///     Get a full list of files/folders inside the specified path
        /// </summary>
        /// <param name="cpath">path to folder to list inside</param>
        /// <param name="skipIgnored">if true, ignored items are not returned</param>
        public virtual async Task <IEnumerable <ClientItem> > ListRecursive(string cpath, bool skipIgnored = true)
        {
            var list = await List(cpath, skipIgnored);

            if (ListingFailed)
            {
                return(default(IEnumerable <ClientItem>));
            }

            if (skipIgnored)
            {
                list = list.Where(x => !Controller.ItemSkipped(x));
            }

            var subItems = list.Where(x => x.Type == ClientItemType.File).ToList();

            foreach (var d in list.Where(x => x.Type == ClientItemType.Folder))
            {
                subItems.Add(d);
                foreach (var f in await ListRecursiveInside(d, skipIgnored))
                {
                    subItems.Add(f);
                }
            }

            return(subItems);
        }
예제 #2
0
        /// <summary>
        /// Raised when a file or folder is changed
        /// </summary>
        private async void OnChanged(object source, FileSystemEventArgs e)
        {
            if (isRecentlyRaised(e.FullPath))
            {
                return;
            }

            if (_controller.ItemSkipped(e.FullPath) || (!File.Exists(e.FullPath) && !Directory.Exists(e.FullPath)))
            {
                return;
            }

            var retries = 0;

            if (File.Exists(e.FullPath))
            {
                // avoid queuing the same file multiple times
                while (true)
                {
                    if (!Common.FileIsUsed(e.FullPath))
                    {
                        break;
                    }
                    // Exit after 5 retries
                    if (retries > 5)
                    {
                        return;
                    }
                    // Sleep for a 10th of a second, then check again
                    Thread.Sleep(100);
                    retries++;
                }
            }
            // Add to queue
            var actionType = e.ChangeType == WatcherChangeTypes.Changed ? ChangeAction.changed : ChangeAction.created;

            await AddToQueue(e, actionType);
        }
예제 #3
0
파일: SyncQueue.cs 프로젝트: zyfzgt/FTPbox
        /// <summary>
        /// Check a local folder and all of its subitems for changes
        /// </summary>
        private async Task CheckLocalFolder(SyncQueueItem folder)
        {
            if (_controller.ItemSkipped(folder.Item) && folder.CommonPath != ".")
            {
                return;
            }

            var cp = (folder.Item.FullPath == _controller.Paths.Local) ? "." : folder.CommonPath;

            var cpExists = cp == "." || _controller.Client.Exists(cp);

            if (!cpExists)
            {
                folder.AddedOn = DateTime.Now;
                base.Add(folder);
            }

            var remoteFilesList = cpExists
                ? (await _controller.Client.ListRecursive(cp)).Select(x => x.FullPath).ToList()
                : new List <string>();

            remoteFilesList = remoteFilesList.ConvertAll(x => _controller.GetCommonPath(x, false));

            if (_controller.Client.ListingFailed)
            {
                folder.Status      = StatusType.Failure;
                folder.CompletedOn = DateTime.Now;
                _completedList.Add(folder, StatusType.Failure);
                await _controller.Client.Reconnect();

                return;
            }

            var di = new DirectoryInfo(folder.LocalPath);

            foreach (var d in di.GetDirectories("*", SearchOption.AllDirectories))
            {
                var cpath = _controller.GetCommonPath(d.FullName, true);

                if (remoteFilesList.Contains(cpath))
                {
                    continue;
                }
                if (_controller.ItemSkipped(d))
                {
                    continue;
                }

                base.Add(new SyncQueueItem(_controller)
                {
                    Item = new ClientItem(d)
                    {
                        FullPath = d.FullName
                    },
                    ActionType = ChangeAction.changed,
                    Status     = StatusType.Waiting,
                    SyncTo     = SyncTo.Remote
                });
            }

            foreach (var f in di.GetFiles("*", SearchOption.AllDirectories))
            {
                if (_controller.ItemSkipped(f))
                {
                    continue;
                }

                var sqi = new SyncQueueItem(_controller)
                {
                    Item       = new ClientItem(f),
                    ActionType = ChangeAction.changed,
                    Status     = StatusType.Waiting,
                    SyncTo     = SyncTo.Remote
                };
                var cpath = sqi.CommonPath;

                if (!_controller.FileLog.Contains(cpath) && remoteFilesList.Contains(cpath))
                {
                    // untracked file, exists both locally and on server.
                    var areIdentical = await _controller.Client.FilesAreIdentical(cpath, f.FullName);

                    if (areIdentical)
                    {
                        _controller.FileLog.PutFile(sqi);
                        continue;
                    }
                }
                else if (remoteFilesList.Contains(cpath) && _controller.FileLog.GetLocal(cpath) == f.LastWriteTime)
                {
                    // tracked file, local file unmodified.
                    continue;
                }
                // local file should be uploaded.
                base.Add(sqi);
            }
        }