github.com/hanwen/go-fuse@v1.0.0/fuse/attr.go (about)

     1  // Copyright 2016 the Go-FUSE Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fuse
     6  
     7  import (
     8  	"os"
     9  	"syscall"
    10  	"time"
    11  )
    12  
    13  func (a *Attr) IsFifo() bool { return (uint32(a.Mode) & syscall.S_IFMT) == syscall.S_IFIFO }
    14  
    15  // IsChar reports whether the FileInfo describes a character special file.
    16  func (a *Attr) IsChar() bool { return (uint32(a.Mode) & syscall.S_IFMT) == syscall.S_IFCHR }
    17  
    18  // IsDir reports whether the FileInfo describes a directory.
    19  func (a *Attr) IsDir() bool { return (uint32(a.Mode) & syscall.S_IFMT) == syscall.S_IFDIR }
    20  
    21  // IsBlock reports whether the FileInfo describes a block special file.
    22  func (a *Attr) IsBlock() bool { return (uint32(a.Mode) & syscall.S_IFMT) == syscall.S_IFBLK }
    23  
    24  // IsRegular reports whether the FileInfo describes a regular file.
    25  func (a *Attr) IsRegular() bool { return (uint32(a.Mode) & syscall.S_IFMT) == syscall.S_IFREG }
    26  
    27  // IsSymlink reports whether the FileInfo describes a symbolic link.
    28  func (a *Attr) IsSymlink() bool { return (uint32(a.Mode) & syscall.S_IFMT) == syscall.S_IFLNK }
    29  
    30  // IsSocket reports whether the FileInfo describes a socket.
    31  func (a *Attr) IsSocket() bool { return (uint32(a.Mode) & syscall.S_IFMT) == syscall.S_IFSOCK }
    32  
    33  func (a *Attr) SetTimes(access *time.Time, mod *time.Time, chstatus *time.Time) {
    34  	if access != nil {
    35  		a.Atime = uint64(access.Unix())
    36  		a.Atimensec = uint32(access.Nanosecond())
    37  	}
    38  	if mod != nil {
    39  		a.Mtime = uint64(mod.Unix())
    40  		a.Mtimensec = uint32(mod.Nanosecond())
    41  	}
    42  	if chstatus != nil {
    43  		a.Ctime = uint64(chstatus.Unix())
    44  		a.Ctimensec = uint32(chstatus.Nanosecond())
    45  	}
    46  }
    47  
    48  func (a *Attr) ChangeTime() time.Time {
    49  	return time.Unix(int64(a.Ctime), int64(a.Ctimensec))
    50  }
    51  
    52  func (a *Attr) AccessTime() time.Time {
    53  	return time.Unix(int64(a.Atime), int64(a.Atimensec))
    54  }
    55  
    56  func (a *Attr) ModTime() time.Time {
    57  	return time.Unix(int64(a.Mtime), int64(a.Mtimensec))
    58  }
    59  
    60  func ToStatT(f os.FileInfo) *syscall.Stat_t {
    61  	s, _ := f.Sys().(*syscall.Stat_t)
    62  	if s != nil {
    63  		return s
    64  	}
    65  	return nil
    66  }
    67  
    68  func ToAttr(f os.FileInfo) *Attr {
    69  	if f == nil {
    70  		return nil
    71  	}
    72  	s := ToStatT(f)
    73  	if s != nil {
    74  		a := &Attr{}
    75  		a.FromStat(s)
    76  		return a
    77  	}
    78  	return nil
    79  }