github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/syscalls/linux/flags.go (about)

     1  // Copyright 2018 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 linux
    16  
    17  import (
    18  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    19  	"github.com/SagerNet/gvisor/pkg/sentry/fs"
    20  )
    21  
    22  // flagsToPermissions returns a Permissions object from Linux flags.
    23  // This includes truncate permission if O_TRUNC is set in the mask.
    24  func flagsToPermissions(mask uint) (p fs.PermMask) {
    25  	if mask&linux.O_TRUNC != 0 {
    26  		p.Write = true
    27  	}
    28  	switch mask & linux.O_ACCMODE {
    29  	case linux.O_WRONLY:
    30  		p.Write = true
    31  	case linux.O_RDWR:
    32  		p.Write = true
    33  		p.Read = true
    34  	case linux.O_RDONLY:
    35  		p.Read = true
    36  	}
    37  	return
    38  }
    39  
    40  // linuxToFlags converts Linux file flags to a FileFlags object.
    41  func linuxToFlags(mask uint) fs.FileFlags {
    42  	return fs.FileFlags{
    43  		Direct:      mask&linux.O_DIRECT != 0,
    44  		DSync:       mask&(linux.O_DSYNC|linux.O_SYNC) != 0,
    45  		Sync:        mask&linux.O_SYNC != 0,
    46  		NonBlocking: mask&linux.O_NONBLOCK != 0,
    47  		Read:        (mask & linux.O_ACCMODE) != linux.O_WRONLY,
    48  		Write:       (mask & linux.O_ACCMODE) != linux.O_RDONLY,
    49  		Append:      mask&linux.O_APPEND != 0,
    50  		Directory:   mask&linux.O_DIRECTORY != 0,
    51  		Async:       mask&linux.O_ASYNC != 0,
    52  		LargeFile:   mask&linux.O_LARGEFILE != 0,
    53  		Truncate:    mask&linux.O_TRUNC != 0,
    54  	}
    55  }