-
Notifications
You must be signed in to change notification settings - Fork 1
/
shredder.go
170 lines (150 loc) · 3.17 KB
/
shredder.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
// this code is responsible for processing the encountered urls.
// we look up the url in a DB, and it it does not exist, we add it , and also add the corresponding host to be active
import (
"bufio"
"io/ioutil"
"log"
"math/rand"
"os"
"os/exec"
"strconv"
"strings"
"time"
"ouroboros/src/murl"
utils "ouroboros/src/utils"
"github.com/pkg/profile"
)
type Conf struct {
Dir0 string
}
var conf Conf
var rs = rand.NewSource(time.Now().UnixNano())
var ra = rand.New(rs)
var WI = make(chan string, 1000)
var tot = 0
var symbol []int
func Proc(fname string) {
f, e := os.OpenFile(fname, os.O_RDONLY, 0644)
if e != nil {
log.Println("cannot open file ", e)
return
}
b := bufio.NewReader(f)
for {
s, e := b.ReadBytes('\n')
if e != nil {
break
}
if len(s) <= 0 {
continue
}
s1 := s[0 : len(s)-1]
h := murl.Ho(string(s1))
LOCK:
if _, err := os.Stat(conf.Dir0 + "/tmp_processor/" + h + ".lock"); err == nil {
log.Println("file is locked ", h)
time.Sleep(time.Duration(1e+7))
goto LOCK
}
g, e := os.OpenFile(conf.Dir0+"/tmp_processor/"+h, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
g.Write(s)
g.Close()
}
f.Close()
os.Remove(fname)
}
func List() {
f, _ := os.OpenFile(conf.Dir0+"/link_proc.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
cmd := exec.Command("ls", conf.Dir0+"/tmp_crawler_links1")
cmd.Stdout = f
cmd.Stderr = os.Stderr
cmd.Run()
f.Close()
}
func count() int {
f, e := os.OpenFile(conf.Dir0+"/link_proc.txt", os.O_RDONLY, 0644)
if e != nil {
return 0
}
b := bufio.NewReader(f)
cou := 0
for {
_, e := b.ReadString('\n')
if e != nil {
break
}
cou += 1
}
return cou
}
func Run() {
for {
List()
nfiles := count()
ioutil.WriteFile(conf.Dir0+"/nfiles_to_process.txt", []byte(strconv.Itoa(nfiles)), 0644)
log.Println("nfiles=", nfiles)
f, e := os.OpenFile(conf.Dir0+"/link_proc.txt", os.O_RDONLY, 0644)
if e != nil {
log.Println("no link_proc file")
time.Sleep(time.Second)
continue
}
fi, _ := f.Stat()
if fi.Size() == 0 {
log.Println("zero file")
f.Close()
time.Sleep(time.Second)
continue
}
b := bufio.NewReader(f)
cou := 0
for {
fn, e := b.ReadString('\n')
if e != nil {
time.Sleep(time.Duration(1e+9))
break
}
if len(fn) <= 1 {
continue
}
fn = fn[0 : len(fn)-1]
cou += 1
Proc(conf.Dir0 + "/tmp_crawler_links1/" + fn)
}
f.Close()
os.Remove(conf.Dir0 + "/link_proc.txt")
if cou == 0 {
time.Sleep(time.Second)
}
log.Println("cou", cou)
}
}
func main() {
defer profile.Start().Stop()
var dir0 string
if len(os.Args) > 1 {
dir0 = os.Args[1]
} else {
dir0 = "/d"
s, e := ioutil.ReadFile("/etc/dse/dse.conf")
if e != nil {
log.Println("error reding config", e)
time.Sleep(time.Second)
os.Exit(0)
}
q := strings.Split(string(s), "\n")
dir0 = q[0]
log.Println("dir0=", dir0)
}
conf.Dir0 = dir0
utils.MakeDir0(conf.Dir0 + "/tmp_processor")
utils.MakeDir0(conf.Dir0 + "/tmp_processor1")
utils.MakeDir0(conf.Dir0 + "/hostsd")
utils.MakeDir0(conf.Dir0 + "/tmp_crawler_links")
utils.MakeDir0(conf.Dir0 + "/tmp_crawler_links1")
for i := 0; i < 256; i++ {
symbol = append(symbol, i)
}
Run()
}