github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/cmd/system.go (about) 1 // +build !windows 2 3 package cmd 4 5 import ( 6 "fmt" 7 "strings" 8 "syscall" 9 10 "golang.org/x/crypto/ssh/terminal" 11 "golang.org/x/sys/unix" 12 ) 13 14 // preferredNumOpenFiles is the perferred number of open files that the process can have. 15 // This value tends to be the recommended value for `ulimit -n`, as seen on github discussions 16 // around various projects such as hugo, mongo, redis. 17 const preferredNumOpenFiles = 10000 18 19 // ensureLargeNumOpenFiles ensures that user can have a large number of open files 20 func ensureLargeNumOpenFiles() { 21 // Get the number of open files currently allowed. 22 var rLimit unix.Rlimit 23 err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rLimit) 24 if err != nil { 25 panic(err) 26 } 27 if rLimit.Cur >= preferredNumOpenFiles { 28 return 29 } 30 31 // Set the number of open files that are allowed to be sufficiently large. This avoids 32 // the error "too many open files" that often occurs when running IPFS or other 33 // local database-like technologies. 34 rLimit.Cur = preferredNumOpenFiles 35 rLimit.Max = preferredNumOpenFiles 36 37 err = unix.Setrlimit(unix.RLIMIT_NOFILE, &rLimit) 38 if err != nil { 39 if strings.Contains(err.Error(), "operation not permitted") { 40 // If permission was denied, just ignore the error silently. 41 return 42 } 43 fmt.Printf("error setting max open files limit: %s\n", err) 44 return 45 } 46 } 47 48 // stdoutIsTerminal returns whether stdout is writing to a terminal, as opposed to something 49 // like a pipe. For example, when running `qri get me/my_ds --format zip` this returns true, 50 // but when running `qri get me/my_ds --format zip | gzip -l` this returns false 51 func stdoutIsTerminal() bool { 52 return terminal.IsTerminal(syscall.Stdout) 53 } 54 55 // defaultFilePermMask is the user's default file permissions mask 56 func defaultFilePermMask() int { 57 mask := syscall.Umask(0) 58 syscall.Umask(mask) 59 return mask 60 } 61 62 // sizeOfTerminal returns the width and height of the terminal, or -1, -1 if there isn't one 63 func sizeOfTerminal() (int, int) { 64 width, height, err := terminal.GetSize(0) 65 if err != nil { 66 return -1, -1 67 } 68 return width, height 69 }