Esempio n. 1
0
func reOpen(f *os.File, maxSize int64) (nf *os.File) {

	oldFi, err := f.Stat()
	if err != nil {
		log.Warn("stat", err)
		return
	}

	successiveFail++
	if successiveFail > 100 || oldFi.Size() >= maxSize {
		successiveFail = 0
		return getNextLog(f.Name())
	}

	newFi, err := os.Stat(f.Name())
	if err != nil {
		log.Warn("stat", err)
		return
	}

	if newFi.Size() == oldFi.Size() {
		return
	}

	f2, err := os.Open(f.Name())
	if err != nil {
		log.Warn("open", f.Name(), "fail:", err)
		return
	}

	return f2
}
Esempio n. 2
0
func getReader(f *os.File, off int64) (*bufio.Reader, int64) {

	newOff, err := f.Seek(off, os.SEEK_SET)
	if err != nil {
		log.Warn("Seek fail: ", err)
	}
	return bufio.NewReader(f), newOff
}
Esempio n. 3
0
func save(conf *config) {

	conf.RW.RLock()
	b, err := json.MarshalIndent(conf, "", "\t")
	conf.RW.RUnlock()
	if err != nil {
		log.Warn("marshal:", err)
		return
	}

	tmpName := *confName + "_"
	if err := ioutil.WriteFile(tmpName, b, 0666); err != nil {
		log.Warn("write:", err)
		return
	}

	if err := os.Rename(tmpName, *confName); err != nil {
		log.Warn("rename:", err)
		return
	}
	return
}
Esempio n. 4
0
func preOp(host string, uri string, params []string, save bool) {

	for i, p := range params {
		url := host + "/" + uri + p
		log.Debug("url:", url)
		req, err := http.NewRequest("GET", url, nil)
		if err != nil {
			log.Warn("http newrequest fail:", url, err)
			continue
		}

		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			log.Warn("http get fail:", url, err)
			continue
		}

		if save {
			saveResult(uri+strconv.Itoa(i), resp.Body)
		}
		resp.Body.Close()
	}
}
Esempio n. 5
0
func parseLine(line string, uids []uint32, imgSuffixs []string) (uri string, ok bool) {

	const (
		RS_REQ    = "REQ\tRS\t"
		RS_PUT    = "/put/"
		RS_INS    = "/ins/"
		MIME_TYPE = "mimeType/"
		TOKEN_UID = "\"uid\":"
	)

	if !strings.HasPrefix(line, RS_REQ) {
		return
	}

	words := strings.SplitN(line[len(RS_REQ):], "\t", 7)
	if len(words) < 7 {
		return
	}

	if words[1] != "POST" || words[5] != "200" {
		return
	}

	url := words[2]
	if !strings.HasPrefix(url, RS_PUT) && !strings.HasPrefix(url, RS_INS) {
		return
	}

	// len(RS_PUT) == len(RS_INS)
	param := ""
	entry := url[len(RS_PUT):]
	if i := strings.Index(entry, "/"); i >= 0 {
		param = entry[i:]
		entry = entry[:i]
	}

	entry, err := rpc.DecodeURI(entry)
	if err != nil {
		log.Warn(entry, " decode fail: ", err)
		return
	}

	mime := ""
	if i := strings.Index(param, MIME_TYPE); i >= 0 {
		mime = param[i+len(MIME_TYPE):]
		if j := strings.Index(mime, "/"); j >= 0 {
			mime = mime[:j]
		}
	}

	mime, err = rpc.DecodeURI(mime)
	if err != nil {
		log.Warn(mime, "decode fail: ", err)
		return
	}

	if !strings.HasPrefix(mime, "image/") && !hasSuffix(entry, imgSuffixs) {
		return
	}

	header := words[3]
	uidIdx := strings.Index(header, TOKEN_UID)
	if uidIdx < 0 {
		return
	}

	strUid := header[uidIdx+len(TOKEN_UID):]
	if i := strings.Index(strUid, ","); i >= 0 {
		strUid = strUid[:i]
	}

	uid, err := strconv.ParseUint(strUid, 10, 32)
	if err != nil {
		return
	}

	if !contain(uids, uint32(uid)) {
		return
	}

	uri = strings.Replace(entry, ":", "/", 1)
	return uri, true
}