Пример #1
0
        public NTTransactCreateRequest(byte[] parameters, byte[] data, bool isUnicode)
        {
            int parametersOffset = 0;

            Flags             = (NTCreateFlags)LittleEndianReader.ReadUInt32(parameters, ref parametersOffset);
            RootDirectoryFID  = LittleEndianReader.ReadUInt32(parameters, ref parametersOffset);
            DesiredAccess     = (AccessMask)LittleEndianReader.ReadUInt32(parameters, ref parametersOffset);
            AllocationSize    = LittleEndianReader.ReadInt64(parameters, ref parametersOffset);
            ExtFileAttributes = (ExtendedFileAttributes)LittleEndianReader.ReadUInt32(parameters, ref parametersOffset);
            ShareAccess       = (ShareAccess)LittleEndianReader.ReadUInt32(parameters, ref parametersOffset);
            CreateDisposition = (CreateDisposition)LittleEndianReader.ReadUInt32(parameters, ref parametersOffset);
            CreateOptions     = (CreateOptions)LittleEndianReader.ReadUInt32(parameters, ref parametersOffset);
            uint securityDescriptiorLength = LittleEndianReader.ReadUInt32(parameters, ref parametersOffset);

            _ = LittleEndianReader.ReadUInt32(parameters, ref parametersOffset);
            uint nameLength = LittleEndianReader.ReadUInt32(parameters, ref parametersOffset);

            ImpersonationLevel = (ImpersonationLevel)LittleEndianReader.ReadUInt32(parameters, ref parametersOffset);
            SecurityFlags      = (SecurityFlags)ByteReader.ReadByte(parameters, ref parametersOffset);

            if (isUnicode)
            {
                parametersOffset++;
            }
            Name = SMB1Helper.ReadFixedLengthString(parameters, ref parametersOffset, isUnicode, (int)nameLength);
            if (securityDescriptiorLength > 0)
            {
                SecurityDescriptor = new SecurityDescriptor(data, 0);
            }
            ExtendedAttributes = FileFullEAInformation.ReadList(data, (int)securityDescriptiorLength);
        }
Пример #2
0
        public NTCreateAndXRequest(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
        {
            Reserved           = ByteReader.ReadByte(this.SMBParameters, 4);
            NameLength         = LittleEndianConverter.ToUInt16(this.SMBParameters, 5);
            Flags              = (NTCreateFlags)LittleEndianConverter.ToUInt32(this.SMBParameters, 7);
            RootDirectoryFID   = LittleEndianConverter.ToUInt32(this.SMBParameters, 11);
            DesiredAccess      = (FileAccessMask)LittleEndianConverter.ToUInt32(this.SMBParameters, 15);
            AllocationSize     = LittleEndianConverter.ToInt64(this.SMBParameters, 19);
            ExtFileAttributes  = (ExtendedFileAttributes)LittleEndianConverter.ToUInt32(this.SMBParameters, 27);
            ShareAccess        = (ShareAccess)LittleEndianConverter.ToUInt32(this.SMBParameters, 31);
            CreateDisposition  = (CreateDisposition)LittleEndianConverter.ToUInt32(this.SMBParameters, 35);
            CreateOptions      = (CreateOptions)LittleEndianConverter.ToUInt32(this.SMBParameters, 39);
            ImpersonationLevel = (ImpersonationLevel)LittleEndianConverter.ToUInt32(this.SMBParameters, 43);
            SecurityFlags      = (SecurityFlags)ByteReader.ReadByte(this.SMBParameters, 47);

            int dataOffset = 0;

            if (isUnicode)
            {
                // A Unicode string MUST be aligned to a 16-bit boundary with respect to the beginning of the SMB Header.
                // Note: SMBData starts at an odd offset.
                dataOffset = 1;
            }
            FileName = SMB1Helper.ReadSMBString(this.SMBData, dataOffset, isUnicode);
        }
Пример #3
0
        public static ExtendedFileAttributes GetExtendedFileAttributes(FileSystemEntry entry)
        {
            ExtendedFileAttributes attributes = (ExtendedFileAttributes)0;

            if (entry.IsHidden)
            {
                attributes |= ExtendedFileAttributes.Hidden;
            }
            if (entry.IsReadonly)
            {
                attributes |= ExtendedFileAttributes.Readonly;
            }
            if (entry.IsArchived)
            {
                attributes |= ExtendedFileAttributes.Archive;
            }
            if (entry.IsDirectory)
            {
                attributes |= ExtendedFileAttributes.Directory;
            }

            if (attributes == (ExtendedFileAttributes)0)
            {
                attributes = ExtendedFileAttributes.Normal;
            }

            return(attributes);
        }
Пример #4
0
 public QueryFileBasicInfo(byte[] buffer, int offset)
 {
     CreationTime      = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
     LastAccessTime    = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
     LastWriteTime     = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
     LastChangeTime    = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
     ExtFileAttributes = (ExtendedFileAttributes)LittleEndianReader.ReadUInt32(buffer, ref offset);
     Reserved          = LittleEndianReader.ReadUInt32(buffer, ref offset);
 }
Пример #5
0
 public SetFileBasicInfo(byte[] buffer, int offset)
 {
     CreationTime      = SMBHelper.ReadSetFileTime(buffer, offset + 0);
     LastAccessTime    = SMBHelper.ReadSetFileTime(buffer, offset + 8);
     LastWriteTime     = SMBHelper.ReadSetFileTime(buffer, offset + 16);
     LastChangeTime    = SMBHelper.ReadSetFileTime(buffer, offset + 24);
     ExtFileAttributes = (ExtendedFileAttributes)LittleEndianConverter.ToUInt32(buffer, offset + 32);
     Reserved          = LittleEndianConverter.ToUInt32(buffer, offset + 36);
 }
Пример #6
0
        private static FileAttributes ToFileAttributes(ExtendedFileAttributes extendedFileAttributes)
        {
            // We only return flags that can be used with NtCreateFile
            FileAttributes fileAttributes = FileAttributes.ReadOnly |
                                            FileAttributes.Hidden |
                                            FileAttributes.System |
                                            FileAttributes.Archive |
                                            FileAttributes.Normal |
                                            FileAttributes.Temporary |
                                            FileAttributes.Offline |
                                            FileAttributes.Encrypted;

            return(fileAttributes & (FileAttributes)extendedFileAttributes);
        }
Пример #7
0
        public FindFileDirectoryInfo(byte[] buffer, int offset, bool isUnicode) : base()
        {
            NextEntryOffset    = LittleEndianReader.ReadUInt32(buffer, ref offset);
            FileIndex          = LittleEndianReader.ReadUInt32(buffer, ref offset);
            CreationTime       = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
            LastAccessTime     = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
            LastWriteTime      = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
            LastAttrChangeTime = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
            EndOfFile          = LittleEndianReader.ReadInt64(buffer, ref offset);
            AllocationSize     = LittleEndianReader.ReadInt64(buffer, ref offset);
            ExtFileAttributes  = (ExtendedFileAttributes)LittleEndianReader.ReadUInt32(buffer, ref offset);
            uint fileNameLength = LittleEndianReader.ReadUInt32(buffer, ref offset);

            FileName = SMB1Helper.ReadFixedLengthString(buffer, ref offset, isUnicode, (int)fileNameLength);
        }
Пример #8
0
        public NTCreateAndXResponse(byte[] buffer, int offset) : base(buffer, offset)
        {
            int parametersOffset = 4;

            OpLockLevel       = (OpLockLevel)ByteReader.ReadByte(SMBParameters, ref parametersOffset);
            FID               = LittleEndianReader.ReadUInt16(SMBParameters, ref parametersOffset);
            CreateDisposition = (CreateDisposition)LittleEndianReader.ReadUInt32(SMBParameters, ref parametersOffset);
            CreateTime        = SMB1Helper.ReadNullableFileTime(SMBParameters, ref parametersOffset);
            LastAccessTime    = SMB1Helper.ReadNullableFileTime(SMBParameters, ref parametersOffset);
            LastWriteTime     = SMB1Helper.ReadNullableFileTime(SMBParameters, ref parametersOffset);
            LastChangeTime    = SMB1Helper.ReadNullableFileTime(SMBParameters, ref parametersOffset);
            ExtFileAttributes = (ExtendedFileAttributes)LittleEndianReader.ReadUInt32(SMBParameters, ref parametersOffset);
            AllocationSize    = LittleEndianReader.ReadInt64(SMBParameters, ref parametersOffset);
            EndOfFile         = LittleEndianReader.ReadInt64(SMBParameters, ref parametersOffset);
            ResourceType      = (ResourceType)LittleEndianReader.ReadUInt16(SMBParameters, ref parametersOffset);
            NMPipeStatus      = NamedPipeStatus.Read(SMBParameters, ref parametersOffset);
            Directory         = (ByteReader.ReadByte(SMBParameters, ref parametersOffset) > 0);
        }
Пример #9
0
        public QueryFileAllInfo(byte[] buffer, int offset)
        {
            CreationTime      = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
            LastAccessTime    = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
            LastWriteTime     = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
            LastChangeTime    = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
            ExtFileAttributes = (ExtendedFileAttributes)LittleEndianReader.ReadUInt32(buffer, ref offset);
            Reserved1         = LittleEndianReader.ReadUInt32(buffer, ref offset);
            AllocationSize    = LittleEndianReader.ReadInt64(buffer, ref offset);
            EndOfFile         = LittleEndianReader.ReadInt64(buffer, ref offset);
            NumberOfLinks     = LittleEndianReader.ReadUInt32(buffer, ref offset);
            DeletePending     = (ByteReader.ReadByte(buffer, ref offset) > 0);
            Directory         = (ByteReader.ReadByte(buffer, ref offset) > 0);
            Reserved2         = LittleEndianReader.ReadUInt16(buffer, ref offset);
            EaSize            = LittleEndianReader.ReadUInt32(buffer, ref offset);
            uint fileNameLength = LittleEndianReader.ReadUInt32(buffer, ref offset);

            FileName = ByteReader.ReadUTF16String(buffer, ref offset, (int)(fileNameLength / 2));
        }
        public FindFileBothDirectoryInfo(byte[] buffer, ref int offset, bool isUnicode) : base(false)
        {
            NextEntryOffset   = LittleEndianReader.ReadUInt32(buffer, ref offset);
            FileIndex         = LittleEndianReader.ReadUInt32(buffer, ref offset);
            CreationTime      = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
            LastAccessTime    = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
            LastWriteTime     = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
            LastChangeTime    = FileTimeHelper.ReadNullableFileTime(buffer, ref offset);
            EndOfFile         = LittleEndianReader.ReadInt64(buffer, ref offset);
            AllocationSize    = LittleEndianReader.ReadInt64(buffer, ref offset);
            ExtFileAttributes = (ExtendedFileAttributes)LittleEndianReader.ReadUInt32(buffer, ref offset);
            uint fileNameLength = LittleEndianReader.ReadUInt32(buffer, ref offset);

            EASize = LittleEndianReader.ReadUInt32(buffer, ref offset);
            byte shortNameLength = ByteReader.ReadByte(buffer, ref offset);

            Reserved  = ByteReader.ReadByte(buffer, ref offset);
            ShortName = ByteReader.ReadUTF16String(buffer, ref offset, 12);
            ShortName = ShortName.Substring(0, shortNameLength);
            FileName  = SMB1Helper.ReadFixedLengthString(buffer, ref offset, isUnicode, (int)fileNameLength);
        }
Пример #11
0
        internal static FileStream CreateFileStreamCore(KernelTransaction transaction, string path,
                                                        ExtendedFileAttributes attributes, FileSecurity fileSecurity, FileMode mode, FileAccess access,
                                                        FileShare share, int bufferSize, PathFormat pathFormat)
        {
            SafeFileHandle safeHandle = null;

            try
            {
                safeHandle = CreateFileCore(transaction, path, attributes, fileSecurity, mode, (FileSystemRights)access, share, true, false, pathFormat);

                return(new FileStream(safeHandle, access, bufferSize, (attributes & ExtendedFileAttributes.Overlapped) != 0));
            }
            catch
            {
                if (null != safeHandle && !safeHandle.IsClosed)
                {
                    safeHandle.Close();
                }

                throw;
            }
        }
        public NTCreateAndXResponseExtended(byte[] buffer, int offset) : base(buffer, offset, false)
        {
            int parametersOffset = 4;

            OpLockLevel       = (OpLockLevel)ByteReader.ReadByte(this.SMBParameters, ref parametersOffset);
            FID               = LittleEndianReader.ReadUInt16(this.SMBParameters, ref parametersOffset);
            CreateDisposition = (CreateDisposition)LittleEndianReader.ReadUInt32(this.SMBParameters, ref parametersOffset);
            CreateTime        = FileTimeHelper.ReadNullableFileTime(this.SMBParameters, ref parametersOffset);
            LastAccessTime    = FileTimeHelper.ReadNullableFileTime(this.SMBParameters, ref parametersOffset);
            LastWriteTime     = FileTimeHelper.ReadNullableFileTime(this.SMBParameters, ref parametersOffset);
            LastChangeTime    = FileTimeHelper.ReadNullableFileTime(this.SMBParameters, ref parametersOffset);
            ExtFileAttributes = (ExtendedFileAttributes)LittleEndianReader.ReadUInt32(this.SMBParameters, ref parametersOffset);
            AllocationSize    = LittleEndianReader.ReadInt64(this.SMBParameters, ref parametersOffset);
            EndOfFile         = LittleEndianReader.ReadInt64(this.SMBParameters, ref parametersOffset);
            ResourceType      = (ResourceType)LittleEndianReader.ReadUInt16(this.SMBParameters, ref parametersOffset);
            NMPipeStatus_or_FileStatusFlags = LittleEndianReader.ReadUInt16(this.SMBParameters, ref parametersOffset);
            Directory                = (ByteReader.ReadByte(this.SMBParameters, ref parametersOffset) > 0);
            VolumeGuid               = LittleEndianReader.ReadGuid(this.SMBParameters, ref parametersOffset);
            FileID                   = LittleEndianReader.ReadUInt64(this.SMBParameters, ref parametersOffset);
            MaximalAccessRights      = (AccessMask)LittleEndianReader.ReadUInt32(this.SMBParameters, ref parametersOffset);
            GuestMaximalAccessRights = (AccessMask)LittleEndianReader.ReadUInt32(this.SMBParameters, ref parametersOffset);
        }
Пример #13
0
        public NTCreateAndXRequest(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
        {
            Reserved = ByteReader.ReadByte(this.SMBParameters, 4);
            ushort nameLength = LittleEndianConverter.ToUInt16(this.SMBParameters, 5);

            Flags              = (NTCreateFlags)LittleEndianConverter.ToUInt32(this.SMBParameters, 7);
            RootDirectoryFID   = LittleEndianConverter.ToUInt32(this.SMBParameters, 11);
            DesiredAccess      = (DesiredAccess)LittleEndianConverter.ToUInt32(this.SMBParameters, 15);
            AllocationSize     = LittleEndianConverter.ToUInt64(this.SMBParameters, 19);
            ExtFileAttributes  = (ExtendedFileAttributes)LittleEndianConverter.ToUInt32(this.SMBParameters, 27);
            ShareAccess        = (ShareAccess)LittleEndianConverter.ToUInt32(this.SMBParameters, 31);
            CreateDisposition  = (CreateDisposition)LittleEndianConverter.ToUInt32(this.SMBParameters, 35);
            CreateOptions      = (CreateOptions)LittleEndianConverter.ToUInt32(this.SMBParameters, 39);
            ImpersonationLevel = (ImpersonationLevel)LittleEndianConverter.ToUInt32(this.SMBParameters, 43);
            SecurityFlags      = (SecurityFlags)ByteReader.ReadByte(this.SMBParameters, 47);

            int dataOffset = 0;

            if (isUnicode)
            {
                dataOffset = 1; // 1 byte padding for 2 byte alignment
            }
            FileName = SMBHelper.ReadSMBString(this.SMBData, dataOffset, isUnicode);
        }
Пример #14
0
        internal static SafeFileHandle CreateFileCore(KernelTransaction transaction, string path, ExtendedFileAttributes attributes, FileSecurity fileSecurity, FileMode fileMode, FileSystemRights fileSystemRights, FileShare fileShare, bool checkPath, bool continueOnException, PathFormat pathFormat)
        {
            if (checkPath && pathFormat == PathFormat.RelativePath)
            {
                Path.CheckSupportedPathFormat(path, true, true);
            }


            // When isFile == null, we're working with a device.
            // When opening a VOLUME or removable media drive (for example, a floppy disk drive or flash memory thumb drive),
            // the path string should be the following form: "\\.\X:"
            // Do not use a trailing backslash ('\'), which indicates the root.

            var pathLp = Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator);


            // CreateFileXxx() does not support FileMode.Append mode.
            var isAppend = fileMode == FileMode.Append;

            if (isAppend)
            {
                fileMode          = FileMode.OpenOrCreate;
                fileSystemRights |= FileSystemRights.AppendData;
            }


            if (null != fileSecurity)
            {
                fileSystemRights |= (FileSystemRights)SECURITY_INFORMATION.UNPROTECTED_SACL_SECURITY_INFORMATION;
            }


            using ((fileSystemRights & (FileSystemRights)SECURITY_INFORMATION.UNPROTECTED_SACL_SECURITY_INFORMATION) != 0 || (fileSystemRights & (FileSystemRights)SECURITY_INFORMATION.UNPROTECTED_DACL_SECURITY_INFORMATION) != 0 ? new PrivilegeEnabler(Privilege.Security) : null)
                using (var securityAttributes = new Security.NativeMethods.SecurityAttributes(fileSecurity))
                {
                    var safeHandle = transaction == null || !NativeMethods.IsAtLeastWindowsVista

                                     // CreateFile() / CreateFileTransacted()
                                     // 2013-01-13: MSDN confirms LongPath usage.

               ? NativeMethods.CreateFile(pathLp, fileSystemRights, fileShare, securityAttributes, fileMode, attributes, IntPtr.Zero)
               : NativeMethods.CreateFileTransacted(pathLp, fileSystemRights, fileShare, securityAttributes, fileMode, attributes, IntPtr.Zero, transaction.SafeHandle, IntPtr.Zero, IntPtr.Zero);


                    var lastError = Marshal.GetLastWin32Error();

                    NativeMethods.IsValidHandle(safeHandle, lastError, pathLp, !continueOnException);


                    if (isAppend)
                    {
                        var success = NativeMethods.SetFilePointerEx(safeHandle, 0, IntPtr.Zero, SeekOrigin.End);

                        lastError = Marshal.GetLastWin32Error();

                        if (!success)
                        {
                            if (!safeHandle.IsClosed)
                            {
                                safeHandle.Close();
                            }

                            NativeError.ThrowException(lastError, path);
                        }
                    }

                    return(safeHandle);
                }
        }
Пример #15
0
 public static FileStream Open(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, ExtendedFileAttributes extendedAttributes, PathFormat pathFormat)
 {
    return OpenInternal(transaction, path, mode, 0, access, share, extendedAttributes, pathFormat);
 }
Пример #16
0
        internal static SafeFileHandle CreateFileCore(KernelTransaction transaction, string path, ExtendedFileAttributes attributes, FileSecurity fileSecurity, FileMode fileMode, FileSystemRights fileSystemRights, FileShare fileShare, bool checkPath, PathFormat pathFormat)
        {
            if (checkPath && pathFormat == PathFormat.RelativePath)
            {
                Path.CheckSupportedPathFormat(path, true, true);
            }

            // When isFile == null, we're working with a device.
            // When opening a VOLUME or removable media drive (for example, a floppy disk drive or flash memory thumb drive),
            // the path string should be the following form: "\\.\X:"
            // Do not use a trailing backslash (\), which indicates the root.

            var pathLp = Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator);

            PrivilegeEnabler privilegeEnabler = null;


            var isAppend = fileMode == FileMode.Append;


            // CreateFileXxx() does not support FileMode.Append mode.
            if (isAppend)
            {
                fileMode          = FileMode.OpenOrCreate;
                fileSystemRights &= FileSystemRights.AppendData; // Add right.
            }


            if (fileSecurity != null)
            {
                fileSystemRights |= (FileSystemRights)0x1000000; // Set right.
            }
            // AccessSystemSecurity = 0x1000000    AccessSystemAcl access type.
            // MaximumAllowed       = 0x2000000    MaximumAllowed  access type.

            if ((fileSystemRights & (FileSystemRights)0x1000000) != 0 ||
                (fileSystemRights & (FileSystemRights)0x2000000) != 0)
            {
                privilegeEnabler = new PrivilegeEnabler(Privilege.Security);
            }


            using (privilegeEnabler)
                using (var securityAttributes = new Security.NativeMethods.SecurityAttributes(fileSecurity))
                {
                    var handle = transaction == null || !NativeMethods.IsAtLeastWindowsVista

                                 // CreateFile() / CreateFileTransacted()
                                 // In the ANSI version of this function, the name is limited to MAX_PATH characters.
                                 // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
                                 // 2013-01-13: MSDN confirms LongPath usage.

               ? NativeMethods.CreateFile(pathLp, fileSystemRights, fileShare, securityAttributes, fileMode, attributes, IntPtr.Zero)
               : NativeMethods.CreateFileTransacted(pathLp, fileSystemRights, fileShare, securityAttributes, fileMode, attributes, IntPtr.Zero, transaction.SafeHandle, IntPtr.Zero, IntPtr.Zero);

                    var lastError = Marshal.GetLastWin32Error();

                    if (handle.IsInvalid)
                    {
                        handle.Close();
                        NativeError.ThrowException(lastError, pathLp);
                    }


                    if (isAppend)
                    {
                        var stream = new FileStream(handle, FileAccess.Write, NativeMethods.DefaultFileBufferSize, (attributes & ExtendedFileAttributes.Overlapped) != 0);
                        stream.Seek(0, SeekOrigin.End);
                    }

                    return(handle);
                }
        }
Пример #17
0
      internal static FileStream OpenInternal(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileAccess access, FileShare share, ExtendedFileAttributes attributes, PathFormat pathFormat)
      {
         SafeFileHandle safeHandle = CreateFileInternal(transaction, path, attributes, null, mode, rights != 0 ? rights : (FileSystemRights)access, share, true, pathFormat);

         return rights != 0
            ? new FileStream(safeHandle, FileAccess.Write)
            : new FileStream(safeHandle, access);
      }
Пример #18
0
 public BackupFileStream(KernelTransaction transaction, string path, FileMode mode, FileSystemRights access, FileShare share, ExtendedFileAttributes attributes, FileSecurity security)
     : this(File.CreateFileInternal(transaction, path, attributes, security, mode, access, share, true, false), access)
 {
 }
Пример #19
0
 public BackupFileStream(string path, FileMode mode, FileSystemRights access, FileShare share, ExtendedFileAttributes attributes)
     : this(File.CreateFileInternal(null, path, attributes, null, mode, access, share, true, false), access)
 {
 }
Пример #20
0
        internal static FileStream OpenInternal(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileAccess access, FileShare share, ExtendedFileAttributes attributes, PathFormat pathFormat)
        {
            SafeFileHandle safeHandle = CreateFileInternal(transaction, path, attributes, null, mode, rights != 0 ? rights : (FileSystemRights)access, share, true, pathFormat);

            return(rights != 0
            ? new FileStream(safeHandle, FileAccess.Write)
            : new FileStream(safeHandle, access));
        }
Пример #21
0
 public static FileStream Open(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, ExtendedFileAttributes extendedAttributes, PathFormat pathFormat)
 {
     return(OpenInternal(transaction, path, mode, 0, access, share, extendedAttributes, pathFormat));
 }
Пример #22
0
      internal static SafeFileHandle CreateFileInternal(KernelTransaction transaction, string path, ExtendedFileAttributes attributes, FileSecurity fileSecurity, FileMode fileMode, FileSystemRights fileSystemRights, FileShare fileShare, bool checkPath, PathFormat pathFormat)
      {
         if (checkPath && pathFormat == PathFormat.RelativePath)
            Path.CheckValidPath(path, true, true);

         // When isFile == null, we're working with a device.
         // When opening a VOLUME or removable media drive (for example, a floppy disk drive or flash memory thumb drive),
         // the path string should be the following form: "\\.\X:"
         // Do not use a trailing backslash (\), which indicates the root.

         string pathLp = Path.GetExtendedLengthPathInternal(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator);

         PrivilegeEnabler privilegeEnabler = null;
         try
         {
            if (fileSecurity != null)
               fileSystemRights |= (FileSystemRights)0x1000000;

            // AccessSystemSecurity = 0x1000000    AccessSystemAcl access type.
            // MaximumAllowed       = 0x2000000    MaximumAllowed access type.            
            if ((fileSystemRights & (FileSystemRights)0x1000000) != 0 ||
                (fileSystemRights & (FileSystemRights)0x2000000) != 0)
               privilegeEnabler = new PrivilegeEnabler(Privilege.Security);


            using (var securityAttributes = new Security.NativeMethods.SecurityAttributes(fileSecurity))
            {
               SafeFileHandle handle = transaction == null || !NativeMethods.IsAtLeastWindowsVista

                  // CreateFile() / CreateFileTransacted()
                  // In the ANSI version of this function, the name is limited to MAX_PATH characters.
                  // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
                  // 2013-01-13: MSDN confirms LongPath usage.

                  ? NativeMethods.CreateFile(pathLp, fileSystemRights, fileShare, securityAttributes, fileMode, attributes, IntPtr.Zero)
                  : NativeMethods.CreateFileTransacted(pathLp, fileSystemRights, fileShare, securityAttributes, fileMode, attributes, IntPtr.Zero, transaction.SafeHandle, IntPtr.Zero, IntPtr.Zero);

               int lastError = Marshal.GetLastWin32Error();
               if (handle.IsInvalid)
               {
                  handle.Close();
                  NativeError.ThrowException(lastError, pathLp);
               }

               return handle;
            }
         }
         finally
         {
            if (privilegeEnabler != null)
               privilegeEnabler.Dispose();
         }
      }
Пример #23
0
 public BackupFileStream(string path, FileMode mode, FileSystemRights access, FileShare share, ExtendedFileAttributes attributes)
     : this(File.CreateFileCore(null, path, attributes, null, mode, access, share, true, PathFormat.RelativePath), access)
 {
 }
Пример #24
0
        internal static SafeFileHandle CreateFileInternal(KernelTransaction transaction, string path, ExtendedFileAttributes attributes, FileSecurity fileSecurity, FileMode fileMode, FileSystemRights fileSystemRights, FileShare fileShare, bool checkPath, PathFormat pathFormat)
        {
            if (checkPath && pathFormat == PathFormat.RelativePath)
            {
                Path.CheckValidPath(path, true, true);
            }

            // When isFile == null, we're working with a device.
            // When opening a VOLUME or removable media drive (for example, a floppy disk drive or flash memory thumb drive),
            // the path string should be the following form: "\\.\X:"
            // Do not use a trailing backslash (\), which indicates the root.

            string pathLp = Path.GetExtendedLengthPathInternal(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator);

            PrivilegeEnabler privilegeEnabler = null;

            try
            {
                if (fileSecurity != null)
                {
                    fileSystemRights |= (FileSystemRights)0x1000000;
                }

                // AccessSystemSecurity = 0x1000000    AccessSystemAcl access type.
                // MaximumAllowed       = 0x2000000    MaximumAllowed access type.
                if ((fileSystemRights & (FileSystemRights)0x1000000) != 0 ||
                    (fileSystemRights & (FileSystemRights)0x2000000) != 0)
                {
                    privilegeEnabler = new PrivilegeEnabler(Privilege.Security);
                }


                using (var securityAttributes = new Security.NativeMethods.SecurityAttributes(fileSecurity))
                {
                    SafeFileHandle handle = transaction == null || !NativeMethods.IsAtLeastWindowsVista

                                            // CreateFile() / CreateFileTransacted()
                                            // In the ANSI version of this function, the name is limited to MAX_PATH characters.
                                            // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
                                            // 2013-01-13: MSDN confirms LongPath usage.

                  ? NativeMethods.CreateFile(pathLp, fileSystemRights, fileShare, securityAttributes, fileMode, attributes, IntPtr.Zero)
                  : NativeMethods.CreateFileTransacted(pathLp, fileSystemRights, fileShare, securityAttributes, fileMode, attributes, IntPtr.Zero, transaction.SafeHandle, IntPtr.Zero, IntPtr.Zero);

                    int lastError = Marshal.GetLastWin32Error();
                    if (handle.IsInvalid)
                    {
                        handle.Close();
                        NativeError.ThrowException(lastError, pathLp);
                    }

                    return(handle);
                }
            }
            finally
            {
                if (privilegeEnabler != null)
                {
                    privilegeEnabler.Dispose();
                }
            }
        }
Пример #25
0
 public BackupFileStream(KernelTransaction transaction, string path, FileMode mode, FileSystemRights access, FileShare share, ExtendedFileAttributes attributes, FileSecurity security)
     : this(File.CreateFileCore(transaction, path, attributes, security, mode, access, share, true, PathFormat.RelativePath), access)
 {
 }
Пример #26
0
 public BackupFileStream(KernelTransaction transaction, string path, FileMode mode, FileSystemRights access, FileShare share, ExtendedFileAttributes attributes, FileSecurity security)
    : this(File.CreateFileInternal(transaction, path, attributes, security, mode, access, share, true, PathFormat.RelativePath), access)
 {
 }
Пример #27
0
 public BackupFileStream(string path, FileMode mode, FileSystemRights access, FileShare share, ExtendedFileAttributes attributes)
    : this(File.CreateFileInternal(null, path, attributes, null, mode, access, share, true, PathFormat.RelativePath), access)
 {
 }
Пример #28
0
      internal static FileStream CreateFileStreamInternal(KernelTransaction transaction, string path, ExtendedFileAttributes attributes, FileSecurity fileSecurity, FileMode mode, FileAccess access, FileShare share, int bufferSize, PathFormat pathFormat)
      {
         SafeFileHandle safeHandle = null;
         try
         {
            safeHandle = CreateFileInternal(transaction, path, attributes, fileSecurity, mode, (FileSystemRights)access, share, true, pathFormat);
            return new FileStream(safeHandle, access, bufferSize, (attributes & ExtendedFileAttributes.Overlapped) != 0);
         }
         catch
         {
            if (safeHandle != null)
               safeHandle.Dispose();

            throw;
         }
      }