Example #1
0
        public bool GetLatestVersion(string vcsPath, string localProjectPath, bool buildTree, object option, bool recursive, bool makeWritable, ReplaceWriteable replaceWriteableState, FileTime fileTimeState, VcsClientBatchState batchState)
        {
            CleanUpDirectory(localProjectPath);

            Queue <Exception> exceptionQueue = new Queue <Exception>();

            vcsPath          = HelperPaths.RemoveFinishSlash(vcsPath);
            localProjectPath = HelperPaths.RemoveFinishSlash(localProjectPath);
            AutoResetEvent getDataEvent    = new AutoResetEvent(true);
            AutoResetEvent decompressEvent = new AutoResetEvent(true);
            AutoResetEvent saveEvent       = new AutoResetEvent(true);
            AutoResetEvent getBlockEvent   = new AutoResetEvent(false);
            int            magicCount;
            string         id = Service.Get(Environment.MachineName, vcsPath, option, recursive, out magicCount);

            AccessDeniedInfo[] accessList = Service.TakeAccessInfo(id);
            if (accessList != null)
            {
                string path = vcsPath.TrimEnd('/');
                foreach (AccessDeniedInfo accessInfo in accessList)
                {
                    string message = string.Format("Resources.NoGetProjectPermissions", DateTime.Now.ToString(), path, accessInfo.ObjectName);
                }
            }
            Dictionary <string, string> localPathDict = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase);
            bool      cancel    = false;
            int       lastMagic = 0;
            BlockInfo blockInfo = new BlockInfo();

            QueueBlock(id, getBlockEvent, blockInfo, exceptionQueue);
            List <string> pathList = new List <string>();

            try {
                while (true)
                {
                    getBlockEvent.WaitOne();
                    ProcessException(exceptionQueue);
                    if (!blockInfo.Last)
                    {
                        break;
                    }
                    int              magicPos   = blockInfo.MagicPos;
                    int              blockIndex = blockInfo.BlockIndex;
                    int[]            paths      = blockInfo.Paths;
                    FileStateInfo2[] info       = blockInfo.Info;
                    QueueBlock(id, getBlockEvent, blockInfo, exceptionQueue);
                    if (paths != null && blockIndex >= 0)
                    {
                        bool[]     restData       = new bool[paths.Length];
                        string[]   localPath      = new string[paths.Length];
                        string[]   path           = new string[paths.Length];
                        DateTime[] fileTime       = new DateTime[paths.Length];
                        bool[]     setNormal      = new bool[paths.Length];
                        bool[]     fileExists     = new bool[paths.Length];
                        bool[]     doCheckOut     = new bool[paths.Length];
                        bool       doGetData      = false;
                        bool       wasCheckOut    = false;
                        double     step           = ((double)magicPos - lastMagic) / paths.Length;
                        string     curProject     = string.Empty;
                        DateTime   curProjectDate = DateTime.MinValue;
                        for (int i = 0; i < paths.Length; i++)
                        {
                            if (info[i].IsNull)
                            {
                                string curPostProject = HelperPaths.RemoveFinishSlash(info[i].Name);
                                pathList.Add(curPostProject);
                                if (!string.IsNullOrEmpty(curPostProject))
                                {
                                    curProject = vcsPath + "/" + curPostProject;
                                }
                                else
                                {
                                    curProject = vcsPath;
                                }
                                string curLocalFolder;
                                if (buildTree)
                                {
                                    curLocalFolder = localProjectPath + @"\" + curPostProject.Replace('/', '\\');
                                }
                                else
                                {
                                    curLocalFolder = info[i].CheckOutFolder; //Hack ;)
                                }
                                curLocalFolder = HelperPaths.RemoveFinishSlash(curLocalFolder);
                                CreateDirectory(curLocalFolder);
                                localPathDict.Add(curProject, curLocalFolder);
                            }
                            else
                            {
                                string curPostProject = pathList[paths[i]];
                                string nextProject    = HelperPaths.Combine(vcsPath, curPostProject);
                                if (nextProject != curProject)
                                {
                                    curProject     = nextProject;
                                    curProjectDate = DateTime.MinValue;
                                }
                                string curLocalFolder;
                                if (localPathDict.TryGetValue(curProject, out curLocalFolder))
                                {
                                    string curFileName  = info[i].Name;
                                    string curLocalPath = curLocalFolder + @"\" + curFileName;
                                    string curPath      = curProject + "/" + curFileName;
                                    try {
                                        FileAttributes fa;
                                        DateTime       fileModification;
                                        if (fileSystem.GetAttributes(curLocalPath, out fa, out fileModification))
                                        {
                                            fileExists[i] = true;
                                            FileBaseInfoState fileState;
                                            bool needWrite = GetLocalFileModified(info[i], curLocalPath, curPath, curProjectDate, out fileState, fileModification);
                                            if (!needWrite && fileState == FileBaseInfoState.Locked)
                                            {
                                                continue;
                                            }
                                            if ((fa & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                                            {
                                                if (needWrite)
                                                {
                                                    doGetData    = true;
                                                    restData[i]  = true;
                                                    localPath[i] = curLocalPath;
                                                    path[i]      = curPath;
                                                    fileTime[i]  = SelectFileTime(info[i], fileTimeState);
                                                }
                                                continue;
                                            }
                                            if ((makeWritable) && !needWrite)
                                            {
                                                continue;
                                            }
                                            if (batchState.ActionToExistFile.Action == ActionWithCopy.Replace)
                                            {
                                                doGetData    = true;
                                                restData[i]  = needWrite;
                                                localPath[i] = curLocalPath;
                                                path[i]      = curPath;
                                                fileTime[i]  = SelectFileTime(info[i], fileTimeState);
                                                continue;
                                            }
                                            if (batchState.ActionToExistFile.Action == ActionWithCopy.Leave)
                                            {
                                                continue;
                                            }
                                            if (batchState.ActionToExistFile.Action == ActionWithCopy.CheckOut)
                                            {
                                                localPath[i]  = curLocalPath;
                                                path[i]       = curPath;
                                                doCheckOut[i] = true;
                                                wasCheckOut   = true;
                                                setNormal[i]  = true;
                                            }
                                        }
                                        else
                                        if (fileSystem.DirectoryExists(curLocalFolder))
                                        {
                                            doGetData    = true;
                                            restData[i]  = true;
                                            localPath[i] = curLocalPath;
                                            path[i]      = curPath;
                                            bool differentHost;
                                            setNormal[i] = (makeWritable || info[i].CheckedOut) && info[i].CheckedOutMe && EqualsCheckOutParams(info[i], curLocalPath, out differentHost);
                                            fileTime[i]  = SelectFileTime(info[i], fileTimeState);
                                        }
                                    }
                                    finally {
                                        if (makeWritable)
                                        {
                                            doGetData    = true;
                                            localPath[i] = curLocalPath;
                                            setNormal[i] = true;
                                        }
                                    }
                                }
                            }
                        }
                        if (doGetData)
                        {
                            Service.GetBlockDataReq(id, blockIndex, restData);
                            getDataEvent.WaitOne();
                            getDataEvent.Set();
                            ProcessException(exceptionQueue);
                            getDataEvent.Reset();
                            QueueAll(id, blockIndex, getDataEvent, decompressEvent, saveEvent, localPath, path, fileTime, doCheckOut, setNormal, fileExists, exceptionQueue, step);
                        }
                        else
                        {
                            getDataEvent.WaitOne();
                            ProcessException(exceptionQueue);
                            getDataEvent.Set();
                        }
                        if (wasCheckOut)
                        {
                            List <string> pathToCheckOut      = new List <string>();
                            List <string> localPathToCheckOut = new List <string>();
                            List <string> commentToCheckOut   = new List <string>();
                            for (int i = 0; i < path.Length; i++)
                            {
                                if (doCheckOut[i])
                                {
                                    pathToCheckOut.Add(path[i]);
                                    localPathToCheckOut.Add(HelperPaths.GetDirectory(localPath[i]));
                                    commentToCheckOut.Add(string.Empty);
                                }
                            }
                            string checkOutId = Service.CheckOut(System.Environment.MachineName, pathToCheckOut.ToArray(), localPathToCheckOut.ToArray(), commentToCheckOut.ToArray(), null);
                            Dictionary <int, AccessDeniedInfo> accessDict = CreateAccessDict(Service.TakeAccessInfo(checkOutId));
                            for (int i = 0; i < localPathToCheckOut.Count; i++)
                            {
                                string local = HelperPaths.Combine(localPathToCheckOut[i], HelperPaths.GetFile(pathToCheckOut[i]));
                                if (accessDict == null || !accessDict.ContainsKey(i))
                                {
                                    fileSystem.SetAttributes(local, FileAttributes.Normal);
                                }
                                else
                                {
                                    string message = string.Format("Resources.NoCheckOutFilePermissions", DateTime.Now, pathToCheckOut[i]);
                                }
                            }
                        }
                        lastMagic = magicPos;
                    }
                }
            }
            finally {
                getDataEvent.WaitOne();
                decompressEvent.WaitOne();
                saveEvent.WaitOne();
                if (!cancel)
                {
                    Service.ConfirmGetEnd(id);
                }
            }
            return(true);
        }
Example #2
0
        public bool GetLatestVersion(string vcsPath, string localProjectPath, bool buildTree, object option, bool recursive, bool makeWritable, ReplaceWriteable replaceWriteableState, FileTime fileTimeState, VcsClientBatchState batchState)
        {
            CleanUpDirectory(localProjectPath);

            Queue<Exception> exceptionQueue = new Queue<Exception>();
            vcsPath = HelperPaths.RemoveFinishSlash(vcsPath);
            localProjectPath = HelperPaths.RemoveFinishSlash(localProjectPath);
            AutoResetEvent getDataEvent = new AutoResetEvent(true);
            AutoResetEvent decompressEvent = new AutoResetEvent(true);
            AutoResetEvent saveEvent = new AutoResetEvent(true);
            AutoResetEvent getBlockEvent = new AutoResetEvent(false);
            int magicCount;
            string id = Service.Get(Environment.MachineName, vcsPath, option, recursive, out magicCount);
            AccessDeniedInfo[] accessList = Service.TakeAccessInfo(id);
            if (accessList != null) {
                string path = vcsPath.TrimEnd('/');
                foreach (AccessDeniedInfo accessInfo in accessList) {
                    string message = string.Format("Resources.NoGetProjectPermissions", DateTime.Now.ToString(), path, accessInfo.ObjectName);
                }
            }
            Dictionary<string, string> localPathDict = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
            bool cancel = false;
            int lastMagic = 0;
            BlockInfo blockInfo = new BlockInfo();
            QueueBlock(id, getBlockEvent, blockInfo, exceptionQueue);
            List<string> pathList = new List<string>();
            try {
                while (true) {
                    getBlockEvent.WaitOne();
                    ProcessException(exceptionQueue);
                    if (!blockInfo.Last)
                        break;
                    int magicPos = blockInfo.MagicPos;
                    int blockIndex = blockInfo.BlockIndex;
                    int[] paths = blockInfo.Paths;
                    FileStateInfo2[] info = blockInfo.Info;
                    QueueBlock(id, getBlockEvent, blockInfo, exceptionQueue);
                    if (paths != null && blockIndex >= 0) {
                        bool[] restData = new bool[paths.Length];
                        string[] localPath = new string[paths.Length];
                        string[] path = new string[paths.Length];
                        DateTime[] fileTime = new DateTime[paths.Length];
                        bool[] setNormal = new bool[paths.Length];
                        bool[] fileExists = new bool[paths.Length];
                        bool[] doCheckOut = new bool[paths.Length];
                        bool doGetData = false;
                        bool wasCheckOut = false;
                        double step = ((double)magicPos - lastMagic) / paths.Length;
                        string curProject = string.Empty;
                        DateTime curProjectDate = DateTime.MinValue;
                        for (int i = 0; i < paths.Length; i++) {
                            if (info[i].IsNull) {
                                string curPostProject = HelperPaths.RemoveFinishSlash(info[i].Name);
                                pathList.Add(curPostProject);
                                if (!string.IsNullOrEmpty(curPostProject)) {
                                    curProject = vcsPath + "/" + curPostProject;
                                }
                                else {
                                    curProject = vcsPath;
                                }
                                string curLocalFolder;
                                if (buildTree) {
                                    curLocalFolder = localProjectPath + @"\" + curPostProject.Replace('/', '\\');
                                }
                                else {
                                    curLocalFolder = info[i].CheckOutFolder; //Hack ;)
                                }
                                curLocalFolder = HelperPaths.RemoveFinishSlash(curLocalFolder);
                                CreateDirectory(curLocalFolder);
                                localPathDict.Add(curProject, curLocalFolder);
                            }
                            else {
                                string curPostProject = pathList[paths[i]];
                                string nextProject = HelperPaths.Combine(vcsPath, curPostProject);
                                if (nextProject != curProject) {
                                    curProject = nextProject;
                                    curProjectDate = DateTime.MinValue;
                                }
                                string curLocalFolder;
                                if (localPathDict.TryGetValue(curProject, out curLocalFolder)) {
                                    string curFileName = info[i].Name;
                                    string curLocalPath = curLocalFolder + @"\" + curFileName;
                                    string curPath = curProject + "/" + curFileName;
                                    try {
                                        FileAttributes fa;
                                        DateTime fileModification;
                                        if (fileSystem.GetAttributes(curLocalPath, out fa, out fileModification)) {
                                            fileExists[i] = true;
                                            FileBaseInfoState fileState;
                                            bool needWrite = GetLocalFileModified(info[i], curLocalPath, curPath, curProjectDate, out fileState, fileModification);
                                            if (!needWrite && fileState == FileBaseInfoState.Locked) {
                                                continue;
                                            }
                                            if ((fa & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) {
                                                if (needWrite) {
                                                    doGetData = true;
                                                    restData[i] = true;
                                                    localPath[i] = curLocalPath;
                                                    path[i] = curPath;
                                                    fileTime[i] = SelectFileTime(info[i], fileTimeState);
                                                }
                                                continue;
                                            }
                                            if ((makeWritable) && !needWrite)
                                                continue;
                                            if (batchState.ActionToExistFile.Action == ActionWithCopy.Replace) {
                                                doGetData = true;
                                                restData[i] = needWrite;
                                                localPath[i] = curLocalPath;
                                                path[i] = curPath;
                                                fileTime[i] = SelectFileTime(info[i], fileTimeState);
                                                continue;
                                            }
                                            if (batchState.ActionToExistFile.Action == ActionWithCopy.Leave) { continue; }
                                            if (batchState.ActionToExistFile.Action == ActionWithCopy.CheckOut) {
                                                localPath[i] = curLocalPath;
                                                path[i] = curPath;
                                                doCheckOut[i] = true;
                                                wasCheckOut = true;
                                                setNormal[i] = true;
                                            }
                                        }
                                        else
                                            if (fileSystem.DirectoryExists(curLocalFolder)) {
                                            doGetData = true;
                                            restData[i] = true;
                                            localPath[i] = curLocalPath;
                                            path[i] = curPath;
                                            bool differentHost;
                                            setNormal[i] = (makeWritable || info[i].CheckedOut) && info[i].CheckedOutMe && EqualsCheckOutParams(info[i], curLocalPath, out differentHost);
                                            fileTime[i] = SelectFileTime(info[i], fileTimeState);
                                        }
                                    }
                                    finally {
                                        if (makeWritable) {
                                            doGetData = true;
                                            localPath[i] = curLocalPath;
                                            setNormal[i] = true;
                                        }
                                    }
                                }
                            }
                        }
                        if (doGetData) {
                            Service.GetBlockDataReq(id, blockIndex, restData);
                            getDataEvent.WaitOne();
                            getDataEvent.Set();
                            ProcessException(exceptionQueue);
                            getDataEvent.Reset();
                            QueueAll(id, blockIndex, getDataEvent, decompressEvent, saveEvent, localPath, path, fileTime, doCheckOut, setNormal, fileExists, exceptionQueue, step);
                        }
                        else {
                            getDataEvent.WaitOne();
                            ProcessException(exceptionQueue);
                            getDataEvent.Set();
                        }
                        if (wasCheckOut) {
                            List<string> pathToCheckOut = new List<string>();
                            List<string> localPathToCheckOut = new List<string>();
                            List<string> commentToCheckOut = new List<string>();
                            for (int i = 0; i < path.Length; i++) {
                                if (doCheckOut[i]) {
                                    pathToCheckOut.Add(path[i]);
                                    localPathToCheckOut.Add(HelperPaths.GetDirectory(localPath[i]));
                                    commentToCheckOut.Add(string.Empty);
                                }
                            }
                            string checkOutId = Service.CheckOut(System.Environment.MachineName, pathToCheckOut.ToArray(), localPathToCheckOut.ToArray(), commentToCheckOut.ToArray(), null);
                            Dictionary<int, AccessDeniedInfo> accessDict = CreateAccessDict(Service.TakeAccessInfo(checkOutId));
                            for (int i = 0; i < localPathToCheckOut.Count; i++) {
                                string local = HelperPaths.Combine(localPathToCheckOut[i], HelperPaths.GetFile(pathToCheckOut[i]));
                                if (accessDict == null || !accessDict.ContainsKey(i)) {
                                    fileSystem.SetAttributes(local, FileAttributes.Normal);
                                }
                                else {
                                    string message = string.Format("Resources.NoCheckOutFilePermissions", DateTime.Now, pathToCheckOut[i]);
                                }
                            }
                        }
                        lastMagic = magicPos;
                    }
                }
            }
            finally {
                getDataEvent.WaitOne();
                decompressEvent.WaitOne();
                saveEvent.WaitOne();
                if (!cancel)
                    Service.ConfirmGetEnd(id);
            }
            return true;
        }