예제 #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) 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)
		}
	}
}