示例#1
0
        private static string GetNormalizedSearchCriteria(string fullSearchString, string fullPathMod)
        {
            string str   = fullPathMod;
            int    index = str.Length - 1;

            return(!Path.IsDirectorySeparator(str[index]) ? fullSearchString.Substring(fullPathMod.Length + 1) : fullSearchString.Substring(fullPathMod.Length));
        }
        internal static bool Exists(String path)
        {
            try
            {
                if (path == null)
                {
                    return(false);
                }
                if (path.Length == 0)
                {
                    return(false);
                }

                path = LongPath.NormalizePath(path);
                // After normalizing, check whether path ends in directory separator.
                // Otherwise, FillAttributeInfo removes it and we may return a false positive.
                // GetFullPathInternal should never return null
                Contract.Assert(path != null, "File.Exists: GetFullPathInternal returned null");
                if (path.Length > 0 && Path.IsDirectorySeparator(path[path.Length - 1]))
                {
                    return(false);
                }

                new FileIOPermission(FileIOPermissionAccess.Read, new String[] { path }, false, false).Demand();

                return(InternalExists(path));
            }
            catch (ArgumentException) {}
            catch (NotSupportedException) {} // Security can throw this on ":"
            catch (SecurityException) {}
            catch (IOException) {}
            catch (UnauthorizedAccessException) {}

            return(false);
        }
示例#3
0
        internal static int GetRootLength(string path)
        {
            Path.CheckInvalidPathChars(path, false);
            int index  = 0;
            int length = path.Length;

            if (length >= 1 && Path.IsDirectorySeparator(path[0]))
            {
                index = 1;
                if (length >= 2 && Path.IsDirectorySeparator(path[1]))
                {
                    index = 2;
                    int num = 2;
                    while (index < length && ((int)path[index] != (int)Path.DirectorySeparatorChar && (int)path[index] != (int)Path.AltDirectorySeparatorChar || --num > 0))
                    {
                        ++index;
                    }
                }
            }
            else if (length >= 2 && (int)path[1] == (int)Path.VolumeSeparatorChar)
            {
                index = 2;
                if (length >= 3 && Path.IsDirectorySeparator(path[2]))
                {
                    ++index;
                }
            }
            return(index);
        }
示例#4
0
        private static string GetNormalizedSearchCriteria(string fullSearchString, string fullPathMod)
        {
            char c = fullPathMod[fullPathMod.Length - 1];

            if (Path.IsDirectorySeparator(c))
            {
                return(fullSearchString.Substring(fullPathMod.Length));
            }
            return(fullSearchString.Substring(fullPathMod.Length + 1));
        }
示例#5
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static String GetDisplayablePath(String path, bool isInvalidPath)
        {
            if (String.IsNullOrEmpty(path))
            {
                return(String.Empty);
            }

            if (path.Length < 2)
            {
                return(path);
            }

            // Return the path as is if we're relative (not fully qualified) and not a bad path
            if (PathInternal.IsPartiallyQualified(path) && !isInvalidPath)
            {
                return(path);
            }

            bool safeToReturn = false;

            try {
                if (!isInvalidPath)
                {
#if !FEATURE_CORECLR
                    FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, path, false, false);
#endif
                    safeToReturn = true;
                }
            }
            catch (SecurityException) {
            }
            catch (ArgumentException) {
                // ? and * characters cause ArgumentException to be thrown from HasIllegalCharacters
                // inside FileIOPermission.AddPathList
            }
            catch (NotSupportedException) {
                // paths like "!Bogus\\dir:with/junk_.in it" can cause NotSupportedException to be thrown
                // from Security.Util.StringExpressionSet.CanonicalizePath when ':' is found in the path
                // beyond string index position 1.
            }

            if (!safeToReturn)
            {
                if (Path.IsDirectorySeparator(path[path.Length - 1]))
                {
                    path = Environment.GetResourceString("IO.IO_NoPermissionToDirectoryName");
                }
                else
                {
                    path = Path.GetFileName(path);
                }
            }

            return(path);
        }
        // Token: 0x060018A4 RID: 6308 RVA: 0x00050A94 File Offset: 0x0004EC94
        private static string GetFullSearchString(string fullPath, string searchPattern)
        {
            string text = Path.InternalCombine(fullPath, searchPattern);
            char   c    = text[text.Length - 1];

            if (Path.IsDirectorySeparator(c) || c == Path.VolumeSeparatorChar)
            {
                text += "*";
            }
            return(text);
        }
示例#7
0
        private static string GetFullSearchString(string fullPath, string searchPattern)
        {
            string str = Path.InternalCombine(fullPath, searchPattern);
            char   c   = str[str.Length - 1];

            if (!Path.IsDirectorySeparator(c) && (c != Path.VolumeSeparatorChar))
            {
                return(str);
            }
            return(str + '*');
        }
示例#8
0
        internal static string GetDisplayablePath(string path, bool isInvalidPath)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(string.Empty);
            }
            bool flag1 = false;

            if (path.Length < 2)
            {
                return(path);
            }
            if (Path.IsDirectorySeparator(path[0]) && Path.IsDirectorySeparator(path[1]))
            {
                flag1 = true;
            }
            else if ((int)path[1] == (int)Path.VolumeSeparatorChar)
            {
                flag1 = true;
            }
            if (!flag1 && !isInvalidPath)
            {
                return(path);
            }
            bool flag2 = false;

            try
            {
                if (!isInvalidPath)
                {
                    new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new string[1] {
                        path
                    }, 0 != 0, 0 != 0).Demand();
                    flag2 = true;
                }
            }
            catch (SecurityException ex)
            {
            }
            catch (ArgumentException ex)
            {
            }
            catch (NotSupportedException ex)
            {
            }
            if (!flag2)
            {
                string str   = path;
                int    index = str.Length - 1;
                path = !Path.IsDirectorySeparator(str[index]) ? Path.GetFileName(path) : Environment.GetResourceString("IO.IO_NoPermissionToDirectoryName");
            }
            return(path);
        }
 internal static string GetDisplayablePath(string path, bool isInvalidPath)
 {
     if (!string.IsNullOrEmpty(path))
     {
         bool flag = false;
         if (path.Length < 2)
         {
             return(path);
         }
         if (Path.IsDirectorySeparator(path[0]) && Path.IsDirectorySeparator(path[1]))
         {
             flag = true;
         }
         else if (path[1] == Path.VolumeSeparatorChar)
         {
             flag = true;
         }
         if (!flag && !isInvalidPath)
         {
             return(path);
         }
         bool flag2 = false;
         try
         {
             if (!isInvalidPath)
             {
                 new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new string[] { path }, false, false).Demand();
                 flag2 = true;
             }
         }
         catch (SecurityException)
         {
         }
         catch (ArgumentException)
         {
         }
         catch (NotSupportedException)
         {
         }
         if (flag2)
         {
             return(path);
         }
         if (Path.IsDirectorySeparator(path[path.Length - 1]))
         {
             path = Environment.GetResourceString("IO.IO_NoPermissionToDirectoryName");
             return(path);
         }
         path = Path.GetFileName(path);
     }
     return(path);
 }
示例#10
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static String GetDisplayablePath(String path, bool isInvalidPath)
        {
            
            if (String.IsNullOrEmpty(path))
                return String.Empty;

            // Is it a fully qualified path?
            bool isFullyQualified = false;
            if (path.Length < 2)
                return path;
            if (Path.IsDirectorySeparator(path[0]) && Path.IsDirectorySeparator(path[1]))
                isFullyQualified = true;
            else if (path[1] == Path.VolumeSeparatorChar) {
                isFullyQualified = true;
            }

            if (!isFullyQualified && !isInvalidPath)
                return path;

#if DISABLE_CAS_USE
            bool safeToReturn = !isInvalidPath;
#else
            bool safeToReturn = false;
            try {
                if (!isInvalidPath) {
#if !FEATURE_CORECLR
                    new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new String[] { path }, false, false).Demand();
#endif
                    safeToReturn = true;
                }
            }
            catch (SecurityException) {
            }
            catch (ArgumentException) {
                // ? and * characters cause ArgumentException to be thrown from HasIllegalCharacters
                // inside FileIOPermission.AddPathList
            }
            catch (NotSupportedException) {
                // paths like "!Bogus\\dir:with/junk_.in it" can cause NotSupportedException to be thrown
                // from Security.Util.StringExpressionSet.CanonicalizePath when ':' is found in the path
                // beyond string index position 1.  
            }
#endif // DISABLE_CAS_USE
            if (!safeToReturn) {
                if (Path.IsDirectorySeparator(path[path.Length - 1]))
                    path = Environment.GetResourceString("IO.IO_NoPermissionToDirectoryName");
                else
                    path = Path.GetFileName(path);
            }

            return path;
        }
示例#11
0
        private static string GetFullSearchString(string fullPath, string searchPattern)
        {
            string str1  = Path.InternalCombine(fullPath, searchPattern);
            string str2  = str1;
            int    index = str2.Length - 1;
            char   c     = str2[index];

            if (Path.IsDirectorySeparator(c) || (int)c == (int)Path.VolumeSeparatorChar)
            {
                str1 += "*";
            }
            return(str1);
        }
示例#12
0
        private static unsafe void InternalCreateDirectory(string fullPath, string path, object dirSecurityObj)
        {
            DirectorySecurity directorySecurity = (DirectorySecurity)dirSecurityObj;
            int length = fullPath.Length;

            if (length >= 2 && Path.IsDirectorySeparator(fullPath[length - 1]))
            {
                --length;
            }
            int rootLength = LongPath.GetRootLength(fullPath);

            if (length == 2 && Path.IsDirectorySeparator(fullPath[1]))
            {
                throw new IOException(Environment.GetResourceString("IO.IO_CannotCreateDirectory", (object)path));
            }
            List <string> stringList1 = new List <string>();
            bool          flag1       = false;

            if (length > rootLength)
            {
                for (int index = length - 1; index >= rootLength && !flag1; --index)
                {
                    string path1 = fullPath.Substring(0, index + 1);
                    if (!LongPathDirectory.InternalExists(path1))
                    {
                        stringList1.Add(path1);
                    }
                    else
                    {
                        flag1 = true;
                    }
                    while (index > rootLength && (int)fullPath[index] != (int)Path.DirectorySeparatorChar && (int)fullPath[index] != (int)Path.AltDirectorySeparatorChar)
                    {
                        --index;
                    }
                }
            }
            int count = stringList1.Count;

            if (stringList1.Count != 0)
            {
                string[] strArray = new string[stringList1.Count];
                stringList1.CopyTo(strArray, 0);
                for (int index = 0; index < strArray.Length; ++index)
                {
                    // ISSUE: explicit reference operation
                    // ISSUE: variable of a reference type
                    string& local = @strArray[index];
        internal static string GetDisplayablePath(string path, bool isInvalidPath)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(string.Empty);
            }
            if (path.Length < 2)
            {
                return(path);
            }
            if (PathInternal.IsPartiallyQualified(path) && !isInvalidPath)
            {
                return(path);
            }
            bool flag = false;

            try
            {
                if (!isInvalidPath)
                {
                    FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, path, false, false);
                    flag = true;
                }
            }
            catch (SecurityException)
            {
            }
            catch (ArgumentException)
            {
            }
            catch (NotSupportedException)
            {
            }
            if (!flag)
            {
                if (Path.IsDirectorySeparator(path[path.Length - 1]))
                {
                    path = Environment.GetResourceString("IO.IO_NoPermissionToDirectoryName");
                }
                else
                {
                    path = Path.GetFileName(path);
                }
            }
            return(path);
        }
        private static String GetFullSearchString(String fullPath, String searchPattern)
        {
            Contract.Requires(fullPath != null);
            Contract.Requires(searchPattern != null);

            String tempStr = Path.InternalCombine(fullPath, searchPattern);

            // If path ends in a trailing slash (\), append a * or we'll get a "Cannot find the file specified" exception
            char lastChar = tempStr[tempStr.Length - 1];

            if (Path.IsDirectorySeparator(lastChar) || lastChar == Path.VolumeSeparatorChar)
            {
                tempStr = tempStr + '*';
            }

            return(tempStr);
        }
示例#15
0
 public SearchData(string fullPath, string userPath, SearchOption searchOption)
 {
     if (Path.IsDirectorySeparator(fullPath[fullPath.Length - 1]))
     {
         this.fullPath = fullPath;
     }
     else
     {
         this.fullPath = fullPath + Path.DirectorySeparatorChar;
     }
     if (string.IsNullOrEmpty(userPath) || Path.IsDirectorySeparator(userPath[userPath.Length - 1]))
     {
         this.userPath = userPath;
     }
     else
     {
         this.userPath = userPath + Path.DirectorySeparatorChar;
     }
     this.searchOption = searchOption;
 }
        private static String GetNormalizedSearchCriteria(String fullSearchString, String fullPathMod)
        {
            Contract.Requires(fullSearchString != null);
            Contract.Requires(fullPathMod != null);
            Contract.Requires(fullSearchString.Length >= fullPathMod.Length);

            String searchCriteria = null;
            char   lastChar       = fullPathMod[fullPathMod.Length - 1];

            if (Path.IsDirectorySeparator(lastChar))
            {
                // Can happen if the path is C:\temp, in which case GetDirectoryName would return C:\
                searchCriteria = fullSearchString.Substring(fullPathMod.Length);
            }
            else
            {
                Contract.Assert(fullSearchString.Length > fullPathMod.Length);
                searchCriteria = fullSearchString.Substring(fullPathMod.Length + 1);
            }
            return(searchCriteria);
        }
示例#17
0
 internal static bool Exists(string path)
 {
     try
     {
         if (path == null || path.Length == 0)
         {
             return(false);
         }
         path = LongPath.NormalizePath(path);
         if (path.Length > 0)
         {
             string str   = path;
             int    index = str.Length - 1;
             if (Path.IsDirectorySeparator(str[index]))
             {
                 return(false);
             }
         }
         new FileIOPermission(FileIOPermissionAccess.Read, new string[1] {
             path
         }, 0 != 0, 0 != 0).Demand();
         return(LongPathFile.InternalExists(path));
     }
     catch (ArgumentException ex)
     {
     }
     catch (NotSupportedException ex)
     {
     }
     catch (SecurityException ex)
     {
     }
     catch (IOException ex)
     {
     }
     catch (UnauthorizedAccessException ex)
     {
     }
     return(false);
 }
 internal static bool Exists(string path)
 {
     try
     {
         if (path == null)
         {
             return(false);
         }
         if (path.Length == 0)
         {
             return(false);
         }
         path = LongPath.NormalizePath(path);
         if ((path.Length > 0) && Path.IsDirectorySeparator(path[path.Length - 1]))
         {
             return(false);
         }
         new FileIOPermission(FileIOPermissionAccess.Read, new string[] { path }, false, false).Demand();
         return(InternalExists(path));
     }
     catch (ArgumentException)
     {
     }
     catch (NotSupportedException)
     {
     }
     catch (SecurityException)
     {
     }
     catch (IOException)
     {
     }
     catch (UnauthorizedAccessException)
     {
     }
     return(false);
 }
        private unsafe static void InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj)
        {
#if FEATURE_MACL
            DirectorySecurity dirSecurity = (DirectorySecurity)dirSecurityObj;
#endif // FEATURE_MACL

            int length = fullPath.Length;

            // We need to trim the trailing slash or the code will try to create 2 directories of the same name.
            if (length >= 2 && Path.IsDirectorySeparator(fullPath[length - 1]))
            {
                length--;
            }

            int lengthRoot = LongPath.GetRootLength(fullPath);

#if !PLATFORM_UNIX
            // For UNC paths that are only // or ///
            if (length == 2 && Path.IsDirectorySeparator(fullPath[1]))
            {
                throw new IOException(Environment.GetResourceString("IO.IO_CannotCreateDirectory", path));
            }
#endif // !PLATFORM_UNIX

            List <string> stackDir = new List <string>();

            // Attempt to figure out which directories don't exist, and only
            // create the ones we need.  Note that InternalExists may fail due
            // to Win32 ACL's preventing us from seeing a directory, and this
            // isn't threadsafe.

            bool somepathexists = false;

            if (length > lengthRoot)
            { // Special case root (fullpath = X:\\)
                int i = length - 1;
                while (i >= lengthRoot && !somepathexists)
                {
                    String dir = fullPath.Substring(0, i + 1);

                    if (!InternalExists(dir)) // Create only the ones missing
                    {
                        stackDir.Add(dir);
                    }
                    else
                    {
                        somepathexists = true;
                    }

                    while (i > lengthRoot && fullPath[i] != Path.DirectorySeparatorChar && fullPath[i] != Path.AltDirectorySeparatorChar)
                    {
                        i--;
                    }
                    i--;
                }
            }

            int count = stackDir.Count;

            if (stackDir.Count != 0)
            {
                String[] securityList = new String[stackDir.Count];
                stackDir.CopyTo(securityList, 0);
                for (int j = 0; j < securityList.Length; j++)
                {
                    securityList[j] += "\\."; // leaf will never have a slash at the end
                }
                // Security check for all directories not present only.
#if !FEATURE_PAL && FEATURE_MACL
                AccessControlActions control = (dirSecurity == null) ? AccessControlActions.None : AccessControlActions.Change;
                new FileIOPermission(FileIOPermissionAccess.Write, control, securityList, false, false).Demand();
#else
                new FileIOPermission(FileIOPermissionAccess.Write, securityList, false, false).Demand();
#endif
            }

            // If we were passed a DirectorySecurity, convert it to a security
            // descriptor and set it in he call to CreateDirectory.
            Win32Native.SECURITY_ATTRIBUTES secAttrs = null;
#if FEATURE_MACL
            if (dirSecurity != null)
            {
                secAttrs         = new Win32Native.SECURITY_ATTRIBUTES();
                secAttrs.nLength = (int)Marshal.SizeOf(secAttrs);

                // For ACL's, get the security descriptor from the FileSecurity.
                byte[] sd           = dirSecurity.GetSecurityDescriptorBinaryForm();
                byte * bytesOnStack = stackalloc byte[sd.Length];
                Buffer.Memcpy(bytesOnStack, 0, sd, 0, sd.Length);
                secAttrs.pSecurityDescriptor = bytesOnStack;
            }
#endif

            bool   r           = true;
            int    firstError  = 0;
            String errorString = path;
            // If all the security checks succeeded create all the directories
            while (stackDir.Count > 0)
            {
                String name = stackDir[stackDir.Count - 1];
                stackDir.RemoveAt(stackDir.Count - 1);
                if (name.Length >= Path.MaxLongPath)
                {
                    throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
                }
                r = Win32Native.CreateDirectory(Path.AddLongPathPrefix(name), secAttrs);
                if (!r && (firstError == 0))
                {
                    int currentError = Marshal.GetLastWin32Error();
                    // While we tried to avoid creating directories that don't
                    // exist above, there are at least two cases that will
                    // cause us to see ERROR_ALREADY_EXISTS here.  InternalExists
                    // can fail because we didn't have permission to the
                    // directory.  Secondly, another thread or process could
                    // create the directory between the time we check and the
                    // time we try using the directory.  Thirdly, it could
                    // fail because the target does exist, but is a file.
                    if (currentError != Win32Native.ERROR_ALREADY_EXISTS)
                    {
                        firstError = currentError;
                    }
                    else
                    {
                        // If there's a file in this directory's place, or if we have ERROR_ACCESS_DENIED when checking if the directory already exists throw.
                        if (LongPathFile.InternalExists(name) || (!InternalExists(name, out currentError) && currentError == Win32Native.ERROR_ACCESS_DENIED))
                        {
                            firstError = currentError;
                            // Give the user a nice error message, but don't leak path information.
                            try
                            {
                                new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new String[] { GetDemandDir(name, true) }, false, false).Demand();
                                errorString = name;
                            }
                            catch (SecurityException) { }
                        }
                    }
                }
            }

            // We need this check to mask OS differences
            // Handle CreateDirectory("X:\\foo") when X: doesn't exist. Similarly for n/w paths.
            if ((count == 0) && !somepathexists)
            {
                String root = InternalGetDirectoryRoot(fullPath);
                if (!InternalExists(root))
                {
                    // Extract the root from the passed in path again for security.
                    __Error.WinIOError(Win32Native.ERROR_PATH_NOT_FOUND, InternalGetDirectoryRoot(path));
                }
                return;
            }

            // Only throw an exception if creating the exact directory we
            // wanted failed to work correctly.
            if (!r && (firstError != 0))
            {
                __Error.WinIOError(firstError, errorString);
            }
        }
        // Given a possible fully qualified path, ensure that we have path
        // discovery permission to that path.  If we do not, return just the
        // file name.  If we know it is a directory, then don't return the
        // directory name.
        internal static String GetDisplayablePath(String path, bool isInvalidPath)
        {
            if (String.IsNullOrEmpty(path))
            {
                return(path);
            }

            // Is it a fully qualified path?
            bool isFullyQualified = false;

            if (path.Length < 2)
            {
                return(path);
            }
            if (Path.IsDirectorySeparator(path[0]) && Path.IsDirectorySeparator(path[1]))
            {
                isFullyQualified = true;
            }
            else if (path[1] == Path.VolumeSeparatorChar)
            {
                isFullyQualified = true;
            }

            if (!isFullyQualified && !isInvalidPath)
            {
                return(path);
            }

            bool safeToReturn = false;

            try {
                if (!isInvalidPath)
                {
                    new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new String[] { path }, false, false).Demand();
                    safeToReturn = true;
                }
            }
            catch (ArgumentException) {
                // Needed for Win9x
            }
            catch (NotSupportedException) {
                // Needed for Win9x, which will sometimes return
                // ERROR_PATH_NOT_FOUND for invalid path names, but also
                // will leak the current working directory.
            }
            catch (SecurityException) {
            }

            if (!safeToReturn)
            {
                if (Path.IsDirectorySeparator(path[path.Length - 1]))
                {
                    path = Environment.GetResourceString("IO.IO_NoPermissionToDirectoryName");
                }
                else
                {
                    path = Path.GetFileName(path);
                }
            }

            return(path);
        }
示例#21
0
        internal static void InternalCreateDirectory(String fullPath, String path)
        {
            int length = fullPath.Length;

            // We need to trim the trailing slash or the code will try to create 2 directories of the same name.
            if (length >= 2 && Path.IsDirectorySeparator(fullPath[length - 1]))
            {
                length--;
            }
            int i = Path.GetRootLength(fullPath);

#if !PLATFORM_UNIX
            // For UNC paths that are only // or ///
            if (length == 2 && Path.IsDirectorySeparator(fullPath[1]))
            {
                throw new IOException(String.Format(Environment.GetResourceString("IO.IO_CannotCreateDirectory"), path));
            }
#endif // !PLATFORM_UNIX

            ArrayList list = new ArrayList();

            while (i < length)
            {
                i++;
                while (i < length && fullPath[i] != Path.DirectorySeparatorChar && fullPath[i] != Path.AltDirectorySeparatorChar)
                {
                    i++;
                }
                String dir = fullPath.Substring(0, i);

                if (!InternalExists(dir))   // Create only the ones missing
                {
                    list.Add(dir);
                }
            }

            if (list.Count != 0)
            {
                String [] securityList = (String[])list.ToArray(typeof(String));
                for (int j = 0; j < securityList.Length; j++)
                {
                    securityList[j] += "\\.";     // leafs will never has a slash at the end
                }
                // Security check for all directories not present only.
                new FileIOPermission(FileIOPermissionAccess.Write, securityList, false, false).Demand();
            }


            // We need this check to mask OS differences
            // Handle CreateDirectory("X:\\") when X: doesn't exist. Similarly for n/w paths.
            String root = InternalGetDirectoryRoot(fullPath);
            if (!InternalExists(root))
            {
                __Error.WinIOError(Win32Native.ERROR_PATH_NOT_FOUND, root);
            }

            // If all the security checks succeeded create all the directories
            for (int j = 0; j < list.Count; j++)
            {
                String name = (String)list[j];
                if (name.Length > Path.MAX_DIRECTORY_PATH)
                {
                    throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
                }
                bool r = Win32Native.CreateDirectory(name, 0);
                if (!r)
                {
                    __Error.WinIOError(Marshal.GetLastWin32Error(), Path.GetDirectoryName(name));
                }
            }
        }
示例#22
0
        private static unsafe void InternalCreateDirectory(string fullPath, string path, object dirSecurityObj)
        {
            DirectorySecurity security = (DirectorySecurity)dirSecurityObj;
            int length = fullPath.Length;

            if ((length >= 2) && Path.IsDirectorySeparator(fullPath[length - 1]))
            {
                length--;
            }
            int rootLength = LongPath.GetRootLength(fullPath);

            if ((length == 2) && Path.IsDirectorySeparator(fullPath[1]))
            {
                throw new IOException(Environment.GetResourceString("IO.IO_CannotCreateDirectory", new object[] { path }));
            }
            List <string> list = new List <string>();
            bool          flag = false;

            if (length > rootLength)
            {
                for (int i = length - 1; (i >= rootLength) && !flag; i--)
                {
                    string str = fullPath.Substring(0, i + 1);
                    if (!InternalExists(str))
                    {
                        list.Add(str);
                    }
                    else
                    {
                        flag = true;
                    }
                    while (((i > rootLength) && (fullPath[i] != Path.DirectorySeparatorChar)) && (fullPath[i] != Path.AltDirectorySeparatorChar))
                    {
                        i--;
                    }
                }
            }
            int count = list.Count;

            if (list.Count != 0)
            {
                string[] array = new string[list.Count];
                list.CopyTo(array, 0);
                for (int j = 0; j < array.Length; j++)
                {
                    string[] strArray2;
                    IntPtr   ptr;
                    (strArray2 = array)[(int)(ptr = (IntPtr)j)] = strArray2[(int)ptr] + @"\.";
                }
                AccessControlActions control = (security == null) ? AccessControlActions.None : AccessControlActions.Change;
                new FileIOPermission(FileIOPermissionAccess.Write, control, array, false, false).Demand();
            }
            Win32Native.SECURITY_ATTRIBUTES structure = null;
            if (security != null)
            {
                structure = new Win32Native.SECURITY_ATTRIBUTES {
                    nLength = Marshal.SizeOf(structure)
                };
                byte[] securityDescriptorBinaryForm = security.GetSecurityDescriptorBinaryForm();
                byte * pDest = stackalloc byte[(IntPtr)securityDescriptorBinaryForm.Length];
                Buffer.memcpy(securityDescriptorBinaryForm, 0, pDest, 0, securityDescriptorBinaryForm.Length);
                structure.pSecurityDescriptor = pDest;
            }
            bool   flag2         = true;
            int    errorCode     = 0;
            string maybeFullPath = path;

            while (list.Count > 0)
            {
                string str3 = list[list.Count - 1];
                list.RemoveAt(list.Count - 1);
                if (str3.Length >= Path.MaxLongPath)
                {
                    throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
                }
                flag2 = Win32Native.CreateDirectory(Path.AddLongPathPrefix(str3), structure);
                if (!flag2 && (errorCode == 0))
                {
                    int lastError = Marshal.GetLastWin32Error();
                    if (lastError != 0xb7)
                    {
                        errorCode = lastError;
                    }
                    else if (LongPathFile.InternalExists(str3) || (!InternalExists(str3, out lastError) && (lastError == 5)))
                    {
                        errorCode = lastError;
                        try
                        {
                            new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new string[] { GetDemandDir(str3, true) }, false, false).Demand();
                            maybeFullPath = str3;
                            continue;
                        }
                        catch (SecurityException)
                        {
                            continue;
                        }
                    }
                }
            }
            if ((count == 0) && !flag)
            {
                if (!InternalExists(InternalGetDirectoryRoot(fullPath)))
                {
                    __Error.WinIOError(3, InternalGetDirectoryRoot(path));
                }
            }
            else if (!flag2 && (errorCode != 0))
            {
                __Error.WinIOError(errorCode, maybeFullPath);
            }
        }
示例#23
0
        // Given a possible fully qualified path, ensure that we have path
        // discovery permission to that path.  If we do not, return just the
        // file name.  If we know it is a directory, then don't return the
        // directory name.
        internal static String GetDisplayablePath(String path, bool isInvalidPath)
        {
            if (String.IsNullOrEmpty(path))
            {
                return(String.Empty);
            }

            // Is it a fully qualified path?
            bool isFullyQualified = false;

            if (path.Length < 2)
            {
                return(path);
            }
            if (Path.IsDirectorySeparator(path[0]) && Path.IsDirectorySeparator(path[1]))
            {
                isFullyQualified = true;
            }
            else if (path[1] == Path.VolumeSeparatorChar)
            {
                isFullyQualified = true;
            }

            if (!isFullyQualified && !isInvalidPath)
            {
                return(path);
            }

            bool safeToReturn = false;

            try {
                if (!isInvalidPath)
                {
                    safeToReturn = true;
                }
            }
            catch (SecurityException) {
            }
            catch (ArgumentException) {
                // ? and * characters cause ArgumentException to be thrown from HasIllegalCharacters
                // inside FileIOPermission.AddPathList
            }
            catch (NotSupportedException) {
                // paths like "!Bogus\\dir:with/junk_.in it" can cause NotSupportedException to be thrown
                // from Security.Util.StringExpressionSet.CanonicalizePath when ':' is found in the path
                // beyond string index position 1.
            }

            if (!safeToReturn)
            {
                if (Path.IsDirectorySeparator(path[path.Length - 1]))
                {
                    path = SR.IO_NoPermissionToDirectoryName;
                }
                else
                {
                    path = Path.GetFileName(path);
                }
            }

            return(path);
        }
示例#24
0
        private unsafe static void InternalCreateDirectory(string fullPath, string path, object dirSecurityObj)
        {
            DirectorySecurity directorySecurity = (DirectorySecurity)dirSecurityObj;
            int num = fullPath.Length;

            if (num >= 2 && Path.IsDirectorySeparator(fullPath[num - 1]))
            {
                num--;
            }
            int rootLength = LongPath.GetRootLength(fullPath);

            if (num == 2 && Path.IsDirectorySeparator(fullPath[1]))
            {
                throw new IOException(Environment.GetResourceString("IO.IO_CannotCreateDirectory", new object[]
                {
                    path
                }));
            }
            List <string> list = new List <string>();
            bool          flag = false;

            if (num > rootLength)
            {
                int num2 = num - 1;
                while (num2 >= rootLength && !flag)
                {
                    string text = fullPath.Substring(0, num2 + 1);
                    if (!LongPathDirectory.InternalExists(text))
                    {
                        list.Add(text);
                    }
                    else
                    {
                        flag = true;
                    }
                    while (num2 > rootLength && fullPath[num2] != Path.DirectorySeparatorChar && fullPath[num2] != Path.AltDirectorySeparatorChar)
                    {
                        num2--;
                    }
                    num2--;
                }
            }
            int count = list.Count;

            if (list.Count != 0 && !CodeAccessSecurityEngine.QuickCheckForAllDemands())
            {
                string[] array = new string[list.Count];
                list.CopyTo(array, 0);
                for (int i = 0; i < array.Length; i++)
                {
                    string[] array2 = array;
                    int      num3   = i;
                    array2[num3] += "\\.";
                }
                AccessControlActions control = (directorySecurity == null) ? AccessControlActions.None : AccessControlActions.Change;
                FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, control, array, false, false);
            }
            Win32Native.SECURITY_ATTRIBUTES security_ATTRIBUTES = null;
            if (directorySecurity != null)
            {
                security_ATTRIBUTES         = new Win32Native.SECURITY_ATTRIBUTES();
                security_ATTRIBUTES.nLength = Marshal.SizeOf <Win32Native.SECURITY_ATTRIBUTES>(security_ATTRIBUTES);
                byte[] securityDescriptorBinaryForm = directorySecurity.GetSecurityDescriptorBinaryForm();
                byte * ptr = stackalloc byte[checked (unchecked ((UIntPtr)securityDescriptorBinaryForm.Length) * 1)];
                Buffer.Memcpy(ptr, 0, securityDescriptorBinaryForm, 0, securityDescriptorBinaryForm.Length);
                security_ATTRIBUTES.pSecurityDescriptor = ptr;
            }
            bool   flag2         = true;
            int    num4          = 0;
            string maybeFullPath = path;

            while (list.Count > 0)
            {
                string text2 = list[list.Count - 1];
                list.RemoveAt(list.Count - 1);
                if (text2.Length >= 32767)
                {
                    throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
                }
                flag2 = Win32Native.CreateDirectory(PathInternal.EnsureExtendedPrefix(text2), security_ATTRIBUTES);
                if (!flag2 && num4 == 0)
                {
                    int lastWin32Error = Marshal.GetLastWin32Error();
                    if (lastWin32Error != 183)
                    {
                        num4 = lastWin32Error;
                    }
                    else if (LongPathFile.InternalExists(text2) || (!LongPathDirectory.InternalExists(text2, out lastWin32Error) && lastWin32Error == 5))
                    {
                        num4 = lastWin32Error;
                        try
                        {
                            FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, LongPathDirectory.GetDemandDir(text2, true), false, false);
                            maybeFullPath = text2;
                        }
                        catch (SecurityException)
                        {
                        }
                    }
                }
            }
            if (count == 0 && !flag)
            {
                string path2 = LongPathDirectory.InternalGetDirectoryRoot(fullPath);
                if (!LongPathDirectory.InternalExists(path2))
                {
                    __Error.WinIOError(3, LongPathDirectory.InternalGetDirectoryRoot(path));
                }
                return;
            }
            if (!flag2 && num4 != 0)
            {
                __Error.WinIOError(num4, maybeFullPath);
            }
        }
示例#25
0
        internal static void InternalCreateDirectory(String !fullPath, String path)
        {
            int length = fullPath.Length;

            // We need to trim the trailing slash or the code will try to create 2 directories of the same name.
            if (length >= 2 && Path.IsDirectorySeparator(fullPath[length - 1]))
            {
                length--;
            }
            int i = Path.GetRootLength(fullPath);

            // For UNC paths that are only // or ///
            if (length == 2 && Path.IsDirectorySeparator(fullPath[1]))
            {
                throw new IOException(String.Format("IO.IO_CannotCreateDirectory", path));
            }

            ArrayList list = new ArrayList();

            while (i < length)
            {
                i++;
                while (i < length && fullPath[i] != Path.DirectorySeparatorChar && fullPath[i] != Path.AltDirectorySeparatorChar)
                {
                    i++;
                }
                String dir = fullPath.Substring(0, i);

                if (!InternalExists(dir))   // Create only the ones missing
                {
                    list.Add(dir);
                }
            }

            if (list.Count != 0)
            {
                String [] securityList = (String[])list.ToArray(typeof(String));
                for (int j = 0; j < securityList.Length; j++)
                {
                    securityList[j] += "\\."; // leafs will never has a slash at the end
                }
            }


            // We need this check to mask OS differences
            // Handle CreateDirectory("X:\\") when X: doesn't exist. Similarly for n/w paths.
            String root = InternalGetDirectoryRoot(fullPath);

            if (!InternalExists(root))
            {
                // Extract the root from the passed in path again for security.
                __Error.WinIOError(Native.ERROR_PATH_NOT_FOUND, InternalGetDirectoryRoot(path));
            }

            bool r          = true;
            int  firstError = 0;

            // If all the security checks succeeded create all the directories
            for (int j = 0; j < list.Count; j++)
            {
                String name = (String !)list[j];
                if (name.Length > Path.MAX_DIRECTORY_PATH)
                {
                    throw new PathTooLongException("IO.PathTooLong");
                }
                r = Native.CreateDirectory(name, 0);
                if (!r && (firstError == 0))
                {
                    firstError = Native.ERROR_PATH_EXISTS;
                }
            }

            if (!r && (firstError != 0))
            {
                __Error.WinIOError(firstError, path);
            }
        }