Example #1
0
        /// <summary>
        /// Locks the item in the remote storage. The item must implement <see cref="IVirtualLock"/> interface.
        /// </summary>
        /// <param name="lockSync">Sync lock.</param>
        /// <param name="lockMode">Indicates automatic or manual lock.</param>
        private async Task <ServerLockInfo> LockAsync(LockSync lockSync, LockMode lockMode)
        {
            ServerLockInfo lockInfo = null;

            try
            {
                logger.LogMessage("Locking in remote storage", userFileSystemPath);

                // Set lock-pending icon.
                await userFileSystemRawItem.SetLockPendingIconAsync(true);

                // Lock file in remote storage.
                IVirtualLock userLock = await GetItemAsync(userFileSystemPath) as IVirtualLock;

                lockInfo = await userLock.LockAsync();

                // Save lock-token and lock-mode.
                await lockManager.SetLockInfoAsync(lockInfo);

                await lockManager.SetLockModeAsync(lockMode);

                logger.LogMessage("Locked in remote storage succesefully.", userFileSystemPath);
            }
            catch (Exception ex)
            {
                logger.LogError("Locking in remote storage failed.", userFileSystemPath, null, ex);

                // Delete lock-token and lock-mode files.
                await lockManager.DeleteLockAsync();

                // Clear lock icon.
                await userFileSystemRawItem.SetLockInfoAsync(null);

                // Rethrow the exception preserving stack trace of the original exception.
                System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw();
            }

            // Set locked icon and all lock properties.
            await userFileSystemRawItem.SetLockInfoAsync(lockInfo);

            return(lockInfo);
        }
Example #2
0
        /// <summary>
        /// Unlocks the file in the remote storage using existing <see cref="LockSync"/>.
        /// </summary>
        /// <param name="lockSync">Sync lock.</param>
        private async Task UnlockAsync(LockSync lockSync)
        {
            logger.LogMessage("Unlocking in remote storage", userFileSystemPath);

            // Set pending icon.
            await userFileSystemRawItem.SetLockPendingIconAsync(true);

            // Read lock-token from lock-info file.
            string lockToken = (await lockManager.GetLockInfoAsync()).LockToken;

            // Unlock file in remote storage.
            IVirtualLock userLock = await GetItemAsync(userFileSystemPath) as IVirtualLock;

            await userLock.UnlockAsync(lockToken);

            // Delete lock-mode and lock-token files.
            await lockManager.DeleteLockAsync();

            // Remove lock icon.
            await userFileSystemRawItem.SetLockInfoAsync(null);

            logger.LogMessage("Unlocked in remote storage succesefully", userFileSystemPath);
        }