Esempio n. 1
0
func ReadFileContents(fileName string) (string, error) {
	file, err := wrappers.CreateFile(
		syscall.StringToUTF16Ptr(fileName),
		wrappers.GENERIC_READ,
		wrappers.FILE_SHARE_READ|wrappers.FILE_SHARE_WRITE|wrappers.FILE_SHARE_DELETE,
		nil,
		wrappers.OPEN_EXISTING,
		0,
		0)
	if err != nil {
		return "", NewWindowsError("CreateFile", err)
	}
	defer wrappers.CloseHandle(file)
	size, err := wrappers.GetFileSize(file, nil)
	if err != nil {
		return "", NewWindowsError("GetFileSize", err)
	}
	if size == 0 {
		return "", nil
	}
	buf := make([]byte, size)
	var bytesRead uint32
	if err := wrappers.ReadFile(file, &buf[0], size, &bytesRead, nil); err != nil {
		return "", NewWindowsError("ReadFile", err)
	}
	return string(buf[0:bytesRead]), nil
}
Esempio n. 2
0
func GetSymbolicLink(symlinkPath string) (*SymbolicLinkData, error) {
	file, err := wrappers.CreateFile(
		syscall.StringToUTF16Ptr(symlinkPath),
		wrappers.FILE_READ_EA,
		wrappers.FILE_SHARE_READ|wrappers.FILE_SHARE_WRITE|wrappers.FILE_SHARE_DELETE,
		nil,
		wrappers.OPEN_EXISTING,
		wrappers.FILE_FLAG_OPEN_REPARSE_POINT|wrappers.FILE_FLAG_BACKUP_SEMANTICS,
		0)
	if err != nil {
		return nil, NewWindowsError("CreateFile", err)
	}
	defer wrappers.CloseHandle(file)
	buf := make([]byte, wrappers.MAXIMUM_REPARSE_DATA_BUFFER_SIZE)
	var bytesReturned uint32
	err = wrappers.DeviceIoControl(
		file,
		wrappers.FSCTL_GET_REPARSE_POINT,
		nil,
		0,
		&buf[0],
		wrappers.MAXIMUM_REPARSE_DATA_BUFFER_SIZE,
		&bytesReturned,
		nil)
	if err != nil {
		return nil, NewWindowsError("DeviceIoControl", err)
	}
	data := (*wrappers.REPARSE_DATA_BUFFER)(unsafe.Pointer(&buf[0]))
	if data.ReparseTag != wrappers.IO_REPARSE_TAG_SYMLINK {
		return nil, nil
	}
	substituteNameBuf := make([]uint16, data.SubstituteNameLength/2)
	printNameBuf := make([]uint16, data.PrintNameLength/2)
	wrappers.RtlMoveMemory(
		(*byte)(unsafe.Pointer(&substituteNameBuf[0])),
		&buf[unsafe.Sizeof(*data)+uintptr(data.SubstituteNameOffset)],
		uintptr(data.SubstituteNameLength))
	wrappers.RtlMoveMemory(
		(*byte)(unsafe.Pointer(&printNameBuf[0])),
		&buf[unsafe.Sizeof(*data)+uintptr(data.PrintNameOffset)],
		uintptr(data.PrintNameLength))
	return &SymbolicLinkData{
		SubstituteName: syscall.UTF16ToString(substituteNameBuf),
		PrintName:      syscall.UTF16ToString(printNameBuf),
		Relative:       (data.Flags & wrappers.SYMLINK_FLAG_RELATIVE) != 0,
	}, nil
}
Esempio n. 3
0
func OpenWindowsFile(fileName string, readWrite bool, shareMode FileShareMode, creationDisposition FileCreationDisposition, attributes FileAttributes, flags FileFlags) (*os.File, error) {
	var accessMask uint32 = wrappers.GENERIC_READ
	if readWrite {
		accessMask |= wrappers.GENERIC_WRITE
	}
	file, err := wrappers.CreateFile(
		syscall.StringToUTF16Ptr(fileName),
		accessMask,
		uint32(shareMode),
		nil,
		uint32(creationDisposition),
		uint32(attributes)|uint32(flags),
		0)
	if err != nil {
		return nil, NewWindowsError("CreateFile", err)
	}
	return os.NewFile(uintptr(file), fileName), nil
}
Esempio n. 4
0
func GetDiskPerformanceInfo(rootPathName string) (*DiskPerformanceInfo, error) {
	hFile, err := wrappers.CreateFile(
		syscall.StringToUTF16Ptr(rootPathName),
		0,
		wrappers.FILE_SHARE_READ|wrappers.FILE_SHARE_WRITE,
		nil,
		wrappers.OPEN_EXISTING,
		0,
		0)
	if err != nil {
		return nil, NewWindowsError("CreateFile", err)
	}
	defer wrappers.CloseHandle(hFile)
	var diskPerformance wrappers.DISK_PERFORMANCE
	var diskPerformanceSize uint32
	err = wrappers.DeviceIoControl(
		hFile,
		wrappers.IOCTL_DISK_PERFORMANCE,
		nil,
		0,
		(*byte)(unsafe.Pointer(&diskPerformance)),
		uint32(unsafe.Sizeof(diskPerformance)),
		&diskPerformanceSize,
		nil)
	if err != nil {
		return nil, NewWindowsError("DeviceIoControl", err)
	}
	return &DiskPerformanceInfo{
		BytesRead:           diskPerformance.BytesRead,
		BytesWritten:        diskPerformance.BytesWritten,
		ReadTime:            diskPerformance.ReadTime,
		WriteTime:           diskPerformance.WriteTime,
		IdleTime:            diskPerformance.IdleTime,
		ReadCount:           uint(diskPerformance.ReadCount),
		WriteCount:          uint(diskPerformance.WriteCount),
		QueueDepth:          uint(diskPerformance.QueueDepth),
		SplitCount:          uint(diskPerformance.SplitCount),
		QueryTime:           diskPerformance.QueryTime,
		StorageDeviceNumber: uint(diskPerformance.StorageDeviceNumber),
		StorageManagerName:  syscall.UTF16ToString(diskPerformance.StorageManagerName[:]),
	}, nil
}