This repository has been archived by the owner on Oct 29, 2019. It is now read-only.
/
util.go
146 lines (126 loc) · 2.64 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"archive/tar"
"bufio"
"bytes"
"io"
"os"
"path/filepath"
"syscall"
"unsafe"
)
type LineReader interface {
ReadLine() ([]byte, bool, error)
}
// ReadLines reads from r, one line at a time, and returns the read
// lines as a [][]byte. If it encounters any errors, it returns nil
// and the error. It does not return io.EOF.
func ReadLines(r io.Reader, trim bool) ([][]byte, error) {
var lr LineReader
if rt, ok := r.(LineReader); ok {
lr = rt
} else {
lr = bufio.NewReader(r)
}
var eof bool
var lines [][]byte
for !eof {
var line []byte
for {
part, pre, err := lr.ReadLine()
if err != nil {
if err != io.EOF {
return nil, err
} else {
eof = true
}
}
line = append(line, part...)
if !pre {
break
}
}
if trim {
line = bytes.TrimSpace(line)
}
lines = append(lines, line)
}
return lines, nil
}
// ExtractTar extracts the contents of tr to the given dir. It
// returns an error, if any.
func ExtractTar(dir string, tr *tar.Reader) error {
for {
hdr, err := tr.Next()
if err != nil {
if err == io.EOF {
break
}
return err
}
if hdr.Typeflag == tar.TypeDir {
err = os.MkdirAll(filepath.Join(dir, hdr.Name), 0755)
if err != nil {
return err
}
} else {
file, err := os.OpenFile(filepath.Join(dir, hdr.Name),
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
os.FileMode(hdr.Mode),
)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, tr)
if err != nil {
return err
}
}
}
return nil
}
// SplitArgs is a convience function that seperates pkgs from other
// arguments.
func SplitArgs(args ...string) (pacargs []string, pkgs []string) {
for i, arg := range args {
if arg == "--" {
pkgs = append(pkgs, args[i+1:]...)
break
}
if arg[0] == '-' {
pacargs = append(pacargs, arg)
} else {
pkgs = append(pkgs, arg)
}
}
return
}
// Copy of exp/terminal's IsTerminal() function.
func IsTerminal(fd int) bool {
var t syscall.Termios
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.TCGETS), uintptr(unsafe.Pointer(&t)), 0, 0, 0)
return err == 0
}
// CheckConfOption checks if a given option is set in pacman.conf.
//
// TODO: Add support for configuration files that aren't located at
// /etc/pacman.conf.
func CheckConfOption(opt string) (bool, error) {
file, err := os.Open("/etc/pacman.conf")
if err != nil {
return false, err
}
defer file.Close()
lines, err := ReadLines(file, true)
if err != nil {
return false, err
}
optS := []byte(opt)
for _, line := range lines {
if bytes.Equal(line, optS) {
return true, nil
}
}
return false, nil
}