Esempio n. 1
0
        /// <summary>
        /// Execute a sequence of dirMv operations. if any fail, it will undo all operations executed.
        /// This command will fail on the follow situations:
        /// (1) If the lenght of the source or destination array do not match
        /// (2) If any of the sources are repeated
        /// (3) If any of the sources matches with any of the destinations
        ///
        /// </summary>
        /// <param name="sources"></param>
        /// <param name="destinations"></param>
        /// <param name="showUi"></param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public static bool stackMv(string[] sources, string[] destinations, bool showUi,
                                   LogMethod logMethod, out string errMsg, out string errSrcDir,
                                   out string errDstDir, out bool opWasCancelled)
        {
            bool opWasCancelledTemp = false;
            bool exceptionError     = false;
            bool retVal             = true;

            opWasCancelled     = false;
            CSharp.stlogMethod = logMethod;
            if (sources == null || destinations == null)
            {
                errMsg    = "NULL VECTOR ERROR";
                errSrcDir = "";
                errDstDir = "";
                return(false);
            }
            if (sources.Length != destinations.Length)
            {
                errMsg = "SOURCE and DESTINATION stack sizes do not match! src:" + sources.Length +
                         ", dst:" + destinations.Length;
                errSrcDir = "";
                errDstDir = "";
                return(false);
            }
            // before moving, check if the src and dst exist and dont match
            for (int i = 0; i < sources.Length; i++)
            {
                string currentItem = sources[i];
                //-- check if there is any repeated element on the src list of elements
                for (int j = 0; j < sources.Length; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    else
                    {
                        // if two elements are equal
                        if ((currentItem.Trim() == sources[j].Trim()))
                        {
                            errMsg = "Error: item[" + i + "] == item[" + j + "] " +
                                     currentItem + "::" + sources[j];
                            errDstDir = "";
                            errSrcDir = currentItem;
                            log.Warn(errMsg);
                            return(false);
                        }
                    }
                }
                //-- check if there is any element on the sources list exit on the destination list
                for (int j = 0; j < destinations.Length; j++)
                {
                    if (currentItem.Trim() == destinations[j].Trim())
                    {
                        errMsg = "Error: item[" + i + "] == item[" + j + "] " +
                                 currentItem + "::" + sources[j];
                        errDstDir = "";
                        errSrcDir = currentItem;
                        log.Warn(errMsg);
                        return(false);
                    }
                }
            }
            //-- check if all src and dst dirs do exit
            foreach (var item in sources)
            {
                if (!Directory.Exists(item))
                {
                    errMsg = "Source directory does not exit {" +
                             item + "}";
                    errDstDir = "";
                    errSrcDir = item;
                    log.Warn("ERROR: " + errMsg);
                    return(false);
                }
            }
            foreach (var item in destinations)
            {
                if (!Directory.Exists(item))
                {
                    errMsg = "Destination directory does not exit {" +
                             item + "}";
                    errDstDir = item;
                    errSrcDir = "";
                    log.Warn("ERROR: " + errMsg);
                    return(false);
                }
            }
            // Move the directories
            Stack <string> dirStackSrc = new Stack <string>();
            Stack <string> dirStackDst = new Stack <string>();

            errMsg = "";
            for (int i = 0; i < sources.Length; i++)
            {
                try
                {
                    if (!sources[i].Trim().Equals("") && !destinations[i].Trim().Equals(""))
                    {
                        CSharp.dirMv(sources[i], destinations[i], showUi);
                        dirStackSrc.Push(sources[i]);
                        dirStackDst.Push(destinations[i]);
                    }
                }
                catch (OperationCanceledException oce)
                {
                    //retVal = true;
                    errMsg         = oce.Message;
                    opWasCancelled = true;
                    exceptionError = true;
                }
                catch (Exception ex)
                {
                    retVal         = false;
                    errMsg         = ex.Message;
                    opWasCancelled = false;
                    exceptionError = true;
                }
                if (exceptionError)
                {
                    while (dirStackSrc.Count > 0)
                    {
                        bool retUndo = undoMv(dirStackSrc.Pop(),
                                              dirStackDst.Pop(),
                                              showUi,
                                              out errSrcDir,
                                              out errDstDir,
                                              out errMsg,
                                              out opWasCancelledTemp);
                    }
                    errSrcDir = sources[i];
                    errDstDir = destinations[i];
                    // if OperationCanceledException or sucess return true
                    // other exception return false
                    return(false);
                }
            }
            errSrcDir = "";
            errDstDir = "";
            errMsg    = "SUCCESS!";
            return(true);
        }
Esempio n. 2
0
        public static Err safeMove(string sourceDirName, string destDirName, LogMethod logMethod)
        {
            sourceDirName = CSharp.cleanPath(sourceDirName);
            destDirName   = CSharp.cleanPath(destDirName);
            try
            {
                Directory.Move(sourceDirName, destDirName);
                return(Err.SUCCESS);
            }
            catch (PathTooLongException ex)
            {
                if (logMethod == LogMethod.LOGGER)
                {
                    log.Warn("The specified path, file name, or both exceed the system-defined maximum length.");
                    log.Warn("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace);
                }
                else if (logMethod == LogMethod.CONSOLE)
                {
                    Console.WriteLine("The specified path, file name, or both exceed the system-defined maximum length.");
                    Console.WriteLine("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace);
                }
                return(Err.PATH_TOO_LONG);
            }
            catch (DirectoryNotFoundException ex)
            {
                if (logMethod == LogMethod.LOGGER)
                {
                    log.Warn("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace);
                    log.Warn("The path specified by sourceDirName is invalid (for example, it is on an unmapped drive).");
                }
                else if (logMethod == LogMethod.CONSOLE)
                {
                    Console.WriteLine("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace);
                    Console.WriteLine("The path specified by sourceDirName is invalid (for example, it is on an unmapped drive).");
                }

                return(Err.DIRECTORY_NOT_FOUND);
            }
            catch (IOException ex)
            {
                if (logMethod == LogMethod.LOGGER)
                {
                    log.Warn("An attempt was made to move a directory to a different volume. - or - " +
                             "destDirName already exists. See the Note in the Remarks section. - or - " +
                             "The sourceDirName and destDirName parameters refer to the same file or directory. - or - " +
                             "The directory or a file within it is being used by another process.");
                    log.Warn("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace);
                }
                else if (logMethod == LogMethod.CONSOLE)
                {
                    Console.WriteLine("An attempt was made to move a directory to a different volume. - or - " +
                                      "destDirName already exists. See the Note in the Remarks section. - or - " +
                                      "The sourceDirName and destDirName parameters refer to the same file or directory. - or - " +
                                      "The directory or a file within it is being used by another process.");
                    Console.WriteLine("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace);
                }

                return(Err.IO_EXCEPTION);
            }
            catch (UnauthorizedAccessException ex)
            {
                if (logMethod == LogMethod.LOGGER)
                {
                    log.Warn("The caller does not have the required permission.");
                    log.Warn("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace);
                }
                else if (logMethod == LogMethod.CONSOLE)
                {
                    Console.WriteLine("The caller does not have the required permission.");
                    Console.WriteLine("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace);
                }
                return(Err.UNAUTHORIZED_ACCESS);
            }
            catch (ArgumentNullException ex)
            {
                if (logMethod == LogMethod.LOGGER)
                {
                    log.Warn("sourceDirName or destDirName is null.");
                    log.Warn("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace);
                }
                else if (logMethod == LogMethod.CONSOLE)
                {
                    Console.WriteLine("sourceDirName or destDirName is null.");
                    Console.WriteLine("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace);
                }
                return(Err.ARGUMENT_NULL);
            }
            catch (ArgumentException ex)
            {
                if (logMethod == LogMethod.LOGGER)
                {
                    log.Warn("sourceDirName or destDirName is a zero-length string, contains only white space, " +
                             "or contains one or more invalid characters. You can query for invalid characters with " +
                             " the GetInvalidPathChars() method.");
                    log.Warn("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace);
                }
                else if (logMethod == LogMethod.CONSOLE)
                {
                    Console.WriteLine("sourceDirName or destDirName is a zero-length string, contains only white space, " +
                                      "or contains one or more invalid characters. You can query for invalid characters with " +
                                      " the GetInvalidPathChars() method.");
                    Console.WriteLine("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace);
                }
                return(Err.ARGUMENT_EXCEPTION);
            }
            catch (Exception ex)
            {
                if (logMethod == LogMethod.LOGGER)
                {
                    log.Warn("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace);
                }
                else if (logMethod == LogMethod.CONSOLE)
                {
                    Console.WriteLine("** Message:" + ex.Message + ", StackTrace:" + ex.StackTrace);
                }
            }
            return(Err.ERR_UNKNOWN);
        }