| // Copyright 2018 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| // +build fuchsia |
| |
| package fdio |
| |
| import ( |
| "syscall/zx" |
| "syscall/zx/fidl" |
| "syscall/zx/io" |
| ) |
| |
| func objectFromInfo(info *io.ObjectInfo, obj *io.ObjectInterface) (result FDIO, err error) { |
| if info == nil { |
| return nil, zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| switch info.ObjectInfoTag { |
| case io.ObjectInfoService: |
| // TODO(mknyszek): Figure out the correct type to return here. |
| result = &Object{ObjectInterface: obj} |
| case io.ObjectInfoFile: |
| result = &File{ |
| Node: Node{Object: Object{ObjectInterface: obj}}, |
| Event: info.File.Event, |
| } |
| case io.ObjectInfoDevice: |
| result = &File{ |
| Node: Node{Object: Object{ObjectInterface: obj}}, |
| Event: info.Device.Event, |
| } |
| case io.ObjectInfoDirectory: |
| result = &Directory{Node: Node{Object: Object{ObjectInterface: obj}}} |
| case io.ObjectInfoPipe: |
| result = NewPipe(info.Pipe.Socket) |
| case io.ObjectInfoVmofile: |
| result, err = NewVMOFile( |
| info.Vmofile.Vmo, |
| info.Vmofile.Offset, |
| info.Vmofile.Length, |
| ) |
| if err != nil { |
| return nil, err |
| } |
| default: |
| return nil, zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| return result, nil |
| } |
| |
| // Object is a wrapper around an ObjectInterface which implements FDIO. |
| type Object struct { |
| *io.ObjectInterface |
| } |
| |
| // IsValid returns true if the Object is valid. |
| func (o *Object) IsValid() bool { |
| if o.ObjectInterface == nil { |
| return false |
| } |
| return ((*fidl.Proxy)(o.ObjectInterface)).IsValid() |
| } |
| |
| // Handle returns the underlying channel as an untyped zx handle. |
| func (o *Object) Handles() []zx.Handle { |
| return []zx.Handle{ |
| zx.Handle(((*fidl.Proxy)(o.ObjectInterface)).Channel), |
| } |
| } |
| |
| // Clone makes a clone of the object. |
| func (o *Object) Clone() (FDIO, error) { |
| req, newObj, err := io.NewObjectInterfaceRequest() |
| if err != nil { |
| return nil, err |
| } |
| if err := o.ObjectInterface.Clone(io.KOpenFlagDescribe, req); err != nil { |
| return nil, err |
| } |
| status, info, err := newObj.ExpectOnOpen() |
| if err != nil { |
| return nil, err |
| } else if status != zx.ErrOk { |
| return nil, zx.Error{Status: status, Text: "io.object"} |
| } |
| return objectFromInfo(info, newObj) |
| } |
| |
| // Close closes the object. |
| func (o *Object) Close() error { |
| defer ((*fidl.Proxy)(o.ObjectInterface)).Close() |
| if status, err := o.ObjectInterface.Close(); err != nil { |
| return err |
| } else if status != zx.ErrOk { |
| return zx.Error{Status: status, Text: "io.object"} |
| } |
| return nil |
| } |
| |
| // Sync implements FDIO for Object. |
| func (o *Object) Sync() error { |
| return zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // GetAttr implements FDIO for Object. |
| func (o *Object) GetAttr() (io.NodeAttributes, error) { |
| return io.NodeAttributes{}, zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // SetAttr implements FDIO for Object. |
| func (o *Object) SetAttr(flags uint32, attr io.NodeAttributes) error { |
| return zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // Ioctl implements FDIO for Object. |
| func (o *Object) Ioctl(op uint32, max uint64, in []byte, handles []zx.Handle) ([]byte, []zx.Handle, error) { |
| return nil, nil, zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // Read implements FDIO for Object. |
| func (o *Object) Read(data []byte) (int, error) { |
| return 0, zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // ReadAt implements FDIO for Object. |
| func (o *Object) ReadAt(data []byte, off int64) (int, error) { |
| return 0, zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // Write implements FDIO for Object. |
| func (o *Object) Write(data []byte) (int, error) { |
| return 0, zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // WriteAt implements FDIO for Object. |
| func (o *Object) WriteAt(data []byte, off int64) (int, error) { |
| return 0, zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // Seek implements FDIO for Object. |
| func (o *Object) Seek(offset int64, whence int) (int64, error) { |
| return 0, zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // Truncate implements FDIO for Object. |
| func (o *Object) Truncate(length uint64) error { |
| return zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // Open implements FDIO for Object. |
| func (o *Object) Open(path string, flags uint32, mode uint32) (FDIO, error) { |
| return nil, zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // Link implements FDIO for Object. |
| func (o *Object) Link(oldpath, newpath string) error { |
| return zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // Rename implements FDIO for Object. |
| func (o *Object) Rename(oldpath, newpath string) error { |
| return zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // Unlink implements FDIO for Object. |
| func (o *Object) Unlink(path string) error { |
| return zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // ReadDirents implements FDIO for Object. |
| func (o *Object) ReadDirents(max uint64) ([]byte, error) { |
| return nil, zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |
| |
| // Rewind implements FDIO for Object. |
| func (o *Object) Rewind() error { |
| return zx.Error{Status: zx.ErrNotSupported, Text: "io.object"} |
| } |