本文整理匯總了Golang中syscall.CreateFile函數的典型用法代碼示例。如果您正苦於以下問題:Golang CreateFile函數的具體用法?Golang CreateFile怎麽用?Golang CreateFile使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了CreateFile函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: openConsole
// openConsole opens a console handle, using a backup if it fails.
// This is used to get the exact console handle instead of the redirected
// handles from panicwrap.
func openConsole(name string, backup *os.File) *os.File {
// Convert to UTF16
path, err := syscall.UTF16PtrFromString(name)
if err != nil {
log.Printf("[ERROR] wrappedstreams: %s", err)
return backup
}
// Determine the share mode
var shareMode uint32
switch name {
case "CONIN$":
shareMode = syscall.FILE_SHARE_READ
case "CONOUT$":
shareMode = syscall.FILE_SHARE_WRITE
}
// Get the file
h, err := syscall.CreateFile(
path,
syscall.GENERIC_READ|syscall.GENERIC_WRITE,
shareMode,
nil,
syscall.OPEN_EXISTING,
0, 0)
if err != nil {
log.Printf("[ERROR] wrappedstreams: %s", err)
return backup
}
// Create the Go file
return os.NewFile(uintptr(h), name)
}
示例2: loadFileId
func (s *winSys) loadFileId() error {
if s.path == "" {
// already done
return nil
}
s.Lock()
defer s.Unlock()
pathp, e := syscall.UTF16PtrFromString(s.path)
if e != nil {
return e
}
h, e := syscall.CreateFile(pathp, 0, 0, nil, syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS, 0)
if e != nil {
return e
}
defer syscall.CloseHandle(h)
var i syscall.ByHandleFileInformation
e = syscall.GetFileInformationByHandle(syscall.Handle(h), &i)
if e != nil {
return e
}
s.path = ""
s.vol = i.VolumeSerialNumber
s.idxhi = i.FileIndexHigh
s.idxlo = i.FileIndexLow
return nil
}
示例3: loadFileId
func (fs *fileStat) loadFileId() error {
fs.Lock()
defer fs.Unlock()
if fs.path == "" {
// already done
return nil
}
pathp, err := syscall.UTF16PtrFromString(fs.path)
if err != nil {
return err
}
h, err := syscall.CreateFile(pathp, 0, 0, nil, syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS, 0)
if err != nil {
return err
}
defer syscall.CloseHandle(h)
var i syscall.ByHandleFileInformation
err = syscall.GetFileInformationByHandle(syscall.Handle(h), &i)
if err != nil {
return err
}
fs.path = ""
fs.vol = i.VolumeSerialNumber
fs.idxhi = i.FileIndexHigh
fs.idxlo = i.FileIndexLow
return nil
}
示例4: openHandle
func (p *Port) openHandle() (err error) {
device := p.config.Device
// add a "\\.\" prefix, e.g. "\\.\COM2"
// optional for COM1-9, required for COM10 and up
//
if !strings.HasPrefix(device, `\\.\`) {
device = `\\.\` + device
}
device_utf16, err := syscall.UTF16PtrFromString(device)
if err != nil {
return
}
p.handle, err = syscall.CreateFile(device_utf16,
syscall.GENERIC_READ|syscall.GENERIC_WRITE,
0,
nil,
syscall.OPEN_EXISTING,
syscall.FILE_ATTRIBUTE_NORMAL,
0)
return
}
示例5: Read
func Read(path string) (string, uint32, error) {
ptr, err := syscall.UTF16PtrFromString(path)
if err != nil {
return "", protocol.FlagSymlinkMissingTarget, err
}
handle, err := syscall.CreateFile(ptr, 0, syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE, nil, syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS|Win32FileFlagOpenReparsePoint, 0)
if err != nil || handle == syscall.InvalidHandle {
return "", protocol.FlagSymlinkMissingTarget, err
}
defer syscall.Close(handle)
var ret uint16
var data reparseData
r1, _, err := syscall.Syscall9(procDeviceIoControl.Addr(), 8, uintptr(handle), Win32FsctlGetReparsePoint, 0, 0, uintptr(unsafe.Pointer(&data)), unsafe.Sizeof(data), uintptr(unsafe.Pointer(&ret)), 0, 0)
if r1 == 0 {
return "", protocol.FlagSymlinkMissingTarget, err
}
var flags uint32
attr, err := syscall.GetFileAttributes(ptr)
if err != nil {
flags = protocol.FlagSymlinkMissingTarget
} else if attr&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 {
flags = protocol.FlagDirectory
}
return osutil.NormalizedFilename(data.PrintName()), flags, nil
}
示例6: getIno
func getIno(path string) (ino *inode, err error) {
pathp, e := syscall.UTF16PtrFromString(path)
if e != nil {
return nil, e
}
h, e := syscall.CreateFile(pathp,
syscall.FILE_LIST_DIRECTORY,
syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE,
nil, syscall.OPEN_EXISTING,
syscall.FILE_FLAG_BACKUP_SEMANTICS|syscall.FILE_FLAG_OVERLAPPED, 0)
if e != nil {
return nil, os.NewSyscallError("CreateFile", e)
}
var fi syscall.ByHandleFileInformation
if e = syscall.GetFileInformationByHandle(h, &fi); e != nil {
syscall.CloseHandle(h)
return nil, os.NewSyscallError("GetFileInformationByHandle", e)
}
ino = &inode{
handle: h,
volume: fi.VolumeSerialNumber,
index: uint64(fi.FileIndexHigh)<<32 | uint64(fi.FileIndexLow),
}
return ino, nil
}
示例7: dial
// dial is a helper to initiate a connection to a named pipe that has been started by a server.
// The timeout is only enforced if the pipe server has already created the pipe, otherwise
// this function will return immediately.
func dial(address string, timeout uint32) (*PipeConn, error) {
name, err := syscall.UTF16PtrFromString(string(address))
if err != nil {
return nil, err
}
// If at least one instance of the pipe has been created, this function
// will wait timeout milliseconds for it to become available.
// It will return immediately regardless of timeout, if no instances
// of the named pipe have been created yet.
// If this returns with no error, there is a pipe available.
if err := waitNamedPipe(name, timeout); err != nil {
if err == error_bad_pathname {
// badly formatted pipe name
return nil, badAddr(address)
}
return nil, err
}
pathp, err := syscall.UTF16PtrFromString(address)
if err != nil {
return nil, err
}
handle, err := syscall.CreateFile(pathp, syscall.GENERIC_READ|syscall.GENERIC_WRITE,
uint32(syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE), nil, syscall.OPEN_EXISTING,
syscall.FILE_FLAG_OVERLAPPED, 0)
if err != nil {
return nil, err
}
return &PipeConn{handle: handle, addr: PipeAddr(address)}, nil
}
示例8: ReadSymlink
func (BasicFilesystem) ReadSymlink(path string) (string, LinkTargetType, error) {
ptr, err := syscall.UTF16PtrFromString(path)
if err != nil {
return "", LinkTargetUnknown, err
}
handle, err := syscall.CreateFile(ptr, 0, syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE, nil, syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS|win32FileFlagOpenReparsePoint, 0)
if err != nil || handle == syscall.InvalidHandle {
return "", LinkTargetUnknown, err
}
defer syscall.Close(handle)
var ret uint16
var data reparseData
r1, _, err := syscall.Syscall9(procDeviceIoControl.Addr(), 8, uintptr(handle), win32FsctlGetReparsePoint, 0, 0, uintptr(unsafe.Pointer(&data)), unsafe.Sizeof(data), uintptr(unsafe.Pointer(&ret)), 0, 0)
if r1 == 0 {
return "", LinkTargetUnknown, err
}
tt := LinkTargetUnknown
if attr, err := syscall.GetFileAttributes(ptr); err == nil {
if attr&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 {
tt = LinkTargetDirectory
} else {
tt = LinkTargetFile
}
}
return osutil.NormalizedFilename(data.printName()), tt, nil
}
示例9: OpenFileForRedirect
func OpenFileForRedirect(name string, read bool) (*os.File, error) {
var wmode, cmode uint32
if read {
wmode = syscall.GENERIC_READ
cmode = syscall.OPEN_EXISTING
} else {
wmode = syscall.GENERIC_WRITE
cmode = syscall.CREATE_ALWAYS
}
h, e := syscall.CreateFile(
syscall.StringToUTF16Ptr(name),
wmode,
syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE,
nil,
cmode,
win32.FILE_FLAG_SEQUENTIAL_SCAN,
0)
if e != nil {
return nil, e
}
return os.NewFile(uintptr(h), name), nil
}
示例10: open
func open(path string, flag int, perm os.FileMode) (*os.File, error) {
if path == "" {
return nil, fmt.Errorf("cannot open empty filename")
}
var access uint32
switch flag {
case syscall.O_RDONLY:
access = syscall.GENERIC_READ
case syscall.O_WRONLY:
access = syscall.GENERIC_WRITE
case syscall.O_RDWR:
access = syscall.GENERIC_READ | syscall.GENERIC_WRITE
case syscall.O_WRONLY | syscall.O_CREAT:
access = syscall.GENERIC_ALL
default:
panic(fmt.Errorf("flag %v is not supported", flag))
}
fd, err := syscall.CreateFile(&(syscall.StringToUTF16(path)[0]),
access,
syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE,
nil,
syscall.OPEN_ALWAYS,
syscall.FILE_ATTRIBUTE_NORMAL,
0)
if err != nil {
return nil, err
}
return os.NewFile(uintptr(fd), path), nil
}
示例11: dialSerial
func dialSerial(path string) (io.ReadWriteCloser, error) {
log.Debug("ron dialSerial: %v", path)
h, err := syscall.CreateFile(syscall.StringToUTF16Ptr(path),
syscall.GENERIC_READ|syscall.GENERIC_WRITE,
0,
nil,
syscall.OPEN_EXISTING,
syscall.FILE_ATTRIBUTE_NORMAL|syscall.FILE_FLAG_OVERLAPPED,
0)
if err != nil {
return nil, err
}
f := os.NewFile(uintptr(h), path)
ro, err := newOverlapped()
if err != nil {
return nil, err
}
wo, err := newOverlapped()
if err != nil {
return nil, err
}
conn := new(virtioPort)
conn.f = f
conn.fd = h
conn.ro = ro
conn.wo = wo
return conn, nil
}
示例12: OpenPort
func OpenPort(name string, baud int) (rwc io.ReadWriteCloser, err error) {
if len(name) > 0 && name[0] != '\\' {
name = "\\\\.\\" + name
}
fd, err := syscall.CreateFile(syscall.StringToUTF16Ptr(name),
syscall.GENERIC_READ|syscall.GENERIC_WRITE,
0,
nil,
syscall.OPEN_EXISTING,
syscall.FILE_ATTRIBUTE_NORMAL|syscall.FILE_FLAG_OVERLAPPED,
0)
if err != nil {
return nil, err
}
f := os.NewFile(uintptr(fd), name)
defer func() {
if err != nil {
f.Close()
}
}()
if err = syscallEvent(fd, "state"); err != nil {
fmt.Printf("[ERROR] oops! %#v\n", err)
return
}
if err = syscallEvent(fd, "setup"); err != nil {
fmt.Printf("[ERROR] oops! %#v\n", err)
return
}
if err = syscallEvent(fd, "timeouts"); err != nil {
fmt.Printf("[ERROR] oops! %#v\n", err)
return
}
if err = syscallEvent(fd, "mask"); err != nil {
fmt.Printf("[ERROR] oops! %#v\n", err)
return
}
ro, err := genOverlap()
if err != nil {
return
}
wo, err := genOverlap()
if err != nil {
return
}
dev := new(SerialDevice)
dev.f = f
dev.fd = fd
dev.ro = ro
dev.wo = wo
return dev, nil
}
示例13: Open
func Open(file string, inictl string) (p Port, err error) {
const (
access = syscall.GENERIC_READ | syscall.GENERIC_WRITE
sharemode = 0
createmode = syscall.OPEN_EXISTING
flags = win.FILE_FLAG_OVERLAPPED
)
// make sure COM interfaces with numbers >9 get prefixed properly
if match, _ := filepath.Match("[cC][oO][mM]1[0-9]", file); match {
file = `\\.\` + file
}
fd, e := syscall.CreateFile(syscall.StringToUTF16Ptr(file), access, sharemode, nil, createmode, flags, 0)
if e != nil {
goto error
}
goto try
error:
err = &os.PathError{"open", file, e}
return
try:
d := new(dev)
d.fd = fd
d.name = file
d.encaps = d
if err = d.Ctl(initDefault, inictl); err != nil {
return
}
d.initDone = true
if d.ev.r, e = win.CreateEvent(win.EvManualReset, !win.EvInitiallyOn); e != nil {
goto error
}
if d.ev.w, e = win.CreateEvent(win.EvManualReset, !win.EvInitiallyOn); e != nil {
goto error
}
cto := win.CommTimeouts{
// ReadIntervalTimeout: ^uint32(0),
// ReadTotalTimeoutMultiplier: ^uint32(0),
// ReadTotalTimeoutConstant: ^uint32(0)-1,
ReadIntervalTimeout: 10,
}
if e = win.SetCommTimeouts(d.fd, &cto); e != nil {
goto error
}
if e = win.SetupComm(d.fd, 4096, 4096); e != nil {
goto error
}
p = d
return
}
示例14: newHandle
func newHandle(c *Config) (handle syscall.Handle, err error) {
handle, err = syscall.CreateFile(
syscall.StringToUTF16Ptr(c.Address),
syscall.GENERIC_READ|syscall.GENERIC_WRITE,
0, // mode
nil, // security
syscall.OPEN_EXISTING, // create mode
0, // attributes
0) // templates
if err != nil {
return
}
defer func() {
if err != nil {
syscall.CloseHandle(handle)
}
}()
var dcb C.DCB
dcb.BaudRate = C.DWORD(c.BaudRate)
// Data bits
if c.DataBits == 0 {
dcb.ByteSize = 8
} else {
dcb.ByteSize = C.BYTE(c.DataBits)
}
// Stop bits
switch c.StopBits {
case 0, 1:
// Default is one stop bit.
dcb.StopBits = C.ONESTOPBIT
case 2:
dcb.StopBits = C.TWOSTOPBITS
default:
err = fmt.Errorf("serial: unsupported stop bits %v", c.StopBits)
return
}
// Parity
switch c.Parity {
case "", "E":
// Default parity mode is Even.
dcb.Parity = C.EVENPARITY
case "O":
dcb.Parity = C.ODDPARITY
case "N":
dcb.Parity = C.NOPARITY
default:
err = fmt.Errorf("serial: unsupported parity %v", c.Parity)
return
}
if C.SetCommState(C.HANDLE(handle), &dcb) == 0 {
err = fmt.Errorf("serial: could not set device state: %v", syscall.GetLastError())
return
}
return
}
示例15: MakeFileMutex
func MakeFileMutex(filename string) *FileMutex {
if filename == "" {
return &FileMutex{fd: INVALID_FILE_HANDLE}
}
fd, err := syscall.CreateFile(&(syscall.StringToUTF16(filename)[0]), syscall.GENERIC_READ|syscall.GENERIC_WRITE,
syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE, nil, syscall.OPEN_ALWAYS, syscall.FILE_ATTRIBUTE_NORMAL, 0)
if err != nil {
panic(err)
}
return &FileMutex{fd: fd}
}