dirent_linux.mx raw

   1  //go:build !baremetal && !js && !wasip1 && !wasip2 && !wasm_unknown && !nintendoswitch
   2  
   3  // Copyright 2020 The Go Authors. All rights reserved.
   4  // Use of this source code is governed by a BSD-style
   5  // license that can be found in the LICENSE file.
   6  
   7  package os
   8  
   9  import (
  10  	"syscall"
  11  	"unsafe"
  12  )
  13  
  14  func direntIno(buf []byte) (uint64, bool) {
  15  	return readInt(buf, unsafe.Offsetof(syscall.Dirent{}.Ino), unsafe.Sizeof(syscall.Dirent{}.Ino))
  16  }
  17  
  18  func direntReclen(buf []byte) (uint64, bool) {
  19  	return readInt(buf, unsafe.Offsetof(syscall.Dirent{}.Reclen), unsafe.Sizeof(syscall.Dirent{}.Reclen))
  20  }
  21  
  22  func direntNamlen(buf []byte) (uint64, bool) {
  23  	reclen, ok := direntReclen(buf)
  24  	if !ok {
  25  		return 0, false
  26  	}
  27  	return reclen - uint64(unsafe.Offsetof(syscall.Dirent{}.Name)), true
  28  }
  29  
  30  func direntType(buf []byte) FileMode {
  31  	off := unsafe.Offsetof(syscall.Dirent{}.Type)
  32  	if off >= uintptr(len(buf)) {
  33  		return ^FileMode(0) // unknown
  34  	}
  35  	typ := buf[off]
  36  	switch typ {
  37  	case syscall.DT_BLK:
  38  		return ModeDevice
  39  	case syscall.DT_CHR:
  40  		return ModeDevice | ModeCharDevice
  41  	case syscall.DT_DIR:
  42  		return ModeDir
  43  	case syscall.DT_FIFO:
  44  		return ModeNamedPipe
  45  	case syscall.DT_LNK:
  46  		return ModeSymlink
  47  	case syscall.DT_REG:
  48  		return 0
  49  	case syscall.DT_SOCK:
  50  		return ModeSocket
  51  	}
  52  	return ^FileMode(0) // unknown
  53  }
  54