| // 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. | |
| // Go's distribution tools attempt to compile everything; this file | |
| // depends on types that don't compile in not-Fuchsia. | |
| //go:build fuchsia | |
| package fdio | |
| import ( | |
| "syscall/zx" | |
| "syscall/zx/internal/context" | |
| "syscall/zx/io" | |
| ) | |
| var _ FDIO = (*File)(nil) | |
| // File is a wrapper around a Node which implements FDIO. | |
| type File struct { | |
| Node | |
| Event zx.Event | |
| } | |
| func NewFileWithCtx(proxy *io.FileWithCtxInterface, event zx.Event) *File { | |
| return &File{Node: Node{NodeWithCtxInterface: (*io.NodeWithCtxInterface)(proxy)}, Event: event} | |
| } | |
| // fileInterface returns the underlying File FIDL client proxy. | |
| func (f *File) fileInterface() *io.FileWithCtxInterface { | |
| return (*io.FileWithCtxInterface)(f.Node.NodeWithCtxInterface) | |
| } | |
| func (f *File) GetBackingMemory(flags io.VmoFlags) (io.File2GetBackingMemoryResult, error) { | |
| return f.fileInterface().GetBackingMemory(context.Background(), flags) | |
| } | |
| // Handles returns all the underlying handles for this file, | |
| // which is the control channel and an event. | |
| func (f *File) Handles() []zx.Handle { | |
| return append(f.Node.Handles(), zx.Handle(f.Event)) | |
| } | |
| // Close closes all the control channel and event associated with the file | |
| // object. | |
| func (f *File) Close() error { | |
| return newMultiError(f.Node.Close(), "nodeCloseErr", f.Event.Close(), "eventCloseErr") | |
| } | |
| // read reads data from a File. If off == -1 then it does a read | |
| // from whatever the seek offset is. | |
| func (f *File) read(data []byte, off int64) (int, error) { | |
| n := 0 | |
| for n < len(data) { | |
| bytesToRead := int(io.MaxBuf) | |
| if len(data)-n < int(io.MaxBuf) { | |
| bytesToRead = len(data) - n | |
| } | |
| out, err := func() ([]byte, error) { | |
| if off == -1 { | |
| result, err := f.fileInterface().Read(context.Background(), uint64(bytesToRead)) | |
| if err != nil { | |
| return nil, err | |
| } | |
| switch result.Which() { | |
| case io.File2ReadResultErr: | |
| return nil, &zx.Error{Status: zx.Status(result.Err), Text: "io.file.Read"} | |
| case io.File2ReadResultResponse: | |
| return result.Response.Data, nil | |
| default: | |
| panic("unknown variant") | |
| } | |
| } else { | |
| result, err := f.fileInterface().ReadAt( | |
| context.Background(), | |
| uint64(bytesToRead), | |
| uint64(int64(n)+off), | |
| ) | |
| if err != nil { | |
| return nil, err | |
| } | |
| switch result.Which() { | |
| case io.File2ReadAtResultErr: | |
| return nil, &zx.Error{Status: zx.Status(result.Err), Text: "io.file.ReadAt"} | |
| case io.File2ReadAtResultResponse: | |
| return result.Response.Data, nil | |
| default: | |
| panic("unknown variant") | |
| } | |
| } | |
| }() | |
| if err != nil { | |
| return n, err | |
| } | |
| if len(out) == 0 { | |
| return n, nil | |
| } | |
| n += copy(data[n:], out) | |
| // Stop at short read. | |
| if len(out) < bytesToRead { | |
| return n, nil | |
| } | |
| } | |
| return n, nil | |
| } | |
| // Read reads data from the internally held offset in the File. | |
| func (f *File) Read(data []byte) (int, error) { | |
| return f.read(data, -1) | |
| } | |
| // ReadAt reads data at an offset in a File. | |
| func (f *File) ReadAt(data []byte, off int64) (int, error) { | |
| return f.read(data, off) | |
| } | |
| // write writes data to a file. If off == -1 then the write occurs at | |
| // whatever the seek offset is. | |
| func (f *File) write(data []byte, off int64) (int, error) { | |
| n := 0 | |
| for n < len(data) { | |
| bytesToWrite := int(io.MaxBuf) | |
| if len(data)-n < int(io.MaxBuf) { | |
| bytesToWrite = len(data) - n | |
| } | |
| written, err := func() (uint64, error) { | |
| if off == -1 { | |
| result, err := f.fileInterface().Write( | |
| context.Background(), | |
| data[n:][:bytesToWrite], | |
| ) | |
| if err != nil { | |
| return 0, err | |
| } | |
| switch result.Which() { | |
| case io.File2WriteResultErr: | |
| return 0, &zx.Error{Status: zx.Status(result.Err), Text: "io.file.Write"} | |
| case io.File2WriteResultResponse: | |
| return result.Response.ActualCount, nil | |
| default: | |
| panic("unknown variant") | |
| } | |
| } else { | |
| result, err := f.fileInterface().WriteAt( | |
| context.Background(), | |
| data[n:][:bytesToWrite], | |
| uint64(int64(n)+off), | |
| ) | |
| if err != nil { | |
| return 0, err | |
| } | |
| switch result.Which() { | |
| case io.File2WriteAtResultErr: | |
| return 0, &zx.Error{Status: zx.Status(result.Err), Text: "io.file.WriteAt"} | |
| case io.File2WriteAtResultResponse: | |
| return result.Response.ActualCount, nil | |
| default: | |
| panic("unknown variant") | |
| } | |
| } | |
| }() | |
| if err != nil { | |
| return n, err | |
| } | |
| n += int(written) | |
| // Error on a short write. | |
| if int(written) < bytesToWrite { | |
| return n, &zx.Error{Status: zx.ErrIO, Text: "io.file"} | |
| } | |
| } | |
| return n, nil | |
| } | |
| // Write writes data at the internally held offset in the File. | |
| func (f *File) Write(data []byte) (int, error) { | |
| return f.write(data, -1) | |
| } | |
| // WriteAt writes data at an offset in a File. | |
| func (f *File) WriteAt(data []byte, off int64) (int, error) { | |
| return f.write(data, off) | |
| } | |
| // Seek moves the read/write head offset of the File. | |
| func (f *File) Seek(offset int64, whence int) (int64, error) { | |
| result, err := f.fileInterface().Seek(context.Background(), io.SeekOrigin(whence), offset) | |
| if err != nil { | |
| return -1, err | |
| } | |
| switch result.Which() { | |
| case io.File2SeekResultErr: | |
| return -1, &zx.Error{Status: zx.Status(result.Err), Text: "io.file.Seek"} | |
| case io.File2SeekResultResponse: | |
| return int64(result.Response.OffsetFromStart), nil | |
| default: | |
| panic("unknown variant") | |
| } | |
| } | |
| // Resize resizes a File. | |
| func (f *File) Resize(length uint64) error { | |
| result, err := f.fileInterface().Resize(context.Background(), length) | |
| if err != nil { | |
| return err | |
| } | |
| switch result.Which() { | |
| case io.File2ResizeResultErr: | |
| return &zx.Error{Status: zx.Status(result.Err), Text: "io.file.Seek"} | |
| case io.File2ResizeResultResponse: | |
| return nil | |
| default: | |
| panic("unknown variant") | |
| } | |
| } |