CreatePathFromString() 공개 정적인 메소드

Return an IActivityIOPath based upont the path string
public static CreatePathFromString ( string path ) : IActivityIOPath
path string
리턴 IActivityIOPath
        public int Put(Stream src, IActivityIOPath dst, Dev2CRUDOperationTO args, string whereToPut, List <string> filesToCleanup)
        {
            int result = -1;

            using (src)
            {
                //2013.05.29: Ashley Lewis for bug 9507 - default destination to source directory when destination is left blank or if it is not a rooted path
                if (!Path.IsPathRooted(dst.Path))
                {
                    //get just the directory path to put into
                    if (whereToPut != null)
                    {
                        //Make the destination directory equal to that directory
                        dst = ActivityIOFactory.CreatePathFromString(whereToPut + "\\" + dst.Path, dst.Username, dst.Password);
                    }
                }

                if ((args.Overwrite) || (!args.Overwrite && !FileExist(dst)))
                {
                    if (!RequiresAuth(dst))
                    {
                        using (src)
                        {
                            File.WriteAllBytes(dst.Path, src.ToByteArray());
                            result = (int)src.Length;
                        }
                    }
                    else
                    {
                        // handle UNC path
                        SafeTokenHandle safeTokenHandle;
                        bool            loginOk = LogonUser(ExtractUserName(dst), ExtractDomain(dst), dst.Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeTokenHandle);


                        if (loginOk)
                        {
                            using (safeTokenHandle)
                            {
                                WindowsIdentity newID = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());
                                using (WindowsImpersonationContext impersonatedUser = newID.Impersonate())
                                {
                                    // Do the operation here

                                    using (src)
                                    {
                                        File.WriteAllBytes(dst.Path, src.ToByteArray());
                                        result = (int)src.Length;
                                    }


                                    // remove impersonation now
                                    impersonatedUser.Undo();
                                }
                            }
                        }
                        else
                        {
                            // login failed
                            throw new Exception("Failed to authenticate with user [ " + dst.Username + " ] for resource [ " + dst.Path + " ] ");
                        }
                    }
                }
            }

            return(result);
        }
        private IList <IActivityIOPath> ListDirectoriesAccordingToType(IActivityIOPath src, ReadTypes type)
        {
            IList <IActivityIOPath> result = new List <IActivityIOPath>();

            string path = src.Path;

            if (!path.EndsWith("\\") && PathIs(src) == enPathType.Directory)
            {
                path += "\\";
            }

            if (!RequiresAuth(src))
            {
                try
                {
                    IEnumerable <string> dirs;

                    if (!Dev2ActivityIOPathUtils.IsStarWildCard(path))
                    {
                        if (Directory.Exists(path))
                        {
                            dirs = GetDirectoriesForType(path, string.Empty, type);
                        }
                        else
                        {
                            throw new Exception("The Directory does not exist.");
                        }
                    }
                    else
                    {
                        // we have a wild-char path ;)
                        string baseDir = Dev2ActivityIOPathUtils.ExtractFullDirectoryPath(path);
                        string pattern = Dev2ActivityIOPathUtils.ExtractFileName(path);

                        dirs = GetDirectoriesForType(baseDir, pattern, type);
                    }

                    if (dirs != null)
                    {
                        foreach (string d in dirs)
                        {
                            result.Add(ActivityIOFactory.CreatePathFromString(d, src.Username, src.Password, true));
                        }
                    }
                }
                catch (Exception)
                {
                    throw new Exception("Directory not found [ " + src.Path + " ] ");
                }
            }
            else
            {
                try
                {
                    // handle UNC path
                    SafeTokenHandle safeTokenHandle;
                    bool            loginOk = LogonUser(ExtractUserName(src), ExtractDomain(src), src.Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeTokenHandle);

                    if (loginOk)
                    {
                        using (safeTokenHandle)
                        {
                            WindowsIdentity newID = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());
                            using (WindowsImpersonationContext impersonatedUser = newID.Impersonate())
                            {
                                // Do the operation here

                                try
                                {
                                    IEnumerable <string> dirs;

                                    if (!Dev2ActivityIOPathUtils.IsStarWildCard(path))
                                    {
                                        dirs = GetDirectoriesForType(path, string.Empty, type);
                                    }
                                    else
                                    {
                                        // we have a wild-char path ;)
                                        string baseDir = Dev2ActivityIOPathUtils.ExtractFullDirectoryPath(path);
                                        string pattern = Dev2ActivityIOPathUtils.ExtractFileName(path);

                                        dirs = GetDirectoriesForType(baseDir, pattern, type);
                                    }

                                    if (dirs != null)
                                    {
                                        foreach (string d in dirs)
                                        {
                                            result.Add(ActivityIOFactory.CreatePathFromString(d, src.Username, src.Password));
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    throw new Exception("Directory not found [ " + src.Path + " ] ");
                                }

                                // remove impersonation now
                                impersonatedUser.Undo();
                                newID.Dispose();
                            }
                        }
                    }
                    else
                    {
                        // login failed
                        throw new Exception("Failed to authenticate with user [ " + src.Username + " ] for resource [ " + src.Path + " ] ");
                    }
                }
                catch (Exception ex)
                {
                    Dev2Logger.Log.Error(ex);
                    throw;
                }
            }

            return(result);
        }
예제 #3
0
        string CreateEndPoint(IActivityIOOperationsEndPoint dst, IDev2CRUDOperationTO args, bool createToFile)
        {
            var result         = ResultOk;
            var activityIOPath = dst.IOPath;
            var dirParts       = MakeDirectoryParts(activityIOPath, dst.PathSeperator());

            var deepestIndex = -1;
            var startDepth   = dirParts.Count - 1;

            var pos = startDepth;

            while (pos >= 0 && deepestIndex == -1)
            {
                var tmpPath = ActivityIOFactory.CreatePathFromString(dirParts[pos], activityIOPath.Username,
                                                                     activityIOPath.Password, true, activityIOPath.PrivateKeyFile);
                try
                {
                    if (dst.ListDirectory(tmpPath) != null)
                    {
                        deepestIndex = pos;
                    }
                }
                catch (Exception e)
                {
                    Dev2Logger.Warn(e.Message, "Warewolf Warn");
                }
                finally
                {
                    pos--;
                }
            }

            pos = deepestIndex + 1;
            var ok = true;

            var origPath = dst.IOPath;

            while (pos <= startDepth && ok)
            {
                var toCreate = ActivityIOFactory.CreatePathFromString(dirParts[pos], dst.IOPath.Username,
                                                                      dst.IOPath.Password, true, dst.IOPath.PrivateKeyFile);
                dst.IOPath = toCreate;
                ok         = CreateDirectory(dst, args);
                pos++;
            }

            dst.IOPath = origPath;

            if (!ok)
            {
                result = ResultBad;
            }
            else
            {
                if (dst.PathIs(dst.IOPath) == enPathType.File && createToFile && !CreateFile(dst, args))
                {
                    result = ResultBad;
                }
            }

            return(result);
        }