示例#1
0
func (state *fileState) pruneToSize(baseFilePath string, maxSize int64) {
	state.bgWriter.Wait()
	baseDir, baseName, _ := shared.SplitPath(baseFilePath)

	files, err := shared.ListFilesByDateMatching(baseDir, baseName+".*")
	if err != nil {
		Log.Error.Print("Error pruning files: ", err)
		return // ### return, error ###
	}

	totalSize := int64(0)
	for _, file := range files {
		totalSize += file.Size()
	}

	for _, file := range files {
		if totalSize <= maxSize {
			return // ### return, done ###
		}
		filePath := fmt.Sprintf("%s/%s", baseDir, file.Name())
		if err := os.Remove(filePath); err != nil {
			Log.Error.Printf("Failed to prune \"%s\": %s", filePath, err.Error())
		} else {
			Log.Note.Printf("Pruned \"%s\"", filePath)
			totalSize -= file.Size()
		}
	}
}
示例#2
0
func (state *fileState) compressAndCloseLog(sourceFile *os.File) {
	state.bgWriter.Add(1)
	defer state.bgWriter.Done()

	// Generate file to zip into
	sourceFileName := sourceFile.Name()
	sourceDir, sourceBase, _ := shared.SplitPath(sourceFileName)

	targetFileName := fmt.Sprintf("%s/%s.gz", sourceDir, sourceBase)

	targetFile, err := os.OpenFile(targetFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
	if err != nil {
		Log.Error.Print("File compress error:", err)
		sourceFile.Close()
		return
	}

	// Create zipfile and compress data
	Log.Note.Print("Compressing " + sourceFileName)

	sourceFile.Seek(0, 0)
	targetWriter := gzip.NewWriter(targetFile)
	spin := shared.NewSpinner(shared.SpinPriorityHigh)

	for err == nil {
		_, err = io.CopyN(targetWriter, sourceFile, 1<<20) // 1 MB chunks
		spin.Yield()                                       // Be async!
	}

	// Cleanup
	sourceFile.Close()
	targetWriter.Close()
	targetFile.Close()

	if err != nil && err != io.EOF {
		Log.Warning.Print("Compression failed:", err)
		err = os.Remove(targetFileName)
		if err != nil {
			Log.Error.Print("Compressed file remove failed:", err)
		}
		return
	}

	// Remove original log
	err = os.Remove(sourceFileName)
	if err != nil {
		Log.Error.Print("Uncompressed file remove failed:", err)
	}
}
示例#3
0
func (state *fileState) pruneByCount(baseFilePath string, count int) {
	state.bgWriter.Wait()
	baseDir, baseName, _ := shared.SplitPath(baseFilePath)

	files, err := shared.ListFilesByDateMatching(baseDir, baseName+".*")
	if err != nil {
		Log.Error.Print("Error pruning files: ", err)
		return // ### return, error ###
	}

	numFilesToPrune := len(files) - count
	if numFilesToPrune < 1 {
		return // ## return, nothing to prune ###
	}

	for i := 0; i < numFilesToPrune; i++ {
		filePath := fmt.Sprintf("%s/%s", baseDir, files[i].Name())
		if err := os.Remove(filePath); err != nil {
			Log.Error.Printf("Failed to prune \"%s\": %s", filePath, err.Error())
		} else {
			Log.Note.Printf("Pruned \"%s\"", filePath)
		}
	}
}