/
info_encoder.go
89 lines (77 loc) · 1.83 KB
/
info_encoder.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
package processor
import (
"encoding/json"
"fmt"
"os"
"os/user"
"syscall"
)
type POSIXDescriptor struct {
GID uint32 `json:"gid"`
Group string `json:"group"`
UID uint32 `json:"uid"`
User string `json:"user"`
Size int64 `json:"size"`
Mode string `json:"mode"`
Type string `json:"type"`
INode uint64 `json:"inode"`
MTime int64 `json:"mtime"`
ATime int64 `json:"atime"`
CTime int64 `json:"ctime"`
}
type PosixInfo struct {
In chan string
Out chan string
Error chan error
}
type JSONEncoder struct {
In chan *GeoFile
Out chan []byte
Error chan error
}
func NewJSONEncoder(errChan chan error) *JSONEncoder {
return &JSONEncoder{
In: make(chan *GeoFile, 100),
Out: make(chan []byte, 100),
Error: errChan,
}
}
func (jp *JSONEncoder) Run() {
defer close(jp.Out)
for geoFile := range jp.In {
out, err := json.Marshal(&geoFile)
if err != nil {
jp.Error <- err
return
}
jp.Out <- []byte(fmt.Sprintf("%s\tgdal\t%s\n", geoFile.FileName, string(out)))
finfo, err := os.Stat(geoFile.FileName)
if err != nil {
jp.Error <- err
continue
}
var rec syscall.Stat_t
err = syscall.Lstat(geoFile.FileName, &rec)
if err != nil {
jp.Error <- err
continue
}
gid, err := user.LookupGroupId(fmt.Sprintf("%d", rec.Gid))
if err != nil {
jp.Error <- err
continue
}
uid, err := user.LookupId(fmt.Sprintf("%d", rec.Uid))
if err != nil {
jp.Error <- err
continue
}
descr := POSIXDescriptor{Group: gid.Name, User: uid.Username, INode: rec.Ino, MTime: rec.Mtim.Sec, CTime: rec.Ctim.Sec, ATime: rec.Atim.Sec, Size: finfo.Size(), Mode: finfo.Mode().String(), Type: "file", UID: rec.Uid, GID: rec.Gid}
out, err = json.Marshal(&descr)
if err != nil {
jp.Error <- err
return
}
jp.Out <- []byte(fmt.Sprintf("%s\tposix\t%s\n", geoFile.FileName, string(out)))
}
}