Пример #1
0
        private static async Task LoadDirectoriesAsync(IFileClient client, string path, string dstPath)
        {
            var existsFiles       = Directory.EnumerateFiles(dstPath).Select(Path.GetFileName).ToList();
            var existsDirectories = Directory.EnumerateDirectories(dstPath).Select(Path.GetFileName).ToList();

            var items = await client.GetItemsAsync(path);

            foreach (var item in items)
            {
                try
                {
                    if (item.Name.IndexOf("\\") != -1)
                    {
                        await _writer.WriteLineAsync($"XX: {item.FullName}");

                        Console.WriteLine($"XX: {item.FullName}");
                        continue;
                    }

                    if (_ignoreRegices.Any(t => t.IsMatch(item.FullName)))
                    {
                        continue;
                    }

                    switch (item.Type)
                    {
                    case ItemType.File:
                    {
                        existsFiles.Remove(item.Name);
                        var file = new FileInfo(Path.Combine(dstPath, item.Name));
                        if (!file.Exists || file.LastWriteTime != item.Modified || file.Length != item.Size)
                        {
                            await _writer.WriteLineAsync($"!: {item.FullName}");

                            Console.WriteLine($"!: {item.FullName}");
                            await client.DownloadFileAsync(item.FullName, file.FullName);

                            file.Refresh();
                            if (file.Length != item.Size)
                            {
                                throw new Exception();
                            }
                            file.LastWriteTime = item.Modified;
                        }
                        else
                        {
                            await _writer.WriteLineAsync($"x: {item.FullName}");

                            Console.WriteLine($"x: {item.FullName}");
                        }
                    }
                    break;

                    case ItemType.Directory:
                    {
                        existsDirectories.Remove(item.Name);
                        await _writer.WriteLineAsync($"D: {item.FullName}");

                        Console.WriteLine($"D: {item.FullName}");
                        var ndpath = Path.Combine(dstPath, item.Name);
                        Directory.CreateDirectory(ndpath);

                        await LoadDirectoriesAsync(client, item.FullName, ndpath);

                        Directory.SetLastWriteTime(ndpath, item.Modified);
                    }
                    break;

                    case ItemType.Others:
                        await _writer.WriteLineAsync($"O: {item.FullName}");

                        Console.WriteLine($"O: {item.FullName}");
                        break;

                    default:
                        await _writer.WriteLineAsync($"?: {item.FullName}");

                        Console.WriteLine($"?: {item.FullName}");
                        break;
                    }
                }
                catch (Exception exp)
                {
                    await _writer.WriteLineAsync("ERROR");

                    await _writer.WriteLineAsync(exp.ToString());
                }
            }

            foreach (var item in existsFiles.Where(t => !_undeletableNames.Contains(t)))
            {
                File.Delete(Path.Combine(dstPath, item));
            }

            foreach (var item in existsDirectories.Where(t => !_undeletableNames.Contains(t)))
            {
                Directory.Delete(Path.Combine(dstPath, item), true);
            }
        }