// ChunkEnd starts from the end of the chunk and increments
// until it finds a newline and returns that position.
// If it encounters EOF before a new line, it returns -1.
func ChunkEnd(f *os.File, start int64) int64 {
	nl := byte('\n')
	buf := make([]byte, 1)
	curpos := start

	readseek := io.ReadSeeker(f)
	offset, err := readseek.Seek(start, 0)
	if err != nil {
		log.Fatalf(fmt.Sprintf("FindString: error on seek-%v\n", err))
		os.Exit(1)
	}
	if offset != start {
		log.Fatalf("Expected offset of %d and received %d\n", start, offset)
	}
	reader := io.ReaderAt(f)

	for {
		nr, _ := reader.ReadAt(buf, curpos)
		if nr == 0 { // EOF
			break
		}
		if buf[0] == nl {
			break
		} else {
			curpos++
		}
	}
	return int64(curpos)
}
// ChunkEnd starts from the end of the chunk and increments
// until it finds a newline and returns that position.
// If it encounters EOF before a new line, it returns -1.
func ChunkEnd(f *os.File, start int64) int64 {
	nl := byte('\n')
	buf := make([]byte, 1)
	curpos := start
	fmt.Println("ChunkeEnd start=", start)

	readseek := io.ReadSeeker(f)
	offset, err := readseek.Seek(start, 0)
	if err != nil {
		log.Fatalf(fmt.Sprintf("FindString: error on seek-%n\n", err))
		os.Exit(1)
	}
	if offset != start {
		log.Fatalf("Expected offset of %d and received %d\n", start, offset)
	}
	reader := io.ReaderAt(f)

	for {
		nr, _ := reader.ReadAt(buf, curpos)
		//fmt.Printf("curpos=%d nr=%d buf=%s\n", curpos, nr, buf)
		curpos++
		if nr == 0 || buf[0] == nl { // EOF or neline
			break
		}
	}
	return int64(curpos)
}
Esempio n. 3
0
//VFSStaticServer returns a StaticServer to serve a Godoc virtual file system
//as defined in golang.org/x/tools/godoc/vfs . The Godoc vfs package contains
//implementations for FileSystem, Map and Zip File based virtual file systems
//Use errorHandlers to provide custom http.HandlerFunc to handle http.StatusNotFound
//and http.StatusInternalServerError or provide nil to use default implementation
//If a log.Logger is provided (ie. not nil), StaticServer does verbose logging
func VFSStaticServer(f vfs.FileSystem, errorHandlers map[int]http.HandlerFunc, logger *log.Logger) StaticServer {
	return StaticServer{
		stat: f.Lstat,
		readerfn: func(name string) (io.ReadSeeker, error) {
			rsc, err := f.Open(name)
			return io.ReadSeeker(rsc), err
		},
		errorHandlers: setupErrorHandlers(errorHandlers),
		logger:        logger,
	}
}
func staticHandler(w http.ResponseWriter, req *http.Request) {
	file_name := req.URL.Path[len("static/"):] // Get filename from URL
	if len(file_name) != 0 {
		f, err := http.Dir("static/").Open(file_name)
		if err == nil {
			content := io.ReadSeeker(f)
			http.ServeContent(w, req, file_name, time.Now(), content)
			return
		}
	}
	http.NotFound(w, req)
}
Esempio n. 5
0
func StaticHandler(w http.ResponseWriter, req *http.Request) {
	static_file := req.URL.Path[len(STATIC_URL):]
	if len(static_file) != 0 {
		f, err := http.Dir(STATIC_ROOT).Open(static_file)
		if err == nil {
			content := io.ReadSeeker(f)
			http.ServeContent(w, req, static_file, time.Now(), content)
			return
		}
	}
	http.NotFound(w, req)
}
Esempio n. 6
0
func StaticHandler(w http.ResponseWriter, r *http.Request) {
	static_file := r.URL.Path[len(STATIC_URL):]
	static_dir := GetAbsDir("template", STATIC_ROOT)
	if len(static_file) != 0 {
		f, err := http.Dir(static_dir).Open(static_file)
		if err == nil {
			content := io.ReadSeeker(f)
			http.ServeContent(w, r, static_file, time.Now(), content)
			return
		}
	}
	http.NotFound(w, r)
}
Esempio n. 7
0
// Static serves static content such as images, JS and CSS files
func Static(c web.C, w http.ResponseWriter, req *http.Request) {
	//log.Printf("STATIC_URL: %s, %d", STATIC_URL, len(STATIC_URL))
	//log.Printf("PATH: %s, %d", req.URL.Path, len(STATIC_URL))
	static_file := req.URL.Path[7:]
	root := TemplateBase + STATIC_ROOT
	if len(static_file) != 0 {
		f, err := http.Dir(root).Open(static_file)
		defer f.Close()
		if err == nil {
			content := io.ReadSeeker(f)
			http.ServeContent(w, req, static_file, time.Now(), content)
			return
		}
		log.Print(err)
	}
	http.NotFound(w, req)
}
Esempio n. 8
0
func FileDownload(w http.ResponseWriter, r *http.Request) {
	var ido string = strconv.Itoa(0)

	if id != 0 {
		ido = strconv.Itoa(rand.Intn(id))
	}

	filename := "/tmp/" + "file_" + ido

	if _, err := os.Stat(filename); os.IsNotExist(err) {
		fmt.Fprintf(w, "File does not exist")
	} else {
		f, _ := os.Open(filename)

		content := io.ReadSeeker(f)
		http.ServeContent(w, r, "uploadedfile", time.Now(), content)
	}

}
func main() {
	tfile := "/tmp/seektest"
	CreateTestFile(tfile)
	f := OpenTestFile(tfile)

	rs := io.ReadSeeker(f)
	start := int64(6)
	offset, err := rs.Seek(start, 0)
	if err != nil {
		log.Fatalf("FindString: error on seek-%v\n", err)
		os.Exit(1)
	}
	if offset != start {
		log.Fatalf("Expected offset of %d and received %d\n", start, offset)
	} else {
		fmt.Printf("Offset = %d\n", offset)
	}

	reader := bufio.NewReader(rs)
	line, _, _ := reader.ReadLine()
	fmt.Println(string(line))
}
Esempio n. 10
0
func main() {
	curFlags := new(cmdFlag)
	if !procFlags(curFlags) {
		os.Exit(64)
	}
	if *curFlags.v {
		fmt.Printf("%s %s\n%s\nWritten by %s\n", APP_NAME, APP_VERSION, APP_LEGAL, APP_AUTHOR)
		os.Exit(0)
	}
	file, err := os.Open(curFlags.filename)
	if err == nil {
		r := io.ReadSeeker(file)
		tr := tar.NewReader(r)
		for {
			hdr, err := tr.Next()
			if err == io.EOF {
				break
			} else if err != nil {
				log.Fatal(err)
				break
			}
			fmt.Printf("%s %s/%s %9d %s %s\n", os.FileMode(hdr.Mode), hdr.Uname, hdr.Gname, hdr.Size, hdr.ModTime.Format("2006-01-02 15:04"), hdr.Name)
			if *curFlags.x {
				outfile, err := os.Create(hdr.Name)
				if err != nil {
					log.Fatal("Error creating file: ", err)
				}
				io.Copy(outfile, tr)
				outfile.Close()
			}
		}
	} else {
		fmt.Printf("Error: File not found - %s\n", curFlags.filename)
		os.Exit(74)
	}
}
// FindString reads lines in a chunk of a file from start to start+chunksize
// and searches for the query string.  Number of matches are sent to the
// done channel
// TODO: returns too man records read and found.
func FindString(f *os.File, start, chunksize int64, query string, done chan []int, name int) {
	//fmt.Println("name: start, chunksize", name, start, chunksize)
	recsread := 0
	recsmatch := 0
	var l string

	readseek := io.ReadSeeker(f)
	offset, err := readseek.Seek(start, 0)
	if err != nil {
		log.Fatalf(fmt.Sprintf("FindString: error on seek-%v\n", err))
		os.Exit(1)
	}
	if offset != start {
		log.Fatalf("Expected offset of %d and received %d\n", start, offset)
	}

	reader := bufio.NewReader(readseek)
	fmt.Printf("FindString: name=%d start=%d, chunksize=%d, query=%s, offset=%d\n",
		name, start, chunksize, query, offset)

	bytes_read := int64(0)

	for bytes_read < chunksize {
		line, isp, err := reader.ReadLine()
		if err != nil {
			if err == io.EOF {
				break
			} else {
				log.Print(fmt.Sprintf("Error reading file.  err=%v\ns", err))
				break
			}
		}
		if isp {
			log.Printf("FindString: Line too long")
			break
		}
		bytes_read += int64(len(line))

		l = string(line)
		recsread += 1
		//fmt.Printf("name: %d line=%s\n",name, l)

		if strings.HasPrefix(l, "#") {
			continue
		}

		match, err := regexp.MatchString(query, l)
		if err != nil {
			log.Printf(fmt.Sprintf("FindString: regexp.MatchString-%v\n", err))
			continue
		}
		if match == true {
			//fmt.Printf("MATCH name: %d line=%s\n\n",name, l)
			//TODO: split line and write rec number to stdout to
			//see which recs are duplicates
			recsmatch += 1
			s := strings.Split(string(line), " ")
			fmt.Printf("linenum:%s\n", s[0])
		}
	}
	recs := []int{recsread, recsmatch}
	done <- recs
}