private void UpdateContext(RemoteState oldRemoteState, RemoteState newRemoteState, WatcherChangeTypes changeType)
        {
            switch (changeType)
            {
            case WatcherChangeTypes.Deleted:

                // remote state was deleted
                // actions: remove remote state from database
                _context.RemoteStates.Remove(oldRemoteState);

                break;

            // remote state was created or modified
            // actions: create or update database state
            case WatcherChangeTypes.Changed:
            case WatcherChangeTypes.Created:
            case WatcherChangeTypes.Renamed:

                if (oldRemoteState != null)
                {
                    _context.RemoteStates.Remove(oldRemoteState);
                }

                _context.RemoteStates.Add(newRemoteState);

                break;

            // nothing changed
            default:
                break;
            }
        }
Esempio n. 2
0
        private IEnumerable <DriveItem> SafelyEnumerateDriveItems(string folderPath, CryptoDriveContext context, [CallerMemberName] string callerName = "")
        {
            var forceNew           = callerName == nameof(this.SafelyEnumerateDriveItems);
            var driveItems         = Enumerable.Empty <DriveItem>();
            var absoluteFolderPath = folderPath.ToAbsolutePath(this.BasePath);

            try
            {
                // get all folders in current folder
                driveItems = driveItems.Concat(Directory.EnumerateDirectories(absoluteFolderPath, "*", SearchOption.TopDirectoryOnly)
                                               .SelectMany(current =>
                {
                    RemoteState oldRemoteState = null;
                    var driveInfo   = new DirectoryInfo(current).ToDriveItem(this.BasePath);
                    var folderPath2 = current.Substring(this.BasePath.Length).NormalizeSlashes();

                    if (!forceNew)
                    {
                        oldRemoteState = context.RemoteStates.FirstOrDefault(remoteState => remoteState.GetItemPath() == folderPath2 &&
                                                                             remoteState.Type == DriveItemType.Folder);
                    }

                    // When we know that the parent folder is not in the database (oldRemoteState == null),
                    // the children aren't there neither (forceNew = true).
                    if (forceNew || oldRemoteState == null)
                    {
                        return(this.SafelyEnumerateDriveItems(folderPath2, context)
                               .Concat(new DriveItem[] { driveInfo }));
                    }
                    else
                    {
                        return new List <DriveItem> {
                            driveInfo
                        }
                    };
                }));

                // get all files in current folder
                driveItems = driveItems.Concat(Directory.EnumerateFiles(absoluteFolderPath)
                                               .SelectMany(current =>
                {
                    var driveInfo = new FileInfo(current).ToDriveItem(this.BasePath);
                    return(new List <DriveItem> {
                        driveInfo
                    });
                }).ToList());

                // get all deleted items
                var remoteStates = context.RemoteStates.Where(current => current.Path == folderPath);

                var deletedItems = remoteStates.Where(current =>
                {
                    switch (current.Type)
                    {
                    case DriveItemType.Folder:
                        return(!Directory.Exists(current.GetItemPath().ToAbsolutePath(this.BasePath)));

                    case DriveItemType.File:
                        return(!File.Exists(current.GetItemPath().ToAbsolutePath(this.BasePath)));

                    case DriveItemType.RemoteItem:
                    default:
                        throw new NotSupportedException();
                    }
                }).Select(current => current.ToDriveItem(deleted: true));

                driveItems = driveItems.Concat(deletedItems);

                return(driveItems);
            }
            catch (UnauthorizedAccessException)
            {
                return(driveItems);
            }
        }