private static string InternalCopy(string fullSourceFileName, string fullDestFileName, string sourceFileName, string destFileName, bool overwrite)
 {
     fullSourceFileName = Path.AddLongPathPrefix(fullSourceFileName);
     fullDestFileName   = Path.AddLongPathPrefix(fullDestFileName);
     if (!Win32Native.CopyFile(fullSourceFileName, fullDestFileName, !overwrite))
     {
         int    errorCode     = Marshal.GetLastWin32Error();
         string maybeFullPath = destFileName;
         if (errorCode != 80)
         {
             using (SafeFileHandle handle = Win32Native.UnsafeCreateFile(fullSourceFileName, -2147483648, FileShare.Read, null, FileMode.Open, 0, IntPtr.Zero))
             {
                 if (handle.IsInvalid)
                 {
                     maybeFullPath = sourceFileName;
                 }
             }
             if ((errorCode == 5) && LongPathDirectory.InternalExists(fullDestFileName))
             {
                 throw new IOException(Environment.GetResourceString("Arg_FileIsDirectory_Name", new object[] { destFileName }), 5, fullDestFileName);
             }
         }
         __Error.WinIOError(errorCode, maybeFullPath);
     }
     return(fullDestFileName);
 }
Пример #2
0
 internal static bool Exists(string path)
 {
     try
     {
         if (path == null)
         {
             return(false);
         }
         if (path.Length == 0)
         {
             return(false);
         }
         string text      = LongPath.NormalizePath(path);
         string demandDir = LongPathDirectory.GetDemandDir(text, true);
         FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, demandDir, false, false);
         return(LongPathDirectory.InternalExists(text));
     }
     catch (ArgumentException)
     {
     }
     catch (NotSupportedException)
     {
     }
     catch (SecurityException)
     {
     }
     catch (IOException)
     {
     }
     catch (UnauthorizedAccessException)
     {
     }
     return(false);
 }
Пример #3
0
 private static string InternalCopy(string fullSourceFileName, string fullDestFileName, string sourceFileName, string destFileName, bool overwrite)
 {
     fullSourceFileName = Path.AddLongPathPrefix(fullSourceFileName);
     fullDestFileName   = Path.AddLongPathPrefix(fullDestFileName);
     if (!Win32Native.CopyFile(fullSourceFileName, fullDestFileName, !overwrite))
     {
         int    lastWin32Error = Marshal.GetLastWin32Error();
         string maybeFullPath  = destFileName;
         if (lastWin32Error != 80)
         {
             using (SafeFileHandle file = Win32Native.UnsafeCreateFile(fullSourceFileName, int.MinValue, FileShare.Read, (Win32Native.SECURITY_ATTRIBUTES)null, FileMode.Open, 0, IntPtr.Zero))
             {
                 if (file.IsInvalid)
                 {
                     maybeFullPath = sourceFileName;
                 }
             }
             if (lastWin32Error == 5 && LongPathDirectory.InternalExists(fullDestFileName))
             {
                 throw new IOException(Environment.GetResourceString("Arg_FileIsDirectory_Name", (object)destFileName), 5, fullDestFileName);
             }
         }
         __Error.WinIOError(lastWin32Error, maybeFullPath);
     }
     return(fullDestFileName);
 }
Пример #4
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];
Пример #5
0
        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);
        }
Пример #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);
            }
        }