mirror of
https://github.com/rwinkhart/go-winio.git
synced 2026-09-05 08:37:37 -04:00
Convert all files to unix line endings
Signed-off-by: John Howard <jhoward@microsoft.com>
This commit is contained in:
@@ -1,137 +1,137 @@
|
|||||||
package winio
|
package winio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fileFullEaInformation struct {
|
type fileFullEaInformation struct {
|
||||||
NextEntryOffset uint32
|
NextEntryOffset uint32
|
||||||
Flags uint8
|
Flags uint8
|
||||||
NameLength uint8
|
NameLength uint8
|
||||||
ValueLength uint16
|
ValueLength uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
fileFullEaInformationSize = binary.Size(&fileFullEaInformation{})
|
fileFullEaInformationSize = binary.Size(&fileFullEaInformation{})
|
||||||
|
|
||||||
errInvalidEaBuffer = errors.New("invalid extended attribute buffer")
|
errInvalidEaBuffer = errors.New("invalid extended attribute buffer")
|
||||||
errEaNameTooLarge = errors.New("extended attribute name too large")
|
errEaNameTooLarge = errors.New("extended attribute name too large")
|
||||||
errEaValueTooLarge = errors.New("extended attribute value too large")
|
errEaValueTooLarge = errors.New("extended attribute value too large")
|
||||||
)
|
)
|
||||||
|
|
||||||
// ExtendedAttribute represents a single Windows EA.
|
// ExtendedAttribute represents a single Windows EA.
|
||||||
type ExtendedAttribute struct {
|
type ExtendedAttribute struct {
|
||||||
Name string
|
Name string
|
||||||
Value []byte
|
Value []byte
|
||||||
Flags uint8
|
Flags uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseEa(b []byte) (ea ExtendedAttribute, nb []byte, err error) {
|
func parseEa(b []byte) (ea ExtendedAttribute, nb []byte, err error) {
|
||||||
var info fileFullEaInformation
|
var info fileFullEaInformation
|
||||||
err = binary.Read(bytes.NewReader(b), binary.LittleEndian, &info)
|
err = binary.Read(bytes.NewReader(b), binary.LittleEndian, &info)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errInvalidEaBuffer
|
err = errInvalidEaBuffer
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
nameOffset := fileFullEaInformationSize
|
nameOffset := fileFullEaInformationSize
|
||||||
nameLen := int(info.NameLength)
|
nameLen := int(info.NameLength)
|
||||||
valueOffset := nameOffset + int(info.NameLength) + 1
|
valueOffset := nameOffset + int(info.NameLength) + 1
|
||||||
valueLen := int(info.ValueLength)
|
valueLen := int(info.ValueLength)
|
||||||
nextOffset := int(info.NextEntryOffset)
|
nextOffset := int(info.NextEntryOffset)
|
||||||
if valueLen+valueOffset > len(b) || nextOffset < 0 || nextOffset > len(b) {
|
if valueLen+valueOffset > len(b) || nextOffset < 0 || nextOffset > len(b) {
|
||||||
err = errInvalidEaBuffer
|
err = errInvalidEaBuffer
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ea.Name = string(b[nameOffset : nameOffset+nameLen])
|
ea.Name = string(b[nameOffset : nameOffset+nameLen])
|
||||||
ea.Value = b[valueOffset : valueOffset+valueLen]
|
ea.Value = b[valueOffset : valueOffset+valueLen]
|
||||||
ea.Flags = info.Flags
|
ea.Flags = info.Flags
|
||||||
if info.NextEntryOffset != 0 {
|
if info.NextEntryOffset != 0 {
|
||||||
nb = b[info.NextEntryOffset:]
|
nb = b[info.NextEntryOffset:]
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodeExtendedAttributes decodes a list of EAs from a FILE_FULL_EA_INFORMATION
|
// DecodeExtendedAttributes decodes a list of EAs from a FILE_FULL_EA_INFORMATION
|
||||||
// buffer retrieved from BackupRead, ZwQueryEaFile, etc.
|
// buffer retrieved from BackupRead, ZwQueryEaFile, etc.
|
||||||
func DecodeExtendedAttributes(b []byte) (eas []ExtendedAttribute, err error) {
|
func DecodeExtendedAttributes(b []byte) (eas []ExtendedAttribute, err error) {
|
||||||
for len(b) != 0 {
|
for len(b) != 0 {
|
||||||
ea, nb, err := parseEa(b)
|
ea, nb, err := parseEa(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
eas = append(eas, ea)
|
eas = append(eas, ea)
|
||||||
b = nb
|
b = nb
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeEa(buf *bytes.Buffer, ea *ExtendedAttribute, last bool) error {
|
func writeEa(buf *bytes.Buffer, ea *ExtendedAttribute, last bool) error {
|
||||||
if int(uint8(len(ea.Name))) != len(ea.Name) {
|
if int(uint8(len(ea.Name))) != len(ea.Name) {
|
||||||
return errEaNameTooLarge
|
return errEaNameTooLarge
|
||||||
}
|
}
|
||||||
if int(uint16(len(ea.Value))) != len(ea.Value) {
|
if int(uint16(len(ea.Value))) != len(ea.Value) {
|
||||||
return errEaValueTooLarge
|
return errEaValueTooLarge
|
||||||
}
|
}
|
||||||
entrySize := uint32(fileFullEaInformationSize + len(ea.Name) + 1 + len(ea.Value))
|
entrySize := uint32(fileFullEaInformationSize + len(ea.Name) + 1 + len(ea.Value))
|
||||||
withPadding := (entrySize + 3) &^ 3
|
withPadding := (entrySize + 3) &^ 3
|
||||||
nextOffset := uint32(0)
|
nextOffset := uint32(0)
|
||||||
if !last {
|
if !last {
|
||||||
nextOffset = withPadding
|
nextOffset = withPadding
|
||||||
}
|
}
|
||||||
info := fileFullEaInformation{
|
info := fileFullEaInformation{
|
||||||
NextEntryOffset: nextOffset,
|
NextEntryOffset: nextOffset,
|
||||||
Flags: ea.Flags,
|
Flags: ea.Flags,
|
||||||
NameLength: uint8(len(ea.Name)),
|
NameLength: uint8(len(ea.Name)),
|
||||||
ValueLength: uint16(len(ea.Value)),
|
ValueLength: uint16(len(ea.Value)),
|
||||||
}
|
}
|
||||||
|
|
||||||
err := binary.Write(buf, binary.LittleEndian, &info)
|
err := binary.Write(buf, binary.LittleEndian, &info)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = buf.Write([]byte(ea.Name))
|
_, err = buf.Write([]byte(ea.Name))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = buf.WriteByte(0)
|
err = buf.WriteByte(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = buf.Write(ea.Value)
|
_, err = buf.Write(ea.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = buf.Write([]byte{0, 0, 0}[0 : withPadding-entrySize])
|
_, err = buf.Write([]byte{0, 0, 0}[0 : withPadding-entrySize])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeExtendedAttributes encodes a list of EAs into a FILE_FULL_EA_INFORMATION
|
// EncodeExtendedAttributes encodes a list of EAs into a FILE_FULL_EA_INFORMATION
|
||||||
// buffer for use with BackupWrite, ZwSetEaFile, etc.
|
// buffer for use with BackupWrite, ZwSetEaFile, etc.
|
||||||
func EncodeExtendedAttributes(eas []ExtendedAttribute) ([]byte, error) {
|
func EncodeExtendedAttributes(eas []ExtendedAttribute) ([]byte, error) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
for i := range eas {
|
for i := range eas {
|
||||||
last := false
|
last := false
|
||||||
if i == len(eas)-1 {
|
if i == len(eas)-1 {
|
||||||
last = true
|
last = true
|
||||||
}
|
}
|
||||||
|
|
||||||
err := writeEa(&buf, &eas[i], last)
|
err := writeEa(&buf, &eas[i], last)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|||||||
+89
-89
@@ -1,89 +1,89 @@
|
|||||||
package winio
|
package winio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
testEas = []ExtendedAttribute{
|
testEas = []ExtendedAttribute{
|
||||||
{Name: "foo", Value: []byte("bar")},
|
{Name: "foo", Value: []byte("bar")},
|
||||||
{Name: "fizz", Value: []byte("buzz")},
|
{Name: "fizz", Value: []byte("buzz")},
|
||||||
}
|
}
|
||||||
|
|
||||||
testEasEncoded = []byte{16, 0, 0, 0, 0, 3, 3, 0, 102, 111, 111, 0, 98, 97, 114, 0, 0, 0, 0, 0, 0, 4, 4, 0, 102, 105, 122, 122, 0, 98, 117, 122, 122, 0, 0, 0}
|
testEasEncoded = []byte{16, 0, 0, 0, 0, 3, 3, 0, 102, 111, 111, 0, 98, 97, 114, 0, 0, 0, 0, 0, 0, 4, 4, 0, 102, 105, 122, 122, 0, 98, 117, 122, 122, 0, 0, 0}
|
||||||
testEasNotPadded = testEasEncoded[0 : len(testEasEncoded)-3]
|
testEasNotPadded = testEasEncoded[0 : len(testEasEncoded)-3]
|
||||||
testEasTruncated = testEasEncoded[0:20]
|
testEasTruncated = testEasEncoded[0:20]
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_RoundTripEas(t *testing.T) {
|
func Test_RoundTripEas(t *testing.T) {
|
||||||
b, err := EncodeExtendedAttributes(testEas)
|
b, err := EncodeExtendedAttributes(testEas)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(testEasEncoded, b) {
|
if !reflect.DeepEqual(testEasEncoded, b) {
|
||||||
t.Fatalf("encoded mismatch %v %v", testEasEncoded, b)
|
t.Fatalf("encoded mismatch %v %v", testEasEncoded, b)
|
||||||
}
|
}
|
||||||
eas, err := DecodeExtendedAttributes(b)
|
eas, err := DecodeExtendedAttributes(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(testEas, eas) {
|
if !reflect.DeepEqual(testEas, eas) {
|
||||||
t.Fatalf("mismatch %+v %+v", testEas, eas)
|
t.Fatalf("mismatch %+v %+v", testEas, eas)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_EasDontNeedPaddingAtEnd(t *testing.T) {
|
func Test_EasDontNeedPaddingAtEnd(t *testing.T) {
|
||||||
eas, err := DecodeExtendedAttributes(testEasNotPadded)
|
eas, err := DecodeExtendedAttributes(testEasNotPadded)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(testEas, eas) {
|
if !reflect.DeepEqual(testEas, eas) {
|
||||||
t.Fatalf("mismatch %+v %+v", testEas, eas)
|
t.Fatalf("mismatch %+v %+v", testEas, eas)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_TruncatedEasFailCorrectly(t *testing.T) {
|
func Test_TruncatedEasFailCorrectly(t *testing.T) {
|
||||||
_, err := DecodeExtendedAttributes(testEasTruncated)
|
_, err := DecodeExtendedAttributes(testEasTruncated)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected error")
|
t.Fatal("expected error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_NilEasEncodeAndDecodeAsNil(t *testing.T) {
|
func Test_NilEasEncodeAndDecodeAsNil(t *testing.T) {
|
||||||
b, err := EncodeExtendedAttributes(nil)
|
b, err := EncodeExtendedAttributes(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if len(b) != 0 {
|
if len(b) != 0 {
|
||||||
t.Fatal("expected empty")
|
t.Fatal("expected empty")
|
||||||
}
|
}
|
||||||
eas, err := DecodeExtendedAttributes(nil)
|
eas, err := DecodeExtendedAttributes(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if len(eas) != 0 {
|
if len(eas) != 0 {
|
||||||
t.Fatal("expected empty")
|
t.Fatal("expected empty")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test_SetFileEa makes sure that the test buffer is actually parsable by NtSetEaFile.
|
// Test_SetFileEa makes sure that the test buffer is actually parsable by NtSetEaFile.
|
||||||
func Test_SetFileEa(t *testing.T) {
|
func Test_SetFileEa(t *testing.T) {
|
||||||
f, err := ioutil.TempFile("", "winio")
|
f, err := ioutil.TempFile("", "winio")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.Remove(f.Name())
|
defer os.Remove(f.Name())
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
ntdll := syscall.MustLoadDLL("ntdll.dll")
|
ntdll := syscall.MustLoadDLL("ntdll.dll")
|
||||||
ntSetEaFile := ntdll.MustFindProc("NtSetEaFile")
|
ntSetEaFile := ntdll.MustFindProc("NtSetEaFile")
|
||||||
var iosb [2]uintptr
|
var iosb [2]uintptr
|
||||||
r, _, _ := ntSetEaFile.Call(f.Fd(), uintptr(unsafe.Pointer(&iosb[0])), uintptr(unsafe.Pointer(&testEasEncoded[0])), uintptr(len(testEasEncoded)))
|
r, _, _ := ntSetEaFile.Call(f.Fd(), uintptr(unsafe.Pointer(&iosb[0])), uintptr(unsafe.Pointer(&testEasEncoded[0])), uintptr(len(testEasEncoded)))
|
||||||
if r != 0 {
|
if r != 0 {
|
||||||
t.Fatalf("NtSetEaFile failed with %08x", r)
|
t.Fatalf("NtSetEaFile failed with %08x", r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+901
-901
File diff suppressed because it is too large
Load Diff
+82
-82
@@ -1,82 +1,82 @@
|
|||||||
// +build windows
|
// +build windows
|
||||||
|
|
||||||
package vhd
|
package vhd
|
||||||
|
|
||||||
import "syscall"
|
import "syscall"
|
||||||
|
|
||||||
//go:generate go run mksyscall_windows.go -output zvhd.go vhd.go
|
//go:generate go run mksyscall_windows.go -output zvhd.go vhd.go
|
||||||
|
|
||||||
//sys createVirtualDisk(virtualStorageType *virtualStorageType, path string, virtualDiskAccessMask uint32, securityDescriptor *uintptr, flags uint32, providerSpecificFlags uint32, parameters *createVirtualDiskParameters, o *syscall.Overlapped, handle *syscall.Handle) (err error) [failretval != 0] = VirtDisk.CreateVirtualDisk
|
//sys createVirtualDisk(virtualStorageType *virtualStorageType, path string, virtualDiskAccessMask uint32, securityDescriptor *uintptr, flags uint32, providerSpecificFlags uint32, parameters *createVirtualDiskParameters, o *syscall.Overlapped, handle *syscall.Handle) (err error) [failretval != 0] = VirtDisk.CreateVirtualDisk
|
||||||
|
|
||||||
type virtualStorageType struct {
|
type virtualStorageType struct {
|
||||||
DeviceID uint32
|
DeviceID uint32
|
||||||
VendorID [16]byte
|
VendorID [16]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
const virtualDiskAccessNONE uint32 = 0
|
const virtualDiskAccessNONE uint32 = 0
|
||||||
const virtualDiskAccessATTACHRO uint32 = 65536
|
const virtualDiskAccessATTACHRO uint32 = 65536
|
||||||
const virtualDiskAccessATTACHRW uint32 = 131072
|
const virtualDiskAccessATTACHRW uint32 = 131072
|
||||||
const virtualDiskAccessDETACH uint32 = 262144
|
const virtualDiskAccessDETACH uint32 = 262144
|
||||||
const virtualDiskAccessGETINFO uint32 = 524288
|
const virtualDiskAccessGETINFO uint32 = 524288
|
||||||
const virtualDiskAccessCREATE uint32 = 1048576
|
const virtualDiskAccessCREATE uint32 = 1048576
|
||||||
const virtualDiskAccessMETAOPS uint32 = 2097152
|
const virtualDiskAccessMETAOPS uint32 = 2097152
|
||||||
const virtualDiskAccessREAD uint32 = 851968
|
const virtualDiskAccessREAD uint32 = 851968
|
||||||
const virtualDiskAccessALL uint32 = 4128768
|
const virtualDiskAccessALL uint32 = 4128768
|
||||||
const virtualDiskAccessWRITABLE uint32 = 3276800
|
const virtualDiskAccessWRITABLE uint32 = 3276800
|
||||||
|
|
||||||
const createVirtualDiskFlagNone uint32 = 0
|
const createVirtualDiskFlagNone uint32 = 0
|
||||||
const createVirtualDiskFlagFullPhysicalAllocation uint32 = 1
|
const createVirtualDiskFlagFullPhysicalAllocation uint32 = 1
|
||||||
const createVirtualDiskFlagPreventWritesToSourceDisk uint32 = 2
|
const createVirtualDiskFlagPreventWritesToSourceDisk uint32 = 2
|
||||||
const createVirtualDiskFlagDoNotCopyMetadataFromParent uint32 = 4
|
const createVirtualDiskFlagDoNotCopyMetadataFromParent uint32 = 4
|
||||||
|
|
||||||
type version2 struct {
|
type version2 struct {
|
||||||
UniqueID [16]byte // GUID
|
UniqueID [16]byte // GUID
|
||||||
MaximumSize uint64
|
MaximumSize uint64
|
||||||
BlockSizeInBytes uint32
|
BlockSizeInBytes uint32
|
||||||
SectorSizeInBytes uint32
|
SectorSizeInBytes uint32
|
||||||
ParentPath *uint16 // string
|
ParentPath *uint16 // string
|
||||||
SourcePath *uint16 // string
|
SourcePath *uint16 // string
|
||||||
OpenFlags uint32
|
OpenFlags uint32
|
||||||
ParentVirtualStorageType virtualStorageType
|
ParentVirtualStorageType virtualStorageType
|
||||||
SourceVirtualStorageType virtualStorageType
|
SourceVirtualStorageType virtualStorageType
|
||||||
ResiliencyGUID [16]byte // GUID
|
ResiliencyGUID [16]byte // GUID
|
||||||
}
|
}
|
||||||
|
|
||||||
type createVirtualDiskParameters struct {
|
type createVirtualDiskParameters struct {
|
||||||
Version uint32 // Must always be set to 2
|
Version uint32 // Must always be set to 2
|
||||||
Version2 version2
|
Version2 version2
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateVhdx will create a simple vhdx file at the given path using default values.
|
// CreateVhdx will create a simple vhdx file at the given path using default values.
|
||||||
func CreateVhdx(path string, maxSizeInGb, blockSizeInMb uint32) error {
|
func CreateVhdx(path string, maxSizeInGb, blockSizeInMb uint32) error {
|
||||||
var defaultType virtualStorageType
|
var defaultType virtualStorageType
|
||||||
|
|
||||||
parameters := createVirtualDiskParameters{
|
parameters := createVirtualDiskParameters{
|
||||||
Version: 2,
|
Version: 2,
|
||||||
Version2: version2{
|
Version2: version2{
|
||||||
MaximumSize: uint64(maxSizeInGb) * 1024 * 1024 * 1024,
|
MaximumSize: uint64(maxSizeInGb) * 1024 * 1024 * 1024,
|
||||||
BlockSizeInBytes: blockSizeInMb * 1024 * 1024,
|
BlockSizeInBytes: blockSizeInMb * 1024 * 1024,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var handle syscall.Handle
|
var handle syscall.Handle
|
||||||
|
|
||||||
if err := createVirtualDisk(
|
if err := createVirtualDisk(
|
||||||
&defaultType,
|
&defaultType,
|
||||||
path,
|
path,
|
||||||
virtualDiskAccessNONE,
|
virtualDiskAccessNONE,
|
||||||
nil,
|
nil,
|
||||||
createVirtualDiskFlagNone,
|
createVirtualDiskFlagNone,
|
||||||
0,
|
0,
|
||||||
¶meters,
|
¶meters,
|
||||||
nil,
|
nil,
|
||||||
&handle); err != nil {
|
&handle); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := syscall.CloseHandle(handle); err != nil {
|
if err := syscall.CloseHandle(handle); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user