Ejemplo n.º 1
0
Archivo: lzop.go Proyecto: presbrey/lzo
func compress(level int, path string) error {
	if level > lzo.BestCompression {
		level = lzo.BestCompression
	} else if level < lzo.BestSpeed {
		level = lzo.BestSpeed
	} else {
		level = lzo.DefaultCompression
	}
	input, err := os.Open(path)
	if err != nil {
		return err
	}
	output, err := os.Create(path + ".lzo")
	if err != nil {
		return err
	}
	compressor, err := lzo.NewWriterLevel(output, level)
	defer compressor.Close()
	compressor.Name = input.Name()
	if err != nil {
		return err
	}
	_, err = io.Copy(compressor, input)
	if err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 2
0
func main() {
	var (
		buf       bytes.Buffer
		file      *os.File
		lzoWriter *lzo.Writer

		conns = make(chan net.Conn, 128)
		lines = make(chan string, *backlog)
		path  = ""
	)

	err := startTCP(conns)
	if err != nil {
		log.Fatalln(err)
	}

	reopen := func() {
		pathNew, _ := strftime()
		if pathNew == path {
			return
		}

		if buf.Len() > 0 {
			if _, err := buf.WriteTo(lzoWriter); err != nil {
				log.Println(err)
			}
		}

		os.MkdirAll(_path.Dir(pathNew), 0777)
		output, err := os.Create(pathNew)
		if err != nil {
			log.Println(err)
			return
		}
		if *lockMode > 0 {
			err = syscall.Flock(int(output.Fd()), *lockMode)
			if err != nil {
				log.Println(err)
				return
			}
		}
		compressor, err := lzo.NewWriterLevel(output, *level)
		if err != nil {
			log.Println(err)
			return
		}

		l0, f0 := lzoWriter, file
		lzoWriter, file = compressor, output

		if l0 != nil {
			l0.Close()
		}
		if f0 != nil {
			f0.Close()
		}
		path = pathNew
	}
	reopen()

	alive := true
	sigCh := make(chan os.Signal, 1)
	signal.Notify(sigCh, os.Interrupt)
	ticker := time.Tick(*rotationTime)
	for alive {
		select {
		case <-sigCh:
			alive = false

		case <-ticker:
			reopen()

		case c := <-conns:
			go func() {
				scanner := bufio.NewScanner(c)
				for scanner.Scan() {
					text := scanner.Text()
					if len(text) > 0 {
						lines <- text + "\n"
					}
				}
				c.Close()
			}()

		case line := <-lines:
			if buf.Len()+len(line) > *blockSizeMax {
				if _, err := buf.WriteTo(lzoWriter); err != nil {
					log.Println(err)
				}
			}
			if _, err := buf.WriteString(line); err != nil {
				log.Println(err)
			}

		} // select
	} // for

	if buf.Len() > 0 {
		buf.WriteTo(lzoWriter)
	}
	if lzoWriter != nil {
		lzoWriter.Close()
	}
	if file != nil {
		file.Close()
	}
}