github.com/shindo/goofys@v0.24.1-0.20210326210429-9e930f0b2d5c/internal/utils.go (about)

     1  // Copyright 2015 - 2017 Ka-Hing Cheung
     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 internal
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  	"unicode"
    21  
    22  	"github.com/jacobsa/fuse"
    23  	"github.com/shirou/gopsutil/process"
    24  )
    25  
    26  var TIME_MAX = time.Unix(1<<63-62135596801, 999999999)
    27  
    28  func MaxInt(a, b int) int {
    29  	if a > b {
    30  		return a
    31  	} else {
    32  		return b
    33  	}
    34  }
    35  
    36  func MinInt(a, b int) int {
    37  	if a < b {
    38  		return a
    39  	} else {
    40  		return b
    41  	}
    42  }
    43  
    44  func MaxInt64(a, b int64) int64 {
    45  	if a > b {
    46  		return a
    47  	} else {
    48  		return b
    49  	}
    50  }
    51  
    52  func MinInt64(a, b int64) int64 {
    53  	if a < b {
    54  		return a
    55  	} else {
    56  		return b
    57  	}
    58  }
    59  
    60  func MaxUInt32(a, b uint32) uint32 {
    61  	if a > b {
    62  		return a
    63  	} else {
    64  		return b
    65  	}
    66  }
    67  
    68  func MinUInt32(a, b uint32) uint32 {
    69  	if a < b {
    70  		return a
    71  	} else {
    72  		return b
    73  	}
    74  }
    75  
    76  func MaxUInt64(a, b uint64) uint64 {
    77  	if a > b {
    78  		return a
    79  	} else {
    80  		return b
    81  	}
    82  }
    83  
    84  func MinUInt64(a, b uint64) uint64 {
    85  	if a < b {
    86  		return a
    87  	} else {
    88  		return b
    89  	}
    90  }
    91  
    92  func PBool(v bool) *bool {
    93  	return &v
    94  }
    95  
    96  func PInt32(v int32) *int32 {
    97  	return &v
    98  }
    99  
   100  func PUInt32(v uint32) *uint32 {
   101  	return &v
   102  }
   103  
   104  func PInt64(v int64) *int64 {
   105  	return &v
   106  }
   107  
   108  func PUInt64(v uint64) *uint64 {
   109  	return &v
   110  }
   111  
   112  func PString(v string) *string {
   113  	return &v
   114  }
   115  
   116  func PTime(v time.Time) *time.Time {
   117  	return &v
   118  }
   119  
   120  func xattrEscape(value []byte) (s string) {
   121  	for _, c := range value {
   122  		if c == '%' {
   123  			s += "%25"
   124  		} else if unicode.IsPrint(rune(c)) {
   125  			s += string(c)
   126  		} else {
   127  			s += "%" + fmt.Sprintf("%02X", c)
   128  		}
   129  	}
   130  
   131  	return
   132  }
   133  
   134  func Dup(value []byte) []byte {
   135  	ret := make([]byte, len(value))
   136  	copy(ret, value)
   137  	return ret
   138  }
   139  
   140  func TryUnmount(mountPoint string) (err error) {
   141  	for i := 0; i < 20; i++ {
   142  		err = fuse.Unmount(mountPoint)
   143  		if err != nil {
   144  			time.Sleep(time.Second)
   145  		} else {
   146  			break
   147  		}
   148  	}
   149  	return
   150  }
   151  
   152  type empty struct{}
   153  
   154  // TODO(dotslash/khc): Remove this semaphore in favor of
   155  // https://godoc.org/golang.org/x/sync/semaphore
   156  type semaphore chan empty
   157  
   158  func (sem semaphore) P(n int) {
   159  	for i := 0; i < n; i++ {
   160  		sem <- empty{}
   161  	}
   162  }
   163  
   164  func (sem semaphore) V(n int) {
   165  	for i := 0; i < n; i++ {
   166  		<-sem
   167  	}
   168  }
   169  
   170  // GetTgid returns the tgid for the given pid.
   171  func GetTgid(pid uint32) (tgid *int32, err error) {
   172  	p, err := process.NewProcess(int32(pid))
   173  	if err != nil {
   174  		return nil, err
   175  	}
   176  	tgidVal, err := p.Tgid()
   177  	if err != nil {
   178  		return nil, err
   179  	}
   180  	return &tgidVal, nil
   181  }