From a6c0881bfdb55a78dca3dcd4e6f4b43a65594696 Mon Sep 17 00:00:00 2001 From: Ron Minnich Date: Sat, 4 Nov 2023 12:19:13 -0600 Subject: [PATCH] windows support: correctly set mode, add inode() Signed-off-by: Ron Minnich --- client/cpu9p_windows.go | 140 ++++++++++++++++++++------------------ client/getattr_windows.go | 61 ----------------- 2 files changed, 75 insertions(+), 126 deletions(-) delete mode 100644 client/getattr_windows.go diff --git a/client/cpu9p_windows.go b/client/cpu9p_windows.go index 694fccdd..e6e2d8c4 100644 --- a/client/cpu9p_windows.go +++ b/client/cpu9p_windows.go @@ -1,18 +1,81 @@ -// Copyright 2022 the u-root Authors. All rights reserved -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Copyright 2018 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package client import ( "errors" - "fmt" "os" - "time" "github.com/hugelgupf/p9/p9" ) +// GetAttr implements p9.File.GetAttr. +// +// Not fully implemented. +func (l *CPU9P) GetAttr(req p9.AttrMask) (p9.QID, p9.AttrMask, p9.Attr, error) { + qid, fi, err := l.info() + if err != nil { + return qid, p9.AttrMask{}, p9.Attr{}, err + } + + t := uint64(fi.ModTime().Unix()) + // In the unix-style code, we derive the Mode directly + // from the syscall.Stat_t. That is not available on + // Windows, so we have to reconstruct some mode bits + // in other ways. + mode := p9.FileMode(fi.Mode()) + if fi.IsDir() { + mode |= p9.ModeDirectory + } + attr := p9.Attr{ + Mode: mode, + UID: 0, + GID: 0, + NLink: 3, + Size: uint64(fi.Size()), + BlockSize: uint64(512), + Blocks: uint64(fi.Size() / 512), + ATimeSeconds: t, + ATimeNanoSeconds: 0, + MTimeSeconds: t, + MTimeNanoSeconds: 0, + CTimeSeconds: t, + CTimeNanoSeconds: 0, + } + valid := p9.AttrMask{ + Mode: true, + UID: true, + GID: true, + NLink: true, + RDev: true, + Size: true, + Blocks: true, + ATime: true, + MTime: true, + CTime: true, + } + + return qid, valid, attr, nil +} + +// Lock implements p9.File.Lock. +// Well, not really. +func (l *CPU9P) Lock(pid int, locktype p9.LockType, flags p9.LockFlags, start, length uint64, client string) (p9.LockStatus, error) { + return p9.LockStatusOK, nil +} + // SetAttr implements p9.File.SetAttr. func (l *CPU9P) SetAttr(mask p9.SetAttrMask, attr p9.SetAttr) error { var err error @@ -30,82 +93,29 @@ func (l *CPU9P) SetAttr(mask p9.SetAttrMask, attr p9.SetAttr) error { // The test actually caught this ... if mask.Size { - panic("truncate") - // if e := unix.Truncate(l.path, int64(attr.Size)); e != nil { - // err = errors.Join(err, e) - // } + err = errors.Join(err, os.ErrInvalid) } if mask.ATime || mask.MTime { - atime, mtime := time.Now(), time.Now() - if mask.ATimeNotSystemTime { - atime = time.Unix(int64(attr.ATimeSeconds), int64(attr.ATimeNanoSeconds)) - } - if mask.MTimeNotSystemTime { - mtime = time.Unix(int64(attr.MTimeSeconds), int64(attr.MTimeNanoSeconds)) - } - if e := os.Chtimes(l.path, atime, mtime); e != nil { - err = errors.Join(err, e) - } + err = errors.Join(err, os.ErrInvalid) } if mask.CTime { - // The Linux client sets CTime. I did not even know that was allowed. - // if e := errors.New("Can not set CTime on Unix"); e != nil { err = errors.Join(e)} + err = errors.Join(err, os.ErrInvalid) verbose("mask.CTime is set by client; ignoring") } if mask.Permissions { - err = errors.Join(err, fmt.Errorf("chmod: %w", os.ErrInvalid)) + err = errors.Join(err, os.ErrInvalid) } if mask.GID { - err = errors.Join(err, fmt.Errorf("chgrp: %w", os.ErrInvalid)) - + err = errors.Join(err, os.ErrInvalid) } if mask.UID { - err = errors.Join(err, fmt.Errorf("chown: %w", os.ErrInvalid)) + err = errors.Join(err, os.ErrInvalid) } return err } -// Lock implements p9.File.Lock. -func (l *CPU9P) Lock(pid int, locktype p9.LockType, flags p9.LockFlags, start, length uint64, client string) (p9.LockStatus, error) { - return p9.LockStatusError, os.ErrInvalid - // var cmd int - - // switch flags { - // case p9.LockFlagsBlock: - // cmd = unix.F_SETLKW - // case p9.LockFlagsReclaim: - // return p9.LockStatusError, os.ErrInvalid - // default: - // cmd = unix.F_SETLK - // } - // var t int16 - // switch locktype { - // case p9.ReadLock: - // t = unix.F_RDLCK - // case p9.WriteLock: - // t = unix.F_WRLCK - // case p9.Unlock: - // t = unix.F_UNLCK - // default: - // return p9.LockStatusError, os.ErrInvalid - // } - // lk := &unix.Flock_t{ - // Type: t, - // Whence: unix.SEEK_SET, - // Start: int64(start), - // Len: int64(length), - // } - // if err := unix.FcntlFlock(l.file.Fd(), cmd, lk); err != nil { - // if errors.Is(err, unix.EAGAIN) { - // return p9.LockStatusBlocked, nil - // } - // return p9.LockStatusError, err - // } - // return p9.LockStatusOK, nil -} - // SetXattr implements p9.File.SetXattr func (l *CPU9P) SetXattr(attr string, data []byte, flags p9.XattrFlags) error { return os.ErrInvalid @@ -127,5 +137,5 @@ func (l *CPU9P) RemoveXattr(attr string) error { } func inode(_ os.FileInfo) uint64 { - return 1 + return 0xd00dfeed } diff --git a/client/getattr_windows.go b/client/getattr_windows.go deleted file mode 100644 index 8aa64f8e..00000000 --- a/client/getattr_windows.go +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package client - -import ( - "github.com/hugelgupf/p9/p9" -) - -// GetAttr implements p9.File.GetAttr. -// -// Not fully implemented. -func (l *CPU9P) GetAttr(req p9.AttrMask) (p9.QID, p9.AttrMask, p9.Attr, error) { - qid, fi, err := l.info() - if err != nil { - return qid, p9.AttrMask{}, p9.Attr{}, err - } - - attr := p9.Attr{ - Mode: p9.FileMode(fi.Mode()), - /* UID: p9.UID(stat.Uid), - GID: p9.GID(stat.Gid), - NLink: p9.NLink(stat.Nlink), - RDev: p9.Dev(stat.Rdev), - Size: uint64(stat.Size), - BlockSize: uint64(stat.Blksize), - Blocks: uint64(stat.Blocks), - ATimeSeconds: uint64(stat.Atimespec.Sec), - ATimeNanoSeconds: uint64(stat.Atimespec.Nsec), - MTimeSeconds: uint64(stat.Mtimespec.Sec), - MTimeNanoSeconds: uint64(stat.Mtimespec.Nsec), - CTimeSeconds: uint64(stat.Ctimespec.Sec), - CTimeNanoSeconds: uint64(stat.Ctimespec.Nsec), - */ - } - valid := p9.AttrMask{ - Mode: true, - UID: true, - GID: true, - NLink: true, - RDev: true, - Size: true, - Blocks: true, - ATime: true, - MTime: true, - CTime: true, - } - - return qid, valid, attr, nil -}