| // Copyright 2016 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. |
| // +build fuchsia |
| |
| package fdio |
| |
| import ( |
| "syscall/zx" |
| "syscall/zx/io" |
| ) |
| |
| type VMOFile struct { |
| h zx.VMO |
| off int64 |
| len int64 |
| end int64 |
| at int64 |
| } |
| |
| func NewVMOFile(handle zx.VMO, offset uint64, length uint64) (*VMOFile, error) { |
| // fdio_vmofile_create |
| f := &VMOFile{ |
| h: handle, |
| off: int64(offset), |
| len: int64(length), |
| } |
| f.end = f.off + f.len |
| f.at = f.off |
| return f, nil |
| } |
| |
| func (f *VMOFile) Handles() []zx.Handle { |
| return []zx.Handle{zx.Handle(f.h)} |
| } |
| |
| func (f *VMOFile) Clone() (FDIO, error) { |
| return nil, &zx.Error{Status: zx.ErrNotSupported, Text: "fdio.VMOFile"} |
| } |
| |
| func (f *VMOFile) Close() error { |
| if err := f.h.Close(); err != nil { |
| return err |
| } |
| f.h = 0 |
| return nil |
| } |
| |
| func (f *VMOFile) Sync() error { |
| return &zx.Error{Status: zx.ErrNotSupported, Text: "fdio.VMOFile"} |
| } |
| |
| func (f *VMOFile) GetAttr() (io.NodeAttributes, error) { |
| return io.NodeAttributes{ |
| ContentSize: uint64(f.len), |
| StorageSize: uint64(f.len), |
| Mode: uint32(io.ModeTypeFile) | uint32(VtypeIRUSR), |
| }, nil |
| } |
| |
| func (f *VMOFile) SetAttr(flags uint32, attr io.NodeAttributes) error { |
| return &zx.Error{Status: zx.ErrNotSupported, Text: "fdio.VMOFile"} |
| } |
| |
| func (f *VMOFile) Read(data []byte) (n int, err error) { |
| if int64(len(data)) > f.end-f.at { |
| data = data[:f.end-f.at] |
| err = zx.EOF |
| } |
| if len(data) == 0 { |
| return 0, err |
| } |
| if err2 := f.h.Read(data, uint64(f.at)); err2 != nil { |
| return 0, err2 |
| } |
| n = len(data) |
| f.at += int64(n) |
| return |
| } |
| |
| func (f *VMOFile) ReadAt(data []byte, off int64) (n int, err error) { |
| if off < 0 { |
| return 0, &zx.Error{Status: zx.ErrInvalidArgs, Text: "fdio.VMOFile.ReadAt"} |
| } |
| if off >= f.end { |
| return 0, zx.EOF |
| } |
| if len(data) == 0 { |
| return 0, zx.EOF |
| } |
| n = len(data) |
| max := int(f.end - off) |
| if max < n { |
| n = max |
| err = zx.EOF |
| } |
| data = data[:n] |
| if err2 := f.h.Read(data, uint64(off)); err2 != nil { |
| return 0, err2 |
| } |
| return |
| } |
| |
| func (f *VMOFile) Write(data []byte) (int, error) { |
| return 0, &zx.Error{Status: zx.ErrNotSupported, Text: "fdio.VMOFile"} |
| } |
| |
| func (f *VMOFile) WriteAt(data []byte, off int64) (int, error) { |
| return 0, &zx.Error{Status: zx.ErrNotSupported, Text: "fdio.VMOFile"} |
| } |
| |
| func (f *VMOFile) Seek(offset int64, whence int) (int64, error) { |
| var at int64 |
| switch whence { |
| case 0: // SEEK_SET |
| at = offset |
| case 1: // SEEK_CUR |
| at = f.at - f.off + offset |
| case 2: // SEEK_END |
| at = f.end - f.off + offset |
| } |
| |
| if at > f.end-f.off { |
| return 0, &zx.Error{Status: zx.ErrOutOfRange, Text: "fdio.VMOFile"} |
| } |
| f.at = f.off + at |
| return at, nil |
| } |
| |
| func (f *VMOFile) Truncate(length uint64) error { |
| return &zx.Error{Status: zx.ErrNotSupported, Text: "fdio.VMOFile"} |
| } |
| |
| func (f *VMOFile) Open(path string, flags uint32, mode uint32) (FDIO, error) { |
| return nil, &zx.Error{Status: zx.ErrNotSupported, Text: "fdio.VMOFile"} |
| } |
| |
| func (f *VMOFile) Link(oldpath, newpath string) error { |
| return &zx.Error{Status: zx.ErrNotSupported, Text: "fdio.VMOFile"} |
| } |
| |
| func (f *VMOFile) Rename(oldpath, newpath string) error { |
| return &zx.Error{Status: zx.ErrNotSupported, Text: "fdio.VMOFile"} |
| } |
| |
| func (f *VMOFile) Unlink(path string) error { |
| return &zx.Error{Status: zx.ErrNotSupported, Text: "fdio.VMOFile"} |
| } |
| |
| func (f *VMOFile) ReadDirents(max uint64) ([]byte, error) { |
| return nil, &zx.Error{Status: zx.ErrNotSupported, Text: "fdio.VMOFile"} |
| } |
| |
| func (f *VMOFile) Rewind() error { |
| return &zx.Error{Status: zx.ErrNotSupported, Text: "fdio.VMOFile"} |
| } |