Example #1
0
        /// <summary>
        /// Remove \c lock \e kind on \e stream.
        /// </summary>
        /// <param name="stream">Name of stream or workspace to unlock.</param>
        /// <param name="kind">Type of lock to remove: \e to, \e from, or \e all.</param>
        /// <returns>\e true if \e stream was unlocked successfully, \e false otherwise.</returns>
        /// <exception cref="AcUtilsException">caught and [logged](@ref AcUtils#AcDebug#initAcLogging)
        /// in <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on \c unlock command failure.</exception>
        /*! \unlock_ <tt>unlock [-kf | -kt] \<stream\></tt> */
        public async Task <bool> unlockAsync(string stream, LockKind kind = LockKind.all)
        {
            bool ret = false; // assume failure

            try
            {
                string cmd = null;
                if (kind == LockKind.from)
                {
                    cmd = $@"unlock -kf ""{stream}""";
                }
                else if (kind == LockKind.to)
                {
                    cmd = $@"unlock -kt ""{stream}""";
                }
                else if (kind == LockKind.all)
                {
                    cmd = $@"unlock ""{stream}""";
                }

                AcResult r = await AcCommand.runAsync(cmd).ConfigureAwait(false);

                ret = (r != null && r.RetVal == 0);
            }

            catch (AcUtilsException ecx)
            {
                AcDebug.Log($"AcUtilsException in AcLocks.unlockAsync caught and logged.{Environment.NewLine}{ecx.Message}");
            }

            return(ret);
        }
Example #2
0
        public static Task <T> Lock <T>(
            string userId,
            Func <long, Task <T> > code,
            LockKind lockKind)
        {
            var nonce = Pool.GetOrAdd(userId, _ => new EpochNonce());

            return(nonce.Lock(code, lockKind));
        }
Example #3
0
        /// <summary>
        /// Put a \c lock on \e stream as per lock \e kind.
        /// </summary>
        /// <param name="stream">Name of stream or workspace to lock.</param>
        /// <param name="comment">Comment to be used for the lock.</param>
        /// <param name="kind">Type of lock to apply: \e to, \e from, or \e all.</param>
        /// <param name="prncpl">AccuRev principal name of user or group in the case of \e to or \e from lock.</param>
        /// <param name="onlyexcept">Apply \c lock to \e prncpl only or to all except \e prncpl.</param>
        /// <returns>\e true if operation succeeded, \e false otherwise.</returns>
        /// <exception cref="AcUtilsException">caught and [logged](@ref AcUtils#AcDebug#initAcLogging)
        /// in <tt>\%LOCALAPPDATA\%\\AcTools\\Logs\\<prog_name\>-YYYY-MM-DD.log</tt> on \c lock command failure.</exception>
        /*! \lock_ <tt>lock -c \<comment\> [-kf [-e|-o prncpl] | -kt [-e|-o prncpl]] \<stream\></tt> */
        /*! \accunote_ The CLI \c lock command can be used on a workspace stream but the same cannot be done using the AccuRev GUI client. AccuRev defect 23850. */
        public async Task <bool> lockAsync(string stream, string comment, LockKind kind = LockKind.all, AcPrincipal prncpl = null, OnlyExcept onlyexcept = OnlyExcept.Except)
        {
            bool ret = false; // assume failure

            try
            {
                string cmd = null;
                if (kind == LockKind.from)
                {
                    if (prncpl != null)
                    {
                        cmd = $@"lock -c ""{comment}"" -kf {((onlyexcept == OnlyExcept.Except) ? "-e" : "-o")} ""{prncpl}"" ""{stream}""";
                    }
                    else
                    {
                        cmd = $@"lock -c ""{comment}"" -kf ""{stream}""";  // lock 'from' for all
                    }
                }
                else if (kind == LockKind.to)
                {
                    if (prncpl != null)
                    {
                        cmd = $@"lock -c ""{comment}"" -kt {((onlyexcept == OnlyExcept.Except) ? "-e" : "-o")} ""{prncpl}"" ""{stream}""";
                    }
                    else
                    {
                        cmd = $@"lock -c ""{comment}"" -kt ""{stream}"""; // lock 'to' for all
                    }
                }
                else if (kind == LockKind.all)
                {
                    cmd = $@"lock -c ""{comment}"" ""{stream}"""; // lock 'to and from' for all
                }
                AcResult r = await AcCommand.runAsync(cmd).ConfigureAwait(false);

                ret = (r != null && r.RetVal == 0);
            }

            catch (AcUtilsException ecx)
            {
                AcDebug.Log($"AcUtilsException in AcLocks.lockAsync caught and logged.{Environment.NewLine}{ecx.Message}");
            }

            return(ret);
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileLockable"></param>
        /// <param name="lockKind"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="CannotAcquireLockException"></exception>
        private static Guid LockTheFile(FileLockable fileLockable, LockKind lockKind)
        {
            Guid newLockId;

            if (lockKind == LockKind.Read)
            {
                newLockId = fileLockable.LockForReading();
            }
            else if (lockKind == LockKind.Write)
            {
                newLockId = fileLockable.LockForWriting();
            }
            else
            {
                throw new ArgumentException("Вид блокировки {0} не поддерживается.".FormatWith(lockKind.ToString()));
            }

            return(newLockId);
        }
Example #5
0
        private async Task <T> Lock <T>(
            Func <long, Task <T> > code,
            LockKind lockKind)
        {
            await _nonceLock.WaitAsync();

            try
            {
                long epoch;

                switch (lockKind)
                {
                case LockKind.Epoch:
                    epoch = DateTime.UtcNow.Epoch();
                    break;

                case LockKind.EpochMilliseconds:
                    epoch = DateTime.UtcNow.EpochMilliseconds();
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(lockKind), lockKind, null);
                }


                if (epoch > _current)
                {
                    _current = epoch;
                }
                else
                {
                    _current++;
                }

                return(await code(_current));
            }
            finally
            {
                _nonceLock.Release();
            }
        }
Example #6
0
            public Locker(ReaderWriterLockSlim locker, LockKind kind)
            {
                _locker = locker;
                _kind   = kind;
                switch (_kind)
                {
                case LockKind.Read:
                    locker.EnterReadLock();
                    break;

                case LockKind.Write:
                    locker.EnterWriteLock();
                    break;

                case LockKind.UpgradeableRead:
                    locker.EnterUpgradeableReadLock();
                    break;

                default:
                    break;
                }
            }
 public Guid AcquireLock(NodeWithSurroundingsResolvingResult <FileNode> fileToLock, LockKind lockKind)
 {
     return(Guid.NewGuid());
 }
Example #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileToLock"></param>
        /// <param name="lockKind"></param>
        /// <returns></returns>
        /// <exception cref="CannotAcquireLockException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public Guid AcquireLock(NodeWithSurroundingsResolvingResult <FileNode> fileToLock, LockKind lockKind)
        {
            if (fileToLock == null)
            {
                throw new ArgumentNullException("fileToLock");
            }

            lock (_operationExecutionCriticalSection)
            {
                var          idOfFileToLock = fileToLock.ResolvedNode.Id;
                FileLockable fileLockable;
                Guid         newLockId;

                if (_fileIdsToLocks.ContainsKey(idOfFileToLock))
                {
                    fileLockable = _fileIdsToLocks[idOfFileToLock];
                }
                else
                {
                    fileLockable = new FileLockable(fileToLock.ResolvedNode, fileToLock.FoldersPassedWhileResolving);
                    _fileIdsToLocks[idOfFileToLock] = fileLockable;
                }

                newLockId = LockTheFile(fileLockable, lockKind);

                ReportToAllFolderNodesLockableThatTheyHaveANewLock(fileToLock, fileLockable, newLockId);

                _lockIdsToLocks[newLockId] = fileLockable;

                return(newLockId);
            }
        }