示例#1
0
        internal static void CreateDirectory(string path)
        {
            string fullPath = LongPath.NormalizePath(path);

            new FileIOPermission(FileIOPermissionAccess.Read, new string[1]
            {
                LongPathDirectory.GetDemandDir(fullPath, true)
            }, 0 != 0, 0 != 0).Demand();
            LongPathDirectory.InternalCreateDirectory(fullPath, path, (object)null);
        }
示例#2
0
        internal static void Move(string sourceDirName, string destDirName)
        {
            string text      = LongPath.NormalizePath(sourceDirName);
            string demandDir = LongPathDirectory.GetDemandDir(text, false);

            if (demandDir.Length >= 32767)
            {
                throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
            }
            string fullPath   = LongPath.NormalizePath(destDirName);
            string demandDir2 = LongPathDirectory.GetDemandDir(fullPath, false);

            if (demandDir2.Length >= 32767)
            {
                throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
            }
            FileIOPermission.QuickDemand(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, demandDir, false, false);
            FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, demandDir2, false, false);
            if (string.Compare(demandDir, demandDir2, StringComparison.OrdinalIgnoreCase) == 0)
            {
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustBeDifferent"));
            }
            string pathRoot  = LongPath.GetPathRoot(demandDir);
            string pathRoot2 = LongPath.GetPathRoot(demandDir2);

            if (string.Compare(pathRoot, pathRoot2, StringComparison.OrdinalIgnoreCase) != 0)
            {
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustHaveSameRoot"));
            }
            string src = PathInternal.EnsureExtendedPrefix(sourceDirName);
            string dst = PathInternal.EnsureExtendedPrefix(destDirName);

            if (!Win32Native.MoveFile(src, dst))
            {
                int num = Marshal.GetLastWin32Error();
                if (num == 2)
                {
                    num = 3;
                    __Error.WinIOError(num, text);
                }
                if (num == 5)
                {
                    throw new IOException(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path", new object[]
                    {
                        sourceDirName
                    }), Win32Native.MakeHRFromErrorCode(num));
                }
                __Error.WinIOError(num, string.Empty);
            }
        }
示例#3
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];
        private static String InternalCopy(String fullSourceFileName, String fullDestFileName, String sourceFileName, String destFileName, bool overwrite)
        {
            Contract.Requires(fullSourceFileName != null);
            Contract.Requires(fullDestFileName != null);
            Contract.Requires(fullSourceFileName.Length > 0);
            Contract.Requires(fullDestFileName.Length > 0);

            fullSourceFileName = Path.AddLongPathPrefix(fullSourceFileName);
            fullDestFileName   = Path.AddLongPathPrefix(fullDestFileName);
            bool r = Win32Native.CopyFile(fullSourceFileName, fullDestFileName, !overwrite);

            if (!r)
            {
                // Save Win32 error because subsequent checks will overwrite this HRESULT.
                int    errorCode = Marshal.GetLastWin32Error();
                String fileName  = destFileName;

                if (errorCode != Win32Native.ERROR_FILE_EXISTS)
                {
                    // For a number of error codes (sharing violation, path
                    // not found, etc) we don't know if the problem was with
                    // the source or dest file.  Try reading the source file.
                    using (SafeFileHandle handle = Win32Native.UnsafeCreateFile(fullSourceFileName, FileStream.GENERIC_READ, FileShare.Read, null, FileMode.Open, 0, IntPtr.Zero)) {
                        if (handle.IsInvalid)
                        {
                            fileName = sourceFileName;
                        }
                    }

                    if (errorCode == Win32Native.ERROR_ACCESS_DENIED)
                    {
                        if (LongPathDirectory.InternalExists(fullDestFileName))
                        {
                            throw new IOException(Environment.GetResourceString("Arg_FileIsDirectory_Name", destFileName), Win32Native.ERROR_ACCESS_DENIED, fullDestFileName);
                        }
                    }
                }

                __Error.WinIOError(errorCode, fileName);
            }

            return(fullDestFileName);
        }
示例#5
0
        private static void InternalDelete(string fullPath, string userPath, bool recursive)
        {
            string demandDir = LongPathDirectory.GetDemandDir(fullPath, !recursive);

            FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, demandDir, false, false);
            string text = Path.AddLongPathPrefix(fullPath);

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA win32_FILE_ATTRIBUTE_DATA = default(Win32Native.WIN32_FILE_ATTRIBUTE_DATA);
            int num = File.FillAttributeInfo(text, ref win32_FILE_ATTRIBUTE_DATA, false, true);

            if (num != 0)
            {
                if (num == 2)
                {
                    num = 3;
                }
                __Error.WinIOError(num, fullPath);
            }
            if ((win32_FILE_ATTRIBUTE_DATA.fileAttributes & 1024) != 0)
            {
                recursive = false;
            }
            LongPathDirectory.DeleteHelper(text, userPath, recursive, true);
        }
示例#6
0
        internal static bool InternalExists(string path)
        {
            int num = 0;

            return(LongPathDirectory.InternalExists(path, out num));
        }
示例#7
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);
            }
        }
示例#8
0
        private static void DeleteHelper(string fullPath, string userPath, bool recursive, bool throwOnTopLevelDirectoryNotFound)
        {
            Exception ex = null;

            if (recursive)
            {
                Win32Native.WIN32_FIND_DATA win32_FIND_DATA = default(Win32Native.WIN32_FIND_DATA);
                string fileName;
                if (fullPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
                {
                    fileName = fullPath + "*";
                }
                else
                {
                    fileName = fullPath + Path.DirectorySeparatorChar.ToString() + "*";
                }
                int num;
                using (SafeFindHandle safeFindHandle = Win32Native.FindFirstFile(fileName, ref win32_FIND_DATA))
                {
                    if (safeFindHandle.IsInvalid)
                    {
                        num = Marshal.GetLastWin32Error();
                        __Error.WinIOError(num, userPath);
                    }
                    for (;;)
                    {
                        bool flag = (win32_FIND_DATA.dwFileAttributes & 16) != 0;
                        if (!flag)
                        {
                            goto IL_180;
                        }
                        if (!win32_FIND_DATA.IsRelativeDirectory)
                        {
                            bool flag2 = (win32_FIND_DATA.dwFileAttributes & 1024) == 0;
                            if (flag2)
                            {
                                string fullPath2 = LongPath.InternalCombine(fullPath, win32_FIND_DATA.cFileName);
                                string userPath2 = LongPath.InternalCombine(userPath, win32_FIND_DATA.cFileName);
                                try
                                {
                                    LongPathDirectory.DeleteHelper(fullPath2, userPath2, recursive, false);
                                    goto IL_1BD;
                                }
                                catch (Exception ex2)
                                {
                                    if (ex == null)
                                    {
                                        ex = ex2;
                                    }
                                    goto IL_1BD;
                                }
                            }
                            if (win32_FIND_DATA.dwReserved0 == -1610612733)
                            {
                                string mountPoint = LongPath.InternalCombine(fullPath, win32_FIND_DATA.cFileName + Path.DirectorySeparatorChar.ToString());
                                if (!Win32Native.DeleteVolumeMountPoint(mountPoint))
                                {
                                    num = Marshal.GetLastWin32Error();
                                    if (num != 3)
                                    {
                                        try
                                        {
                                            __Error.WinIOError(num, win32_FIND_DATA.cFileName);
                                        }
                                        catch (Exception ex3)
                                        {
                                            if (ex == null)
                                            {
                                                ex = ex3;
                                            }
                                        }
                                    }
                                }
                            }
                            string path = LongPath.InternalCombine(fullPath, win32_FIND_DATA.cFileName);
                            if (!Win32Native.RemoveDirectory(path))
                            {
                                num = Marshal.GetLastWin32Error();
                                if (num != 3)
                                {
                                    try
                                    {
                                        __Error.WinIOError(num, win32_FIND_DATA.cFileName);
                                        goto IL_1BD;
                                    }
                                    catch (Exception ex4)
                                    {
                                        if (ex == null)
                                        {
                                            ex = ex4;
                                        }
                                        goto IL_1BD;
                                    }
                                    goto IL_180;
                                }
                            }
                        }
IL_1BD:
                        if (!Win32Native.FindNextFile(safeFindHandle, ref win32_FIND_DATA))
                        {
                            break;
                        }
                        continue;
IL_180:
                        string path2 = LongPath.InternalCombine(fullPath, win32_FIND_DATA.cFileName);
                        if (Win32Native.DeleteFile(path2))
                        {
                            goto IL_1BD;
                        }
                        num = Marshal.GetLastWin32Error();
                        if (num != 2)
                        {
                            try
                            {
                                __Error.WinIOError(num, win32_FIND_DATA.cFileName);
                            }
                            catch (Exception ex5)
                            {
                                if (ex == null)
                                {
                                    ex = ex5;
                                }
                            }
                            goto IL_1BD;
                        }
                        goto IL_1BD;
                    }
                    num = Marshal.GetLastWin32Error();
                }
                if (ex != null)
                {
                    throw ex;
                }
                if (num != 0 && num != 18)
                {
                    __Error.WinIOError(num, userPath);
                }
            }
            if (!Win32Native.RemoveDirectory(fullPath))
            {
                int num = Marshal.GetLastWin32Error();
                if (num == 2)
                {
                    num = 3;
                }
                if (num == 5)
                {
                    throw new IOException(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path", new object[]
                    {
                        userPath
                    }));
                }
                if (num == 3 && !throwOnTopLevelDirectoryNotFound)
                {
                    return;
                }
                __Error.WinIOError(num, userPath);
            }
        }
示例#9
0
        internal static void Delete(string path, bool recursive)
        {
            string fullPath = LongPath.NormalizePath(path);

            LongPathDirectory.InternalDelete(fullPath, path, recursive);
        }