github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fsimpl/ext/disklayout/inode_old.go (about)

     1  // Copyright 2019 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package disklayout
    16  
    17  import (
    18  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    19  	"github.com/SagerNet/gvisor/pkg/sentry/kernel/auth"
    20  	"github.com/SagerNet/gvisor/pkg/sentry/kernel/time"
    21  )
    22  
    23  const (
    24  	// OldInodeSize is the inode size in ext2/ext3.
    25  	OldInodeSize = 128
    26  )
    27  
    28  // InodeOld implements Inode interface. It emulates ext2/ext3 inode struct.
    29  // Inode struct size and record size are both 128 bytes for this.
    30  //
    31  // All fields representing time are in seconds since the epoch. Which means that
    32  // they will overflow in January 2038.
    33  //
    34  // +marshal
    35  type InodeOld struct {
    36  	ModeRaw uint16
    37  	UIDLo   uint16
    38  	SizeLo  uint32
    39  
    40  	// The time fields are signed integers because they could be negative to
    41  	// represent time before the epoch.
    42  	AccessTimeRaw       int32
    43  	ChangeTimeRaw       int32
    44  	ModificationTimeRaw int32
    45  	DeletionTimeRaw     int32
    46  
    47  	GIDLo         uint16
    48  	LinksCountRaw uint16
    49  	BlocksCountLo uint32
    50  	FlagsRaw      uint32
    51  	VersionLo     uint32 // This is OS dependent.
    52  	DataRaw       [60]byte
    53  	Generation    uint32
    54  	FileACLLo     uint32
    55  	SizeHi        uint32
    56  	ObsoFaddr     uint32
    57  
    58  	// OS dependent fields have been inlined here.
    59  	BlocksCountHi uint16
    60  	FileACLHi     uint16
    61  	UIDHi         uint16
    62  	GIDHi         uint16
    63  	ChecksumLo    uint16
    64  	_             uint16
    65  }
    66  
    67  // Compiles only if InodeOld implements Inode.
    68  var _ Inode = (*InodeOld)(nil)
    69  
    70  // Mode implements Inode.Mode.
    71  func (in *InodeOld) Mode() linux.FileMode { return linux.FileMode(in.ModeRaw) }
    72  
    73  // UID implements Inode.UID.
    74  func (in *InodeOld) UID() auth.KUID {
    75  	return auth.KUID((uint32(in.UIDHi) << 16) | uint32(in.UIDLo))
    76  }
    77  
    78  // GID implements Inode.GID.
    79  func (in *InodeOld) GID() auth.KGID {
    80  	return auth.KGID((uint32(in.GIDHi) << 16) | uint32(in.GIDLo))
    81  }
    82  
    83  // Size implements Inode.Size.
    84  func (in *InodeOld) Size() uint64 {
    85  	// In ext2/ext3, in.SizeHi did not exist, it was instead named in.DirACL.
    86  	return uint64(in.SizeLo)
    87  }
    88  
    89  // InodeSize implements Inode.InodeSize.
    90  func (in *InodeOld) InodeSize() uint16 { return OldInodeSize }
    91  
    92  // AccessTime implements Inode.AccessTime.
    93  func (in *InodeOld) AccessTime() time.Time {
    94  	return time.FromUnix(int64(in.AccessTimeRaw), 0)
    95  }
    96  
    97  // ChangeTime implements Inode.ChangeTime.
    98  func (in *InodeOld) ChangeTime() time.Time {
    99  	return time.FromUnix(int64(in.ChangeTimeRaw), 0)
   100  }
   101  
   102  // ModificationTime implements Inode.ModificationTime.
   103  func (in *InodeOld) ModificationTime() time.Time {
   104  	return time.FromUnix(int64(in.ModificationTimeRaw), 0)
   105  }
   106  
   107  // DeletionTime implements Inode.DeletionTime.
   108  func (in *InodeOld) DeletionTime() time.Time {
   109  	return time.FromUnix(int64(in.DeletionTimeRaw), 0)
   110  }
   111  
   112  // LinksCount implements Inode.LinksCount.
   113  func (in *InodeOld) LinksCount() uint16 { return in.LinksCountRaw }
   114  
   115  // Flags implements Inode.Flags.
   116  func (in *InodeOld) Flags() InodeFlags { return InodeFlagsFromInt(in.FlagsRaw) }
   117  
   118  // Data implements Inode.Data.
   119  func (in *InodeOld) Data() []byte { return in.DataRaw[:] }