gopkg.in/ro-ag/posix.v1@v1.0.6/utils.go (about)

     1  //go:build darwin || linux
     2  // +build darwin linux
     3  
     4  package posix
     5  
     6  import (
     7  	"fmt"
     8  )
     9  
    10  func (m ModeT) valid(t ModeT) bool {
    11  	if m&t != 0 {
    12  		return true
    13  	}
    14  	return false
    15  }
    16  
    17  //S_ISBLK  block special
    18  func (m ModeT) S_ISBLK() bool { return m&S_IFMT == S_IFBLK }
    19  
    20  //S_ISCHR char special
    21  func (m ModeT) S_ISCHR() bool { return m&S_IFMT == S_IFCHR }
    22  
    23  //S_ISDIR directory
    24  func (m ModeT) S_ISDIR() bool { return m&S_IFMT == S_IFDIR }
    25  
    26  //S_ISFIFO fifo or socket
    27  func (m ModeT) S_ISFIFO() bool { return m&S_IFMT == S_IFIFO }
    28  
    29  //S_ISREG regular file
    30  func (m ModeT) S_ISREG() bool { return m&S_IFMT == S_IFREG }
    31  
    32  //S_ISLNK symbolic link
    33  func (m ModeT) S_ISLNK() bool { return m&S_IFMT == S_IFLNK }
    34  
    35  //S_ISSOCK socket
    36  func (m ModeT) S_ISSOCK() bool { return m&S_IFMT == S_IFSOCK }
    37  
    38  func (x DevT) major() DevT {
    39  	return x >> 24 & 0xff
    40  }
    41  func (x DevT) minor() DevT {
    42  	return x & 0xffffff
    43  }
    44  
    45  func FilePermStr(perm ModeT, flags int) string {
    46  
    47  	/* If FP_SPECIAL was specified, we emulate the trickery of ls(1) in
    48  	   returning set-user-ID, set-group-ID, and sticky bit information in
    49  	   the user/group/other execute fields. This is made more complex by
    50  	   the fact that the case of the character displayed for this bits
    51  	   depends on whether the corresponding execute bit is on or off. */
    52  
    53  	ru := '-'
    54  	if perm.valid(S_IRUSR) {
    55  		ru = 'r'
    56  	}
    57  	wu := '-'
    58  	if perm.valid(S_IWUSR) {
    59  		wu = 'w'
    60  	}
    61  	xu := ' '
    62  	if perm.valid(S_IXUSR) {
    63  		if perm.valid(S_ISUID) && flags&FP_SPECIAL != 0 {
    64  			xu = 's'
    65  		} else {
    66  			xu = 'x'
    67  		}
    68  	} else {
    69  		if perm.valid(S_ISUID) && flags&FP_SPECIAL != 0 {
    70  			xu = 's'
    71  		} else {
    72  			xu = '-'
    73  		}
    74  	}
    75  	rg := '-'
    76  	if perm.valid(S_IRGRP) {
    77  		rg = 'r'
    78  	}
    79  	wg := '-'
    80  	if perm.valid(S_IWGRP) {
    81  		wg = 'w'
    82  	}
    83  	xg := ' '
    84  	if perm.valid(S_IXGRP) {
    85  		if perm.valid(S_ISGID) && flags&FP_SPECIAL != 0 {
    86  			xg = 's'
    87  		} else {
    88  			xg = 'x'
    89  		}
    90  	} else {
    91  		if perm.valid(S_ISGID) && flags&FP_SPECIAL != 0 {
    92  			xg = 's'
    93  		} else {
    94  			xg = '-'
    95  		}
    96  	}
    97  	ro := '-'
    98  	if perm.valid(S_IROTH) {
    99  		ro = 'r'
   100  	}
   101  	wo := '-'
   102  	if perm.valid(S_IWOTH) {
   103  		wo = 'w'
   104  	}
   105  	xo := ' '
   106  	if perm.valid(S_IXOTH) {
   107  		if perm.valid(S_ISVTX) && flags&FP_SPECIAL != 0 {
   108  			xo = 's'
   109  		} else {
   110  			xo = 'x'
   111  		}
   112  	} else {
   113  		if perm.valid(S_ISVTX) && flags&FP_SPECIAL != 0 {
   114  			xo = 's'
   115  		} else {
   116  			xo = '-'
   117  		}
   118  	}
   119  
   120  	return fmt.Sprintf("[%04o] %c%c%c%c%c%c%c%c%c", perm, ru, wu, xu, rg, wg, xg, ro, wo, xo)
   121  }
   122  
   123  func (sb *Stat_t) DisplayStatInfo() {
   124  	fmt.Printf("File type:                ")
   125  	switch sb.Mode & S_IFMT {
   126  	case S_IFREG:
   127  		fmt.Println("regular file")
   128  	case S_IFDIR:
   129  		fmt.Println("directory")
   130  	case S_IFCHR:
   131  		fmt.Println("character device")
   132  	case S_IFBLK:
   133  		fmt.Println("block device")
   134  	case S_IFLNK:
   135  		fmt.Println("symbolic (soft) link")
   136  	case S_IFIFO:
   137  		fmt.Println("FIFO or pipe")
   138  	case S_IFSOCK:
   139  		fmt.Println("socket")
   140  	default:
   141  		fmt.Println("unknown file type?")
   142  	}
   143  	fmt.Printf("Device containing i-node: major=%d   minor=%d\n", sb.Dev.major(), sb.Dev.minor())
   144  	fmt.Printf("I-node number:            %d\n", sb.Ino)
   145  	fmt.Printf("Mode:                     %o (%s)\n", sb.Mode, FilePermStr(ModeT(sb.Mode), 0))
   146  
   147  	if sb.Mode.valid(S_ISUID | S_ISGID | S_ISVTX) {
   148  		uid, gid, sicky := "", "", ""
   149  		if sb.Mode.valid(S_ISUID) {
   150  			uid = "set-UID "
   151  		}
   152  		if sb.Mode.valid(S_ISGID) {
   153  			gid = "set-GID "
   154  		}
   155  		if sb.Mode.valid(S_ISVTX) {
   156  			sicky = "sticky "
   157  		}
   158  		fmt.Printf("    special bits set:     %s%s%s\n", uid, gid, sicky)
   159  	}
   160  
   161  	fmt.Printf("Number of (hard) links:   %d\n", sb.Nlink)
   162  	fmt.Printf("Ownership:                UID=%d   GID=%d\n", sb.Uid, sb.Gid)
   163  
   164  	if sb.Mode.S_ISCHR() || sb.Mode.S_ISBLK() {
   165  		fmt.Printf("Device number (st_rdev):  major=%d; minor=%d\n", sb.Rdev.major(), sb.Rdev.minor())
   166  	}
   167  
   168  	fmt.Printf("File size:                %d bytes\n", sb.Size)
   169  	fmt.Printf("Optimal I/O block size:   %d bytes\n", sb.Blksize)
   170  	fmt.Printf("512B blocks allocated:    %d\n", sb.Blocks)
   171  }